Make WordPress Core

Ticket #65569: 65569-optional-category-uncategorized.diff

File 65569-optional-category-uncategorized.diff, 7.8 KB (added by sachinrajcp123, 2 months ago)
  • src/wp-includes/abilities-api/class-wp-abilities-registry.php

    diff --git a/src/wp-includes/abilities-api/class-wp-abilities-registry.php b/src/wp-includes/abilities-api/class-wp-abilities-registry.php
    a b final class WP_Abilities_Registry {  
    9494         *     @type string   $description         The detailed description of what the ability does.
    9595         *     @type string   $input_schema        The schema of the input data. Default empty array.
    9696         *     @type string   $output_schema       The schema of the output data. Default empty array.
    97          *     @type string   $category            The ability category slug this ability belongs to.
     97         *     @type string   $category            Optional. The ability category slug this ability
     98         *                                        belongs to. Defaults to `uncategorized`.
    9899         *     @type callable $permission_callback The callback used to check if the current user can perform the ability.
    99100         *     @type callable $execute_callback    The callback used to perform the ability.
    100101         *     @type array    $meta                Optional metadata for the ability. Default empty array.
    final class WP_Abilities_Registry {  
    198199         *     @type string   $description         The detailed description of what the ability does.
    199200         *     @type string   $input_schema        The schema of the input data. Default empty array.
    200201         *     @type string   $output_schema       The schema of the output data. Default empty array.
    201          *     @type string   $category            The ability category slug this ability belongs to.
     202         *     @type string   $category            Optional. The ability category slug this ability
     203         *                                        belongs to. Defaults to `uncategorized`.
    202204         *     @type callable $permission_callback The callback used to check if the current user can perform the ability.
    203205         *     @type callable $execute_callback    The callback used to perform the ability.
    204206         *     @type array    $meta                Optional metadata for the ability. Default empty array.
    final class WP_Abilities_Registry {  
    228230
    229231                $args = apply_filters( 'wp_register_ability_args', $args, $name );
    230232
    231                 // Validate ability category exists if provided (will be validated as required in WP_Ability).
     233                if ( empty( $args['category'] ) ) {
     234                        $args['category'] = 'uncategorized';
     235                }
     236
     237                // Validate ability category exists after applying the default.
    232238                if ( isset( $args['category'] ) ) {
    233239                        $category = $this->get_registered_category( $args['category'] );
    234240
  • src/wp-includes/abilities-api.php

     
    diff --git a/src/wp-includes/abilities-api.php b/src/wp-includes/abilities-api.php
    a b function wp_abilities_api_init() {  
    20502050 * Registers a new ability.
    20512051 *
    20522052 * An ability is a specific piece of functionality that can be registered and executed within WordPress.
    2053  * Abilities can be organized into categories which must be registered before abilities that reference them.
     2053 * Abilities can be organized into categories. If no category is provided, the ability is assigned to the
     2054 * built-in `uncategorized` category. Custom categories must be registered before abilities that reference them.
    20542055 *
    20552056 * This function should be called on the {@see 'wp_abilities_api_init'} action hook.
    20562057 * The ability name must be in the format `namespace/name` and can contain lowercase alphanumeric
    20572058 * characters, dashes, and underscores. Abilities will not be registered if the ability name is not unique,
    2058  * if a category is specified but not registered, or if the ability data is invalid.
     2059 * if a custom category is specified but not registered, or if the ability data is invalid.
    20592060 *
    20602061 *     wp_register_ability( 'myplugin/hello-world', array(
    20612062 *         'label'               => __( 'Hello World', 'myplugin' ),
    function wp_abilities_api_init() {  
    20722073 *     @type string   $description         The detailed description of what the ability does.
    20732074 *     @type array    $input_schema        The schema of the input data. Default empty array.
    20742075 *     @type array    $output_schema       The schema of the output data. Default empty array.
    2075  *     @type string   $category            The ability category slug this ability belongs to. The category
    2076  *                                        must be registered using {@see wp_register_ability_category()}.
     2076 *     @type string   $category            Optional. The ability category slug this ability belongs to.
     2077 *                                        Defaults to `uncategorized`. Custom categories must be registered
     2078 *                                        using {@see wp_register_ability_category()}.
    20772079 *     @type callable $permission_callback The callback used to check if the current user can perform the ability.
    20782080 *     @type callable $execute_callback    The callback used to perform the ability.
    20792081 *     @type array    $meta                Optional metadata for the ability. Default empty array.
    function wp_unregister_ability( string $name ): bool {  
    22342236 *
    22352237 * Ability categories are used to group related abilities together. Categories must be registered before
    22362238 * abilities that reference them are registered. Core provides built-in categories for common use cases.
    2237  * Plugin and theme developers can register their own categories to organize their abilities.
     2239 * Plugin and theme developers can register their own categories to organize their abilities. Abilities that
     2240 * omit a category are assigned to the built-in `uncategorized` category, which is intended as an escape hatch
     2241 * for simple or transitional registrations.
    22382242 *
    22392243 * This function should be called on the {@see 'wp_abilities_api_categories_init'} action hook.
    22402244 * The category slug must be unique and can contain lowercase alphanumeric characters, dashes, and underscores.
  • src/wp-includes/abilities.php

     
    diff --git a/src/wp-includes/abilities.php b/src/wp-includes/abilities.php
    a b function wp_register_core_ability_categories(): void {  
    995995                )
    996996        );
     997
     998        wp_register_ability_category(
     999                'uncategorized',
     1000                array(
     1001                        'label'       => __( 'Uncategorized' ),
     1002                        'description' => __( 'Abilities that have not been assigned to a specific category.' ),
     1003                )
     1004        );
    9971005}
    9981006
    9991007/**
    10001008
  • tests/phpunit/tests/abilities-api/wpRegisterAbility.php

    diff --git a/tests/phpunit/tests/abilities-api/wpRegisterAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterAbility.php
    a b class Tests_Abilities_API_wpRegisterAbility extends WP_UnitTestCase {  
    117117                $this->assertSame( 'Test ability description.', $ability->get_description() );
    118118        }
     119
     120        /**
     121         * @ticket 65569
     122         */
     123        public function test_register_ability_without_category_uses_uncategorized_category(): void {
     124                global $wp_current_filter;
     125
     126                $wp_current_filter[] = 'wp_abilities_api_categories_init';
     127                wp_register_ability_category(
     128                        'uncategorized',
     129                        array(
     130                                'label'       => 'Uncategorized',
     131                                'description' => 'Abilities that have not been assigned to a specific category.',
     132                        )
     133                );
     134                array_pop( $wp_current_filter );
     135
     136                $ability = wp_register_ability(
     137                        'test/without-category',
     138                        array(
     139                                'label'               => 'Test ability without category',
     140                                'description'         => 'Test ability description.',
     141                                'input_schema'        => array(),
     142                                'output_schema'       => array(),
     143                                'permission_callback' => '__return_true',
     144                                'execute_callback'    => static function (): array {
     145                                        return array( 'success' => true );
     146                                },
     147                        )
     148                );
     149
     150                $this->assertInstanceOf( WP_Ability::class, $ability );
     151                $this->assertSame( 'uncategorized', $ability->get_category() );
     152
     153                wp_unregister_ability_category( 'uncategorized' );
     154        }
    119155
    120156        /**
    121157         * @ticket 64098