Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions src/wp-includes/class-wp-entity-fields-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -575,40 +586,47 @@ 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
*
* @param string $method The calling method, for the notice.
* @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;
}

Expand Down
36 changes: 36 additions & 0 deletions tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading