Skip to content

Commit 3fe7aba

Browse files
committed
Abilities API: Require a registered category for abilities
Partially reverts [63343]. Abilities that omit a category are no longer assigned to the built-in `uncategorized` category, which is removed. Registration fails with a `_doing_it_wrong()` notice instead. Updates the docs to explain why categories are required and how to reuse existing ones. Developed in WordPress#15462. Props gziolo, jason_the_adams, jorgefilipecosta. Fixes #65569. git-svn-id: https://develop.svn.wordpress.org/trunk@65308 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6af9238 commit 3fe7aba

6 files changed

Lines changed: 31 additions & 74 deletions

File tree

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@
5252
* }
5353
* add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' );
5454
*
55-
* Use a category to group related abilities and improve discoverability. The
56-
* `data-export` category must be registered first with `wp_register_ability_category()`
55+
* Every ability requires a category to group related abilities and improve discoverability.
56+
* The `data-export` category must be registered first with `wp_register_ability_category()`
5757
* on `wp_abilities_api_categories_init`, or the ability registration fails.
58-
* Abilities that omit `category` fall back to the built-in `uncategorized` category.
5958
*
6059
* Once registered, abilities can be checked, retrieved, and managed:
6160
*
@@ -148,9 +147,11 @@
148147
*
149148
* ### Categories
150149
*
151-
* Abilities can be organized into categories. If no category is provided, the ability is
152-
* assigned to the built-in `uncategorized` category. Custom categories must be registered
153-
* before the abilities that reference them:
150+
* Every ability must specify a registered category. Categories group related abilities
151+
* so clients can discover and retrieve relevant subsets. Plugins can reuse an existing
152+
* category when appropriate, including categories registered by core. Custom categories must be
153+
* registered on `wp_abilities_api_categories_init` before abilities that reference them
154+
* are registered on `wp_abilities_api_init`:
154155
*
155156
* function my_plugin_register_categories(): void {
156157
* wp_register_ability_category(
@@ -286,7 +287,6 @@
286287
*
287288
* @since 6.9.0
288289
* @since 7.1.0 Added the `public` meta argument.
289-
* @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
290290
* @since 7.2.0 Added the `required_capabilities` argument.
291291
*
292292
* @see WP_Abilities_Registry::register()
@@ -304,9 +304,9 @@
304304
* @type string $label Required. The human-readable label for the ability.
305305
* @type string $description Required. A detailed description of what the ability does
306306
* and when it should be used.
307-
* @type string $category Optional. The ability category slug this ability belongs to.
308-
* Defaults to `uncategorized`. Custom categories must be registered
309-
* via `wp_register_ability_category()` before registering the ability.
307+
* @type string $category Required. The registered ability category slug this ability belongs to.
308+
* Custom categories must be registered via
309+
* `wp_register_ability_category()` before registering the ability.
310310
* @type callable $execute_callback Required. A callback function to execute when the ability is invoked.
311311
* Receives optional mixed input data and must return either a result
312312
* value (any type) or a `WP_Error` object on failure.
@@ -707,10 +707,8 @@ function _wp_get_abilities_match_meta( array $meta, array $conditions ): bool {
707707
* Registers a new ability category.
708708
*
709709
* Ability categories provide a way to organize and group related abilities for better
710-
* discoverability and management. Custom categories must be registered before abilities
711-
* that reference them. Abilities that omit a category are assigned to the built-in
712-
* `uncategorized` category, which is intended as an escape hatch for simple or transitional
713-
* registrations.
710+
* discoverability and management. Every ability requires a registered category.
711+
* Custom categories must be registered before abilities that reference them.
714712
*
715713
* Ability categories must be registered on the `wp_abilities_api_categories_init` action hook.
716714
*

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ final class WP_Abilities_Registry {
4141
*
4242
* @since 6.9.0
4343
* @since 7.1.0 Added the `public` meta argument.
44-
* @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
4544
* @since 7.2.0 Added the `required_capabilities` argument.
4645
*
4746
* @see wp_register_ability()
@@ -54,8 +53,7 @@ final class WP_Abilities_Registry {
5453
*
5554
* @type string $label The human-readable label for the ability.
5655
* @type string $description A detailed description of what the ability does.
57-
* @type string $category Optional. The ability category slug this ability belongs to.
58-
* Defaults to `uncategorized`.
56+
* @type string $category The registered ability category slug this ability belongs to.
5957
* @type callable $execute_callback A callback function to execute when the ability is invoked.
6058
* Receives optional mixed input and returns mixed result or WP_Error.
6159
* @type callable $permission_callback A callback function to check permissions before execution.
@@ -119,16 +117,14 @@ public function register( string $name, array $args ): ?WP_Ability {
119117
*
120118
* @since 6.9.0
121119
* @since 7.1.0 Added the `public` meta argument.
122-
* @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
123120
* @since 7.2.0 Added the `required_capabilities` argument.
124121
*
125122
* @param array<string, mixed> $args {
126123
* An associative array of arguments for the ability.
127124
*
128125
* @type string $label The human-readable label for the ability.
129126
* @type string $description A detailed description of what the ability does.
130-
* @type string $category Optional. The ability category slug this ability belongs to.
131-
* Defaults to `uncategorized`.
127+
* @type string $category The registered ability category slug this ability belongs to.
132128
* @type callable $execute_callback A callback function to execute when the ability is invoked.
133129
* Receives optional mixed input and returns mixed result or WP_Error.
134130
* @type callable $permission_callback A callback function to check permissions before execution.
@@ -156,14 +152,10 @@ public function register( string $name, array $args ): ?WP_Ability {
156152
*/
157153
$args = apply_filters( 'wp_register_ability_args', $args, $name );
158154

159-
if ( ! array_key_exists( 'category', $args ) ) {
160-
$args['category'] = 'uncategorized';
161-
}
162-
163-
if ( ! is_string( $args['category'] ) ) {
155+
if ( ! isset( $args['category'] ) || ! is_string( $args['category'] ) ) {
164156
_doing_it_wrong(
165157
__METHOD__,
166-
__( 'Ability category must be a string.' ),
158+
__( 'The ability properties must contain a `category` string.' ),
167159
'6.9.0'
168160
);
169161
return null;

‎src/wp-includes/abilities.php‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Registers the core ability categories.
1818
*
1919
* @since 6.9.0
20-
* @since 7.2.0 Added the `content` and `uncategorized` categories.
20+
* @since 7.2.0 Added the `content` category.
2121
*/
2222
function wp_register_core_ability_categories(): void {
2323
wp_register_ability_category(
@@ -43,14 +43,6 @@ function wp_register_core_ability_categories(): void {
4343
'description' => __( 'Abilities that retrieve or manage posts and other content.' ),
4444
)
4545
);
46-
47-
wp_register_ability_category(
48-
'uncategorized',
49-
array(
50-
'label' => __( 'Uncategorized' ),
51-
'description' => __( 'Abilities that have not been assigned to a specific category.' ),
52-
)
53-
);
5446
}
5547

5648
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function test_register_ability_rejects_invalid_category_type( $category )
245245

246246
$this->assertNull( $result );
247247
$this->assertStringContainsString(
248-
'Ability category must be a string.',
248+
'The ability properties must contain a `category` string.',
249249
$this->caught_doing_it_wrong['WP_Abilities_Registry::register']
250250
);
251251
}
@@ -265,7 +265,7 @@ public static function data_invalid_category_types(): array {
265265
}
266266

267267
/**
268-
* Tests that an empty category is rejected rather than replaced by the default.
268+
* Tests that an empty category is rejected.
269269
*
270270
* @ticket 65569
271271
*

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

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ 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-
}
106103

107104
parent::tear_down();
108105
}
@@ -226,39 +223,21 @@ public function test_register_valid_ability(): void {
226223
}
227224

228225
/**
226+
* Tests that registering an ability requires a category.
227+
*
229228
* @ticket 65569
229+
* @expectedIncorrectUsage WP_Abilities_Registry::register
230230
*/
231-
public function test_register_ability_without_category_uses_uncategorized_category(): void {
232-
global $wp_current_filter;
233-
231+
public function test_register_ability_without_category_fails(): void {
234232
$this->simulate_doing_wp_abilities_init_action();
235233

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 );
234+
$args = self::$test_ability_args;
235+
unset( $args['category'] );
245236

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-
);
237+
$ability = wp_register_ability( self::$test_ability_name, $args );
259238

260-
$this->assertInstanceOf( WP_Ability::class, $ability );
261-
$this->assertSame( 'uncategorized', $ability->get_category() );
239+
$this->assertNull( $ability );
240+
$this->assertFalse( wp_has_ability( self::$test_ability_name ) );
262241
}
263242

264243
/**

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,12 @@ public static function tear_down_after_class(): void {
5757
}
5858

5959
/**
60-
* Tests that the uncategorized fallback category is registered by core.
60+
* Tests that core does not register an uncategorized fallback category.
6161
*
6262
* @ticket 65569
6363
*/
64-
public function test_uncategorized_category_is_registered(): void {
65-
$category = wp_get_ability_category( 'uncategorized' );
66-
67-
$this->assertInstanceOf( WP_Ability_Category::class, $category );
68-
$this->assertSame( 'Uncategorized', $category->get_label() );
69-
$this->assertSame( 'Abilities that have not been assigned to a specific category.', $category->get_description() );
64+
public function test_uncategorized_category_is_not_registered(): void {
65+
$this->assertFalse( wp_has_ability_category( 'uncategorized' ) );
7066
}
7167

7268
/**

0 commit comments

Comments
 (0)