Skip to content

Commit 4c9fe2c

Browse files
Editor: Register the core entity actions on the server.
Loads `register_core_entity_actions()` from the `core-actions` package, which registers the actions of posts, pages, patterns and template parts through the entity actions API at priority 0, so a plugin can unregister or replace them. The build copies its PHP files to `wp-includes/build/scripts/core-actions/`. The post editor also preloads the actions of the post type it edits. Synced from WordPress/gutenberg#84660. See #66343.
1 parent fef7474 commit 4c9fe2c

7 files changed

Lines changed: 616 additions & 7 deletions

File tree

‎Gruntfile.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ module.exports = function(grunt) {
697697
src: 'vendor/composer/ca-bundle/res/cacert.pem',
698698
dest: SOURCE_DIR + 'wp-includes/certificates/ca-bundle.crt'
699699
},
700-
// Gutenberg PHP infrastructure files (routes.php, pages.php, constants.php, pages/).
700+
// Gutenberg PHP infrastructure files (routes.php, pages.php, constants.php, pages/, core actions).
701701
'gutenberg-php': {
702702
options: {
703703
process: function( content ) {
@@ -716,6 +716,8 @@ module.exports = function(grunt) {
716716
'pages.php',
717717
'constants.php',
718718
'pages/**/*.php',
719+
// The actions Core registers through the entity actions API.
720+
'scripts/core-actions/**/*.php',
719721
],
720722
dest: SOURCE_DIR + 'wp-includes/build/',
721723
} ],

‎src/wp-includes/default-filters.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@
855855

856856
// Entity Actions API.
857857
add_action( 'admin_footer', '_wp_add_entity_action_modules_to_editor_script', 10, 0 );
858+
add_filter( 'block_editor_rest_api_preload_paths', '_wp_preload_entity_actions', 10, 2 );
858859

859860
// Entity Fields API.
860861
add_action( 'admin_footer', '_wp_add_entity_field_modules_to_editor_script', 10, 0 );

‎src/wp-includes/entity-actions.php‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*
1616
* Actions are registered on the `wp_entity_actions_api_init` action, and only there,
1717
* with {@see wp_register_entity_actions()} and {@see wp_unregister_entity_actions()}. The
18-
* other functions read the registry.
18+
* other functions read the registry. WordPress core registers its own
19+
* actions the same way, from packages/core-actions.
1920
*
2021
* @package WordPress
2122
* @subpackage Entity Actions
@@ -186,3 +187,32 @@ function _wp_add_entity_action_modules_to_editor_script( $scripts = null ) {
186187
$scripts->add_data( 'wp-editor', 'module_dependencies', $dependencies );
187188
}
188189
}
190+
191+
/**
192+
* Preloads the actions of the post type the post editor edits.
193+
*
194+
* The actions menu of the post editor reads them from the `wp/v2/entity-actions`
195+
* route as it loads, see `useEntityActions()` in
196+
* packages/editor/src/dataviews/entity-actions-loader.ts. The path must match the
197+
* request of the `getEntityActionsConfig` core data resolver, see
198+
* packages/core-data/src/resolvers.js.
199+
*
200+
* @since 7.2.0
201+
*
202+
* @param array $paths REST API paths to preload.
203+
* @param WP_Block_Editor_Context $context Block editor context.
204+
* @return array Filtered preload paths.
205+
*/
206+
function _wp_preload_entity_actions( $paths, $context ) {
207+
if ( 'core/edit-post' === $context->name && isset( $context->post ) ) {
208+
$paths[] = add_query_arg(
209+
array(
210+
'kind' => 'postType',
211+
'name' => $context->post->post_type,
212+
),
213+
'/wp/v2/entity-actions'
214+
);
215+
}
216+
217+
return $paths;
218+
}

‎src/wp-settings.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@
254254
if ( file_exists( ABSPATH . WPINC . '/build/pages.php' ) ) {
255255
require ABSPATH . WPINC . '/build/pages.php';
256256
}
257+
if ( file_exists( ABSPATH . WPINC . '/build/scripts/core-actions/index.php' ) ) {
258+
require ABSPATH . WPINC . '/build/scripts/core-actions/index.php';
259+
}
257260
require ABSPATH . WPINC . '/taxonomy.php';
258261
require ABSPATH . WPINC . '/class-wp-taxonomy.php';
259262
require ABSPATH . WPINC . '/class-wp-term.php';

