Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Speculation Rules to new location as purely a standalone plugin #946

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
9b20562
Implement basic module to experiment with speculationrules for preren…
felixarntz May 19, 2023
c7f365e
Include proper backslashes in rules source.
felixarntz May 22, 2023
286e50a
Add hook to make it easy to use origin trial.
felixarntz May 22, 2023
cf16aed
Merge branch 'trunk' into speculationrules-experiment
felixarntz Jun 6, 2023
5ec445a
Merge branch 'trunk' into speculationrules-experiment
felixarntz Nov 13, 2023
5b79bb8
Print speculationrules script tag directly without using JS.
felixarntz Nov 13, 2023
25fc9da
Introduce filter for which paths to exclude for speculative prerender…
felixarntz Nov 13, 2023
ce1c469
Add CODEOWNERS entry.
felixarntz Nov 13, 2023
f32401f
Add initial test coverage for Speculation Rules.
felixarntz Nov 13, 2023
346f0cb
Fix function name caught as misspelling.
felixarntz Nov 13, 2023
a1a65cf
Merge branch 'trunk' into speculationrules-experiment
felixarntz Nov 14, 2023
6d338ab
Address CR feedback.
felixarntz Nov 20, 2023
7404f02
Add TODO regarding eventual wildcard removal.
felixarntz Nov 20, 2023
ce0e56b
Add speculation rules URL pattern prefixer to support WordPress site …
felixarntz Nov 21, 2023
cd536d5
Add missing return type hint.
felixarntz Nov 28, 2023
5c134db
Remove unnecessary relative_to directive.
felixarntz Dec 5, 2023
813a757
Simplify escaping.
felixarntz Dec 6, 2023
307034e
Merge pull request #733 from WordPress/speculationrules-experiment
felixarntz Dec 15, 2023
2d8a23e
Merge pull request #936 from WordPress/trunk
felixarntz Jan 15, 2024
cb742f2
Fix new WPCS violation after trunk refresh.
felixarntz Jan 15, 2024
b55e874
Add readme.txt for Speculation Rules plugin.
felixarntz Jan 15, 2024
cee6cc7
Add Speculation Rules settings and UI.
felixarntz Jan 16, 2024
2cfd37b
Add references to UI for customization of Speculation Rules.
felixarntz Jan 16, 2024
b165b3a
Add link to WICG spec draft.
felixarntz Jan 16, 2024
a893b1d
Remove Speculation Rules origin trial logic.
felixarntz Jan 16, 2024
06ce1f5
Remove now unnecessary path suffixes.
felixarntz Jan 16, 2024
c797e20
Remove outdated suffixes from tests as well.
felixarntz Jan 16, 2024
56e16e9
Explicitly fall back to setting default if setting somehow missing (f…
felixarntz Jan 16, 2024
f7cf351
Only consider no-prerender class when mode is actually set to prerender.
felixarntz Jan 16, 2024
e04f75b
Remove irrelevant assertion.
felixarntz Jan 16, 2024
2d89017
Add test coverage.
felixarntz Jan 16, 2024
0cdca0e
Fix test.
felixarntz Jan 16, 2024
0470909
Merge pull request #937 from WordPress/fix/speculation-rules-phpcs-vi…
mukeshpanchal27 Jan 16, 2024
2364734
Update grammar.
felixarntz Jan 16, 2024
29027cf
Merge branch 'feature/speculationrules' into add/speculation-rules-re…
felixarntz Jan 16, 2024
671eb6b
Clarify behavior and browser support in readme.
felixarntz Jan 16, 2024
b6d4a67
Merge branch 'feature/speculationrules' into add/speculation-rules-se…
felixarntz Jan 16, 2024
b139961
Clarify eager configuration.
felixarntz Jan 16, 2024
9a6c3a9
Clarify looseness of eagerness values.
felixarntz Jan 16, 2024
24c3bb0
Clarify eagerness.
felixarntz Jan 16, 2024
4cfaa2b
Change link format.
felixarntz Jan 16, 2024
8e0696c
Merge pull request #938 from WordPress/add/speculation-rules-readme
felixarntz Jan 16, 2024
48c1728
Merge pull request #940 from WordPress/enhance/speculation-rules-chro…
felixarntz Jan 17, 2024
40709c5
Merge branch 'feature/speculationrules' into add/speculation-rules-se…
felixarntz Jan 17, 2024
86ea050
Use more precise array doc type annotation.
felixarntz Jan 17, 2024
3c6aec2
Use more precise array doc syntax in more places.
felixarntz Jan 17, 2024
e24b010
Limit maximum width instead of using br.
felixarntz Jan 17, 2024
da0b883
Use better phrased description for REST API too.
felixarntz Jan 17, 2024
d490f93
More defensive coding when accessing array keys.
felixarntz Jan 17, 2024
13bc154
Merge pull request #939 from WordPress/add/speculation-rules-settings
felixarntz Jan 17, 2024
f1e9ee3
Move Speculation Rules to new location as purely a standalone plugin.
felixarntz Jan 17, 2024
34af17b
Escape path prefix and restrict it to be a pathname in Speculation Rules
jeremyroman Jan 20, 2024
06b6d30
Force HTML5 script theme support when printing JSON script
westonruter Jan 22, 2024
f031c21
Negate condition
westonruter Jan 22, 2024
13d4546
Add tests for valid JSON workaround
westonruter Jan 22, 2024
e9924bc
Add ticket reference
westonruter Jan 22, 2024
804a49b
Limit workaround to WP 6.4
westonruter Jan 22, 2024
3c8d640
Use braces to deal with component confusion issue instead (and other …
jeremyroman Jan 23, 2024
3a16b14
Use addcslashes
jeremyroman Jan 23, 2024
78fae85
Update modules/js-and-css/speculation-rules/class-plsr-url-pattern-pr…
adamsilverstein Jan 29, 2024
9d6cab4
Merge pull request #951 from jeremyroman/sr-escape-pathname
westonruter Jan 29, 2024
c8681d4
Guard against alpha, beta, and RC from interfering with versions
westonruter Jan 29, 2024
a1037f8
Ensure theme features are cleaned up after tests
westonruter Feb 6, 2024
075e307
Merge pull request #952 from WordPress/fix/non-html5-script-printing
westonruter Feb 6, 2024
851d0d4
Add plugin directory assets for Speculation Rules
westonruter Feb 14, 2024
647141d
Squoosh images from Figma
westonruter Feb 14, 2024
d152b03
Compress icon.svg with SVGOMG
westonruter Feb 14, 2024
df10806
Merge pull request #987 from WordPress/add/speculation-rules-plugin-a…
swissspidy Feb 15, 2024
99189bc
Bump version and update readme in Speculation Rules
westonruter Feb 15, 2024
bc831a3
Merge branch 'feature/speculationrules' of https://github.com/WordPre…
westonruter Feb 15, 2024
acce7ef
Fix typo in changelog
westonruter Feb 16, 2024
710514c
Merge pull request #993 from WordPress/update/speculation-rules-chang…
westonruter Feb 16, 2024
a4d8dd4
Merge branch 'feature/speculationrules' into prepare/speculation-rule…
felixarntz Feb 27, 2024
8bc988a
Update hard-coded Speculation Rules version to 1.0.1.
felixarntz Feb 27, 2024
22cd743
Merge branch 'feature/modules-to-plugins' into prepare/speculation-ru…
felixarntz Feb 27, 2024
d353b7f
Move Speculation Rules to tests/plugins and refresh setup based on la…
felixarntz Feb 27, 2024
8c1b390
Merge branch 'feature/modules-to-plugins' into prepare/speculation-ru…
felixarntz Feb 27, 2024
bc74074
Update composer.json shortcuts to include speculation-rules.
felixarntz Feb 27, 2024
2df2c0a
Include phpcs.xml.dist for speculation-rules.
felixarntz Feb 27, 2024
4a2f084
Update composer.json shortcuts to include speculation-rules linting.
felixarntz Feb 27, 2024
9418bd1
Add missing ext-json requirement to composer.json
westonruter Feb 27, 2024
2012d2e
Run composer update after adding ext-json
westonruter Feb 27, 2024
dbfabff
Fix ticket reference and add covers
westonruter Feb 27, 2024
9576083
Fix test for CDATA
westonruter Feb 27, 2024
f40859e
Update package name in speculation-rules tests.
felixarntz Feb 28, 2024
6fb35a4
Fix incorrect Plugin URI for new standalone plugins.
felixarntz Feb 28, 2024
3c4a347
Replace n.e.x.t in Speculation Rules with the actual version numbers.
felixarntz Feb 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@
# Plugin: Auto-sizes for Lazy-Loaded Images
/plugins/auto-sizes @joemcgill
/tests/plugins/auto-sizes @joemcgill

# Plugin: Speculation Rules
/plugins/speculation-rules @felixarntz
/tests/plugins/speculation-rules @felixarntz
15 changes: 11 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"require": {
"composer/installers": "~1.0",
"ext-json": "*",
"php": ">=7|^8"
},
"scripts": {
Expand All @@ -27,9 +28,11 @@
"lint": "build-cs/vendor/bin/phpcs",
"lint:all": [
"@lint",
"@lint:auto-sizes"
"@lint:auto-sizes",
"@lint:speculation-rules"
],
"lint:auto-sizes": "@lint -- ./plugins/auto-sizes --standard=./plugins/auto-sizes/phpcs.xml.dist",
"lint:speculation-rules": "@lint -- ./plugins/speculation-rules --standard=./plugins/speculation-rules/phpcs.xml.dist",
"test": "phpunit --verbose --testsuite performance-lab",
"test-multisite": "phpunit -c tests/multisite.xml --verbose --testsuite performance-lab",
"test:all": [
Expand All @@ -41,13 +44,17 @@
"@test-multisite:plugins"
],
"test:plugins": [
"@test:auto-sizes"
"@test:auto-sizes",
"@test:speculation-rules"
],
"test-multisite:plugins": [
"@test-multisite:auto-sizes"
"@test-multisite:auto-sizes",
"@test-multisite:speculation-rules"
],
"test:auto-sizes": "phpunit --verbose --testsuite auto-sizes",
"test-multisite:auto-sizes": "phpunit -c tests/multisite.xml --verbose --testsuite auto-sizes"
"test-multisite:auto-sizes": "phpunit -c tests/multisite.xml --verbose --testsuite auto-sizes",
"test:speculation-rules": "phpunit --verbose --testsuite speculation-rules",
"test-multisite:speculation-rules": "phpunit -c tests/multisite.xml --verbose --testsuite speculation-rules"
},
"config": {
"allow-plugins": {
Expand Down
29 changes: 15 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ruleset name="WPP-PerfLab">
<description>WordPress Coding Standards for the Performance Lab Plugin</description>
<rule ref="phpcs.ruleset.xml"/>

<config name="text_domain" value="performance-lab,default"/>

<!-- Do not require file headers on generated files -->
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<testsuite name="auto-sizes">
<directory suffix=".php">./tests/plugins/auto-sizes</directory>
</testsuite>
<testsuite name="speculation-rules">
<directory suffix=".php">./tests/plugins/speculation-rules</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
Expand Down
5 changes: 4 additions & 1 deletion plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"version": "1.0.5"
}
},
"plugins": [ "auto-sizes" ]
"plugins": [
"auto-sizes",
"speculation-rules"
]
}
2 changes: 1 addition & 1 deletion plugins/auto-sizes/auto-sizes.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: Auto-sizes for Lazy-loaded Images
* Plugin URI: https://github.com/WordPress/performance/tree/trunk/modules/images/auto-sizes
* Plugin URI: https://github.com/WordPress/performance/tree/trunk/plugins/auto-sizes
* Description: This plugin implements the HTML spec for adding `sizes="auto"` to lazy-loaded images.
* Requires at least: 6.3
* Requires PHP: 7.0
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions plugins/speculation-rules/.wordpress-org/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
122 changes: 122 additions & 0 deletions plugins/speculation-rules/class-plsr-url-pattern-prefixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Class 'PLSR_URL_Pattern_Prefixer'.
*
* @package speculation-rules
* @since 1.0.0
*/

/**
* Class for prefixing URL patterns.
*
* @since 1.0.0
*/
class PLSR_URL_Pattern_Prefixer {

/**
* Map of `$context_string => $base_path` pairs.
*
* @since 1.0.0
* @var array
*/
private $contexts;

/**
* Constructor.
*
* @since 1.0.0
*
* @param array $contexts Optional. Map of `$context_string => $base_path` pairs. Default is the contexts returned
* by the {@see PLSR_URL_Pattern_Prefixer::get_default_contexts()} method.
*/
public function __construct( array $contexts = array() ) {
if ( $contexts ) {
$this->contexts = array_map(
static function ( string $str ): string {
return self::escape_pattern_string( trailingslashit( $str ) );
},
$contexts
);
} else {
$this->contexts = self::get_default_contexts();
}
}

/**
* Prefixes the given URL path pattern with the base path for the given context.
*
* This ensures that these path patterns work correctly on WordPress subdirectory sites, for example in a multisite
* network, or when WordPress itself is installed in a subdirectory of the hostname.
*
* The given URL path pattern is only prefixed if it does not already include the expected prefix.
*
* @since 1.0.0
*
* @param string $path_pattern URL pattern starting with the path segment.
* @param string $context Optional. Either 'home' (any frontend content) or 'site' (content relative to the
* directory that WordPress is installed in). Default 'home'.
* @return string URL pattern, prefixed as necessary.
*/
public function prefix_path_pattern( string $path_pattern, string $context = 'home' ): string {
// If context path does not exist, the context is invalid.
if ( ! isset( $this->contexts[ $context ] ) ) {
_doing_it_wrong(
__FUNCTION__,
esc_html(
sprintf(
/* translators: %s: context string */
__( 'Invalid context %s.', 'speculation-rules' ),
$context
)
),
'Performance Lab 1.0.0'
);
return $path_pattern;
}

// In the event that the context path contains a :, ? or # (which can cause the URL pattern parser to
// switch to another state, though only the latter two should be percent encoded anyway), we need to
// additionally enclose it in grouping braces. The final forward slash (trailingslashit ensures there is
// one) affects the meaning of the * wildcard, so is left outside the braces.
$context_path = $this->contexts[ $context ];
$escaped_context_path = $context_path;
if ( strcspn( $context_path, ':?#' ) !== strlen( $context_path ) ) {
$escaped_context_path = '{' . substr( $context_path, 0, -1 ) . '}/';
}

// If the path already starts with the context path (including '/'), remove it first
// since it is about to be added back.
if ( str_starts_with( $path_pattern, $context_path ) ) {
$path_pattern = substr( $path_pattern, strlen( $context_path ) );
}

return $escaped_context_path . ltrim( $path_pattern, '/' );
}

/**
* Returns the default contexts used by the class.
*
* @since 1.0.0
*
* @return array Map of `$context_string => $base_path` pairs.
*/
public static function get_default_contexts(): array {
return array(
'home' => self::escape_pattern_string( trailingslashit( wp_parse_url( home_url( '/' ), PHP_URL_PATH ) ) ),
'site' => self::escape_pattern_string( trailingslashit( wp_parse_url( site_url( '/' ), PHP_URL_PATH ) ) ),
);
}

/**
* Escapes a string for use in a URL pattern component.
*
* @link https://urlpattern.spec.whatwg.org/#escape-a-pattern-string
* @since 1.0.1
*
* @param string $str String to be escaped.
* @return string String with backslashes added where required.
*/
private static function escape_pattern_string( string $str ): string {
return addcslashes( $str, '+*?:{}()\\' );
}
}
Loading
Loading