Make WordPress Core

Changeset 65782


Ignore:
Timestamp:
11/25/2026 03:48:38 PM (45 hours ago)
Author:
ntsekouras
Message:

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 ​https://github.com/WordPress/wordpress-develop/pull/16009.

Reviewed by oandregal.
Merges [65781] to the 7.2 branch.

Props banghamid, oandregal, abhishekfdd, wildworks, mamaduka, ntsekouras.
Fixes #66703.

Location:
branches/7.2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/7.2

  • branches/7.2/src/wp-includes/class-wp-entity-fields-registry.php

    r65725 r65782  
    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
    … …  
    7072         */
    7173        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;
    7283
    7384        /**
    … …  
    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' );
    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.
    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
    … …  
    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                }
  • branches/7.2/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php

    r65725 r65782  
    4444
    4545        /**
     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        /**
    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.
Note: See TracChangeset for help on using the changeset viewer.