|
21 | 21 | * format. |
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 |
29 | 31 | * fires the `wp_entity_fields_api_init` action, on which the default fields of every |
@@ -70,6 +72,15 @@ class WP_Entity_Fields_Registry { |
70 | 72 | */ |
71 | 73 | protected $initialized = false; |
72 | 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; |
| 83 | + |
73 | 84 | /** |
74 | 85 | * The singleton instance. |
75 | 86 | * |
@@ -583,40 +594,47 @@ protected function initialize() { |
583 | 594 | // the fields it updates does not fire the action again. |
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' ); |
| 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 | + } |
602 | 618 | } |
603 | 619 |
|
604 | 620 | /** |
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. |
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 |
614 | 632 | * |
615 | 633 | * @param string $method The calling method, for the notice. |
616 | 634 | * @return bool Whether the action is firing. |
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 | } |
622 | 640 |
|
|
0 commit comments