Skip to content

Commit bc3b776

Browse files
committed
feat: adds support for versioning abilities
1 parent 38c3584 commit bc3b776

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ final class WP_Abilities_Registry {
4444
* @see wp_register_ability()
4545
*
4646
* @param string $name The name of the ability. The name must be a string containing a namespace
47-
* prefix, i.e. `my-plugin/my-ability`. It can only contain lowercase
48-
* alphanumeric characters, dashes and the forward slash.
47+
* prefix, i.e. `my-plugin/my-ability` or `my-plugin/v1/my-ability`. It can
48+
* only contain lowercase alphanumeric characters, dashes and the forward slash.
4949
* @param array<string, mixed> $args {
5050
* An associative array of arguments for the ability.
5151
*
@@ -78,11 +78,11 @@ final class WP_Abilities_Registry {
7878
* @return WP_Ability|null The registered ability instance on success, null on failure.
7979
*/
8080
public function register( string $name, array $args ): ?WP_Ability {
81-
if ( ! preg_match( '/^[a-z0-9-]+\/[a-z0-9-]+$/', $name ) ) {
81+
if ( ! preg_match( '/^[a-z0-9-]+\/([a-z0-9-]+\/)?[a-z0-9-]+$/', $name ) ) {
8282
_doing_it_wrong(
8383
__METHOD__,
8484
__(
85-
'Ability name must be a string containing a namespace prefix, i.e. "my-plugin/my-ability". It can only contain lowercase alphanumeric characters, dashes and the forward slash.'
85+
'Ability name must be a string containing a namespace prefix, i.e. "my-plugin/my-ability" or "my-plugin/v1/my-ability". It can only contain lowercase alphanumeric characters, dashes and the forward slash.'
8686
),
8787
'6.9.0'
8888
);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ public function test_register_invalid_uppercase_characters_in_name() {
134134
$this->assertNull( $result );
135135
}
136136

137+
/**
138+
* Should reject ability name with more than two slashes.
139+
*
140+
* @ticket 64098
141+
*
142+
* @covers WP_Abilities_Registry::register
143+
*
144+
* @expectedIncorrectUsage WP_Abilities_Registry::register
145+
*/
146+
public function test_register_invalid_too_many_slashes_in_name() {
147+
$result = $this->registry->register( 'test/v1/extra/add-numbers', self::$test_ability_args );
148+
$this->assertNull( $result );
149+
}
150+
137151
/**
138152
* Should reject ability registration without a label.
139153
*
@@ -411,6 +425,23 @@ public function test_register_new_ability() {
411425
);
412426
}
413427

428+
/**
429+
* Should successfully register a new ability with a versioned name.
430+
*
431+
* @ticket 64098
432+
*
433+
* @covers WP_Abilities_Registry::register
434+
*/
435+
public function test_register_new_ability_with_versioned_name() {
436+
$versioned_name = 'test/v1/add-numbers';
437+
$result = $this->registry->register( $versioned_name, self::$test_ability_args );
438+
439+
$this->assertEquals(
440+
new WP_Ability( $versioned_name, self::$test_ability_args ),
441+
$result
442+
);
443+
}
444+
414445
/**
415446
* Should return false for ability that's not registered.
416447
*

0 commit comments

Comments
 (0)