This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathclass-wp-ability.php
More file actions
332 lines (300 loc) · 7.31 KB
/
class-wp-ability.php
File metadata and controls
332 lines (300 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* Abilities API
*
* Defines WP_Ability class.
*
* @package WordPress
* @subpackage Abilities API
* @since 0.1.0
*/
declare( strict_types = 1 );
/**
* Encapsulates the properties and methods related to a specific ability in the registry.
*
* @since 0.1.0
* @access private
*
* @see WP_Abilities_Registry
*/
class WP_Ability {
/**
* The name of the ability, with its namespace.
* Example: `my-plugin/my-ability`.
*
* @since 0.1.0
* @var string
*/
protected $name;
/**
* The human-readable ability label.
*
* @since 0.1.0
* @var string
*/
protected $label;
/**
* The detailed ability description.
*
* @since 0.1.0
* @var string
*/
protected $description;
/**
* The optional ability input schema.
*
* @since 0.1.0
* @var array<string,mixed>
*/
protected $input_schema = array();
/**
* The optional ability output schema.
*
* @since 0.1.0
* @var array<string,mixed>
*/
protected $output_schema = array();
/**
* The ability execute callback.
*
* @since 0.1.0
* @var callable
*/
protected $execute_callback;
/**
* The optional ability permission callback.
*
* @since 0.1.0
* @var ?callable
*/
protected $permission_callback = null;
/**
* The optional ability metadata.
*
* @since 0.1.0
* @var array<string,mixed>
*/
protected $meta = array();
/**
* Constructor.
*
* Do not use this constructor directly. Instead, use the `wp_register_ability()` function.
*
* @see wp_register_ability()
*
* @since 0.1.0
*
* @param string $name The name of the ability, with its namespace.
* @param array<string,mixed> $properties An associative array of properties for the ability. This should
* include `label`, `description`, `input_schema`, `output_schema`,
* `execute_callback`, `permission_callback`, and `meta`.
*/
public function __construct( string $name, array $properties ) {
$this->name = $name;
foreach ( $properties as $property_name => $property_value ) {
$this->$property_name = $property_value;
}
}
/**
* Retrieves the name of the ability, with its namespace.
* Example: `my-plugin/my-ability`.
*
* @since 0.1.0
*
* @return string The ability name, with its namespace.
*/
public function get_name(): string {
return $this->name;
}
/**
* Retrieves the human-readable label for the ability.
*
* @since 0.1.0
*
* @return string The human-readable ability label.
*/
public function get_label(): string {
return $this->label;
}
/**
* Retrieves the detailed description for the ability.
*
* @since 0.1.0
*
* @return string The detailed description for the ability.
*/
public function get_description(): string {
return $this->description;
}
/**
* Retrieves the input schema for the ability.
*
* @since 0.1.0
*
* @return array<string,mixed> The input schema for the ability.
*/
public function get_input_schema(): array {
return $this->input_schema;
}
/**
* Retrieves the output schema for the ability.
*
* @since 0.1.0
*
* @return array<string,mixed> The output schema for the ability.
*/
public function get_output_schema(): array {
return $this->output_schema;
}
/**
* Retrieves the metadata for the ability.
*
* @since 0.1.0
*
* @return array<string,mixed> The metadata for the ability.
*/
public function get_meta(): array {
return $this->meta;
}
/**
* Validates input data against the input schema.
*
* @since 0.1.0
*
* @param array<string,mixed> $input Optional. The input data to validate.
* @return bool Returns true if valid, false if validation fails.
*/
protected function validate_input( array $input = array() ): bool {
$input_schema = $this->get_input_schema();
if ( empty( $input_schema ) ) {
return true;
}
$valid_input = rest_validate_value_from_schema( $input, $input_schema );
if ( is_wp_error( $valid_input ) ) {
_doing_it_wrong(
__METHOD__,
esc_html(
sprintf(
/* translators: %1$s ability name, %2$s error message. */
__( 'Invalid input provided for ability "%1$s": %2$s.' ),
$this->name,
$valid_input->get_error_message()
)
),
'0.1.0'
);
return false;
}
return true;
}
/**
* Checks whether the ability has the necessary permissions.
* If the permission callback is not set, the default behavior is to allow access
* when the input provided passes validation.
*
* @since 0.1.0
*
* @param array<string,mixed> $input Optional. The input data for permission checking.
* @return bool Whether the ability has the necessary permission.
*/
public function has_permission( array $input = array() ): bool {
if ( ! $this->validate_input( $input ) ) {
return false;
}
if ( ! is_callable( $this->permission_callback ) ) {
return true;
}
return call_user_func( $this->permission_callback, $input );
}
/**
* Executes the ability callback.
*
* @since 0.1.0
*
* @param array<string,mixed> $input The input data for the ability.
* @return mixed|\WP_Error The result of the ability execution, or WP_Error on failure.
*/
protected function do_execute( array $input ) {
if ( ! is_callable( $this->execute_callback ) ) {
_doing_it_wrong(
__METHOD__,
esc_html(
/* translators: %s ability name. */
sprintf( __( 'Ability "%s" does not have a valid execute callback.' ), $this->name )
),
'0.1.0'
);
return null;
}
return call_user_func( $this->execute_callback, $input );
}
/**
* Validates output data against the output schema.
*
* @since 0.1.0
*
* @param mixed $output The output data to validate.
* @return bool Returns true if valid, false if validation fails.
*/
protected function validate_output( $output ): bool {
$output_schema = $this->get_output_schema();
if ( empty( $output_schema ) ) {
return true;
}
$valid_output = rest_validate_value_from_schema( $output, $output_schema );
if ( is_wp_error( $valid_output ) ) {
_doing_it_wrong(
__METHOD__,
esc_html(
sprintf(
/* translators: %1$s ability name, %2$s error message. */
__( 'Invalid output provided for ability "%1$s": %2$s.' ),
$this->name,
$valid_output->get_error_message()
)
),
'0.1.0'
);
return false;
}
return true;
}
/**
* Executes the ability after input validation and running a permission check.
* Before returning the return value, it also validates the output.
*
* @since 0.1.0
*
* @param array<string,mixed> $input Optional. The input data for the ability.
* @return mixed|\WP_Error The result of the ability execution, or WP_Error on failure.
*/
public function execute( array $input = array() ) {
if ( ! $this->has_permission( $input ) ) {
_doing_it_wrong(
__METHOD__,
esc_html(
/* translators: %s ability name. */
sprintf( __( 'Ability "%s" does not have necessary permission.' ), $this->name )
),
'0.1.0'
);
return null;
}
$result = $this->do_execute( $input );
if ( is_wp_error( $result ) ) {
return $result;
}
if ( ! $this->validate_output( $result ) ) {
return null;
}
return $result;
}
/**
* Wakeup magic method.
*
* @since 0.1.0
*/
public function __wakeup(): void {
throw new \LogicException( self::class . ' should never be unserialized.' );
}
}