Skip to content

Commit 5ba7fb8

Browse files
committed
Make the ability category optional
When the `category` argument is omitted during ability registration, the registry now assigns the built-in `uncategorized` category. This category is registered alongside the other core ability categories. Previously, the registry only validated a category when one was provided, but `WP_Ability` still required the argument, so a registration without a category could never succeed. The fallback is applied before validation, so invalid values are still rejected. This includes non-string types and empty or unregistered category slugs. Also updates the public API documentation and adds regression coverage for the fallback and for invalid category values. Props hokansardar, sachinrajcp123, jorgefilipecosta, gziolo. Fixes #65569. git-svn-id: https://develop.svn.wordpress.org/trunk@63343 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e5f0811 commit 5ba7fb8

6 files changed

Lines changed: 171 additions & 24 deletions

File tree

‎src/wp-includes/abilities-api.php‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
* }
129129
* add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' );
130130
*
131+
* On failure, this function returns `null` and calls `_doing_it_wrong()` with the reason.
132+
* By default, the resulting notice is displayed when `WP_DEBUG` is enabled.
133+
*
131134
* ### Naming Conventions
132135
*
133136
* Ability names must follow these rules:
@@ -138,8 +141,9 @@
138141
*
139142
* ### Categories
140143
*
141-
* Abilities must be organized into categories. Ability categories provide better
142-
* discoverability and must be registered before the abilities that reference them:
144+
* Abilities can be organized into categories. If no category is provided, the ability is
145+
* assigned to the built-in `uncategorized` category. Custom categories must be registered
146+
* before the abilities that reference them:
143147
*
144148
* function my_plugin_register_categories(): void {
145149
* wp_register_ability_category(
@@ -228,6 +232,7 @@
228232
* ),
229233
*
230234
* @since 6.9.0
235+
* @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
231236
*
232237
* @see WP_Abilities_Registry::register()
233238
* @see wp_register_ability_category()
@@ -242,9 +247,9 @@
242247
* @type string $label Required. The human-readable label for the ability.
243248
* @type string $description Required. A detailed description of what the ability does
244249
* and when it should be used.
245-
* @type string $category Required. The ability category slug this ability belongs to.
246-
* The ability category must be registered via `wp_register_ability_category()`
247-
* before registering the ability.
250+
* @type string $category Optional. The ability category slug this ability belongs to.
251+
* Defaults to `uncategorized`. Custom categories must be registered
252+
* via `wp_register_ability_category()` before registering the ability.
248253
* @type callable $execute_callback Required. A callback function to execute when the ability is invoked.
249254
* Receives optional mixed input data and must return either a result
250255
* value (any type) or a `WP_Error` object on failure.
@@ -607,8 +612,10 @@ function _wp_get_abilities_match_meta( array $meta, array $conditions ): bool {
607612
* Registers a new ability category.
608613
*
609614
* Ability categories provide a way to organize and group related abilities for better
610-
* discoverability and management. Ability categories must be registered before abilities
611-
* that reference them.
615+
* discoverability and management. Custom categories must be registered before abilities
616+
* that reference them. Abilities that omit a category are assigned to the built-in
617+
* `uncategorized` category, which is intended as an escape hatch for simple or transitional
618+
* registrations.
612619
*
613620
* Ability categories must be registered on the `wp_abilities_api_categories_init` action hook.
614621
*
@@ -625,6 +632,9 @@ function _wp_get_abilities_match_meta( array $meta, array $conditions ): bool {
625632
* }
626633
* add_action( 'wp_abilities_api_categories_init', 'my_plugin_register_categories' );
627634
*
635+
* On failure, this function returns `null` and calls `_doing_it_wrong()` with the reason.
636+
* By default, the resulting notice is displayed when `WP_DEBUG` is enabled.
637+
*
628638
* @since 6.9.0
629639
*
630640
* @see WP_Ability_Categories_Registry::register()

‎src/wp-includes/abilities-api/class-wp-abilities-registry.php‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ final class WP_Abilities_Registry {
4040
* Do not use this method directly. Instead, use the `wp_register_ability()` function.
4141
*
4242
* @since 6.9.0
43+
* @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
4344
*
4445
* @see wp_register_ability()
4546
*
@@ -51,7 +52,8 @@ final class WP_Abilities_Registry {
5152
*
5253
* @type string $label The human-readable label for the ability.
5354
* @type string $description A detailed description of what the ability does.
54-
* @type string $category The ability category slug this ability belongs to.
55+
* @type string $category Optional. The ability category slug this ability belongs to.
56+
* Defaults to `uncategorized`.
5557
* @type callable $execute_callback A callback function to execute when the ability is invoked.
5658
* Receives optional mixed input and returns mixed result or WP_Error.
5759
* @type callable $permission_callback A callback function to check permissions before execution.
@@ -108,13 +110,15 @@ public function register( string $name, array $args ): ?WP_Ability {
108110
* Filters the ability arguments before they are validated and used to instantiate the ability.
109111
*
110112
* @since 6.9.0
113+
* @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
111114
*
112115
* @param array<string, mixed> $args {
113116
* An associative array of arguments for the ability.
114117
*
115118
* @type string $label The human-readable label for the ability.
116119
* @type string $description A detailed description of what the ability does.
117-
* @type string $category The ability category slug this ability belongs to.
120+
* @type string $category Optional. The ability category slug this ability belongs to.
121+
* Defaults to `uncategorized`.
118122
* @type callable $execute_callback A callback function to execute when the ability is invoked.
119123
* Receives optional mixed input and returns mixed result or WP_Error.
120124
* @type callable $permission_callback A callback function to check permissions before execution.
@@ -138,21 +142,31 @@ public function register( string $name, array $args ): ?WP_Ability {
138142
*/
139143
$args = apply_filters( 'wp_register_ability_args', $args, $name );
140144

141-
// Validate ability category exists if provided (will be validated as required in WP_Ability).
142-
if ( isset( $args['category'] ) ) {
143-
if ( ! wp_has_ability_category( $args['category'] ) ) {
144-
_doing_it_wrong(
145-
__METHOD__,
146-
sprintf(
147-
/* translators: %1$s: ability category slug, %2$s: ability name */
148-
__( 'Ability category "%1$s" is not registered. Please register the ability category before assigning it to ability "%2$s".' ),
149-
esc_html( $args['category'] ),
150-
esc_html( $name )
151-
),
152-
'6.9.0'
153-
);
154-
return null;
155-
}
145+
if ( ! array_key_exists( 'category', $args ) ) {
146+
$args['category'] = 'uncategorized';
147+
}
148+
149+
if ( ! is_string( $args['category'] ) ) {
150+
_doing_it_wrong(
151+
__METHOD__,
152+
__( 'Ability category must be a string.' ),
153+
'6.9.0'
154+
);
155+
return null;
156+
}
157+
158+
if ( ! wp_has_ability_category( $args['category'] ) ) {
159+
_doing_it_wrong(
160+
__METHOD__,
161+
sprintf(
162+
/* translators: %1$s: ability category slug, %2$s: ability name */
163+
__( 'Ability category "%1$s" is not registered. Please register the ability category before assigning it to ability "%2$s".' ),
164+
esc_html( $args['category'] ),
165+
esc_html( $name )
166+
),
167+
'6.9.0'
168+
);
169+
return null;
156170
}
157171

158172
// The class is only used to instantiate the ability, and is not a property of the ability itself.

‎src/wp-includes/abilities.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Registers the core ability categories.
1414
*
1515
* @since 6.9.0
16+
* @since 7.2.0 Added the `uncategorized` category.
1617
*/
1718
function wp_register_core_ability_categories(): void {
1819
wp_register_ability_category(
@@ -30,6 +31,14 @@ function wp_register_core_ability_categories(): void {
3031
'description' => __( 'Abilities that retrieve or modify user information and settings.' ),
3132
)
3233
);
34+
35+
wp_register_ability_category(
36+
'uncategorized',
37+
array(
38+
'label' => __( 'Uncategorized' ),
39+
'description' => __( 'Abilities that have not been assigned to a specific category.' ),
40+
)
41+
);
3342
}
3443

3544
/**

‎tests/phpunit/tests/abilities-api/wpAbilitiesRegistry.php‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,68 @@ public function test_register_ability_nonexistent_category(): void {
224224
$this->assertNull( $result, 'Should return null when category does not exist.' );
225225
}
226226

227+
/**
228+
* Tests that an invalid category type is rejected before the category lookup.
229+
*
230+
* @ticket 65569
231+
*
232+
* @dataProvider data_invalid_category_types
233+
*
234+
* @covers WP_Abilities_Registry::register
235+
*
236+
* @expectedIncorrectUsage WP_Abilities_Registry::register
237+
*
238+
* @param mixed $category Invalid category value.
239+
*/
240+
public function test_register_ability_rejects_invalid_category_type( $category ): void {
241+
$args = self::$test_ability_args;
242+
$args['category'] = $category;
243+
244+
$result = $this->registry->register( self::$test_ability_name, $args );
245+
246+
$this->assertNull( $result );
247+
$this->assertStringContainsString(
248+
'Ability category must be a string.',
249+
$this->caught_doing_it_wrong['WP_Abilities_Registry::register']
250+
);
251+
}
252+
253+
/**
254+
* Data provider for invalid category types.
255+
*
256+
* @return array<string, array<mixed>> Test cases.
257+
*/
258+
public static function data_invalid_category_types(): array {
259+
return array(
260+
'null' => array( null ),
261+
'boolean' => array( false ),
262+
'integer' => array( 1 ),
263+
'array' => array( array() ),
264+
);
265+
}
266+
267+
/**
268+
* Tests that an empty category is rejected rather than replaced by the default.
269+
*
270+
* @ticket 65569
271+
*
272+
* @covers WP_Abilities_Registry::register
273+
*
274+
* @expectedIncorrectUsage WP_Abilities_Registry::register
275+
*/
276+
public function test_register_ability_rejects_empty_category(): void {
277+
$args = self::$test_ability_args;
278+
$args['category'] = '';
279+
280+
$result = $this->registry->register( self::$test_ability_name, $args );
281+
282+
$this->assertNull( $result );
283+
$this->assertStringContainsString(
284+
'Ability category "" is not registered.',
285+
$this->caught_doing_it_wrong['WP_Abilities_Registry::register']
286+
);
287+
}
288+
227289
/**
228290
* Should reject ability registration without an execute callback.
229291
*

‎tests/phpunit/tests/abilities-api/wpRegisterAbility.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public function tear_down(): void {
100100

101101
// Clean up registered test ability category.
102102
wp_unregister_ability_category( 'math' );
103+
if ( wp_has_ability_category( 'uncategorized' ) ) {
104+
wp_unregister_ability_category( 'uncategorized' );
105+
}
103106

104107
parent::tear_down();
105108
}
@@ -222,6 +225,42 @@ public function test_register_valid_ability(): void {
222225
);
223226
}
224227

228+
/**
229+
* @ticket 65569
230+
*/
231+
public function test_register_ability_without_category_uses_uncategorized_category(): void {
232+
global $wp_current_filter;
233+
234+
$this->simulate_doing_wp_abilities_init_action();
235+
236+
$wp_current_filter[] = 'wp_abilities_api_categories_init';
237+
wp_register_ability_category(
238+
'uncategorized',
239+
array(
240+
'label' => 'Uncategorized',
241+
'description' => 'Abilities that have not been assigned to a specific category.',
242+
)
243+
);
244+
array_pop( $wp_current_filter );
245+
246+
$ability = wp_register_ability(
247+
'test/without-category',
248+
array(
249+
'label' => 'Test ability without category',
250+
'description' => 'Test ability description.',
251+
'input_schema' => array(),
252+
'output_schema' => array(),
253+
'permission_callback' => '__return_true',
254+
'execute_callback' => static function (): array {
255+
return array( 'success' => true );
256+
},
257+
)
258+
);
259+
260+
$this->assertInstanceOf( WP_Ability::class, $ability );
261+
$this->assertSame( 'uncategorized', $ability->get_category() );
262+
}
263+
225264
/**
226265
* Tests executing an ability with no permissions.
227266
*

‎tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ public static function tear_down_after_class(): void {
5353
parent::tear_down_after_class();
5454
}
5555

56+
/**
57+
* Tests that the uncategorized fallback category is registered by core.
58+
*
59+
* @ticket 65569
60+
*/
61+
public function test_uncategorized_category_is_registered(): void {
62+
$category = wp_get_ability_category( 'uncategorized' );
63+
64+
$this->assertInstanceOf( WP_Ability_Category::class, $category );
65+
$this->assertSame( 'Uncategorized', $category->get_label() );
66+
$this->assertSame( 'Abilities that have not been assigned to a specific category.', $category->get_description() );
67+
}
68+
5669
/**
5770
* Tests that the `core/get-site-info` ability is registered with the expected schema.
5871
* @ticket 64146

0 commit comments

Comments
 (0)