Make WordPress Core


Ignore:
Timestamp:
11/04/2025 04:32:45 PM (9 months ago)
Author:
jorgefilipecosta
Message:

Abilities API: Use stricter doing_action check when registering.

Applies feedback provided that doing_action would be better check on this situation to avoid developers facing future registration timing issues.

Developed in #10452.

Props gziolo, jorgefilipecosta, flixos90, mukesh27, jason_the_adams.
See #64098.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php

    r61032 r61130  
    3838                );
    3939
    40                 // Fire the init hook to allow test ability categories registration.
    41                 do_action( 'wp_abilities_api_categories_init' );
    4240                self::register_test_categories();
    4341        }
     
    6866                do_action( 'rest_api_init' );
    6967
    70                 // Initialize Abilities API.
    71                 do_action( 'wp_abilities_api_init' );
    7268                $this->register_test_abilities();
    7369
     
    10096         */
    10197        public static function register_test_categories(): void {
     98                // Simulates the init hook to allow test ability categories registration.
     99                global $wp_current_filter;
     100                $wp_current_filter[] = 'wp_abilities_api_categories_init';
     101
    102102                wp_register_ability_category(
    103103                        'math',
     
    123123                        )
    124124                );
     125
     126                array_pop( $wp_current_filter );
     127        }
     128
     129        /**
     130         * Helper to register a test ability.
     131         *
     132         * @param string $name Ability name.
     133         * @param array  $args Ability arguments.
     134         */
     135        private function register_test_ability( string $name, array $args ): void {
     136                // Simulates the init hook to allow test abilities registration.
     137                global $wp_current_filter;
     138                $wp_current_filter[] = 'wp_abilities_api_init';
     139
     140                wp_register_ability( $name, $args );
     141
     142                array_pop( $wp_current_filter );
    125143        }
    126144
     
    130148        private function register_test_abilities(): void {
    131149                // Register a regular ability.
    132                 wp_register_ability(
     150                $this->register_test_ability(
    133151                        'test/calculator',
    134152                        array(
     
    174192
    175193                // Register a read-only ability.
    176                 wp_register_ability(
     194                $this->register_test_ability(
    177195                        'test/system-info',
    178196                        array(
     
    221239
    222240                // Ability that does not show in REST.
    223                 wp_register_ability(
     241                $this->register_test_ability(
    224242                        'test/not-show-in-rest',
    225243                        array(
     
    236254                // Register multiple abilities for pagination testing
    237255                for ( $i = 1; $i <= 60; $i++ ) {
    238                         wp_register_ability(
     256                        $this->register_test_ability(
    239257                                "test/ability-{$i}",
    240258                                array(
     
    590608        public function test_ability_name_with_valid_special_characters(): void {
    591609                // Register ability with hyphen (valid).
    592                 wp_register_ability(
     610                $this->register_test_ability(
    593611                        'test-hyphen/ability',
    594612                        array(
Note: See TracChangeset for help on using the changeset viewer.