You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dev-notes/abilities-api.md
+19-40Lines changed: 19 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ The Abilities API automatically exposes registered abilities through REST API en
48
48
-`GET /wp-abilities/v1/categories/{slug}` – Get a single ability category
49
49
-`GET /wp-abilities/v1/abilities` – List all abilities
50
50
-`GET /wp-abilities/v1/abilities/{name}` – Get a single ability
51
-
-`GET|POST /wp-abilities/v1/abilities/{name}/run` – Execute an ability
51
+
-`GET|POST|DELETE /wp-abilities/v1/abilities/{name}/run` – Execute an ability
52
52
53
53
#### 3\. Hooks
54
54
@@ -102,23 +102,13 @@ function my_plugin_register_abilities() {
102
102
'description' => __( 'Retrieves the total number of published posts.', 'my-plugin' ),
103
103
'category' => 'content-management',
104
104
'input_schema' => array(
105
-
'type' => 'object',
106
-
'properties' => array(
107
-
'post_type' => array(
108
-
'type' => 'string',
109
-
'description' => __( 'The post type to count.', 'my-plugin' ),
110
-
'default' => 'post',
111
-
),
112
-
),
105
+
'type' => 'string',
106
+
'description' => __( 'The post type to count.', 'my-plugin' ),
107
+
'default' => 'post',
113
108
),
114
109
'output_schema' => array(
115
-
'type' => 'object',
116
-
'properties' => array(
117
-
'count' => array(
118
-
'type' => 'integer',
119
-
'description' => __( 'The number of published posts.', 'my-plugin' ),
120
-
),
121
-
),
110
+
'type' => 'integer',
111
+
'description' => __( 'The number of published posts.', 'my-plugin' ),
122
112
),
123
113
'execute_callback' => 'my_plugin_get_post_count',
124
114
'permission_callback' => function() {
@@ -132,19 +122,17 @@ function my_plugin_register_abilities() {
132
122
* Execute callback for get-post-count ability.
133
123
*/
134
124
function my_plugin_get_post_count( $input ) {
135
-
$post_type = $input['post_type'] ?? 'post';
125
+
$post_type = $input ?? 'post';
136
126
137
127
$count = wp_count_posts( $post_type );
138
128
139
-
return array(
140
-
'count' => (int) $count->publish,
141
-
);
129
+
return (int) $count->publish;
142
130
}
143
131
```
144
132
145
133
#### More Complex Example
146
134
147
-
Here's an example with more advanced input validation and error handling:
135
+
Here's an example with more advanced input and output schemas, input validation, and error handling:
148
136
149
137
```php
150
138
<?php
@@ -254,7 +242,7 @@ Abilities must be assigned to a category. Categories provide better discoverabil
254
242
255
243
### JSON Schema Validation
256
244
257
-
The Abilities API uses JSON Schema for input and output validation. WordPress implements a validator based on a subset of JSON Schema Version 4\. The schemas serve two purposes:
245
+
The Abilities API uses [JSON Schema](https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/) for input and output validation. WordPress implements a validator based on a subset of JSON Schema Version 4\. The schemas serve two purposes:
258
246
259
247
1. Automatic validation of data passed to and returned from abilities
260
248
2. Self-documenting API contracts for developers
@@ -263,7 +251,7 @@ Defining schemas is mandatory when there is a value to pass or return.
263
251
264
252
### Using REST API Endpoints
265
253
266
-
Developers can also enable Abilities to support the default REST API endpoints. This is possible by setting the `meta.show_in_rest` argument to `true` when registering an ability.
254
+
Developers can also enable Abilities to support the default [REST API](https://developer.wordpress.org/rest-api/) endpoints. This is possible by setting the `meta.show_in_rest` argument to `true` when registering an ability.
267
255
268
256
```php
269
257
wp_register_ability(
@@ -273,23 +261,13 @@ Developers can also enable Abilities to support the default REST API endpoints.
273
261
'description' => __( 'Retrieves the total number of published posts.', 'my-plugin' ),
274
262
'category' => 'content-management',
275
263
'input_schema' => array(
276
-
'type' => 'object',
277
-
'properties' => array(
278
-
'post_type' => array(
279
-
'type' => 'string',
280
-
'description' => __( 'The post type to count.', 'my-plugin' ),
281
-
'default' => 'post',
282
-
),
283
-
),
264
+
'type' => 'string',
265
+
'description' => __( 'The post type to count.', 'my-plugin' ),
266
+
'default' => 'post',
284
267
),
285
268
'output_schema' => array(
286
-
'type' => 'object',
287
-
'properties' => array(
288
-
'count' => array(
289
-
'type' => 'integer',
290
-
'description' => __( 'The number of published posts.', 'my-plugin' ),
291
-
),
292
-
),
269
+
'type' => 'integer',
270
+
'description' => __( 'The number of published posts.', 'my-plugin' ),
0 commit comments