‎tests/phpunit/tests/entity-actions.php‎

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @group entity-actions
99
*
1010
* @covers ::_wp_add_entity_action_modules_to_editor_script
11+
* @covers ::_wp_preload_entity_actions
1112
* @covers ::wp_register_entity_actions
1213
* @covers ::wp_unregister_entity_actions
1314
* @covers WP_Entity_Actions_Registry::initialize
@@ -17,12 +18,15 @@
1718
class Tests_Entity_Actions extends WP_UnitTestCase {
1819

1920
/**
20-
* Sets up each test: the default actions of the tests are registered at
21-
* priority 0, so every test runs against a registry holding actions
22-
* registered before the callbacks hooked at the default priority run.
21+
* Sets up each test: the actions WordPress registers are replaced with
22+
* the default actions of the tests, hooked at priority 0 as WordPress
23+
* registers its own, so every test runs against a registry holding
24+
* actions registered before the callbacks hooked at the default priority
25+
* run.
2326
*/
2427
public function set_up() {
2528
parent::set_up();
29+
remove_action( 'wp_entity_actions_api_init', 'register_core_entity_actions', 0 );
2630
add_action( 'wp_entity_actions_api_init', array( __CLASS__, 'register_default_actions' ), 0 );
2731
}
2832

@@ -31,8 +35,9 @@ public function set_up() {
3135
*
3236
* Resetting the registry drops the actions a test registered along with
3337
* the defaults; the next read fires `wp_entity_actions_api_init` again and
34-
* registers the defaults anew. The test case removes the callbacks the
35-
* test hooked, the defaults included.
38+
* registers the defaults anew. The test case restores the hooks: it
39+
* removes the callbacks the test hooked, the defaults included, and hooks
40+
* the actions WordPress registers back.
3641
*/
3742
public function tear_down() {
3843
self::reset_registry();
@@ -387,6 +392,51 @@ public function test_action_script_modules_reach_the_import_map() {
387392
$this->assertArrayHasKey( 'plugin/feature', $import_map['imports'] );
388393
}
389394

395+
/**
396+
* The post editor preloads the actions of the post type it edits, with
397+
* the path the `getEntityActionsConfig` resolver of core data requests.
398+
*
399+
* @ticket 66343
400+
*/
401+
public function test_the_post_editor_preloads_the_actions_of_the_post_type() {
402+
$context = new WP_Block_Editor_Context( array( 'post' => self::factory()->post->create_and_get( array( 'post_type' => 'page' ) ) ) );
403+
404+
$paths = apply_filters( 'block_editor_rest_api_preload_paths', array(), $context );
405+
406+
$this->assertContains( '/wp/v2/entity-actions?kind=postType&name=page', $paths, 'The actions of pages are preloaded.' );
407+
}
408+
409+
/**
410+
* The preloaded path serves the actions of the post type.
411+
*
412+
* @ticket 66343
413+
*/
414+
public function test_the_preloaded_actions_path_serves_the_actions() {
415+
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
416+
$context = new WP_Block_Editor_Context( array( 'post' => self::factory()->post->create_and_get( array( 'post_type' => 'page' ) ) ) );
417+
418+
$paths = _wp_preload_entity_actions( array(), $context );
419+
$preloaded = array_reduce( $paths, 'rest_preload_api_request', array() );
420+
421+
$this->assertSame(
422+
array( 'rename-post', 'move-to-trash' ),
423+
array_column( $preloaded['/wp/v2/entity-actions?kind=postType&name=page']['body']['actions'], 'id' ),
424+
'The preloaded response holds the actions of pages.'
425+
);
426+
}
427+
428+
/**
429+
* Only the post editor preloads the actions: the other editors have no
430+
* post type of their own.
431+
*
432+
* @ticket 66343
433+
*/
434+
public function test_other_editors_do_not_preload_the_actions() {
435+
$context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
436+
437+
$this->assertSame( array( '/wp/v2/types' ), _wp_preload_entity_actions( array( '/wp/v2/types' ), $context ), 'The paths are unchanged.' );
438+
}
439+
390440
/**
391441
* Reading the registry fires `wp_entity_actions_api_init` once, whichever
392442
* getter is read first and however many times it is read.

0 commit comments

Comments
 (0)