Changeset 65782
- Timestamp:
- 11/25/2026 03:48:38 PM (45 hours ago)
- Location:
- branches/7.2
- Files:
-
- 3 edited
-
. (modified) (1 prop)
-
src/wp-includes/class-wp-entity-fields-registry.php (modified) (4 diffs)
-
tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/7.2
-
branches/7.2/src/wp-includes/class-wp-entity-fields-registry.php
r65725 r65782 22 22 * 23 23 * 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. 27 29 * 28 30 * The registry is filled lazily: the first time its fields are read, it … … 70 72 */ 71 73 protected $initialized = false; 74 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; 72 83 73 84 /** … … 584 595 $this->initialized = true; 585 596 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' ); 602 } 603 604 /** 605 * Checks that the `wp_entity_fields_api_init` action is firing, as register(), 606 * update(), and unregister() require. 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 } 618 } 619 620 /** 621 * Checks that the registry is firing the `wp_entity_fields_api_init` action, 622 * as register(), update(), unregister(), and register_collection() require. 607 623 * 608 624 * Outside the action a registration would either come before the 609 625 * defaults, which could then not be registered, or after the fields have been read 610 626 * 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(). 612 630 * 613 631 * @since 7.2.0 … … 617 635 */ 618 636 protected function doing_entity_fields_api_init( $method ) { 619 if ( doing_action( 'wp_entity_fields_api_init' )) {637 if ( $this->initializing ) { 620 638 return true; 621 639 } -
branches/7.2/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php
r65725 r65782 44 44 45 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 81 /** 46 82 * A plugin hooking the action at the default priority sees the fields 47 83 * registered at an earlier priority: it can update a field, or remove it.
Note:
See TracChangeset
for help on using the changeset viewer.