fix: Reinstate Jetpack assets to editor assets endpoint#44274
fix: Reinstate Jetpack assets to editor assets endpoint#44274
Conversation
Recent changes fixed de-registering of assets not present on the allow list. However, it was overlooked that certain Jetpack assets were then erroneously excluded. Namely, the `jetpack-blocks-editor` assets were excluded as their `jetpack-blocks-assets-base-url` dependency was also excluded. The latter was excluded as it is an "inline" script without a `src` attribute. The lack of a `src` meant the path-based exclusion erroneously de-registered the script. Using a handle-based exclusion means that both inline and external scripts are correct assessed by the plugin filtering logic.
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: The Jetpack plugin has different release cadences depending on the platform:
If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
Code Coverage SummaryCoverage changed in 2 files.
|
WPCOM loads Gutenberg from a custom directory.
| /** | ||
| * Mock function for block editor assets. | ||
| * This provides minimal required assets without loading actual files. | ||
| */ | ||
| public function mock_block_editor_assets() { | ||
| // Register minimal mock assets that don't require actual files | ||
| wp_register_script( 'mock-editor-script', 'http://example.org/plugins/jetpack/mock-editor.js', array(), '1.0', true ); | ||
| wp_register_style( 'mock-editor-style', 'http://example.org/plugins/jetpack/mock-editor.css', array(), '1.0' ); | ||
| wp_register_script( 'disallowed-plugin-script', 'http://example.org/plugins/disallowed-plugin/script.js', array(), '1.0', true ); | ||
| wp_register_script( 'disallowed-plugin-style', 'http://example.org/plugins/disallowed-plugin/style.css', array(), '1.0', true ); | ||
|
|
||
| // Enqueue our mock assets | ||
| wp_enqueue_script( 'mock-editor-script' ); | ||
| wp_enqueue_style( 'mock-editor-style' ); | ||
| wp_enqueue_script( 'disallowed-plugin-script' ); | ||
| wp_enqueue_style( 'disallowed-plugin-style' ); |
There was a problem hiding this comment.
Relocated these mocks to be co-located with related test cases to better communicate intent.
| const ALLOWED_PLUGINS = array( | ||
| '/plugins/gutenberg/', // Default plugin location | ||
| '/plugins/gutenberg-core/', // WPCOM Simple site location | ||
| '/plugins/jetpack/', // Default plugin location | ||
| '/plugins/jetpack-dev/', // Used for loading in-progress work | ||
| '/mu-plugins/jetpack-mu-wpcom-plugin/', // WPCOM Simple site location | ||
| '/mu-plugins/wpcomsh/', // WoA helpers, including Jetpack assets in vendor directories |
There was a problem hiding this comment.
The path-based allow list meant that inline script tags were erroneously disallowed, as inline scripts do not have a path assigned to a src attribute.
There was a problem hiding this comment.
Pull Request Overview
This PR replaces the previous path-based asset exclusion in the block editor assets endpoint with a handle-prefix–based approach to ensure inline and external Jetpack assets are correctly retained.
- Switched from URL substring checks to handle-prefix checks for asset whitelisting in the endpoint class.
- Renamed helper methods and updated constants (
ALLOWED_PLUGIN_HANDLE_PREFIXES,is_core_or_gutenberg_asset,is_allowed_plugin_handle). - Updated tests to mock and verify allowed, disallowed, and WPCOM Gutenberg assets using the new handle-based logic, and added a changelog entry.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-block-editor-assets.php | Swapped path-based exclusion for handle-prefix logic and renamed related methods/constants. |
| projects/plugins/jetpack/tests/php/core-api/wpcom-endpoints/WPCOM_REST_API_V2_Endpoint_Block_Editor_Assets_Test.php | Removed old mocks, added new handle-based tests and mock methods. |
| projects/plugins/jetpack/changelog/fix-reinstate-jetpack-assets-to-editor-assets-endpoint | Added a patch-level changelog entry. |
...k/tests/php/core-api/wpcom-endpoints/WPCOM_REST_API_V2_Endpoint_Block_Editor_Assets_Test.php
Outdated
Show resolved
Hide resolved
...k/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-block-editor-assets.php
Outdated
Show resolved
Hide resolved
The designation was erroneous to start.
Avoid confusion regarding the purpose of each allowed handle. Namely, the usage of `wp-` which is used by core WordPress scripts, but also some WPCOM and Jetpack scripts too.
kean
left a comment
There was a problem hiding this comment.
I reviewed it, and to the best of my knowledge, the changes look good.
#44093 fixed de-registering of assets not present on the allow list. However, it was overlooked that certain Jetpack assets were then erroneously excluded.
Namely, the
jetpack-blocks-editorassets were excluded as theirjetpack-blocks-assets-base-urldependency was also excluded. The latter was excluded as it is an "inline" script without asrcattribute. The lack of asrcmeant the path-based exclusion erroneously de-registered the script.Using a handle-based exclusion means that both inline and external scripts are correct assessed by the plugin filtering logic.
Proposed changes:
Replace asset path-based exclusion logic with asset handle-based exclusion logic.
Other information:
Jetpack product discussion
0b1591ec5132-linear-project
Does this pull request change what data or activity we track or use?
No
Testing instructions:
/wpcom/v2/sites/<site_id>/editor-assetsendpoint for the WPCOM site—via API Console,curl, etc.jetpack-blocks-editor-jsscript.