From a7255fe49e7b66bc390d1778b561cacc6df642ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 13:45:51 +0200 Subject: [PATCH 01/14] Editor: Guard entity preload filters against non-array paths. `_wp_preload_entity_actions()` and `_wp_preload_entity_fields()` assumed `$paths` was an array. If an earlier `block_editor_rest_api_preload_paths` callback returned `null`, `false`, `''`, another string or an object, the editor crashed. Empty values are now turned into an array so the entity paths are still added. Other non-array values, or a context that isn't a `WP_Block_Editor_Context`, are returned unchanged. `$context` defaults to `null` so a call with a single argument no longer throws an `ArgumentCountError`. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/entity-actions.php | 10 +++++++++- src/wp-includes/entity-fields.php | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/entity-actions.php b/src/wp-includes/entity-actions.php index 426fd1d30943a..9f77be4c9ff2c 100644 --- a/src/wp-includes/entity-actions.php +++ b/src/wp-includes/entity-actions.php @@ -206,7 +206,15 @@ function _wp_add_entity_action_modules_to_editor_script( $scripts = null ) { * @param WP_Block_Editor_Context $context Block editor context. * @return array Filtered preload paths. */ -function _wp_preload_entity_actions( $paths, $context ) { +function _wp_preload_entity_actions( $paths, $context = null ) { + if ( empty( $paths ) && ! is_array( $paths ) ) { + $paths = array(); + } + + if ( ! is_array( $paths ) || ! $context instanceof WP_Block_Editor_Context ) { + return $paths; + } + $post_types = array(); if ( 'core/edit-post' === $context->name && isset( $context->post ) ) { $post_types[] = $context->post->post_type; diff --git a/src/wp-includes/entity-fields.php b/src/wp-includes/entity-fields.php index 779a32f7fe3f2..2a730ad59c9d0 100644 --- a/src/wp-includes/entity-fields.php +++ b/src/wp-includes/entity-fields.php @@ -338,7 +338,15 @@ function _wp_get_site_editor_screen_post_types() { * @param WP_Block_Editor_Context $context Block editor context. * @return array Filtered preload paths. */ -function _wp_preload_entity_fields( $paths, $context ) { +function _wp_preload_entity_fields( $paths, $context = null ) { + if ( empty( $paths ) && ! is_array( $paths ) ) { + $paths = array(); + } + + if ( ! is_array( $paths ) || ! $context instanceof WP_Block_Editor_Context ) { + return $paths; + } + $post_types = array(); if ( 'core/edit-post' === $context->name && isset( $context->post ) ) { $post_types[] = $context->post->post_type; From 0bd8747b5d0091356a63b9ea6de76fd10d77ef19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 13:47:45 +0200 Subject: [PATCH 02/14] Editor: Inline the entity fields preload paths helper. `_wp_get_post_type_fields_preload_paths()` was only used by `_wp_preload_entity_fields()`. The loop now lives in the callback, matching `_wp_preload_entity_actions()`, and the helper is removed. The test for unregistered post types now goes through the callback. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/entity-fields.php | 54 ++++++------------- .../entity-fields/wpPreloadEntityFields.php | 11 ++-- 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/src/wp-includes/entity-fields.php b/src/wp-includes/entity-fields.php index 2a730ad59c9d0..08d3a73c05607 100644 --- a/src/wp-includes/entity-fields.php +++ b/src/wp-includes/entity-fields.php @@ -255,38 +255,6 @@ function wp_register_entity_field_collection( $directory ) { return WP_Entity_Fields_Registry::get_instance()->register_collection( $directory ); } -/** - * Returns the paths that serve the fields of the given post types, skipping - * the post types that are not registered. - * - * A path must match the request of the `getFieldsConfig` core data resolver, - * see packages/core-data/src/resolvers.js in Gutenberg. - * - * @since 7.2.0 - * @access private - * - * @param string[] $post_types The post types. - * @return string[] One path of the `/wp/v2/entity-fields` route per post type, - * in the order the post types are given, without duplicates. - */ -function _wp_get_post_type_fields_preload_paths( $post_types ) { - $paths = array(); - foreach ( array_unique( $post_types ) as $post_type ) { - if ( ! post_type_exists( $post_type ) ) { - continue; - } - $paths[] = add_query_arg( - array( - 'kind' => 'postType', - 'name' => $post_type, - ), - '/wp/v2/entity-fields' - ); - } - - return $paths; -} - /** * Returns the post types whose items the screen of the site editor being * loaded lists or edits, from its `p` query arg. @@ -329,7 +297,9 @@ function _wp_get_site_editor_screen_post_types() { * in packages/editor/src/dataviews/fields-loader/index.ts in Gutenberg. The * post editor reads those of the edited post in the post summary of its * inspector. The site editor reads those of the edited post, if any, and of - * the post types of the screen it opens on. + * the post types of the screen it opens on. A path must match the request of + * the `getFieldsConfig` core data resolver, see + * packages/core-data/src/resolvers.js in Gutenberg. * * @since 7.2.0 * @access private @@ -357,8 +327,18 @@ function _wp_preload_entity_fields( $paths, $context = null ) { $post_types = array_merge( $post_types, _wp_get_site_editor_screen_post_types() ); } - return array_merge( - $paths, - _wp_get_post_type_fields_preload_paths( $post_types ) - ); + foreach ( array_unique( $post_types ) as $post_type ) { + if ( ! post_type_exists( $post_type ) ) { + continue; + } + $paths[] = add_query_arg( + array( + 'kind' => 'postType', + 'name' => $post_type, + ), + '/wp/v2/entity-fields' + ); + } + + return $paths; } diff --git a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php index 58593c5727c36..3373be296273e 100644 --- a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php +++ b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php @@ -12,7 +12,6 @@ * @group entity-fields * * @covers ::_wp_preload_entity_fields - * @covers ::_wp_get_post_type_fields_preload_paths * @covers ::_wp_get_site_editor_screen_post_types */ class Tests_Entity_Fields_WpPreloadEntityFields extends WP_Entity_Fields_UnitTestCase { @@ -140,10 +139,14 @@ public function test_the_site_editor_preloads_the_fields_of_the_edited_post() { * @ticket 66229 */ public function test_unregistered_post_types_are_not_preloaded() { - $this->assertSame( - array( '/wp/v2/entity-fields?kind=postType&name=page' ), - _wp_get_post_type_fields_preload_paths( array( 'page', 'not_a_post_type', 'page' ) ) + $context = new WP_Block_Editor_Context( + array( + 'name' => 'core/edit-post', + 'post' => new WP_Post( (object) array( 'post_type' => 'not_a_post_type' ) ), + ) ); + + $this->assertSame( array(), _wp_preload_entity_fields( array(), $context ) ); } /** From bba4a8695a7b7a60372484ced26873970a9cf338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:05:54 +0200 Subject: [PATCH 03/14] Editor: Share the entity actions and fields preload logic. `_wp_preload_entity_actions()` and `_wp_preload_entity_fields()` only differed in the route they added paths for. The shared logic now lives in `_wp_add_entity_preload_paths()`, which takes the route, and both callbacks call it. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/entity-actions.php | 33 +------------ src/wp-includes/entity-fields.php | 49 ++++++++++++++----- tests/phpunit/tests/entity-actions.php | 1 + .../entity-fields/wpPreloadEntityFields.php | 1 + 4 files changed, 39 insertions(+), 45 deletions(-) diff --git a/src/wp-includes/entity-actions.php b/src/wp-includes/entity-actions.php index 9f77be4c9ff2c..599adfe74e66e 100644 --- a/src/wp-includes/entity-actions.php +++ b/src/wp-includes/entity-actions.php @@ -207,36 +207,5 @@ function _wp_add_entity_action_modules_to_editor_script( $scripts = null ) { * @return array Filtered preload paths. */ function _wp_preload_entity_actions( $paths, $context = null ) { - if ( empty( $paths ) && ! is_array( $paths ) ) { - $paths = array(); - } - - if ( ! is_array( $paths ) || ! $context instanceof WP_Block_Editor_Context ) { - return $paths; - } - - $post_types = array(); - if ( 'core/edit-post' === $context->name && isset( $context->post ) ) { - $post_types[] = $context->post->post_type; - } elseif ( 'core/edit-site' === $context->name ) { - if ( isset( $context->post ) ) { - $post_types[] = $context->post->post_type; - } - $post_types = array_merge( $post_types, _wp_get_site_editor_screen_post_types() ); - } - - foreach ( array_unique( $post_types ) as $post_type ) { - if ( ! post_type_exists( $post_type ) ) { - continue; - } - $paths[] = add_query_arg( - array( - 'kind' => 'postType', - 'name' => $post_type, - ), - '/wp/v2/entity-actions' - ); - } - - return $paths; + return _wp_add_entity_preload_paths( $paths, $context, '/wp/v2/entity-actions' ); } diff --git a/src/wp-includes/entity-fields.php b/src/wp-includes/entity-fields.php index 08d3a73c05607..5a39b933474f7 100644 --- a/src/wp-includes/entity-fields.php +++ b/src/wp-includes/entity-fields.php @@ -291,24 +291,25 @@ function _wp_get_site_editor_screen_post_types() { } /** - * Preloads the fields of the post types the editors list. + * Adds the paths of an entity route for the post types the editors list. * - * The editors read them from the `/wp/v2/entity-fields` route, see `useFields` - * in packages/editor/src/dataviews/fields-loader/index.ts in Gutenberg. The - * post editor reads those of the edited post in the post summary of its - * inspector. The site editor reads those of the edited post, if any, and of - * the post types of the screen it opens on. A path must match the request of - * the `getFieldsConfig` core data resolver, see - * packages/core-data/src/resolvers.js in Gutenberg. + * The post editor lists the post type of the edited post. The site editor + * lists that of the edited post, if any, and those of the screen it opens on. + * Post types that are not registered are skipped, and each post type is added + * once. * * @since 7.2.0 * @access private * - * @param array $paths REST API paths to preload. - * @param WP_Block_Editor_Context $context Block editor context. - * @return array Filtered preload paths. + * @param mixed $paths REST API paths to preload. + * @param mixed $context Block editor context. + * @param string $route The entity route, such as `/wp/v2/entity-fields`. + * @return mixed The paths with one path per post type added. An empty value + * that is not an array becomes an array. Any other value that + * is not an array, or a context that is not a block editor + * context, returns `$paths` unchanged. */ -function _wp_preload_entity_fields( $paths, $context = null ) { +function _wp_add_entity_preload_paths( $paths, $context, $route ) { if ( empty( $paths ) && ! is_array( $paths ) ) { $paths = array(); } @@ -336,9 +337,31 @@ function _wp_preload_entity_fields( $paths, $context = null ) { 'kind' => 'postType', 'name' => $post_type, ), - '/wp/v2/entity-fields' + $route ); } return $paths; } + +/** + * Preloads the fields of the post types the editors list. + * + * The editors read them from the `/wp/v2/entity-fields` route, see `useFields` + * in packages/editor/src/dataviews/fields-loader/index.ts in Gutenberg. The + * post editor reads those of the edited post in the post summary of its + * inspector. The site editor reads those of the edited post, if any, and of + * the post types of the screen it opens on. A path must match the request of + * the `getFieldsConfig` core data resolver, see + * packages/core-data/src/resolvers.js in Gutenberg. + * + * @since 7.2.0 + * @access private + * + * @param array $paths REST API paths to preload. + * @param WP_Block_Editor_Context $context Block editor context. + * @return array Filtered preload paths. + */ +function _wp_preload_entity_fields( $paths, $context = null ) { + return _wp_add_entity_preload_paths( $paths, $context, '/wp/v2/entity-fields' ); +} diff --git a/tests/phpunit/tests/entity-actions.php b/tests/phpunit/tests/entity-actions.php index 2928351e5df10..5cc6776224ee7 100644 --- a/tests/phpunit/tests/entity-actions.php +++ b/tests/phpunit/tests/entity-actions.php @@ -8,6 +8,7 @@ * @group entity-actions * * @covers ::_wp_add_entity_action_modules_to_editor_script + * @covers ::_wp_add_entity_preload_paths * @covers ::_wp_preload_entity_actions * @covers ::wp_get_all_registered_entity_action_modules * @covers ::wp_register_entity_actions diff --git a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php index 3373be296273e..ca96b884bc7f8 100644 --- a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php +++ b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php @@ -11,6 +11,7 @@ * * @group entity-fields * + * @covers ::_wp_add_entity_preload_paths * @covers ::_wp_preload_entity_fields * @covers ::_wp_get_site_editor_screen_post_types */ From 4bc177652a0851c44c3b4d20d242af17237acc4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:18:39 +0200 Subject: [PATCH 04/14] Editor: Inline the site editor screen post types helper. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/entity-fields.php | 58 +++++++------------ .../entity-fields/wpPreloadEntityFields.php | 1 - 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/src/wp-includes/entity-fields.php b/src/wp-includes/entity-fields.php index 5a39b933474f7..34b28a5f38bde 100644 --- a/src/wp-includes/entity-fields.php +++ b/src/wp-includes/entity-fields.php @@ -255,41 +255,6 @@ function wp_register_entity_field_collection( $directory ) { return WP_Entity_Fields_Registry::get_instance()->register_collection( $directory ); } -/** - * Returns the post types whose items the screen of the site editor being - * loaded lists or edits, from its `p` query arg. - * - * The screens are the routes in - * packages/edit-site/src/components/site-editor-routes in Gutenberg: the - * lists and the editors of pages, templates, patterns, and template parts. - * The patterns screen lists both patterns and template parts. - * - * @since 7.2.0 - * @access private - * - * @return string[] The post types, empty for any other screen. - */ -function _wp_get_site_editor_screen_post_types() { - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks what to preload. - $path = isset( $_GET['p'] ) && is_string( $_GET['p'] ) ? sanitize_text_field( wp_unslash( $_GET['p'] ) ) : ''; - $segments = explode( '/', trim( $path, '/' ) ); - - switch ( $segments[0] ) { - case 'page': - return array( 'page' ); - case 'template': - case 'wp_template': - return array( 'wp_template' ); - case 'pattern': - return array( 'wp_block', 'wp_template_part' ); - case 'wp_block': - return array( 'wp_block' ); - case 'wp_template_part': - return array( 'wp_template_part' ); - } - return array(); -} - /** * Adds the paths of an entity route for the post types the editors list. * @@ -325,7 +290,28 @@ function _wp_add_entity_preload_paths( $paths, $context, $route ) { if ( isset( $context->post ) ) { $post_types[] = $context->post->post_type; } - $post_types = array_merge( $post_types, _wp_get_site_editor_screen_post_types() ); + + /* + * Adds the post types the screen lists or edits, from the first + * segment of its `p` query arg. The screens are the routes in + * packages/edit-site/src/components/site-editor-routes in Gutenberg. + * The patterns screen lists both patterns and template parts. + */ + $screen_post_types = array( + 'page' => array( 'page' ), + 'template' => array( 'wp_template' ), + 'wp_template' => array( 'wp_template' ), + 'pattern' => array( 'wp_block', 'wp_template_part' ), + 'wp_block' => array( 'wp_block' ), + 'wp_template_part' => array( 'wp_template_part' ), + ); + + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks what to preload. + $screen = isset( $_GET['p'] ) && is_string( $_GET['p'] ) ? sanitize_text_field( wp_unslash( $_GET['p'] ) ) : ''; + $segments = explode( '/', trim( $screen, '/' ) ); + if ( isset( $screen_post_types[ $segments[0] ] ) ) { + $post_types = array_merge( $post_types, $screen_post_types[ $segments[0] ] ); + } } foreach ( array_unique( $post_types ) as $post_type ) { diff --git a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php index ca96b884bc7f8..883c7af6ee9a2 100644 --- a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php +++ b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php @@ -13,7 +13,6 @@ * * @covers ::_wp_add_entity_preload_paths * @covers ::_wp_preload_entity_fields - * @covers ::_wp_get_site_editor_screen_post_types */ class Tests_Entity_Fields_WpPreloadEntityFields extends WP_Entity_Fields_UnitTestCase { From 8851c80cc89befd921787fc31c81b84e27a7514c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:31:11 +0200 Subject: [PATCH 05/14] Editor: List numeric entity field module ids as strings. `WP_Entity_Fields_Registry::get_all_registered_field_modules()` returned the module ids from the array keys, so a numeric id came back as an integer. They are now cast to strings, matching `WP_Entity_Actions_Registry::get_all_registered_action_modules()`, so the editor script declares the id as it was registered. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/class-wp-entity-fields-registry.php | 8 +++++++- .../tests/entity-fields/wpEntityFieldsRegistry.php | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-entity-fields-registry.php b/src/wp-includes/class-wp-entity-fields-registry.php index e25f2e026a0fe..9f3e6b8b8a479 100644 --- a/src/wp-includes/class-wp-entity-fields-registry.php +++ b/src/wp-includes/class-wp-entity-fields-registry.php @@ -499,7 +499,13 @@ public function get_all_registered_field_modules() { $this->initialize(); return array_map( static function ( $entities ) { - return array_map( 'array_keys', $entities ); + return array_map( + static function ( $modules ) { + // A numeric module id is an integer as an array key. + return array_map( 'strval', array_keys( $modules ) ); + }, + $entities + ); }, $this->field_modules ); diff --git a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php index e91c910b81c2d..215f6e34346d5 100644 --- a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php +++ b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php @@ -222,6 +222,18 @@ function () use ( &$result ) { $this->assertArrayNotHasKey( 'a/b', wp_get_all_registered_entity_field_modules(), 'The kind of the first entity, emptied, is forgotten.' ); } + /** + * A numeric module id is listed as a string, as it was registered, not + * as the integer its array key becomes. + * + * @ticket 66229 + */ + public function test_all_registered_field_modules_lists_a_numeric_module_id_as_a_string() { + $this->register_fields( 'postType', 'post', array( $this->field( 'color' ) ), '42' ); + + $this->assertSame( array( '42' ), wp_get_all_registered_entity_field_modules()['postType']['post'] ); + } + /** * Unregistering returns the definitions it removes, in registration * order and with their origin, whatever the order of the ids asked for; From b8b57c5518422900b6069a3217515b852e88e836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:31:54 +0200 Subject: [PATCH 06/14] Editor: Move the shared entity preload helper to its own file. `_wp_add_entity_preload_paths()` is used by both the Entity Actions and Entity Fields APIs, but lived in entity-fields.php, which is loaded after entity-actions.php. It now lives in entity-utils.php, loaded before both, so neither API depends on the other's file. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/entity-fields.php | 75 ---------------------------- src/wp-includes/entity-utils.php | 82 +++++++++++++++++++++++++++++++ src/wp-settings.php | 1 + 3 files changed, 83 insertions(+), 75 deletions(-) create mode 100644 src/wp-includes/entity-utils.php diff --git a/src/wp-includes/entity-fields.php b/src/wp-includes/entity-fields.php index 34b28a5f38bde..11330833cc502 100644 --- a/src/wp-includes/entity-fields.php +++ b/src/wp-includes/entity-fields.php @@ -255,81 +255,6 @@ function wp_register_entity_field_collection( $directory ) { return WP_Entity_Fields_Registry::get_instance()->register_collection( $directory ); } -/** - * Adds the paths of an entity route for the post types the editors list. - * - * The post editor lists the post type of the edited post. The site editor - * lists that of the edited post, if any, and those of the screen it opens on. - * Post types that are not registered are skipped, and each post type is added - * once. - * - * @since 7.2.0 - * @access private - * - * @param mixed $paths REST API paths to preload. - * @param mixed $context Block editor context. - * @param string $route The entity route, such as `/wp/v2/entity-fields`. - * @return mixed The paths with one path per post type added. An empty value - * that is not an array becomes an array. Any other value that - * is not an array, or a context that is not a block editor - * context, returns `$paths` unchanged. - */ -function _wp_add_entity_preload_paths( $paths, $context, $route ) { - if ( empty( $paths ) && ! is_array( $paths ) ) { - $paths = array(); - } - - if ( ! is_array( $paths ) || ! $context instanceof WP_Block_Editor_Context ) { - return $paths; - } - - $post_types = array(); - if ( 'core/edit-post' === $context->name && isset( $context->post ) ) { - $post_types[] = $context->post->post_type; - } elseif ( 'core/edit-site' === $context->name ) { - if ( isset( $context->post ) ) { - $post_types[] = $context->post->post_type; - } - - /* - * Adds the post types the screen lists or edits, from the first - * segment of its `p` query arg. The screens are the routes in - * packages/edit-site/src/components/site-editor-routes in Gutenberg. - * The patterns screen lists both patterns and template parts. - */ - $screen_post_types = array( - 'page' => array( 'page' ), - 'template' => array( 'wp_template' ), - 'wp_template' => array( 'wp_template' ), - 'pattern' => array( 'wp_block', 'wp_template_part' ), - 'wp_block' => array( 'wp_block' ), - 'wp_template_part' => array( 'wp_template_part' ), - ); - - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks what to preload. - $screen = isset( $_GET['p'] ) && is_string( $_GET['p'] ) ? sanitize_text_field( wp_unslash( $_GET['p'] ) ) : ''; - $segments = explode( '/', trim( $screen, '/' ) ); - if ( isset( $screen_post_types[ $segments[0] ] ) ) { - $post_types = array_merge( $post_types, $screen_post_types[ $segments[0] ] ); - } - } - - foreach ( array_unique( $post_types ) as $post_type ) { - if ( ! post_type_exists( $post_type ) ) { - continue; - } - $paths[] = add_query_arg( - array( - 'kind' => 'postType', - 'name' => $post_type, - ), - $route - ); - } - - return $paths; -} - /** * Preloads the fields of the post types the editors list. * diff --git a/src/wp-includes/entity-utils.php b/src/wp-includes/entity-utils.php new file mode 100644 index 0000000000000..2980f7b17fafa --- /dev/null +++ b/src/wp-includes/entity-utils.php @@ -0,0 +1,82 @@ +name && isset( $context->post ) ) { + $post_types[] = $context->post->post_type; + } elseif ( 'core/edit-site' === $context->name ) { + if ( isset( $context->post ) ) { + $post_types[] = $context->post->post_type; + } + + /* + * Adds the post types the screen lists or edits, from the first + * segment of its `p` query arg. The screens are the routes in + * packages/edit-site/src/components/site-editor-routes in Gutenberg. + * The patterns screen lists both patterns and template parts. + */ + $screen_post_types = array( + 'page' => array( 'page' ), + 'template' => array( 'wp_template' ), + 'wp_template' => array( 'wp_template' ), + 'pattern' => array( 'wp_block', 'wp_template_part' ), + 'wp_block' => array( 'wp_block' ), + 'wp_template_part' => array( 'wp_template_part' ), + ); + + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks what to preload. + $screen = isset( $_GET['p'] ) && is_string( $_GET['p'] ) ? sanitize_text_field( wp_unslash( $_GET['p'] ) ) : ''; + $segments = explode( '/', trim( $screen, '/' ) ); + if ( isset( $screen_post_types[ $segments[0] ] ) ) { + $post_types = array_merge( $post_types, $screen_post_types[ $segments[0] ] ); + } + } + + foreach ( array_unique( $post_types ) as $post_type ) { + if ( ! post_type_exists( $post_type ) ) { + continue; + } + $paths[] = add_query_arg( + array( + 'kind' => 'postType', + 'name' => $post_type, + ), + $route + ); + } + + return $paths; +} diff --git a/src/wp-settings.php b/src/wp-settings.php index 8bd37b43d635e..cf8fb4d090781 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -206,6 +206,7 @@ require ABSPATH . WPINC . '/template.php'; require ABSPATH . WPINC . '/class-wp-view-config-data.php'; require ABSPATH . WPINC . '/view-config.php'; +require ABSPATH . WPINC . '/entity-utils.php'; require ABSPATH . WPINC . '/class-wp-entity-actions-registry.php'; require ABSPATH . WPINC . '/entity-actions.php'; require ABSPATH . WPINC . '/class-wp-entity-fields-registry.php'; From 47f84c9f2219dd68e5df651ea79decfd83115478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:32:32 +0200 Subject: [PATCH 07/14] Editor: Share the entity actions and fields editor script logic. `_wp_add_entity_action_modules_to_editor_script()` and `_wp_add_entity_field_modules_to_editor_script()` only differed in the registry they read the script modules from. The shared logic now lives in `_wp_add_entity_modules_to_editor_script()` in entity-utils.php, which takes the reader of the modules, and both callbacks call it. The reader is a callable rather than the modules themselves so the registry is still only read, and its init action only fired, on the pages that load the editor script. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/entity-actions.php | 68 ++--------------- src/wp-includes/entity-fields.php | 73 ++---------------- src/wp-includes/entity-utils.php | 74 +++++++++++++++++++ tests/phpunit/tests/entity-actions.php | 1 + .../wpAddEntityFieldModulesToEditorScript.php | 1 + 5 files changed, 88 insertions(+), 129 deletions(-) diff --git a/src/wp-includes/entity-actions.php b/src/wp-includes/entity-actions.php index 599adfe74e66e..bb40b6edfe9b6 100644 --- a/src/wp-includes/entity-actions.php +++ b/src/wp-includes/entity-actions.php @@ -114,26 +114,10 @@ function wp_get_all_registered_entity_action_modules() { /** * Declares the script modules of the entity actions as dependencies of the - * editor script. - * - * The editor imports the script modules of an entity on demand, with a dynamic - * `import()` the browser resolves through the import map of the page. Listing - * the modules as dynamic `module_dependencies` of the `wp-editor` script adds - * them to the import map of every page that loads the editor script: the post - * editor, the site editor, and the pages booted by `@wordpress/boot`, whose - * prerequisites depend on it. A dynamic dependency is only listed in the - * import map; the module is fetched when it is imported. - * - * Reading the registry fires `wp_entity_actions_api_init`, so the actions and their - * script modules are registered on demand. It only reads the registry on the - * pages that load the editor script, not on every admin request (heartbeat, - * Ajax, other screens). It runs on `admin_footer`, the last action before the - * import map is printed that fires both in the admin template and in the pages - * rendered outside it, which print the import map themselves. By then `init` - * has completed, so the post types and supports the actions derive from are - * final. - * The registry only accepts registrations while the action fires, so the - * modules declared here are the modules of every action the page can show. + * editor script, see _wp_add_entity_modules_to_editor_script(). + * + * The actions are loaded by `usePostActions()` of the editor script, so every + * screen showing them loads it. * * The `admin_footer` action passes it no argument, so the global registry is * used. In other scenarios (tests), the scripts are given directly. @@ -143,49 +127,7 @@ function wp_get_all_registered_entity_action_modules() { * @param WP_Scripts|null $scripts The scripts registry. Defaults to the global one. */ function _wp_add_entity_action_modules_to_editor_script( $scripts = null ) { - if ( ! $scripts instanceof WP_Scripts ) { - $scripts = wp_scripts(); - } - - // The actions are loaded by `usePostActions()` of the editor script, so - // every screen showing them loads it. - if ( ! $scripts->query( 'wp-editor', 'enqueued' ) && ! $scripts->query( 'wp-editor', 'done' ) ) { - return; - } - - $kinds = wp_get_all_registered_entity_action_modules(); - if ( empty( $kinds ) ) { - return; - } - - $dependencies = $scripts->get_data( 'wp-editor', 'module_dependencies' ); - $dependencies = is_array( $dependencies ) ? $dependencies : array(); - - $declared = array(); - foreach ( $dependencies as $dependency ) { - $declared[] = is_array( $dependency ) ? $dependency['id'] : $dependency; - } - - $added = false; - foreach ( $kinds as $entities ) { - foreach ( $entities as $modules ) { - foreach ( $modules as $module ) { - if ( ! in_array( $module, $declared, true ) ) { - $dependencies[] = array( - 'id' => $module, - 'import' => 'dynamic', - ); - $declared[] = $module; - $added = true; - } - } - } - } - - if ( $added ) { - // `add_data()` replaces the value, hence the merge above. - $scripts->add_data( 'wp-editor', 'module_dependencies', $dependencies ); - } + _wp_add_entity_modules_to_editor_script( $scripts, 'wp_get_all_registered_entity_action_modules' ); } /** diff --git a/src/wp-includes/entity-fields.php b/src/wp-includes/entity-fields.php index 11330833cc502..b8ab90dabd17d 100644 --- a/src/wp-includes/entity-fields.php +++ b/src/wp-includes/entity-fields.php @@ -144,25 +144,12 @@ function wp_get_all_registered_entity_field_modules() { /** * Declares the script modules of the entity fields as dependencies of the - * editor script. - * - * The editor imports the script modules of an entity on demand, with a dynamic - * `import()` the browser resolves through the import map of the page. Listing - * the modules as dynamic `module_dependencies` of the `wp-editor` script adds - * them to the import map of every page that loads the editor script: the post - * editor, the site editor, and the pages booted by `@wordpress/boot`, whose - * prerequisites depend on it. A dynamic dependency is only listed in the - * import map; the module is fetched when it is imported. - * - * Reading the registry fires `wp_entity_fields_api_init`, so the fields and their - * script modules are registered on demand. It only reads the registry on the - * pages that load the editor script, not on every admin request (heartbeat, - * Ajax, other screens). It runs on `admin_footer`, the last action before the - * import map is printed that fires both in the admin template and in the pages - * rendered outside it, which print the import map themselves. By then `init` - * has completed, so the supports the default fields derive from are final. - * The registry only accepts registrations while the action fires, so the - * modules declared here are the modules of every field the page can show. + * editor script, see _wp_add_entity_modules_to_editor_script(). + * + * The fields are loaded by the editor package, see + * packages/editor/src/dataviews/store/private-resolvers.ts in Gutenberg, which + * means any screen that wants to use this mechanism needs to load the editor + * script. * * It is hooked to `admin_footer` without arguments, so the global scripts * registry is used. In other scenarios (tests), the scripts are given directly. @@ -174,53 +161,7 @@ function wp_get_all_registered_entity_field_modules() { * the global one. */ function _wp_add_entity_field_modules_to_editor_script( $scripts = null ) { - if ( ! $scripts instanceof WP_Scripts ) { - $scripts = wp_scripts(); - } - - /* - * The fields are loaded by the editor package, see - * packages/editor/src/dataviews/store/private-resolvers.ts in Gutenberg, - * which means any screen that wants to use this mechanism needs to load - * the editor script. - */ - if ( ! $scripts->query( 'wp-editor', 'enqueued' ) && ! $scripts->query( 'wp-editor', 'done' ) ) { - return; - } - - $kinds = wp_get_all_registered_entity_field_modules(); - if ( empty( $kinds ) ) { - return; - } - - $dependencies = $scripts->get_data( 'wp-editor', 'module_dependencies' ); - $dependencies = is_array( $dependencies ) ? $dependencies : array(); - - $declared = array(); - foreach ( $dependencies as $dependency ) { - $declared[] = is_array( $dependency ) ? $dependency['id'] : $dependency; - } - - $added = false; - foreach ( $kinds as $entities ) { - foreach ( $entities as $modules ) { - foreach ( $modules as $module ) { - if ( ! in_array( $module, $declared, true ) ) { - $dependencies[] = array( - 'id' => $module, - 'import' => 'dynamic', - ); - $declared[] = $module; - $added = true; - } - } - } - } - - if ( $added ) { - // `add_data()` replaces the value, hence the merge above. - $scripts->add_data( 'wp-editor', 'module_dependencies', $dependencies ); - } + _wp_add_entity_modules_to_editor_script( $scripts, 'wp_get_all_registered_entity_field_modules' ); } /** diff --git a/src/wp-includes/entity-utils.php b/src/wp-includes/entity-utils.php index 2980f7b17fafa..9f2489886b18a 100644 --- a/src/wp-includes/entity-utils.php +++ b/src/wp-includes/entity-utils.php @@ -80,3 +80,77 @@ function _wp_add_entity_preload_paths( $paths, $context, $route ) { return $paths; } + +/** + * Declares the script modules of an entity API as dependencies of the editor + * script. + * + * The editor imports the script modules of an entity on demand, with a dynamic + * `import()` the browser resolves through the import map of the page. Listing + * the modules as dynamic `module_dependencies` of the `wp-editor` script adds + * them to the import map of every page that loads the editor script: the post + * editor, the site editor, and the pages booted by `@wordpress/boot`, whose + * prerequisites depend on it. A dynamic dependency is only listed in the + * import map; the module is fetched when it is imported. + * + * The modules are read with `$get_modules`, which reads the registry of the + * API and so fires its init action, registering the entities and their script + * modules on demand. It is only called on the pages that load the editor + * script, not on every admin request (heartbeat, Ajax, other screens). The + * callers run on `admin_footer`, the last action before the import map is + * printed that fires both in the admin template and in the pages rendered + * outside it, which print the import map themselves. By then `init` has + * completed, so the post types and supports the registrations derive from are + * final. The registries only accept registrations while their action fires, + * so the modules declared here are those of everything the page can show. + * + * @since 7.2.0 + * @access private + * + * @param WP_Scripts|null $scripts The scripts registry. Null for the global one. + * @param callable $get_modules Returns the module ids of the API, keyed + * by kind, then by name. + */ +function _wp_add_entity_modules_to_editor_script( $scripts, $get_modules ) { + if ( ! $scripts instanceof WP_Scripts ) { + $scripts = wp_scripts(); + } + + if ( ! $scripts->query( 'wp-editor', 'enqueued' ) && ! $scripts->query( 'wp-editor', 'done' ) ) { + return; + } + + $kinds = call_user_func( $get_modules ); + if ( empty( $kinds ) ) { + return; + } + + $dependencies = $scripts->get_data( 'wp-editor', 'module_dependencies' ); + $dependencies = is_array( $dependencies ) ? $dependencies : array(); + + $declared = array(); + foreach ( $dependencies as $dependency ) { + $declared[] = is_array( $dependency ) ? $dependency['id'] : $dependency; + } + + $added = false; + foreach ( $kinds as $entities ) { + foreach ( $entities as $modules ) { + foreach ( $modules as $module ) { + if ( ! in_array( $module, $declared, true ) ) { + $dependencies[] = array( + 'id' => $module, + 'import' => 'dynamic', + ); + $declared[] = $module; + $added = true; + } + } + } + } + + if ( $added ) { + // `add_data()` replaces the value, hence the merge above. + $scripts->add_data( 'wp-editor', 'module_dependencies', $dependencies ); + } +} diff --git a/tests/phpunit/tests/entity-actions.php b/tests/phpunit/tests/entity-actions.php index 5cc6776224ee7..12c4d95112eb1 100644 --- a/tests/phpunit/tests/entity-actions.php +++ b/tests/phpunit/tests/entity-actions.php @@ -8,6 +8,7 @@ * @group entity-actions * * @covers ::_wp_add_entity_action_modules_to_editor_script + * @covers ::_wp_add_entity_modules_to_editor_script * @covers ::_wp_add_entity_preload_paths * @covers ::_wp_preload_entity_actions * @covers ::wp_get_all_registered_entity_action_modules diff --git a/tests/phpunit/tests/entity-fields/wpAddEntityFieldModulesToEditorScript.php b/tests/phpunit/tests/entity-fields/wpAddEntityFieldModulesToEditorScript.php index f345156089e5d..aa7e4929c5360 100644 --- a/tests/phpunit/tests/entity-fields/wpAddEntityFieldModulesToEditorScript.php +++ b/tests/phpunit/tests/entity-fields/wpAddEntityFieldModulesToEditorScript.php @@ -12,6 +12,7 @@ * @group entity-fields * * @covers ::_wp_add_entity_field_modules_to_editor_script + * @covers ::_wp_add_entity_modules_to_editor_script */ class Tests_Entity_Fields_WpAddEntityFieldModulesToEditorScript extends WP_Entity_Fields_UnitTestCase { From d3e8ca71b7ae9afc697692a1e2dd080419c48e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:01:45 +0200 Subject: [PATCH 08/14] Editor: Document the shape returned by get_all_registered_field_modules(). Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/class-wp-entity-fields-registry.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/wp-includes/class-wp-entity-fields-registry.php b/src/wp-includes/class-wp-entity-fields-registry.php index 9f3e6b8b8a479..4f1e491ceda47 100644 --- a/src/wp-includes/class-wp-entity-fields-registry.php +++ b/src/wp-includes/class-wp-entity-fields-registry.php @@ -497,6 +497,16 @@ public function get_registered_field_modules( $kind, $name ) { */ public function get_all_registered_field_modules() { $this->initialize(); + /* + * Keeps the module ids and drops the field ids they apply to, e.g.: + * + * array( + * 'postType' => array( + * 'page' => array( 'my-plugin/fields', '123' ), + * 'post' => array( 'my-plugin/fields' ), + * ), + * ) + */ return array_map( static function ( $entities ) { return array_map( From 4e13c3ebd43e3bbc9c31ed8ac430f2872f206a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:03:19 +0200 Subject: [PATCH 09/14] Tests: Cover non-array and context-less entity preload paths. Co-Authored-By: Claude Opus 5.5 --- .../entity-fields/wpPreloadEntityFields.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php index 883c7af6ee9a2..0c3141326a82b 100644 --- a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php +++ b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php @@ -164,4 +164,60 @@ public function test_other_editors_do_not_preload_the_fields() { $this->assertSame( array(), _wp_preload_entity_fields( array(), $no_post ) ); $this->assertSame( array(), _wp_preload_entity_fields( array(), $widgets ) ); } + + /** + * Empty paths that are not an array become an array the fields are added + * to. Other paths that are not an array are returned unchanged. + * + * @ticket 66229 + * + * @dataProvider data_non_array_paths + * + * @param mixed $paths The paths passed to the filter. + * @param mixed $expected The filtered paths, or null for the fields path + * only. + */ + public function test_non_array_paths( $paths, $expected ) { + $post = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) ); + $context = new WP_Block_Editor_Context( + array( + 'name' => 'core/edit-post', + 'post' => $post, + ) + ); + + if ( null === $expected ) { + $expected = array( '/wp/v2/entity-fields?kind=postType&name=page' ); + } + + $this->assertSame( $expected, _wp_preload_entity_fields( $paths, $context ) ); + } + + /** + * Data provider for test_non_array_paths(). + * + * @return array[] + */ + public function data_non_array_paths() { + $object = new stdClass(); + + return array( + 'null' => array( null, null ), + 'false' => array( false, null ), + 'empty string' => array( '', null ), + 'string' => array( '/wp/v2/types', '/wp/v2/types' ), + 'object' => array( $object, $object ), + ); + } + + /** + * Called with the paths only, as by a filter added with one accepted + * argument, the paths are returned unchanged. + * + * @ticket 66229 + */ + public function test_the_paths_are_unchanged_without_a_context() { + $this->assertSame( array( '/wp/v2/types' ), _wp_preload_entity_fields( array( '/wp/v2/types' ) ) ); + $this->assertSame( array(), _wp_preload_entity_fields( null ) ); + } } From 475f95291d716882c7d3232b2bc4ecc684972c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:09:33 +0200 Subject: [PATCH 10/14] Editor: Mark the entity actions hook callbacks as private. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/entity-actions.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wp-includes/entity-actions.php b/src/wp-includes/entity-actions.php index bb40b6edfe9b6..d4a413009878f 100644 --- a/src/wp-includes/entity-actions.php +++ b/src/wp-includes/entity-actions.php @@ -123,6 +123,7 @@ function wp_get_all_registered_entity_action_modules() { * used. In other scenarios (tests), the scripts are given directly. * * @since 7.2.0 + * @access private * * @param WP_Scripts|null $scripts The scripts registry. Defaults to the global one. */ @@ -143,6 +144,7 @@ function _wp_add_entity_action_modules_to_editor_script( $scripts = null ) { * packages/core-data/src/resolvers.js in Gutenberg. * * @since 7.2.0 + * @access private * * @param array $paths REST API paths to preload. * @param WP_Block_Editor_Context $context Block editor context. From a5532f339000991494cffbec71853d4a92098c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:10:03 +0200 Subject: [PATCH 11/14] Editor: Document the loose types of the entity preload callbacks. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/entity-actions.php | 6 +++--- src/wp-includes/entity-fields.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/entity-actions.php b/src/wp-includes/entity-actions.php index d4a413009878f..bf5a89980b39f 100644 --- a/src/wp-includes/entity-actions.php +++ b/src/wp-includes/entity-actions.php @@ -146,9 +146,9 @@ function _wp_add_entity_action_modules_to_editor_script( $scripts = null ) { * @since 7.2.0 * @access private * - * @param array $paths REST API paths to preload. - * @param WP_Block_Editor_Context $context Block editor context. - * @return array Filtered preload paths. + * @param mixed $paths REST API paths to preload. + * @param mixed $context Optional. Block editor context. Default null. + * @return mixed Filtered preload paths. See _wp_add_entity_preload_paths(). */ function _wp_preload_entity_actions( $paths, $context = null ) { return _wp_add_entity_preload_paths( $paths, $context, '/wp/v2/entity-actions' ); diff --git a/src/wp-includes/entity-fields.php b/src/wp-includes/entity-fields.php index b8ab90dabd17d..d3b710a1c779e 100644 --- a/src/wp-includes/entity-fields.php +++ b/src/wp-includes/entity-fields.php @@ -210,9 +210,9 @@ function wp_register_entity_field_collection( $directory ) { * @since 7.2.0 * @access private * - * @param array $paths REST API paths to preload. - * @param WP_Block_Editor_Context $context Block editor context. - * @return array Filtered preload paths. + * @param mixed $paths REST API paths to preload. + * @param mixed $context Optional. Block editor context. Default null. + * @return mixed Filtered preload paths. See _wp_add_entity_preload_paths(). */ function _wp_preload_entity_fields( $paths, $context = null ) { return _wp_add_entity_preload_paths( $paths, $context, '/wp/v2/entity-fields' ); From 90516e701bd2c56da2d195a2e2337259ea580013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:13:37 +0200 Subject: [PATCH 12/14] REST API: Return numeric entity field module ids as strings. PHP turns a numeric module id into an integer array key. The entity fields controller now casts it back, like the entity actions controller, so the response matches its schema. Co-Authored-By: Claude Opus 5.5 --- ...class-wp-rest-entity-fields-controller.php | 3 +- .../rest-entity-fields-controller.php | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php index e7674e2a80ba7..63caf2e7164c9 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php @@ -162,7 +162,8 @@ public function get_items( $request ) { $script_modules = array(); foreach ( wp_get_registered_entity_field_modules( $kind, $name ) as $module => $field_ids ) { $script_modules[] = array( - 'id' => $module, + // A numeric module id is an integer as an array key. + 'id' => (string) $module, 'fields' => array_values( $field_ids ), ); } diff --git a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php index f6d424817d7df..f5ac37779f8b3 100644 --- a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php +++ b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php @@ -593,6 +593,41 @@ public function test_get_items_exposes_registered_fields_and_their_script_module ); } + /** + * A numeric script module id is returned as a string, like any other. + * + * @covers ::get_items + * + * @ticket 66229 + */ + public function test_get_items_returns_a_numeric_module_id_as_a_string() { + $this->register_fields( + 'customKind', + 'customName', + array( + array( + 'id' => 'color', + 'type' => 'text', + 'label' => 'Color', + ), + ), + '123' + ); + + wp_set_current_user( self::$editor_id ); + $data = $this->dispatch_request( 'customKind', 'customName' )->get_data(); + + $this->assertSame( + array( + array( + 'id' => '123', + 'fields' => array( 'color' ), + ), + ), + $data['script_modules'] + ); + } + /** * Fields registered without a script module have none listed. * From 34cf4a5da277230656f847ccca624bcb6ddfd3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:22:11 +0200 Subject: [PATCH 13/14] Editor: List the script modules of an entity with string ids. 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, which now use them as is. Co-Authored-By: Claude Opus 5.5 --- .../class-wp-entity-actions-registry.php | 24 ++++++-- .../class-wp-entity-fields-registry.php | 22 ++++++-- src/wp-includes/entity-actions.php | 17 ++++-- src/wp-includes/entity-fields.php | 16 ++++-- ...lass-wp-rest-entity-actions-controller.php | 11 +--- ...class-wp-rest-entity-fields-controller.php | 11 +--- tests/phpunit/tests/entity-actions.php | 55 ++++++++++++++++--- .../registerCoreEntityActions.php | 8 +-- .../entity-fields/wpEntityFieldsRegistry.php | 20 +++++-- .../wpRegisterCoreFieldCollections.php | 16 +++--- .../wpRegisterEntityFieldCollection.php | 11 +++- 11 files changed, 144 insertions(+), 67 deletions(-) diff --git a/src/wp-includes/class-wp-entity-actions-registry.php b/src/wp-includes/class-wp-entity-actions-registry.php index 1ee5e722cf818..664819e078add 100644 --- a/src/wp-includes/class-wp-entity-actions-registry.php +++ b/src/wp-includes/class-wp-entity-actions-registry.php @@ -404,17 +404,31 @@ static function ( $entities ) { * * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). - * @return array The ids of the actions each module - * applies to, keyed by module id, in - * registration order. Empty when the - * entity is invalid. + * @return array[] { + * The script modules, in registration order. Empty when the entity + * is invalid. + * + * @type array ...$0 { + * @type string $id The id of the script module. + * @type string[] $actions The ids of the actions the module + * applies to, in registration order. + * } + * } */ public function get_registered_action_modules( $kind, $name ) { if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) { return array(); } $this->initialize(); - return $this->action_modules[ $kind ][ $name ] ?? array(); + $modules = array(); + foreach ( $this->action_modules[ $kind ][ $name ] ?? array() as $module => $action_ids ) { + $modules[] = array( + // A numeric module id is an integer as an array key. + 'id' => (string) $module, + 'actions' => $action_ids, + ); + } + return $modules; } /** diff --git a/src/wp-includes/class-wp-entity-fields-registry.php b/src/wp-includes/class-wp-entity-fields-registry.php index 4f1e491ceda47..21f6dbdfb8fd3 100644 --- a/src/wp-includes/class-wp-entity-fields-registry.php +++ b/src/wp-includes/class-wp-entity-fields-registry.php @@ -477,13 +477,27 @@ static function ( $entities ) { * * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). - * @return array The ids of the fields each module - * applies to, keyed by module id, in - * registration order. + * @return array[] { + * The script modules, in registration order. + * + * @type array ...$0 { + * @type string $id The id of the script module. + * @type string[] $fields The ids of the fields the module applies + * to, in registration order. + * } + * } */ public function get_registered_field_modules( $kind, $name ) { $this->initialize(); - return $this->field_modules[ $kind ][ $name ] ?? array(); + $modules = array(); + foreach ( $this->field_modules[ $kind ][ $name ] ?? array() as $module => $field_ids ) { + $modules[] = array( + // A numeric module id is an integer as an array key. + 'id' => (string) $module, + 'fields' => $field_ids, + ); + } + return $modules; } /** diff --git a/src/wp-includes/entity-actions.php b/src/wp-includes/entity-actions.php index bf5a89980b39f..80aced696b1c6 100644 --- a/src/wp-includes/entity-actions.php +++ b/src/wp-includes/entity-actions.php @@ -80,17 +80,22 @@ function wp_get_registered_entity_actions( $kind, $name ) { } /** - * Returns the ids of the script modules registered for the given entity. + * Returns the script modules registered for the given entity. * * @since 7.2.0 * * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). - * @return array The ids of the actions each module - * applies to, keyed by module id, in - * registration order. Empty until the - * `init` action has completed: a read - * before then is refused. + * @return array[] { + * The script modules, in registration order. Empty until the `init` + * action has completed: a read before then is refused. + * + * @type array ...$0 { + * @type string $id The id of the script module. + * @type string[] $actions The ids of the actions the module applies + * to, in registration order. + * } + * } */ function wp_get_registered_entity_action_modules( $kind, $name ) { return WP_Entity_Actions_Registry::get_instance()->get_registered_action_modules( $kind, $name ); diff --git a/src/wp-includes/entity-fields.php b/src/wp-includes/entity-fields.php index d3b710a1c779e..c08bf519fcaa4 100644 --- a/src/wp-includes/entity-fields.php +++ b/src/wp-includes/entity-fields.php @@ -111,16 +111,22 @@ function wp_get_registered_entity_fields( $kind, $name ) { } /** - * Returns the ids of the script modules registered for the given entity. + * Returns the script modules registered for the given entity. * * @since 7.2.0 * * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). - * @return array The ids of the fields each module applies - * to, keyed by module id, in registration - * order. Empty until the `init` action has - * completed: a read before then is refused. + * @return array[] { + * The script modules, in registration order. Empty until the `init` + * action has completed: a read before then is refused. + * + * @type array ...$0 { + * @type string $id The id of the script module. + * @type string[] $fields The ids of the fields the module applies to, + * in registration order. + * } + * } */ function wp_get_registered_entity_field_modules( $kind, $name ) { return WP_Entity_Fields_Registry::get_instance()->get_registered_field_modules( $kind, $name ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php index d72cab8dc26c7..bf9ebcf895a16 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php @@ -197,20 +197,11 @@ public function get_items( $request ) { $kind = $request->get_param( 'kind' ); $name = $request->get_param( 'name' ); - $script_modules = array(); - foreach ( wp_get_registered_entity_action_modules( $kind, $name ) as $module => $action_ids ) { - $script_modules[] = array( - // A numeric module id is an integer as an array key. - 'id' => (string) $module, - 'actions' => array_values( $action_ids ), - ); - } - $response = array( 'kind' => $kind, 'name' => $name, 'actions' => wp_get_registered_entity_actions( $kind, $name ), - 'script_modules' => $script_modules, + 'script_modules' => wp_get_registered_entity_action_modules( $kind, $name ), ); return rest_ensure_response( $response ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php index 63caf2e7164c9..c9738f2a7bb6e 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php @@ -159,20 +159,11 @@ public function get_items( $request ) { $fields[] = wp_prepare_json_value_for_client( $field, $field_schema ); } - $script_modules = array(); - foreach ( wp_get_registered_entity_field_modules( $kind, $name ) as $module => $field_ids ) { - $script_modules[] = array( - // A numeric module id is an integer as an array key. - 'id' => (string) $module, - 'fields' => array_values( $field_ids ), - ); - } - $response = array( 'kind' => $kind, 'name' => $name, 'fields' => $fields, - 'script_modules' => $script_modules, + 'script_modules' => wp_get_registered_entity_field_modules( $kind, $name ), ); return rest_ensure_response( $response ); diff --git a/tests/phpunit/tests/entity-actions.php b/tests/phpunit/tests/entity-actions.php index 12c4d95112eb1..3951c15ee2d50 100644 --- a/tests/phpunit/tests/entity-actions.php +++ b/tests/phpunit/tests/entity-actions.php @@ -673,7 +673,16 @@ public function test_a_callback_at_the_default_priority_alters_the_defaults() { $actions, 'The removed action is gone, and the replaced one has the new definition and origin.' ); - $this->assertSame( array( 'core/defaults' => array( 'move-to-trash' ) ), $modules, 'The replaced action keeps the script module of the default.' ); + $this->assertSame( + array( + array( + 'id' => 'core/defaults', + 'actions' => array( 'move-to-trash' ), + ), + ), + $modules, + 'The replaced action keeps the script module of the default.' + ); } /** @@ -719,7 +728,7 @@ public function test_registering_a_registered_action_is_skipped() { $this->assertArrayHasKey( 'feature', $actions, 'The rest of the actions of the call are registered.' ); $this->assertSame( 'Rename…', $actions['rename-post']['label'], 'The registered action is left untouched.' ); $this->assertSame( 'core', $actions['rename-post']['origin']['registeredBy'] ); - $this->assertSame( array( 'feature' ), wp_get_registered_entity_action_modules( 'postType', 'page' )['plugin/actions'], 'The script module only applies to the actions registered.' ); + $this->assertSame( array( 'feature' ), array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'actions', 'id' )['plugin/actions'], 'The script module only applies to the actions registered.' ); $this->assertCount( 1, $reported ); $this->assertStringContainsString( 'postType "page"', $reported[0], 'The notice names the entity.' ); $this->assertStringContainsString( 'already registered: rename-post', $reported[0], 'The notice names the action that is already registered, not the others of the call.' ); @@ -815,9 +824,18 @@ public function test_actions_keep_their_registration_order() { ); $this->assertSame( array( - 'core/defaults' => array( 'rename-post', 'move-to-trash' ), - 'plugin/actions' => array( 'feature', 'archive' ), - 'other-plugin/actions' => array( 'share' ), + array( + 'id' => 'core/defaults', + 'actions' => array( 'rename-post', 'move-to-trash' ), + ), + array( + 'id' => 'plugin/actions', + 'actions' => array( 'feature', 'archive' ), + ), + array( + 'id' => 'other-plugin/actions', + 'actions' => array( 'share' ), + ), ), wp_get_registered_entity_action_modules( 'postType', 'page' ), 'Each module applies to the actions it was registered with, in registration order.' @@ -846,7 +864,16 @@ function () use ( &$result ) { $this->assertSame( array( 'feature' ), array_column( $result['unregistered'], 'id' ), 'Unregistering the first entity only removes its own action.' ); $this->assertSame( array(), wp_get_registered_entity_actions( 'a/b', 'c' ) ); $this->assertSame( array( 'feature', 'archive' ), array_column( wp_get_registered_entity_actions( 'a', 'b/c' ), 'id' ), 'The second entity keeps its actions.' ); - $this->assertSame( array( 'plugin/second' => array( 'feature', 'archive' ) ), wp_get_registered_entity_action_modules( 'a', 'b/c' ), 'The second entity keeps only its own module.' ); + $this->assertSame( + array( + array( + 'id' => 'plugin/second', + 'actions' => array( 'feature', 'archive' ), + ), + ), + wp_get_registered_entity_action_modules( 'a', 'b/c' ), + 'The second entity keeps only its own module.' + ); $this->assertSame( array( 'b/c' => array( 'plugin/second' ) ), wp_get_all_registered_entity_action_modules()['a'], 'Only the second entity has modules under its kind.' ); $this->assertArrayNotHasKey( 'a/b', wp_get_all_registered_entity_action_modules(), 'The kind of the first entity, emptied, is forgotten.' ); } @@ -883,7 +910,16 @@ function () use ( &$unregistered, &$modules ) { $unregistered['by_id'], 'The actions unregistered by id are returned in registration order.' ); - $this->assertSame( array( 'plugin/first' => array( 'b' ) ), $modules['by_id'], 'The unregistered actions leave their modules, and an emptied module is forgotten.' ); + $this->assertSame( + array( + array( + 'id' => 'plugin/first', + 'actions' => array( 'b' ), + ), + ), + $modules['by_id'], + 'The unregistered actions leave their modules, and an emptied module is forgotten.' + ); $this->assertSame( array(), $unregistered['missing'], 'Nothing is returned when no action is registered.' ); $this->assertSame( array( array_merge( $this->action( 'b' ), array( 'origin' => $origin ) ) ), @@ -990,7 +1026,7 @@ static function () use ( &$results ) { ); $this->assertCount( 3, $reported ); $this->assertNotContains( 'feature', array_column( wp_get_registered_entity_actions( 'postType', 'page' ), 'id' ) ); - $this->assertArrayNotHasKey( 'plugin/actions', wp_get_registered_entity_action_modules( 'postType', 'page' ) ); + $this->assertNotContains( 'plugin/actions', array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'id' ) ); } /** @@ -1086,6 +1122,7 @@ public function test_a_numeric_module_id_is_read_as_a_string() { $this->register_actions( 'postType', 'page', array( $this->action( 'feature' ) ), '123' ); $this->assertSame( array( 'core/defaults', '123' ), wp_get_all_registered_entity_action_modules()['postType']['page'] ); + $this->assertSame( array( 'core/defaults', '123' ), array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'id' ) ); } /** @@ -1104,7 +1141,7 @@ public function test_registering_outside_the_action_is_refused() { $this->assertSame( array(), wp_register_entity_actions( 'test-plugin', 'postType', 'page', array( $this->action( 'feature' ) ), 'plugin/feature' ) ); $this->assertSame( $fired, did_action( 'wp_entity_actions_api_init' ), 'Refusing does not fire the action.' ); $this->assertNotContains( 'feature', array_column( wp_get_registered_entity_actions( 'postType', 'page' ), 'id' ) ); - $this->assertArrayNotHasKey( 'plugin/feature', wp_get_registered_entity_action_modules( 'postType', 'page' ) ); + $this->assertNotContains( 'plugin/feature', array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'id' ) ); } /** diff --git a/tests/phpunit/tests/entity-actions/registerCoreEntityActions.php b/tests/phpunit/tests/entity-actions/registerCoreEntityActions.php index c7b960d2349fe..23c0a88d7b2c0 100644 --- a/tests/phpunit/tests/entity-actions/registerCoreEntityActions.php +++ b/tests/phpunit/tests/entity-actions/registerCoreEntityActions.php @@ -164,7 +164,7 @@ public function test_the_actions_of_pages() { self::POST_TYPE_SUPPORTS => $ids, self::PAGE => self::PAGE_ACTIONS, ), - wp_get_registered_entity_action_modules( 'postType', 'page' ), + array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'actions', 'id' ), 'Each action of pages is registered with the module of its folder.' ); $this->assertSame( array( 'core' ), array_unique( array_column( array_column( wp_get_registered_entity_actions( 'postType', 'page' ), 'origin' ), 'registeredBy' ) ), 'Every action is registered by core.' ); @@ -205,7 +205,7 @@ public function test_the_actions_of_patterns() { self::POST_TYPE_SUPPORTS => array_merge( array( 'view-post-revisions', 'rename-post' ), self::EVERY_POST_TYPE ), self::WP_BLOCK => array( 'duplicate-pattern', 'export-pattern' ), ), - wp_get_registered_entity_action_modules( 'postType', 'wp_block' ), + array_column( wp_get_registered_entity_action_modules( 'postType', 'wp_block' ), 'actions', 'id' ), 'The actions of patterns come from the `wp_block` module.' ); } @@ -243,7 +243,7 @@ public function test_template_parts_are_only_duplicated_with_a_block_theme() { ); $this->assertSame( array( 'duplicate-template-part' ), - wp_get_registered_entity_action_modules( 'postType', 'wp_template_part' )[ self::WP_TEMPLATE_PART ], + array_column( wp_get_registered_entity_action_modules( 'postType', 'wp_template_part' ), 'actions', 'id' )[ self::WP_TEMPLATE_PART ], 'The action comes from the `wp_template_part` module.' ); @@ -522,7 +522,7 @@ public function test_a_plugin_can_remove_and_replace_core_actions() { self::POST_TYPE_SUPPORTS => array_merge( $ids, array( 'move-to-trash' ) ), self::PAGE => self::PAGE_ACTIONS, ), - wp_get_registered_entity_action_modules( 'postType', 'page' ), + array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'actions', 'id' ), 'The replaced action keeps the module of the core action.' ); diff --git a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php index 215f6e34346d5..c086a0d4c2648 100644 --- a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php +++ b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php @@ -129,7 +129,7 @@ public function test_registering_a_registered_field_is_skipped() { $this->assertArrayHasKey( 'color', $fields, 'The rest of the fields of the call are registered.' ); $this->assertSame( 'Author', $fields['author']['label'], 'The registered field is left untouched.' ); $this->assertSame( 'core', $fields['author']['origin']['registeredBy'] ); - $this->assertSame( array( 'color' ), wp_get_registered_entity_field_modules( 'postType', 'page' )['plugin/fields'], 'The script module only applies to the fields registered.' ); + $this->assertSame( array( 'color' ), array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'fields', 'id' )['plugin/fields'], 'The script module only applies to the fields registered.' ); $this->assertCount( 1, $reported ); $this->assertStringContainsString( 'postType "page"', $reported[0], 'The notice names the entity.' ); $this->assertStringContainsString( 'already registered: author', $reported[0], 'The notice names the field that is already registered, not the others of the call.' ); @@ -217,7 +217,16 @@ function () use ( &$result ) { $this->assertSame( array( 'color' ), array_column( $result['unregistered'], 'id' ), 'Unregistering the first entity only removes its own field.' ); $this->assertSame( array(), wp_get_registered_entity_fields( 'a/b', 'c' ) ); $this->assertSame( array( 'color', 'size' ), array_column( wp_get_registered_entity_fields( 'a', 'b/c' ), 'id' ), 'The second entity keeps its fields.' ); - $this->assertSame( array( 'plugin/second' => array( 'color', 'size' ) ), wp_get_registered_entity_field_modules( 'a', 'b/c' ), 'The second entity keeps only its own module.' ); + $this->assertSame( + array( + array( + 'id' => 'plugin/second', + 'fields' => array( 'color', 'size' ), + ), + ), + wp_get_registered_entity_field_modules( 'a', 'b/c' ), + 'The second entity keeps only its own module.' + ); $this->assertSame( array( 'b/c' => array( 'plugin/second' ) ), wp_get_all_registered_entity_field_modules()['a'], 'Only the second entity has modules under its kind.' ); $this->assertArrayNotHasKey( 'a/b', wp_get_all_registered_entity_field_modules(), 'The kind of the first entity, emptied, is forgotten.' ); } @@ -232,6 +241,7 @@ public function test_all_registered_field_modules_lists_a_numeric_module_id_as_a $this->register_fields( 'postType', 'post', array( $this->field( 'color' ) ), '42' ); $this->assertSame( array( '42' ), wp_get_all_registered_entity_field_modules()['postType']['post'] ); + $this->assertSame( array( '42' ), array_column( wp_get_registered_entity_field_modules( 'postType', 'post' ), 'id' ) ); } /** @@ -334,7 +344,7 @@ static function () use ( &$updated ) { ), $fields[0]['origin'] ); - $modules = wp_get_registered_entity_field_modules( 'postType', 'page' ); + $modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'fields', 'id' ); $this->assertContains( 'author', $modules['test/defaults'], 'The field keeps its modules.' ); $this->assertSame( array( 'author' ), $modules['plugin/author'], 'The module applies to the field.' ); } @@ -408,7 +418,7 @@ static function () use ( &$updated ) { $this->assertArrayNotHasKey( 'color', $fields, 'The field is not registered.' ); $this->assertSame( 'Writer', $fields['author']['label'], 'The rest of the fields of the call are updated.' ); $this->assertSame( array( 'test-plugin' ), $fields['author']['origin']['updatedBy'] ); - $this->assertSame( array( 'author' ), wp_get_registered_entity_field_modules( 'postType', 'page' )['plugin/fields'], 'The script module only applies to the fields updated.' ); + $this->assertSame( array( 'author' ), array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'fields', 'id' )['plugin/fields'], 'The script module only applies to the fields updated.' ); $this->assertCount( 1, $reported ); $this->assertStringContainsString( 'postType "page"', $reported[0], 'The notice names the entity.' ); $this->assertStringContainsString( 'not registered: color', $reported[0], 'The notice names only the field that is not registered.' ); @@ -569,7 +579,7 @@ public function test_registering_outside_the_action_is_refused() { $this->assertSame( array(), wp_register_entity_fields( 'test-plugin', 'postType', 'page', array( $this->field( 'color' ) ), 'plugin/color' ) ); $this->assertSame( $fired, did_action( 'wp_entity_fields_api_init' ), 'Refusing does not fire the action.' ); $this->assertNotContains( 'color', array_column( wp_get_registered_entity_fields( 'postType', 'page' ), 'id' ) ); - $this->assertArrayNotHasKey( 'plugin/color', wp_get_registered_entity_field_modules( 'postType', 'page' ) ); + $this->assertNotContains( 'plugin/color', array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'id' ) ); } /** diff --git a/tests/phpunit/tests/entity-fields/wpRegisterCoreFieldCollections.php b/tests/phpunit/tests/entity-fields/wpRegisterCoreFieldCollections.php index 7749ccc0a5ec9..7470ec30b4127 100644 --- a/tests/phpunit/tests/entity-fields/wpRegisterCoreFieldCollections.php +++ b/tests/phpunit/tests/entity-fields/wpRegisterCoreFieldCollections.php @@ -209,7 +209,7 @@ public function test_a_custom_post_type_gets_the_default_fields_of_its_supports( $this->assertSame( array( 'core' ), array_unique( array_column( array_column( $fields, 'origin' ), 'registeredBy' ) ), 'The fields carry the origin of WordPress core.' ); $this->assertSame( array( self::POST_TYPE_SUPPORTS => array_column( $fields, 'id' ) ), - wp_get_registered_entity_field_modules( 'postType', 'test_book' ), + array_column( wp_get_registered_entity_field_modules( 'postType', 'test_book' ), 'fields', 'id' ), 'Every default field is registered with the module of its folder.' ); } @@ -363,7 +363,7 @@ public function test_the_defaults_make_no_exception() { */ public function test_pages_get_their_own_title_field() { $fields = self::get_fields( 'postType', 'page' ); - $modules = wp_get_registered_entity_field_modules( 'postType', 'page' ); + $modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'fields', 'id' ); $this->assertFalse( $fields['title']['enableHiding'], 'The page title is the one of the collection.' ); $this->assertSame( 'core', $fields['title']['origin']['registeredBy'], 'The page title is registered by WordPress core.' ); @@ -390,7 +390,7 @@ public function test_templates_get_their_own_fields() { $this->assertSame( 'core', $fields['author']['origin']['registeredBy'], 'The template author is registered by WordPress core.' ); $this->assertSame( $ids, - wp_get_registered_entity_field_modules( 'postType', 'wp_template' )['@wordpress/core-fields/wp_template'], + array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_template' ), 'fields', 'id' )['@wordpress/core-fields/wp_template'], 'The fields of templates ship their JavaScript parts in the module of the collection.' ); } @@ -412,7 +412,7 @@ public function test_template_parts_get_their_own_fields() { $this->assertSame( 'Title', $fields['title']['label'], 'The title is the one of the collection.' ); $this->assertSame( array( 'author', 'title' ), - wp_get_registered_entity_field_modules( 'postType', 'wp_template_part' )['@wordpress/core-fields/wp_template_part'], + array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_template_part' ), 'fields', 'id' )['@wordpress/core-fields/wp_template_part'], 'The fields of template parts ship their JavaScript parts in the module of the collection.' ); } @@ -425,7 +425,7 @@ public function test_template_parts_get_their_own_fields() { */ public function test_patterns_get_their_own_fields() { $fields = self::get_fields( 'postType', 'wp_block' ); - $modules = wp_get_registered_entity_field_modules( 'postType', 'wp_block' ); + $modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_block' ), 'fields', 'id' ); $this->assertTrue( post_type_supports( 'wp_block', 'excerpt' ), 'The post type supports excerpts.' ); $this->assertSame( 'Description', $fields['excerpt']['label'], 'The excerpt is the description of the collection.' ); @@ -486,7 +486,7 @@ public function test_attachments_get_the_media_fields_instead_of_the_defaults() $this->assertSame( 'core', $fields['date']['origin']['registeredBy'], 'The media fields are registered by WordPress core.' ); $this->assertSame( array( '@wordpress/core-fields/attachment' => $ids ), - wp_get_registered_entity_field_modules( 'postType', 'attachment' ), + array_column( wp_get_registered_entity_field_modules( 'postType', 'attachment' ), 'fields', 'id' ), 'Every media field is registered with the module of the collection.' ); } @@ -508,7 +508,7 @@ public function test_the_site_gets_the_fields_of_its_collection() { $this->assertSame( 'core', $fields['title']['origin']['registeredBy'], 'The fields of the site are registered by WordPress core.' ); $this->assertSame( array( '@wordpress/core-fields/root_site' => $ids ), - wp_get_registered_entity_field_modules( 'root', 'site' ), + array_column( wp_get_registered_entity_field_modules( 'root', 'site' ), 'fields', 'id' ), 'Every field of the site is registered with the module of the collection.' ); } @@ -713,7 +713,7 @@ static function () { } ); - $modules = wp_get_registered_entity_field_modules( 'postType', 'wp_template' ); + $modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_template' ), 'fields', 'id' ); $this->assertSame( 'my-plugin', self::get_fields( 'postType', 'wp_template' )['author']['origin']['registeredBy'], 'The author field is the one of the plugin.' ); $this->assertNotContains( 'author', $modules['@wordpress/core-fields/wp_template'], 'The module of the replaced field no longer applies to it.' ); diff --git a/tests/phpunit/tests/entity-fields/wpRegisterEntityFieldCollection.php b/tests/phpunit/tests/entity-fields/wpRegisterEntityFieldCollection.php index cc7bc78f6357f..52e8c5abfc874 100644 --- a/tests/phpunit/tests/entity-fields/wpRegisterEntityFieldCollection.php +++ b/tests/phpunit/tests/entity-fields/wpRegisterEntityFieldCollection.php @@ -100,7 +100,16 @@ public function test_a_collection_places_its_fields() { $book = wp_get_registered_entity_fields( 'postType', 'test_book' ); $this->assertSame( array( 'subtitle', 'isbn' ), array_column( $book, 'id' ), 'The fields registered first come first: the fixture collections run at priority 0.' ); $this->assertSame( 'fixture', $book[0]['origin']['registeredBy'], 'The fields carry the origin of their collection.' ); - $this->assertSame( array( 'fixture/book' => array( 'subtitle' ) ), wp_get_registered_entity_field_modules( 'postType', 'test_book' ), 'Each field is registered with the module of its collection.' ); + $this->assertSame( + array( + array( + 'id' => 'fixture/book', + 'fields' => array( 'subtitle' ), + ), + ), + wp_get_registered_entity_field_modules( 'postType', 'test_book' ), + 'Each field is registered with the module of its collection.' + ); $this->assertSame( array( 'issue' ), array_column( wp_get_registered_entity_fields( 'postType', 'test_magazine' ), 'id' ) ); $this->assertSame( array(), wp_get_registered_entity_field_modules( 'postType', 'test_magazine' ), 'A collection without module registers none.' ); From 865e92e83c3283a643a505fde6f5cf62ae92d760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:30:18 +0200 Subject: [PATCH 14/14] Editor: Accept 0 as an entity field id. 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 --- src/wp-includes/class-wp-entity-fields-registry.php | 2 +- .../tests/entity-fields/wpEntityFieldsRegistry.php | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-entity-fields-registry.php b/src/wp-includes/class-wp-entity-fields-registry.php index 21f6dbdfb8fd3..864828ca1c243 100644 --- a/src/wp-includes/class-wp-entity-fields-registry.php +++ b/src/wp-includes/class-wp-entity-fields-registry.php @@ -761,7 +761,7 @@ protected function skip_invalid_definitions( $method, $kind, $name, $fields ) { $valid = array(); $invalid = array(); foreach ( $fields as $position => $field ) { - if ( is_array( $field ) && ! empty( $field['id'] ) && is_string( $field['id'] ) ) { + if ( is_array( $field ) && isset( $field['id'] ) && is_string( $field['id'] ) && '' !== $field['id'] ) { $valid[] = $field; } else { $invalid[] = '#' . ( $position + 1 ); diff --git a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php index c086a0d4c2648..6a035503d1f1f 100644 --- a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php +++ b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php @@ -452,6 +452,7 @@ function () use ( &$registered, &$updated ) { 'label' => 'Numeric id', ), $this->field( 'weight' ), + $this->field( '0' ), ) ); $updated = wp_update_entity_fields( @@ -464,15 +465,20 @@ function () use ( &$registered, &$updated ) { 'id' => 'color', 'label' => 'Colour', ), + array( + 'id' => '0', + 'label' => 'Zero', + ), ) ); } ); - $this->assertSame( array( 'color', 'weight' ), $registered, 'The valid definitions are registered.' ); - $this->assertSame( array( 'color' ), $updated, 'The valid definitions are updated.' ); + $this->assertSame( array( 'color', 'weight', '0' ), $registered, 'The valid definitions are registered, `0` included.' ); + $this->assertSame( array( 'color', '0' ), $updated, 'The valid definitions are updated, `0` included.' ); $fields = array_column( wp_get_registered_entity_fields( 'postType', 'page' ), null, 'id' ); $this->assertSame( 'Colour', $fields['color']['label'] ); + $this->assertSame( 'Zero', $fields['0']['label'] ); $this->assertArrayHasKey( 'weight', $fields ); $this->assertCount( 1, $reported ); $this->assertStringContainsString( 'postType "page"', $reported[0], 'The notice names the entity.' );