Skip to content

Commit 91b3f6b

Browse files
committed
Introduce wp_has_ability() and wp_has_ability_category() public methods
1 parent ff227c6 commit 91b3f6b

File tree

9 files changed

+113
-18
lines changed

9 files changed

+113
-18
lines changed

src/wp-includes/abilities-api.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ function wp_unregister_ability( string $name ): ?WP_Ability {
7777
return WP_Abilities_Registry::get_instance()->unregister( $name );
7878
}
7979

80+
/**
81+
* Checks if an ability is registered.
82+
*
83+
* @since 6.9.0
84+
*
85+
* @see WP_Abilities_Registry::is_registered()
86+
*
87+
* @param string $name The name of the registered ability, with its namespace.
88+
* @return bool True if the ability is registered, false otherwise.
89+
*/
90+
function wp_has_ability( string $name ): bool {
91+
return WP_Abilities_Registry::get_instance()->is_registered( $name );
92+
}
93+
8094
/**
8195
* Retrieves a registered ability using Abilities API.
8296
*
@@ -140,6 +154,20 @@ function wp_unregister_ability_category( string $slug ): ?WP_Ability_Category {
140154
return WP_Ability_Categories_Registry::get_instance()->unregister( $slug );
141155
}
142156

157+
/**
158+
* Checks if an ability category is registered.
159+
*
160+
* @since 6.9.0
161+
*
162+
* @see WP_Ability_Categories_Registry::is_registered()
163+
*
164+
* @param string $slug The slug of the ability category.
165+
* @return bool True if the ability category is registered, false otherwise.
166+
*/
167+
function wp_has_ability_category( string $slug ): bool {
168+
return WP_Ability_Categories_Registry::get_instance()->is_registered( $slug );
169+
}
170+
143171
/**
144172
* Retrieves a registered ability category.
145173
*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,12 @@ public function get_all_registered(): array {
214214
/**
215215
* Checks if an ability is registered.
216216
*
217+
* Do not use this method directly. Instead, use the `wp_has_ability()` function.
218+
*
217219
* @since 6.9.0
218220
*
221+
* @see wp_has_ability()
222+
*
219223
* @param string $name The name of the registered ability, with its namespace.
220224
* @return bool True if the ability is registered, false otherwise.
221225
*/

src/wp-includes/abilities-api/class-wp-ability-categories-registry.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function register( string $slug, array $args ): ?WP_Ability_Category {
121121
}
122122

123123
/**
124-
* Unregisters a ability category.
124+
* Unregisters an ability category.
125125
*
126126
* Do not use this method directly. Instead, use the `wp_unregister_ability_category()` function.
127127
*
@@ -165,10 +165,14 @@ public function get_all_registered(): array {
165165
}
166166

167167
/**
168-
* Checks if a ability category is registered.
168+
* Checks if an ability category is registered.
169+
*
170+
* Do not use this method directly. Instead, use the `wp_has_ability_category()` function.
169171
*
170172
* @since 6.9.0
171173
*
174+
* @see wp_has_ability_category()
175+
*
172176
* @param string $slug The slug of the ability category.
173177
* @return bool True if the ability category is registered, false otherwise.
174178
*/

