Repository navigation
Editor: Add the entity actions API - #14276
Draft
jorgefilipecosta wants to merge 1 commit into
Draft
jorgefilipecosta wants to merge 1 commit into
jorgefilipecosta wants to merge 1 commit into
Conversation
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.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: TBD
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.