From aeac15339290f19e94c9aa2c9629517a74bdbb6b Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:44:40 +0700 Subject: [PATCH 1/2] Editor: Track wp_entity_fields_api_init firing internally in WP_Entity_Fields_Registry. Refusing mutations once read keeps the registry immutable even when wp_entity_fields_api_init is fired again with do_action(), matching WP_Entity_Actions_Registry. Fixes #66703. --- .../class-wp-entity-fields-registry.php | 56 ++++++++++++------- .../entity-fields/wpEntityFieldsRegistry.php | 36 ++++++++++++ 2 files changed, 72 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/class-wp-entity-fields-registry.php b/src/wp-includes/class-wp-entity-fields-registry.php index 864828ca1c243..28d0bd0baf13f 100644 --- a/src/wp-includes/class-wp-entity-fields-registry.php +++ b/src/wp-includes/class-wp-entity-fields-registry.php @@ -70,6 +70,15 @@ class WP_Entity_Fields_Registry { */ protected $initialized = false; + /** + * Whether the registry is firing the `wp_entity_fields_api_init` action, the + * only time register(), update(), unregister(), and register_collection() run. + * + * @since 7.2.0 + * @var bool + */ + protected $initializing = false; + /** * The singleton instance. * @@ -575,32 +584,39 @@ protected function initialize() { // the fields it updates does not fire the action again. $this->initialized = true; - /** - * Fires the first time the registered fields are read, after `init`. - * - * Register or adjust fields here, with wp_register_entity_fields(), - * wp_update_entity_fields(), and wp_unregister_entity_fields(): it is the only - * time they run. - * - * The first read happens wherever the fields are needed: while - * handling a REST request as well as on `admin_footer` of the pages - * that load the editor script. A callback should register fields and - * nothing else; script modules, styles, and their enqueue hooks - * belong on `init`. - * - * @since 7.2.0 - */ - do_action( 'wp_entity_fields_api_init' ); + $this->initializing = true; + try { + /** + * Fires the first time the registered fields are read, after `init`. + * + * Register or adjust fields here, with wp_register_entity_fields(), + * wp_update_entity_fields(), and wp_unregister_entity_fields(): it is the only + * time they run. + * + * The first read happens wherever the fields are needed: while + * handling a REST request as well as on `admin_footer` of the pages + * that load the editor script. A callback should register fields and + * nothing else; script modules, styles, and their enqueue hooks + * belong on `init`. + * + * @since 7.2.0 + */ + do_action( 'wp_entity_fields_api_init' ); + } finally { + $this->initializing = false; + } } /** - * Checks that the `wp_entity_fields_api_init` action is firing, as register(), - * update(), and unregister() require. + * Checks that the registry is firing the `wp_entity_fields_api_init` action, + * as register(), update(), unregister(), and register_collection() require. * * Outside the action a registration would either come before the * defaults, which could then not be registered, or after the fields have been read * and the import map of the editor script built from them. Refusing - * keeps the registry immutable once read. + * keeps the registry immutable once read, even when the action is fired + * again with do_action(), which is why the registry tracks its own firing + * rather than check doing_action(). * * @since 7.2.0 * @@ -608,7 +624,7 @@ protected function initialize() { * @return bool Whether the action is firing. */ protected function doing_entity_fields_api_init( $method ) { - if ( doing_action( 'wp_entity_fields_api_init' ) ) { + if ( $this->initializing ) { return true; } diff --git a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php index 6a035503d1f1f..5c0f97c6910fd 100644 --- a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php +++ b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php @@ -42,6 +42,42 @@ public function test_reading_the_registry_fires_the_action_once() { $this->assertSame( $fired + 1, did_action( 'wp_entity_fields_api_init' ), 'Further reads do not fire it again.' ); } + /** + * Firing the action again once the registry has been read does not reopen + * it: the registrations, updates, and unregistrations made then are + * refused. + * + * @ticket 66229 + */ + public function test_firing_the_action_again_does_not_reopen_the_registry() { + $this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::register' ); + $this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::update' ); + $this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::unregister' ); + $fields = wp_get_registered_entity_fields( 'postType', 'page' ); + add_action( + 'wp_entity_fields_api_init', + function () { + wp_register_entity_fields( 'test-plugin', 'postType', 'page', array( $this->field( 'late' ) ) ); + wp_update_entity_fields( + 'test-plugin', + 'postType', + 'page', + array( + array( + 'id' => 'author', + 'label' => 'Late', + ), + ) + ); + wp_unregister_entity_fields( 'postType', 'page', array( 'date' ) ); + } + ); + + do_action( 'wp_entity_fields_api_init' ); + + $this->assertSame( $fields, wp_get_registered_entity_fields( 'postType', 'page' ) ); + } + /** * A plugin hooking the action at the default priority sees the fields * registered at an earlier priority: it can update a field, or remove it. From 488bbfc8890827c83655f7290e6ad010cfc2d325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:02:49 +0100 Subject: [PATCH 2/2] Editor: Describe the WP_Entity_Fields_Registry init guard in the class docblock. The guard now checks that the registry itself is firing wp_entity_fields_api_init, not just that the action is firing. Update the class docblock to match, and list register_collection() among the guarded methods. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/class-wp-entity-fields-registry.php | 8 +++++--- 1 file changed, 5 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 28d0bd0baf13f..eae0010c1c2d8 100644 --- a/src/wp-includes/class-wp-entity-fields-registry.php +++ b/src/wp-includes/class-wp-entity-fields-registry.php @@ -21,9 +21,11 @@ * format. * * Fields are registered on the `wp_entity_fields_api_init` action, and only - * there, with wp_register_entity_fields(), wp_update_entity_fields(), and - * wp_unregister_entity_fields(): register(), update(), and unregister() refuse - * to run while the action is not firing. + * there, with wp_register_entity_fields(), wp_update_entity_fields(), + * wp_unregister_entity_fields(), and wp_register_entity_field_collection(): + * register(), update(), unregister(), and register_collection() refuse to run + * unless the registry itself is firing the action, on the first read of its + * fields. Firing the action by other means does not allow them to run. * * The registry is filled lazily: the first time its fields are read, it * fires the `wp_entity_fields_api_init` action, on which the default fields of every