src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-list-controller.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,17 @@ public function get_collection_params(): array {
312312
return array(
313313
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
314314
'page' => array(
315-
'description' => __( 'Current page of the collection.' ),
316-
'type' => 'integer',
317-
'default' => 1,
318-
'minimum' => 1,
315+
'description' => __( 'Current page of the collection.' ),
316+
'type' => 'integer',
317+
'default' => 1,
318+
'minimum' => 1,
319319
),
320320
'per_page' => array(
321-
'description' => __( 'Maximum number of items to be returned in result set.' ),
322-
'type' => 'integer',
323-
'default' => self::DEFAULT_PER_PAGE,
324-
'minimum' => 1,
325-
'maximum' => 100,
321+
'description' => __( 'Maximum number of items to be returned in result set.' ),
322+
'type' => 'integer',
323+
'default' => self::DEFAULT_PER_PAGE,
324+
'minimum' => 1,
325+
'maximum' => 100,
326326
),
327327
'category' => array(
328328
'description' => __( 'Limit results to abilities in specific ability category.' ),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function set_up(): void {
3333
add_action(
3434
'wp_abilities_api_categories_init',
3535
function () {
36-
if ( ! WP_Ability_Categories_Registry::get_instance()->is_registered( 'math' ) ) {
36+
if ( ! wp_has_ability_category( 'math' ) ) {
3737
wp_register_ability_category(
3838
'math',
3939
array(
@@ -464,7 +464,7 @@ public function test_get_registered_for_known_ability() {
464464
}
465465

466466
/**
467-
* Unregistering should fail if a ability is not registered.
467+
* Unregistering should fail if an ability is not registered.
468468
*
469469
* @ticket 64098
470470
*

tests/phpunit/tests/abilities-api/wpAbility.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function set_up(): void {
2222
add_action(
2323
'wp_abilities_api_categories_init',
2424
function () {
25-
if ( ! WP_Ability_Categories_Registry::get_instance()->is_registered( 'math' ) ) {
25+
if ( ! wp_has_ability_category( 'math' ) ) {
2626
wp_register_ability_category(
2727
'math',
2828
array(

tests/phpunit/tests/abilities-api/wpAbilityCategory.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,37 @@ public function test_get_nonexistent_category(): void {
355355
$this->assertDoingItWrongTriggered( 'WP_Ability_Categories_Registry::get_registered' );
356356
}
357357

358+
/**
359+
* Tests checking if an ability category is registered.
360+
*
361+
* @ticket 64098
362+
*/
363+
public function test_has_registered_ability_category(): void {
364+
$category_slug = 'test-math';
365+
$this->register_category_during_hook(
366+
$category_slug,
367+
array(
368+
'label' => 'Math',
369+
'description' => 'Mathematical operations.',
370+
)
371+
);
372+
373+
$result = wp_has_ability_category( $category_slug );
374+
375+
$this->assertTrue( $result );
376+
}
377+
378+
/**
379+
* Tests checking if a non-existent ability category is registered.
380+
*
381+
* @ticket 64098
382+
*/
383+
public function test_has_registered_nonexistent_ability_category(): void {
384+
$result = wp_has_ability_category( 'test/non-existent' );
385+
386+
$this->assertFalse( $result );
387+
}
388+
358389
/**
359390
* Test retrieving all registered categories.
360391
*
@@ -416,7 +447,7 @@ public function test_ability_requires_existing_category(): void {
416447

417448
// Ensure category doesn't exist - test should fail if it does.
418449
$this->assertFalse(
419-
WP_Ability_Categories_Registry::get_instance()->is_registered( 'test-nonexistent' ),
450+
wp_has_ability_category( 'test-nonexistent' ),
420451
'The test-nonexistent category should not be registered - test isolation may be broken'
421452
);
422453

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function set_up(): void {
3434
add_action(
3535
'wp_abilities_api_categories_init',
3636
function () {
37-
if ( ! WP_Ability_Categories_Registry::get_instance()->is_registered( 'math' ) ) {
37+
if ( ! wp_has_ability_category( 'math' ) ) {
3838
wp_register_ability_category(
3939
'math',
4040
array(
@@ -471,6 +471,34 @@ public function test_get_existing_ability() {
471471
);
472472
}
473473

474+
/**
475+
* Tests checking if an ability is registered.
476+
*
477+
* @ticket 64098
478+
*/
479+
public function test_has_registered_ability() {
480+
do_action( 'wp_abilities_api_init' );
481+
482+
wp_register_ability( self::$test_ability_name, self::$test_ability_args );
483+
484+
$result = wp_has_ability( self::$test_ability_name );
485+
486+
$this->assertTrue( $result );
487+
}
488+
489+
/**
490+
* Tests checking if a non-existent ability is registered.
491+
*
492+
* @ticket 64098
493+
*/
494+
public function test_has_registered_nonexistent_abi() {
495+
do_action( 'wp_abilities_api_init' );
496+
497+
$result = wp_has_ability( 'test/non-existent' );
498+
499+
$this->assertFalse( $result );
500+
}
501+
474502
/**
475503
* Tests retrieving all registered abilities.
476504
*
@@ -513,7 +541,7 @@ public function test_register_ability_nonexistent_category(): void {
513541

514542
// Ensure category doesn't exist - test should fail if it does.
515543
$this->assertFalse(
516-
WP_Ability_Categories_Registry::get_instance()->is_registered( 'nonexistent' ),
544+
wp_has_ability_category( 'nonexistent' ),
517545
'The nonexistent category should not be registered - test isolation may be broken'
518546
);
519547

tests/phpunit/tests/rest-api/wpRestAbilitiesListController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ public function test_filter_by_category(): void {
760760
public function test_filter_by_nonexistent_category(): void {
761761
// Ensure category doesn't exist - test should fail if it does.
762762
$this->assertFalse(
763-
WP_Ability_Categories_Registry::get_instance()->is_registered( 'nonexistent' ),
763+
wp_has_ability_category( 'nonexistent' ),
764764
'The nonexistent category should not be registered - test isolation may be broken'
765765
);
766766

0 commit comments

Comments
 (0)