Skip to content

Commit b2f6518

Browse files
committed
Docs: Expand information about new annotations property
1 parent 287313e commit b2f6518

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

docs/3.registering-abilities.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ The `$args` array accepts the following keys:
2828
- The callback should return a boolean (`true` if the user has permission, `false` otherwise), or a `WP_Error` object on failure.
2929
- If the input does not validate against the input schema, the permission callback will not be called, and a `WP_Error` will be returned instead.
3030
- `meta` (`array`, **Optional**): An associative array for storing arbitrary additional metadata about the ability.
31+
- `annotations` (`array`, **Optional**): An associative array of annotations providing hints about the ability's behavior characteristics. Supports the following keys:
32+
- `instructions` (`string`, **Optional**): Custom instructions or guidance for using the ability (default: `''`).
33+
- `readonly` (`boolean`, **Optional**): Whether the ability only reads data without modifying its environment (default: `false`).
34+
- `destructive` (`boolean`, **Optional**): Whether the ability may perform destructive updates to its environment. If `true`, the ability may perform any type of modification, including deletions or other destructive changes. If `false`, the ability performs only additive updates (default: `true`).
35+
- `idempotent` (`boolean`, **Optional**): Whether calling the ability repeatedly with the same arguments will have no additional effect on its environment (default: `false`).
3136
- `show_in_rest` (`boolean`, **Optional**): Whether to expose this ability via the REST API. Default: `false`.
3237
- When `true`, the ability will be listed in REST API responses and can be executed via REST endpoints.
3338
- When `false`, the ability will be hidden from REST API listings and cannot be executed via REST endpoints, but remains available for internal PHP usage.
@@ -80,7 +85,13 @@ function my_plugin_register_site_info_ability() {
8085
'url' => home_url()
8186
);
8287
},
83-
'permission_callback' => '__return_true'
88+
'permission_callback' => '__return_true',
89+
'meta' => array(
90+
'annotations' => array(
91+
'readonly' => true,
92+
'destructive' => false
93+
),
94+
),
8495
));
8596
}
8697
```
@@ -134,7 +145,13 @@ function my_plugin_register_update_option_ability() {
134145
},
135146
'permission_callback' => function() {
136147
return current_user_can( 'manage_options' );
137-
}
148+
},
149+
'meta' => array(
150+
'annotations' => array(
151+
'destructive' => false,
152+
'idempotent' => true
153+
),
154+
),
138155
));
139156
}
140157
```

docs/5.rest-api.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ Abilities are represented in JSON with the following structure:
4242
}
4343
}
4444
},
45-
"meta": {}
45+
"meta": {
46+
"annotations": {
47+
"instructions": "",
48+
"readonly": true,
49+
"destructive": false,
50+
"idempotent": false
51+
}
52+
}
4653
}
4754
```
4855

@@ -85,7 +92,14 @@ curl https://example.com/wp-json/wp/v2/abilities
8592
}
8693
}
8794
},
88-
"meta": {}
95+
"meta": {
96+
"annotations": {
97+
"instructions": "",
98+
"readonly": false,
99+
"destructive": true,
100+
"idempotent": false
101+
}
102+
}
89103
}
90104
]
91105
```
@@ -128,31 +142,55 @@ curl https://example.com/wp-json/wp/v2/abilities/my-plugin/get-site-info
128142
}
129143
}
130144
},
131-
"meta": {}
145+
"meta": {
146+
"annotations": {
147+
"instructions": "",
148+
"readonly": true,
149+
"destructive": false,
150+
"idempotent": false
151+
}
152+
}
132153
}
133154
```
134155

135156
## Execute an Ability
136157

158+
Abilities are executed via the `/run` endpoint. The required HTTP method depends on the ability's `readonly` annotation:
159+
160+
- **Read-only abilities** (`readonly: true`) must use **GET**
161+
- **Regular abilities** (default) must use **POST**
162+
163+
This distinction ensures read-only operations use safe HTTP methods that can be cached and don't modify server state.
164+
137165
### Definition
138166

139-
`POST /wp/v2/abilities/(?P<namespace>[a-z0-9-]+)/(?P<ability>[a-z0-9-]+)/run`
167+
`GET|POST /wp/v2/abilities/(?P<namespace>[a-z0-9-]+)/(?P<ability>[a-z0-9-]+)/run`
140168

141169
### Arguments
142170

143171
- `namespace` _(string)_: The namespace part of the ability name.
144172
- `ability` _(string)_: The ability name part.
145173
- `input` _(integer|number|boolean|string|array|object|null)_: Optional input data for the ability as defined by its input schema.
174+
- For **GET requests**: pass as `input` query parameter (URL-encoded JSON)
175+
- For **POST requests**: pass in JSON body
146176

147-
### Example Request (No Input)
177+
### Example Request (Read-only, GET)
148178

149179
```bash
150-
curl -X POST https://example.com/wp-json/wp/v2/abilities/my-plugin/get-site-info/run
180+
# No input
181+
curl https://example.com/wp-json/wp/v2/abilities/my-plugin/get-site-info/run
182+
183+
# With input (URL-encoded)
184+
curl "https://example.com/wp-json/wp/v2/abilities/my-plugin/get-user-info/run?input=%7B%22user_id%22%3A1%7D"
151185
```
152186

153-
### Example Request (With Input)
187+
### Example Request (Regular, POST)
154188

155189
```bash
190+
# No input
191+
curl -X POST https://example.com/wp-json/wp/v2/abilities/my-plugin/create-draft/run
192+
193+
# With input
156194
curl -X POST \
157195
-H "Content-Type: application/json" \
158196
-d '{"input":{"option_name":"blogname","option_value":"New Site Name"}}' \
@@ -205,5 +243,5 @@ The API returns standard WordPress REST API error responses with these common co
205243
- `ability_invalid_output` - output validation failed according to the ability's schema.
206244
- `ability_invalid_execute_callback` - the ability's execute callback is not callable.
207245
- `rest_ability_not_found` - the requested ability is not registered.
208-
- `rest_ability_invalid_method` - the requested HTTP method is not allowed for executing the selected ability.
246+
- `rest_ability_invalid_method` - the requested HTTP method is not allowed for executing the selected ability (e.g., using GET on a read-only ability, or POST on a regular ability).
209247
- `rest_ability_cannot_execute` - the ability cannot be executed due to insufficient permissions.

tests/unit/abilities-api/wpAbility.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,6 @@ public function test_show_in_rest_can_be_set_to_false() {
199199
);
200200
}
201201

202-
/**
203-
* Tests that `show_in_rest` can be set to false.
204-
*/
205-
public function test_show_in_rest_can_be_set_to_false() {
206-
$args = array_merge(
207-
self::$test_ability_properties,
208-
array(
209-
'meta' => array(
210-
'show_in_rest' => false,
211-
),
212-
)
213-
);
214-
$ability = new WP_Ability( self::$test_ability_name, $args );
215-
216-
$this->assertFalse( $ability->get_meta_item( 'show_in_rest' ), '`show_in_rest` metadata should be false.' );
217-
}
218-
219202
/**
220203
* Tests that invalid `show_in_rest` value throws an exception.
221204
*/

0 commit comments

Comments
 (0)