diff --git a/src/wp-includes/class-wp-entity-fields-registry.php b/src/wp-includes/class-wp-entity-fields-registry.php index 864828ca1c243..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 @@ -70,6 +72,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 +586,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 +626,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.