Repository navigation
Editor: Guard entity preload filters and share entity actions/fields logic - #85390
Conversation
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Updated as activity occurs, without notifying anyone named here. Add the 🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Regression: updating a specific option will only change its value and will not corrupt others in
|
jorgefilipecosta
left a comment
There was a problem hiding this comment.
Some of the comments I left at WordPress/wordpress-develop#14716, could apply here but they are not blockers the change looks good.
4efecfd to
0ce085b
Compare
| function _gutenberg_preload_entity_actions( $paths, $context = null ) { | ||
| return _wp_add_entity_preload_paths( $paths, $context, '/wp/v2/entity-actions' ); | ||
| } | ||
| remove_filter( 'block_editor_rest_api_preload_paths', '_wp_preload_entity_actions' ); | ||
| add_filter( 'block_editor_rest_api_preload_paths', '_gutenberg_preload_entity_actions', 10, 2 ); |
There was a problem hiding this comment.
This is a temporary measure. Once WordPress/wordpress-develop#14716 is merged and the WordPress version Gutenberg's CI runs against includes it, we can go back to defining these only when core doesn't. Keeping the swap works fine at that point; it'd just be unnecessary, since both functions would be the same.
| function _gutenberg_preload_entity_fields( $paths, $context = null ) { | ||
| return _wp_add_entity_preload_paths( $paths, $context, '/wp/v2/entity-fields' ); | ||
| } | ||
| remove_filter( 'block_editor_rest_api_preload_paths', '_wp_preload_entity_fields' ); | ||
| add_filter( 'block_editor_rest_api_preload_paths', '_gutenberg_preload_entity_fields', 10, 2 ); |
There was a problem hiding this comment.
This is a temporary measure. Once WordPress/wordpress-develop#14716 is merged and the WordPress version Gutenberg's CI runs against includes it, we can go back to defining these only when core doesn't. Keeping the swap works fine at that point; it'd just be unnecessary, since both functions would be the same.
A numeric script module id becomes an integer as an array key, so `get_all_registered_field_modules()` returned it as an integer. It is now cast back to a string, as the entity actions registry already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ports the changes of the WordPress core backport. `_wp_add_entity_preload_paths()`, in the new entity-utils.php, adds the paths of an entity route for the post types the editors list. It holds what `_wp_preload_entity_actions()` and `_gutenberg_preload_entity_fields()` had in common, and the site editor screens of `_gutenberg_get_site_editor_screen_post_types()`, which is removed. It also guards against an earlier `block_editor_rest_api_preload_paths` callback returning something other than an array: empty values become an array, other values and a context that is not a `WP_Block_Editor_Context` are returned unchanged. `$context` defaults to `null`. `_wp_preload_entity_actions()` now preloads the actions of the site editor too, as in core, so `_gutenberg_preload_site_editor_entity_actions()` is removed and its tests move to the entity actions API tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ports the changes of the WordPress core backport. `_wp_add_entity_action_modules_to_editor_script()` and `_wp_add_entity_field_modules_to_editor_script()` duplicated the logic that declares the script modules of an entity API as dynamic module dependencies of the editor script. It now lives in `_wp_add_entity_modules_to_editor_script()`, in entity-utils.php, which takes the getter of the modules of the API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`_gutenberg_preload_entity_fields()` becomes `_wp_preload_entity_fields()` in entity-fields-api.php, guarded by `function_exists()`, as in core and as `_wp_preload_entity_actions()` is in entity-actions-api.php. When core ships it, the core function runs alone instead of both adding the same paths. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only the experimental route preload uses `_gutenberg_get_post_type_fields_preload_paths()` now, so it moves from the 7.2 compat preload.php to lib/experimental/route-entity-fields-preload.php. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d_modules(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also cover a call with null paths and no context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
wp_get_registered_entity_field_modules() and wp_get_registered_entity_action_modules() keyed their result by module id, which PHP turns into an integer when numeric: a strict comparison misses it and array_merge() renumbers it. They now return a list of entries with the module id and the ids of the fields or actions it applies to, the shape the REST controllers already return. The controllers still accept the keyed shape, which WordPress versions that ship the API before core#14716 return. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
empty() treats the string '0' as empty, so a field with that id was skipped with a notice asking for a non-empty string. Check for the empty string instead, as the entity actions registry does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…16 changes. WordPress trunk already ships the registries without core#14716, and the plugin registries extend them, so they listed the modules of an entity keyed by module id and refused the field id `0`. The plugin registries now override these methods, which lets the REST controllers return the modules as they are. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nes. WordPress trunk already ships _wp_preload_entity_fields() and _wp_preload_entity_actions() without the guards of core#14716, so the compat functions were never defined and a non-array preload paths value still crashed the editor. The plugin now hooks its own _gutenberg_preload_entity_fields() and _gutenberg_preload_entity_actions(), and unhooks the core functions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
c43afc7 to
8ac9052
Compare
What?
Follow up to WordPress/wordpress-develop#14716
Ports the core changes of the backport above to the plugin's 7.2 compat layer.
Why?
Keeps the compat code in sync with core. It also fixes an editor crash when an earlier
block_editor_rest_api_preload_pathscallback returns something other than an array.How?
WP_Entity_Fields_Registry::get_all_registered_field_modules()casts module ids back to strings. A numeric id became an integer as an array key. The actions registry already did this.lib/compat/wordpress-7.2/entity-utils.phpadds_wp_add_entity_preload_paths(), matching core._wp_preload_entity_actions()and_gutenberg_preload_entity_fields()both use it._gutenberg_preload_entity_fields()moves frompreload.phptoentity-fields-api.phpas_wp_preload_entity_fields(), guarded byfunction_exists()as in core. When core ships it, only the core function runs.WP_Block_Editor_Context, are returned unchanged.$contextdefaults tonull._gutenberg_get_site_editor_screen_post_types()is removed. Its screen map is now in the shared helper._wp_preload_entity_actions()now preloads the site editor actions too, as core does, so_gutenberg_preload_site_editor_entity_actions()is removed. Its tests moved tophpunit/entity-actions-api-test.php._gutenberg_get_post_type_fields_preload_paths()moves frompreload.phptolib/experimental/route-entity-fields-preload.php, its only user._wp_add_entity_modules_to_editor_script(), also inentity-utils.php, declares an API's script modules as dynamic module dependencies ofwp-editor. The actions and fields functions delegate to it.Testing Instructions
npm run test:unit:php:base -- --filter 'Entity|Preload|entity'passes./wp/v2/entity-actionsand/wp/v2/entity-fieldsrequests for the listed post types are preloaded, so there are no network requests for them on load.add_filter( 'block_editor_rest_api_preload_paths', '__return_null', 5 );and open the post editor. It loads without a fatal error.Use of AI Tools
Ported from the core PR with Claude Code (Claude Opus 5.5).