Skip to content

Commit fe5874d

Browse files
committed
Sync latest changes from Abilities API repo
1 parent e78cebd commit fe5874d

File tree

10 files changed

+2156
-49
lines changed

10 files changed

+2156
-49
lines changed

src/wp-includes/abilities-api/abilities-api.php renamed to src/wp-includes/abilities-api.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php declare( strict_types = 1 );
2-
1+
<?php
32
/**
43
* Abilities API
54
*
@@ -10,6 +9,8 @@
109
* @since 0.1.0
1110
*/
1211

12+
declare( strict_types = 1 );
13+
1314
/**
1415
* Registers a new ability using Abilities API.
1516
*
@@ -19,13 +20,13 @@
1920
*
2021
* @since 0.1.0
2122
*
22-
* @param string|WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
23-
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
24-
* contain lowercase alphanumeric characters, dashes and the forward slash.
25-
* @param array $properties Optional. An associative array of properties for the ability. This should
26-
* include `label`, `description`, `input_schema`, `output_schema`,
27-
* `execute_callback`, `permission_callback`, and `meta`.
28-
* @return ?WP_Ability An instance of registered ability on success, null on failure.
23+
* @param string|\WP_Ability $name The name of the ability, or WP_Ability instance.
24+
* The name must be a string containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
25+
* contain lowercase alphanumeric characters, dashes and the forward slash.
26+
* @param array<string,mixed> $properties Optional. An associative array of properties for the ability. This should
27+
* include `label`, `description`, `input_schema`, `output_schema`,
28+
* `execute_callback`, `permission_callback`, and `meta`.
29+
* @return ?\WP_Ability An instance of registered ability on success, null on failure.
2930
*/
3031
function wp_register_ability( $name, array $properties = array() ): ?WP_Ability {
3132
if ( ! did_action( 'abilities_api_init' ) ) {
@@ -35,7 +36,7 @@ function wp_register_ability( $name, array $properties = array() ): ?WP_Ability
3536
/* translators: 1: abilities_api_init, 2: string value of the ability name. */
3637
esc_html__( 'Abilities must be registered on the %1$s action. The ability %2$s was not registered.' ),
3738
'<code>abilities_api_init</code>',
38-
'<code>' . esc_attr( $name ) . '</code>'
39+
'<code>' . esc_html( $name instanceof WP_Ability ? $name->get_name() : $name ) . '</code>'
3940
),
4041
'0.1.0'
4142
);
@@ -53,7 +54,7 @@ function wp_register_ability( $name, array $properties = array() ): ?WP_Ability
5354
* @since 0.1.0
5455
*
5556
* @param string $name The name of the registered ability, with its namespace.
56-
* @return ?WP_Ability The unregistered ability instance on success, null on failure.
57+
* @return ?\WP_Ability The unregistered ability instance on success, null on failure.
5758
*/
5859
function wp_unregister_ability( string $name ): ?WP_Ability {
5960
return WP_Abilities_Registry::get_instance()->unregister( $name );
@@ -67,7 +68,7 @@ function wp_unregister_ability( string $name ): ?WP_Ability {
6768
* @since 0.1.0
6869
*
6970
* @param string $name The name of the registered ability, with its namespace.
70-
* @return ?WP_Ability The registered ability instance, or null if it is not registered.
71+
* @return ?\WP_Ability The registered ability instance, or null if it is not registered.
7172
*/
7273
function wp_get_ability( string $name ): ?WP_Ability {
7374
return WP_Abilities_Registry::get_instance()->get_registered( $name );
@@ -80,7 +81,7 @@ function wp_get_ability( string $name ): ?WP_Ability {
8081
*
8182
* @since 0.1.0
8283
*
83-
* @return WP_Ability[] The array of registered abilities.
84+
* @return \WP_Ability[] The array of registered abilities.
8485
*/
8586
function wp_get_abilities(): array {
8687
return WP_Abilities_Registry::get_instance()->get_all_registered();

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

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php declare( strict_types = 1 );
2-
1+
<?php
32
/**
43
* Abilities API
54
*
@@ -10,6 +9,8 @@
109
* @since 0.1.0
1110
*/
1211

12+
declare( strict_types = 1 );
13+
1314
/**
1415
* Manages the registration and lookup of abilities.
1516
*
@@ -21,18 +22,10 @@ final class WP_Abilities_Registry {
2122
* Holds the registered abilities.
2223
*
2324
* @since 0.1.0
24-
* @var WP_Ability[]
25+
* @var \WP_Ability[]
2526
*/
2627
private $registered_abilities = array();
2728

28-
/**
29-
* Container for the main instance of the class.
30-
*
31-
* @since 0.1.0
32-
* @var ?WP_Abilities_Registry
33-
*/
34-
private static $instance = null;
35-
3629
/**
3730
* Registers a new ability.
3831
*
@@ -42,13 +35,13 @@ final class WP_Abilities_Registry {
4235
*
4336
* @since 0.1.0
4437
*
45-
* @param string|WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
46-
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
47-
* contain lowercase alphanumeric characters, dashes and the forward slash.
48-
* @param array $properties Optional. An associative array of properties for the ability. This should
49-
* include `label`, `description`, `input_schema`, `output_schema`,
50-
* `execute_callback`, `permission_callback`, and `meta`.
51-
* @return ?WP_Ability The registered ability instance on success, null on failure.
38+
* @param string|\WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
39+
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
40+
* contain lowercase alphanumeric characters, dashes and the forward slash.
41+
* @param array<string,mixed> $properties Optional. An associative array of properties for the ability. This should
42+
* include `label`, `description`, `input_schema`, `output_schema`,
43+
* `execute_callback`, `permission_callback`, and `meta`.
44+
* @return ?\WP_Ability The registered ability instance on success, null on failure.
5245
*/
5346
public function register( $name, array $properties = array() ): ?WP_Ability {
5447
$ability = null;
@@ -159,6 +152,7 @@ public function register( $name, array $properties = array() ): ?WP_Ability {
159152
'meta' => $properties['meta'] ?? array(),
160153
)
161154
);
155+
162156
$this->registered_abilities[ $name ] = $ability;
163157
return $ability;
164158
}
@@ -173,9 +167,9 @@ public function register( $name, array $properties = array() ): ?WP_Ability {
173167
* @since 0.1.0
174168
*
175169
* @param string $name The name of the registered ability, with its namespace.
176-
* @return ?WP_Ability The unregistered ability instance on success, null on failure.
170+
* @return ?\WP_Ability The unregistered ability instance on success, null on failure.
177171
*/
178-
public function unregister( $name ): ?WP_Ability {
172+
public function unregister( string $name ): ?WP_Ability {
179173
if ( ! $this->is_registered( $name ) ) {
180174
_doing_it_wrong(
181175
__METHOD__,
@@ -201,7 +195,7 @@ public function unregister( $name ): ?WP_Ability {
201195
*
202196
* @since 0.1.0
203197
*
204-
* @return WP_Ability[] The array of registered abilities.
198+
* @return \WP_Ability[] The array of registered abilities.
205199
*/
206200
public function get_all_registered(): array {
207201
return $this->registered_abilities;
@@ -215,7 +209,7 @@ public function get_all_registered(): array {
215209
* @param string $name The name of the registered ability, with its namespace.
216210
* @return bool True if the ability is registered, false otherwise.
217211
*/
218-
public function is_registered( $name ): bool {
212+
public function is_registered( string $name ): bool {
219213
return isset( $this->registered_abilities[ $name ] );
220214
}
221215

@@ -229,9 +223,9 @@ public function is_registered( $name ): bool {
229223
* @since 0.1.0
230224
*
231225
* @param string $name The name of the registered ability, with its namespace.
232-
* @return ?WP_Ability The registered ability instance, or null if it is not registered.
226+
* @return ?\WP_Ability The registered ability instance, or null if it is not registered.
233227
*/
234-
public function get_registered( $name ): ?WP_Ability {
228+
public function get_registered( string $name ): ?WP_Ability {
235229
if ( ! $this->is_registered( $name ) ) {
236230
_doing_it_wrong(
237231
__METHOD__,
@@ -251,10 +245,10 @@ public function get_registered( $name ): ?WP_Ability {
251245
*
252246
* @since 0.1.0
253247
*
254-
* @return WP_Abilities_Registry The main registry instance.
248+
* @return \WP_Abilities_Registry The main registry instance.
255249
*/
256-
public static function get_instance(): WP_Abilities_Registry {
257-
/* @var WP_Abilities_Registry $wp_abilities */
250+
public static function get_instance(): self {
251+
/** @var \WP_Abilities_Registry $wp_abilities */
258252
global $wp_abilities;
259253

260254
if ( empty( $wp_abilities ) ) {
@@ -267,7 +261,7 @@ public static function get_instance(): WP_Abilities_Registry {
267261
*
268262
* @since 0.1.0
269263
*
270-
* @param WP_Abilities_Registry $instance Abilities registry object.
264+
* @param \WP_Abilities_Registry $instance Abilities registry object.
271265
*/
272266
do_action( 'abilities_api_init', $wp_abilities );
273267
}
@@ -279,15 +273,12 @@ public static function get_instance(): WP_Abilities_Registry {
279273
* Wakeup magic method.
280274
*
281275
* @since 0.1.0
276+
* @throws \UnexpectedValueException If any of the registered abilities is not an instance of WP_Ability.
282277
*/
283278
public function __wakeup(): void {
284-
if ( empty( $this->registered_abilities ) ) {
285-
return;
286-
}
287-
288279
foreach ( $this->registered_abilities as $ability ) {
289280
if ( ! $ability instanceof WP_Ability ) {
290-
throw new UnexpectedValueException();
281+
throw new \UnexpectedValueException();
291282
}
292283
}
293284
}

src/wp-includes/rest-api.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,12 @@ function create_initial_rest_routes() {
416416
// Font Collections.
417417
$font_collections_controller = new WP_REST_Font_Collections_Controller();
418418
$font_collections_controller->register_routes();
419+
420+
// Abilities.
421+
$abilities_list_controller = new WP_REST_Abilities_List_Controller();
422+
$abilities_list_controller->register_routes();
423+
$abilities_run_controller = new WP_REST_Abilities_Run_Controller();
424+
$abilities_run_controller->register_routes();
419425
}
420426

421427
/**

0 commit comments

Comments
 (0)