Skip to content

Commit d3e1cd7

Browse files
committed
Improve handling for init hooks related to abilities and their categories
1 parent 0c0719e commit d3e1cd7

File tree

8 files changed

+117
-251
lines changed

8 files changed

+117
-251
lines changed

src/wp-includes/abilities-api.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ function wp_get_abilities(): array {
137137
* @return WP_Ability_Category|null The registered ability category instance on success, null on failure.
138138
*/
139139
function wp_register_ability_category( string $slug, array $args ): ?WP_Ability_Category {
140+
if ( ! did_action( 'wp_abilities_api_categories_init' ) ) {
141+
_doing_it_wrong(
142+
__METHOD__,
143+
sprintf(
144+
/* translators: 1: abilities_api_categories_init, 2: ability category slug. */
145+
__( 'Ability categories must be registered on the %1$s action. The category %2$s was not registered.' ),
146+
'<code>wp_abilities_api_categories_init</code>',
147+
'<code>' . esc_html( $slug ) . '</code>'
148+
),
149+
'6.9.0'
150+
);
151+
return null;
152+
}
153+
140154
return WP_Ability_Categories_Registry::get_instance()->register( $slug, $args );
141155
}
142156

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,6 @@ final class WP_Ability_Categories_Registry {
5555
* @return WP_Ability_Category|null The registered ability category instance on success, null on failure.
5656
*/
5757
public function register( string $slug, array $args ): ?WP_Ability_Category {
58-
if ( ! doing_action( 'wp_abilities_api_categories_init' ) ) {
59-
_doing_it_wrong(
60-
__METHOD__,
61-
sprintf(
62-
/* translators: 1: abilities_api_categories_init, 2: ability category slug. */
63-
__( 'Ability categories must be registered during the %1$s action. The category %2$s was not registered.' ),
64-
'<code>wp_abilities_api_categories_init</code>',
65-
'<code>' . esc_html( $slug ) . '</code>'
66-
),
67-
'6.9.0'
68-
);
69-
return null;
70-
}
71-
7258
if ( $this->is_registered( $slug ) ) {
7359
_doing_it_wrong(
7460
__METHOD__,

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,15 @@ public function set_up(): void {
2929

3030
remove_all_filters( 'wp_register_ability_args' );
3131

32-
// Register category during the hook.
33-
add_action(
34-
'wp_abilities_api_categories_init',
35-
function () {
36-
if ( ! wp_has_ability_category( 'math' ) ) {
37-
wp_register_ability_category(
38-
'math',
39-
array(
40-
'label' => 'Math',
41-
'description' => 'Mathematical operations and calculations.',
42-
)
43-
);
44-
}
45-
}
46-
);
47-
48-
// Fire the hook to allow category registration.
32+
// Fire the init hook to allow test ability category registration.
4933
do_action( 'wp_abilities_api_categories_init' );
34+
wp_register_ability_category(
35+
'math',
36+
array(
37+
'label' => 'Math',
38+
'description' => 'Mathematical operations and calculations.',
39+
)
40+
);
5041

5142
self::$test_ability_args = array(
5243
'label' => 'Add numbers',
@@ -93,11 +84,8 @@ public function tear_down(): void {
9384

9485
remove_all_filters( 'wp_register_ability_args' );
9586

96-
// Clean up registered categories.
97-
$category_registry = WP_Ability_Categories_Registry::get_instance();
98-
if ( $category_registry->is_registered( 'math' ) ) {
99-
wp_unregister_ability_category( 'math' );
100-
}
87+
// Clean up registered test ability category.
88+
wp_unregister_ability_category( 'math' );
10189

10290
parent::tear_down();
10391
}
@@ -214,6 +202,24 @@ public function test_register_invalid_description_type() {
214202
$this->assertNull( $result );
215203
}
216204

205+
/**
206+
* Tests registering an ability with non-existent category.
207+
*
208+
* @ticket 64098
209+
*
210+
* @expectedIncorrectUsage WP_Abilities_Registry::register
211+
*/
212+
public function test_register_ability_nonexistent_category(): void {
213+
$args = array_merge(
214+
self::$test_ability_args,
215+
array( 'category' => 'nonexistent' )
216+
);
217+
218+
$result = $this->registry->register( self::$test_ability_name, $args );
219+
220+
$this->assertNull( $result, 'Should return null when category does not exist.' );
221+
}
222+
217223
/**
218224
* Should reject ability registration without an execute callback.
219225
*

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

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,15 @@ class Tests_Abilities_API_WpAbility extends WP_UnitTestCase {
1818
public function set_up(): void {
1919
parent::set_up();
2020

21-
// Register category during the hook.
22-
add_action(
23-
'wp_abilities_api_categories_init',
24-
function () {
25-
if ( ! wp_has_ability_category( 'math' ) ) {
26-
wp_register_ability_category(
27-
'math',
28-
array(
29-
'label' => 'Math',
30-
'description' => 'Mathematical operations and calculations.',
31-
)
32-
);
33-
}
34-
}
35-
);
36-
37-
// Fire the hook to allow category registration.
21+
// Fire the init hook to allow test ability category registration.
3822
do_action( 'wp_abilities_api_categories_init' );
23+
wp_register_ability_category(
24+
'math',
25+
array(
26+
'label' => 'Math',
27+
'description' => 'Mathematical operations and calculations.',
28+
)
29+
);
3930

4031
self::$test_ability_properties = array(
4132
'label' => 'Calculator',
@@ -65,11 +56,8 @@ function () {
6556
* Tear down after each test.
6657
*/
6758
public function tear_down(): void {
68-
// Clean up registered categories.
69-
$category_registry = WP_Ability_Categories_Registry::get_instance();
70-
if ( $category_registry->is_registered( 'math' ) ) {
71-
wp_unregister_ability_category( 'math' );
72-
}
59+
// Clean up registered test ability category.
60+
wp_unregister_ability_category( 'math' );
7361

7462
parent::tear_down();
7563
}

0 commit comments

Comments
 (0)