Repository navigation
Editor: Add the entity actions API - #14276
jorgefilipecosta wants to merge 3 commits into
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
The PHP this pull request adds, the core actions registered on the server and the preload of the actions of the post editor, is backported with the entity actions API in WordPress/wordpress-develop#14276.
The PHP this pull request adds, the core actions registered on the server and the preload of the actions of the post editor, is backported with the entity actions API in WordPress/wordpress-develop#14276.
Adds a registry for the actions DataViews shows for an entity, so they can be registered on the server. An action definition is the serializable part of a DataViews action. Its JavaScript parts come from a script module registered along with the actions. Actions are registered with `wp_register_entity_actions()` and unregistered with `wp_unregister_entity_actions()`, only on the `wp_entity_actions_api_init` action, which fires the first time the registry is read after `init`. The `GET /wp/v2/entity-actions` route returns the actions of an entity and their script modules, and the script modules are added to the import map of the pages that load the editor script, as dynamic module dependencies of `wp-editor`. Backports WordPress/gutenberg#84650.
aa7a7e0 to
934e73a
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
See #66343. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
oandregal
left a comment
There was a problem hiding this comment.
Did some smoke testing and 1) the happy path works as expected and 2) hooking into the wrong hook raises a warning.
The `context` and `modalSize` enums of the action schema now read WP_Entity_Actions_Registry::SUPPORTED_VALUES instead of repeating the values.
Adds a registry for the actions DataViews shows for an entity, so they can be registered on the server with `wp_register_entity_actions()` and `wp_unregister_entity_actions()` on the `wp_entity_actions_api_init` action. An action definition is the serializable part of a DataViews action; its JavaScript parts come from a script module registered with the actions. The `GET /wp/v2/entity-actions` route returns the actions of an entity and their script modules, which are also added to the import map of the pages that load the editor script. Developed in #14276. Synced from WordPress/gutenberg#84650. Props jorgefilipecosta, oandregal. Fixes #66343. git-svn-id: https://develop.svn.wordpress.org/trunk@64517 602fd350-edb4-49c9-b593-d223f7449a82
Adds a registry for the actions DataViews shows for an entity, so they can be registered on the server with `wp_register_entity_actions()` and `wp_unregister_entity_actions()` on the `wp_entity_actions_api_init` action. An action definition is the serializable part of a DataViews action; its JavaScript parts come from a script module registered with the actions. The `GET /wp/v2/entity-actions` route returns the actions of an entity and their script modules, which are also added to the import map of the pages that load the editor script. Developed in WordPress/wordpress-develop#14276. Synced from WordPress/gutenberg#84650. Props jorgefilipecosta, oandregal. Fixes #66343. Built from https://develop.svn.wordpress.org/trunk@64517 git-svn-id: http://core.svn.wordpress.org/trunk@63667 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Backports the server side of the entity actions API from WordPress/gutenberg#84650, part of WordPress/gutenberg#83537.
Right now the actions DataViews shows for an entity, in the lists of the site editor and in the inspector of the editor, are only defined on the client. The editor computes the core ones, and plugins can only add or remove actions in JavaScript, with
registerEntityAction()andunregisterEntityAction(), which only exist in the Gutenberg plugin. This PR lets them be registered on the server, the way the entity fields API (#13911) does for fields.This PR:
WP_Entity_Actions_Registry, thewp_entity_actions_api_initaction,wp_register_entity_actions()andwp_unregister_entity_actions(), and thewp_get_registered_entity_actions(),wp_get_registered_entity_action_modules()andwp_get_all_registered_entity_action_modules()readers. The registry fireswp_entity_actions_api_initthe first time it is read afterinit, only accepts registrations while that action fires, reports misuse with_doing_it_wrong(), and keeps who registered each action in itsorigin.id,label,isPrimary,supportsBulk,contextand the modal options). The JavaScript parts (callback,RenderModal,isEligible,icon, …) come from a script module registered with the actions: its default export maps the id of each action to its parts, and it only applies to the actions it was registered with. The registry reports acontextor amodalSizethat DataViews does not support, which would otherwise hide the action without a word, and removes acallback,RenderModalorisEligibleset in PHP, which only a script module can provide.GET /wp/v2/entity-actions?kind=<kind>&name=<name>route, which returns the actions of an entity and its script modules, each with the ids of the actions it applies to. It requires the same capabilities as the view config route. When a post type or taxonomy has its items on/wp/v2/entity-actions, the route is left to it, and that is reported.module_dependenciesofwp-editor.Some notes:
wp_update_entity_actions(). To change an action, a plugin unregisters it and registers it again with the script module of the original, which keeps its JavaScript parts.WP_Entity_Actions_Registryis not final and its members are protected, because the Gutenberg plugin replaces the registry with a subclass oninit, as it does for the entity fields registry.Testing Instructions
Save this snippet as
wp-content/mu-plugins/entity-actions-test.php(src/wp-content/mu-plugins/in a wordpress-develop checkout):Open
wp-admin/post-new.php?post_type=page. In the console, runawait wp.apiFetch( { path: '/wp/v2/entity-actions?kind=postType&name=page' } )and verify it returns the action, withoriginset to{ registeredBy: 'my-plugin' }, and themy-plugin/actionsmodule with[ 'my-plugin/feature' ]as its actions.In the same console, run
JSON.parse( document.querySelector( 'script[type=importmap]' ).textContent ).imports[ 'my-plugin/actions' ]and verify it returns the URL of the module.Run
await wp.apiFetch( { path: '/wp/v2/entity-actions?kind=postType&name=missing' } )and verify it fails withrest_entity_actions_invalid_entity.Move the
wp_register_entity_actions()call into theinitcallback. WithWP_DEBUGon, reload and verify a notice says actions can only be registered and unregistered on thewp_entity_actions_api_initaction, and that the route no longer returns the action.Delete the snippet and run
npm run test:php -- --group entity-actions.Gutenberg PR: WordPress/gutenberg#84650
Trac ticket: https://core.trac.wordpress.org/ticket/66343
Use of AI Tools
AI assistance: Yes
Used for: porting the code and the tests from the Gutenberg PR, and drafting this description; reviewed and edited by me.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.