-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbility_Category_Command.php
More file actions
297 lines (277 loc) · 8.05 KB
/
Ability_Category_Command.php
File metadata and controls
297 lines (277 loc) · 8.05 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
<?php
namespace WP_CLI\Ability;
use WP_Ability_Category;
use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI_Command;
/**
* Lists and inspects ability categories registered via the WordPress Abilities API.
*
* The Abilities API, introduced in WordPress 6.9, uses categories to organize
* related abilities for better discoverability.
*
* ## EXAMPLES
*
* # List all registered ability categories.
* $ wp ability category list
* +------+-------+-----------------------------------------------------+
* | slug | label | description |
* +------+-------+-----------------------------------------------------+
* | site | Site | Abilities that retrieve or modify site information. |
* | user | User | Abilities that retrieve or modify user information. |
* +------+-------+-----------------------------------------------------+
*
* # Get details of a specific category.
* $ wp ability category get site
* +-------------+-----------------------------------------------------+
* | Field | Value |
* +-------------+-----------------------------------------------------+
* | slug | site |
* | label | Site |
* | description | Abilities that retrieve or modify site information. |
* | meta | {} |
* +-------------+-----------------------------------------------------+
*
* # Check if a category exists.
* $ wp ability category exists site
* $ echo $?
* 0
*
* @when after_wp_load
* @package wp-cli
*/
class Ability_Category_Command extends WP_CLI_Command {
/**
* Default fields for list output.
*
* @var string[]
*/
protected $default_fields = [
'slug',
'label',
'description',
];
/**
* All fields for get output.
*
* @var string[]
*/
protected $get_fields = [
'slug',
'label',
'description',
'meta',
];
/**
* Lists all registered ability categories.
*
* ## OPTIONS
*
* [--field=<field>]
* : Prints the value of a single field for each category.
*
* [--fields=<fields>]
* : Limit the output to specific category fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* - count
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each category:
*
* * slug
* * label
* * description
*
* ## EXAMPLES
*
* # List all categories.
* $ wp ability category list
* +------+-------+-----------------------------------------------------+
* | slug | label | description |
* +------+-------+-----------------------------------------------------+
* | site | Site | Abilities that retrieve or modify site information. |
* | user | User | Abilities that retrieve or modify user information. |
* +------+-------+-----------------------------------------------------+
*
* # List categories as JSON.
* $ wp ability category list --format=json
* [{"slug":"site","label":"Site","description":"..."},{"slug":"user",...}]
*
* # List only category slugs.
* $ wp ability category list --field=slug
* site
* user
*
* @subcommand list
*
* @param string[] $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ): void {
$categories = wp_get_ability_categories();
$items = [];
foreach ( $categories as $category ) {
$items[] = $this->format_category( $category );
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( $items );
}
/**
* Gets details about a registered ability category.
*
* ## OPTIONS
*
* <slug>
* : The category slug.
*
* [--field=<field>]
* : Instead of returning the whole category, returns the value of a single field.
*
* [--fields=<fields>]
* : Limit the output to specific fields. Defaults to all fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* * slug
* * label
* * description
* * meta
*
* ## EXAMPLES
*
* # Get details of a specific category.
* $ wp ability category get site
* +-------------+-----------------------------------------------------+
* | Field | Value |
* +-------------+-----------------------------------------------------+
* | slug | site |
* | label | Site |
* | description | Abilities that retrieve or modify site information. |
* | meta | {} |
* +-------------+-----------------------------------------------------+
*
* # Get category as JSON.
* $ wp ability category get site --format=json
* {"slug":"site","label":"Site","description":"...","meta":"{}"}
*
* # Get only the label.
* $ wp ability category get site --field=label
* Site
*
* @param string[] $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ): void {
$slug = $args[0];
$category = wp_get_ability_category( $slug );
if ( null === $category ) {
WP_CLI::error( "Ability category '{$slug}' not found." );
}
$category_data = $this->format_category_for_get( $category );
$formatter = $this->get_formatter_for_get( $assoc_args );
$formatter->display_item( $category_data );
}
/**
* Checks whether an ability category is registered.
*
* Exits with return code 0 if the category exists, 1 if it does not.
*
* ## OPTIONS
*
* <slug>
* : The category slug.
*
* ## EXAMPLES
*
* # Check if a category exists.
* $ wp ability category exists site
* $ echo $?
* 0
*
* # Check for non-existent category.
* $ wp ability category exists nonexistent
* $ echo $?
* 1
*
* # Use in a script.
* $ if wp ability category exists site; then
* > echo "Category exists"
* > fi
*
* @param string[] $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function exists( $args, $assoc_args ): void {
$slug = $args[0];
$exists = wp_has_ability_category( $slug );
WP_CLI::halt( $exists ? 0 : 1 );
}
/**
* Formats a category for list output.
*
* @param WP_Ability_Category $category The category object.
* @return array<string,mixed>
*/
private function format_category( $category ) {
return [
'slug' => $category->get_slug(),
'label' => $category->get_label(),
'description' => $category->get_description(),
];
}
/**
* Formats a category for get output.
*
* @param WP_Ability_Category $category The category object.
* @return array<string,mixed>
*/
private function format_category_for_get( $category ) {
$meta = $category->get_meta();
return [
'slug' => $category->get_slug(),
'label' => $category->get_label(),
'description' => $category->get_description(),
'meta' => ! empty( $meta ) ? wp_json_encode( $meta ) : '{}',
];
}
/**
* Gets the formatter object for list output.
*
* @param array $assoc_args Associative arguments.
* @return Formatter
*/
private function get_formatter( &$assoc_args ) {
return new Formatter( $assoc_args, $this->default_fields );
}
/**
* Gets the formatter object for get output.
*
* @param array $assoc_args Associative arguments.
* @return Formatter
*/
private function get_formatter_for_get( &$assoc_args ) {
return new Formatter( $assoc_args, $this->get_fields );
}
}