Skip to content

Commit 0165d8c

Browse files
committed
Sync latest changes from Abilities API repo
1 parent 18e5806 commit 0165d8c

13 files changed

+2293
-68
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/abilities-api/class-wp-ability.php

Lines changed: 20 additions & 19 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
* Encapsulates the properties and methods related to a specific ability in the registry.
1516
*
@@ -49,15 +50,15 @@ class WP_Ability {
4950
* The optional ability input schema.
5051
*
5152
* @since 0.1.0
52-
* @var array
53+
* @var array<string,mixed>
5354
*/
5455
protected $input_schema = array();
5556

5657
/**
5758
* The optional ability output schema.
5859
*
5960
* @since 0.1.0
60-
* @var array
61+
* @var array<string,mixed>
6162
*/
6263
protected $output_schema = array();
6364

@@ -81,7 +82,7 @@ class WP_Ability {
8182
* The optional ability metadata.
8283
*
8384
* @since 0.1.0
84-
* @var array
85+
* @var array<string,mixed>
8586
*/
8687
protected $meta = array();
8788

@@ -94,10 +95,10 @@ class WP_Ability {
9495
*
9596
* @since 0.1.0
9697
*
97-
* @param string $name The name of the ability, with its namespace.
98-
* @param array $properties An associative array of properties for the ability. This should
99-
* include `label`, `description`, `input_schema`, `output_schema`,
100-
* `execute_callback`, `permission_callback`, and `meta`.
98+
* @param string $name The name of the ability, with its namespace.
99+
* @param array<string,mixed> $properties An associative array of properties for the ability. This should
100+
* include `label`, `description`, `input_schema`, `output_schema`,
101+
* `execute_callback`, `permission_callback`, and `meta`.
101102
*/
102103
public function __construct( string $name, array $properties ) {
103104
$this->name = $name;
@@ -145,7 +146,7 @@ public function get_description(): string {
145146
*
146147
* @since 0.1.0
147148
*
148-
* @return array The input schema for the ability.
149+
* @return array<string,mixed> The input schema for the ability.
149150
*/
150151
public function get_input_schema(): array {
151152
return $this->input_schema;
@@ -156,7 +157,7 @@ public function get_input_schema(): array {
156157
*
157158
* @since 0.1.0
158159
*
159-
* @return array The output schema for the ability.
160+
* @return array<string,mixed> The output schema for the ability.
160161
*/
161162
public function get_output_schema(): array {
162163
return $this->output_schema;
@@ -167,7 +168,7 @@ public function get_output_schema(): array {
167168
*
168169
* @since 0.1.0
169170
*
170-
* @return array The metadata for the ability.
171+
* @return array<string,mixed> The metadata for the ability.
171172
*/
172173
public function get_meta(): array {
173174
return $this->meta;
@@ -178,7 +179,7 @@ public function get_meta(): array {
178179
*
179180
* @since 0.1.0
180181
*
181-
* @param array $input Optional. The input data to validate.
182+
* @param array<string,mixed> $input Optional. The input data to validate.
182183
* @return bool Returns true if valid, false if validation fails.
183184
*/
184185
protected function validate_input( array $input = array() ): bool {
@@ -214,7 +215,7 @@ protected function validate_input( array $input = array() ): bool {
214215
*
215216
* @since 0.1.0
216217
*
217-
* @param array $input Optional. The input data for permission checking.
218+
* @param array<string,mixed> $input Optional. The input data for permission checking.
218219
* @return bool Whether the ability has the necessary permission.
219220
*/
220221
public function has_permission( array $input = array() ): bool {
@@ -234,8 +235,8 @@ public function has_permission( array $input = array() ): bool {
234235
*
235236
* @since 0.1.0
236237
*
237-
* @param array $input The input data for the ability.
238-
* @return mixed|WP_Error The result of the ability execution, or WP_Error on failure.
238+
* @param array<string,mixed> $input The input data for the ability.
239+
* @return mixed|\WP_Error The result of the ability execution, or WP_Error on failure.
239240
*/
240241
protected function do_execute( array $input ) {
241242
if ( ! is_callable( $this->execute_callback ) ) {
@@ -292,8 +293,8 @@ protected function validate_output( $output ): bool {
292293
*
293294
* @since 0.1.0
294295
*
295-
* @param array $input Optional. The input data for the ability.
296-
* @return mixed|WP_Error The result of the ability execution, or WP_Error on failure.
296+
* @param array<string,mixed> $input Optional. The input data for the ability.
297+
* @return mixed|\WP_Error The result of the ability execution, or WP_Error on failure.
297298
*/
298299
public function execute( array $input = array() ) {
299300
if ( ! $this->has_permission( $input ) ) {
@@ -326,6 +327,6 @@ public function execute( array $input = array() ) {
326327
* @since 0.1.0
327328
*/
328329
public function __wakeup(): void {
329-
throw new \LogicException( __CLASS__ . ' should never be unserialized.' );
330+
throw new \LogicException( self::class . ' should never be unserialized.' );
330331
}
331332
}

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_run_controller = new WP_REST_Abilities_Run_Controller();
422+
$abilities_run_controller->register_routes();
423+
$abilities_list_controller = new WP_REST_Abilities_List_Controller();
424+
$abilities_list_controller->register_routes();
419425
}
420426

421427
/**

0 commit comments

Comments
 (0)