Skip to content

Commit 3ea03ed

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/`, translating their labels with the default text domain. The post editor also preloads the actions of the post type it edits. Synced from WordPress/gutenberg#84660. See #66343.
1 parent c30b2af commit 3ea03ed

6 files changed

Lines changed: 625 additions & 9 deletions

File tree

‎Gruntfile.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,15 +697,25 @@ 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: {
703-
process: function( content ) {
703+
process: function( content, srcpath ) {
704704
// Fix boot module asset file path for Core's different directory structure.
705-
return content.replace(
705+
content = content.replace(
706706
/__DIR__\s*\.\s*(['"])\/..\/\..\/modules\/boot\/index\.min\.asset\.php\1/g,
707707
'ABSPATH . WPINC . \'/js/dist/script-modules/boot/index.min.asset.php\''
708708
);
709+
710+
/*
711+
* The PHP files of the packages translate their strings with the `gutenberg`
712+
* text domain of the plugin. Core translates them with the default one.
713+
*/
714+
if ( srcpath.startsWith( 'gutenberg/build/scripts/' ) ) {
715+
content = content.replace( /,\s*'gutenberg'(\s*\))/g, '$1' );
716+
}
717+
718+
return content;
709719
}
710720
},
711721
files: [ {
@@ -716,6 +726,8 @@ module.exports = function(grunt) {
716726
'pages.php',
717727
'constants.php',
718728
'pages/**/*.php',
729+
// The actions Core registers through the entity actions API.
730+
'scripts/core-actions/**/*.php',
719731
],
720732
dest: SOURCE_DIR + 'wp-includes/build/',
721733
} ],

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,5 +855,6 @@
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
unset( $filter, $action, $post_type );

‎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
@@ -252,6 +252,9 @@
252252
if ( file_exists( ABSPATH . WPINC . '/build/pages.php' ) ) {
253253
require ABSPATH . WPINC . '/build/pages.php';
254254
}
255+
if ( file_exists( ABSPATH . WPINC . '/build/scripts/core-actions/index.php' ) ) {
256+
require ABSPATH . WPINC . '/build/scripts/core-actions/index.php';
257+
}
255258
require ABSPATH . WPINC . '/taxonomy.php';
256259
require ABSPATH . WPINC . '/class-wp-taxonomy.php';
257260
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)