Skip to content

Commit 20f9614

Browse files
committed
Editor: Keep the entity fields registry immutable when its init action fires again.
`WP_Entity_Fields_Registry` checked `doing_action( 'wp_entity_fields_api_init' )` to allow `register()`, `update()`, `unregister()`, and `register_collection()`. So firing the action again with `do_action()` after the registry had been read reopened it to changes. The registry now records when it fires the action itself, as `WP_Entity_Actions_Registry` already does. Developed in #16009. Props banghamid, oandregal, abhishekfdd, wildworks, mamaduka, ntsekouras. Fixes #66703. git-svn-id: https://develop.svn.wordpress.org/trunk@65781 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ffbf93d commit 20f9614

2 files changed

Lines changed: 77 additions & 23 deletions

File tree

‎src/wp-includes/class-wp-entity-fields-registry.php‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
* format.
2222
*
2323
* Fields are registered on the `wp_entity_fields_api_init` action, and only
24-
* there, with wp_register_entity_fields(), wp_update_entity_fields(), and
25-
* wp_unregister_entity_fields(): register(), update(), and unregister() refuse
26-
* to run while the action is not firing.
24+
* there, with wp_register_entity_fields(), wp_update_entity_fields(),
25+
* wp_unregister_entity_fields(), and wp_register_entity_field_collection():
26+
* register(), update(), unregister(), and register_collection() refuse to run
27+
* unless the registry itself is firing the action, on the first read of its
28+
* fields. Firing the action by other means does not allow them to run.
2729
*
2830
* The registry is filled lazily: the first time its fields are read, it
2931
* fires the `wp_entity_fields_api_init` action, on which the default fields of every
@@ -70,6 +72,15 @@ class WP_Entity_Fields_Registry {
7072
*/
7173
protected $initialized = false;
7274

75+
/**
76+
* Whether the registry is firing the `wp_entity_fields_api_init` action, the
77+
* only time register(), update(), unregister(), and register_collection() run.
78+
*
79+
* @since 7.2.0
80+
* @var bool
81+
*/
82+
protected $initializing = false;
83+
7384
/**
7485
* The singleton instance.
7586
*
@@ -583,40 +594,47 @@ protected function initialize() {
583594
// the fields it updates does not fire the action again.
584595
$this->initialized = true;
585596

586-
/**
587-
* Fires the first time the registered fields are read, after `init`.
588-
*
589-
* Register or adjust fields here, with wp_register_entity_fields(),
590-
* wp_update_entity_fields(), and wp_unregister_entity_fields(): it is the only
591-
* time they run.
592-
*
593-
* The first read happens wherever the fields are needed: while
594-
* handling a REST request as well as on `admin_footer` of the pages
595-
* that load the editor script. A callback should register fields and
596-
* nothing else; script modules, styles, and their enqueue hooks
597-
* belong on `init`.
598-
*
599-
* @since 7.2.0
600-
*/
601-
do_action( 'wp_entity_fields_api_init' );
597+
$this->initializing = true;
598+
try {
599+
/**
600+
* Fires the first time the registered fields are read, after `init`.
601+
*
602+
* Register or adjust fields here, with wp_register_entity_fields(),
603+
* wp_update_entity_fields(), and wp_unregister_entity_fields(): it is the only
604+
* time they run.
605+
*
606+
* The first read happens wherever the fields are needed: while
607+
* handling a REST request as well as on `admin_footer` of the pages
608+
* that load the editor script. A callback should register fields and
609+
* nothing else; script modules, styles, and their enqueue hooks
610+
* belong on `init`.
611+
*
612+
* @since 7.2.0
613+
*/
614+
do_action( 'wp_entity_fields_api_init' );
615+
} finally {
616+
$this->initializing = false;
617+
}
602618
}
603619

604620
/**
605-
* Checks that the `wp_entity_fields_api_init` action is firing, as register(),
606-
* update(), and unregister() require.
621+
* Checks that the registry is firing the `wp_entity_fields_api_init` action,
622+
* as register(), update(), unregister(), and register_collection() require.
607623
*
608624
* Outside the action a registration would either come before the
609625
* defaults, which could then not be registered, or after the fields have been read
610626
* and the import map of the editor script built from them. Refusing
611-
* keeps the registry immutable once read.
627+
* keeps the registry immutable once read, even when the action is fired
628+
* again with do_action(), which is why the registry tracks its own firing
629+
* rather than check doing_action().
612630
*
613631
* @since 7.2.0
614632
*
615633
* @param string $method The calling method, for the notice.
616634
* @return bool Whether the action is firing.
617635
*/
618636
protected function doing_entity_fields_api_init( $method ) {
619-
if ( doing_action( 'wp_entity_fields_api_init' ) ) {
637+
if ( $this->initializing ) {
620638
return true;
621639
}
622640

‎tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,42 @@ public function test_reading_the_registry_fires_the_action_once() {
4242
$this->assertSame( $fired + 1, did_action( 'wp_entity_fields_api_init' ), 'Further reads do not fire it again.' );
4343
}
4444

45+
/**
46+
* Firing the action again once the registry has been read does not reopen
47+
* it: the registrations, updates, and unregistrations made then are
48+
* refused.
49+
*
50+
* @ticket 66229
51+
*/
52+
public function test_firing_the_action_again_does_not_reopen_the_registry() {
53+
$this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::register' );
54+
$this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::update' );
55+
$this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::unregister' );
56+
$fields = wp_get_registered_entity_fields( 'postType', 'page' );
57+
add_action(
58+
'wp_entity_fields_api_init',
59+
function () {
60+
wp_register_entity_fields( 'test-plugin', 'postType', 'page', array( $this->field( 'late' ) ) );
61+
wp_update_entity_fields(
62+
'test-plugin',
63+
'postType',
64+
'page',
65+
array(
66+
array(
67+
'id' => 'author',
68+
'label' => 'Late',
69+
),
70+
)
71+
);
72+
wp_unregister_entity_fields( 'postType', 'page', array( 'date' ) );
73+
}
74+
);
75+
76+
do_action( 'wp_entity_fields_api_init' );
77+
78+
$this->assertSame( $fields, wp_get_registered_entity_fields( 'postType', 'page' ) );
79+
}
80+
4581
/**
4682
* A plugin hooking the action at the default priority sees the fields
4783
* registered at an earlier priority: it can update a field, or remove it.

0 commit comments

Comments
 (0)