-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathTaxonomy_Command.php
More file actions
284 lines (258 loc) · 7.36 KB
/
Taxonomy_Command.php
File metadata and controls
284 lines (258 loc) · 7.36 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
<?php
use WP_CLI\Formatter;
use WP_CLI\Utils;
/**
* Retrieves information about registered taxonomies.
*
* See references for [built-in taxonomies](https://developer.wordpress.org/themes/basics/categories-tags-custom-taxonomies/) and [custom taxonomies](https://developer.wordpress.org/plugins/taxonomies/working-with-custom-taxonomies/).
*
* ## EXAMPLES
*
* # List all taxonomies with 'post' object type.
* $ wp taxonomy list --object_type=post --fields=name,public
* +-------------+--------+
* | name | public |
* +-------------+--------+
* | category | 1 |
* | post_tag | 1 |
* | post_format | 1 |
* +-------------+--------+
*
* # Get capabilities of 'post_tag' taxonomy.
* $ wp taxonomy get post_tag --field=cap
* {"manage_terms":"manage_categories","edit_terms":"manage_categories","delete_terms":"manage_categories","assign_terms":"edit_posts"}
*
* @package wp-cli
*/
class Taxonomy_Command extends WP_CLI_Command {
private $fields = array(
'name',
'label',
'description',
'object_type',
'show_tagcloud',
'hierarchical',
'public',
);
/**
* Gets the term counts for each supplied taxonomy.
*
* @param array $taxonomies Taxonomies to fetch counts for.
* @return array Associative array of term counts keyed by taxonomy.
*/
protected function get_counts( $taxonomies ) {
global $wpdb;
if ( count( $taxonomies ) <= 0 ) {
return [];
}
$query = $wpdb->prepare(
"SELECT `taxonomy`, COUNT(*) AS `count`
FROM $wpdb->term_taxonomy
WHERE `taxonomy` IN (" . implode( ',', array_fill( 0, count( $taxonomies ), '%s' ) ) . ')
GROUP BY `taxonomy`',
$taxonomies
);
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $query is already prepared above.
$counts = $wpdb->get_results( $query );
// Make sure there's a count for every item.
$counts = array_merge(
array_fill_keys( $taxonomies, 0 ),
wp_list_pluck( $counts, 'count', 'taxonomy' )
);
return $counts;
}
/**
* Lists registered taxonomies.
*
* ## OPTIONS
*
* [--<field>=<value>]
* : Filter by one or more fields (see get_taxonomies() first parameter for a list of available fields).
*
* [--field=<field>]
* : Prints the value of a single field for each taxonomy.
*
* [--fields=<fields>]
* : Limit the output to specific taxonomy fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each term:
*
* * name
* * label
* * description
* * object_type
* * show_tagcloud
* * hierarchical
* * public
*
* These fields are optionally available:
*
* * count
*
* ## EXAMPLES
*
* # List all taxonomies.
* $ wp taxonomy list --format=csv
* name,label,description,object_type,show_tagcloud,hierarchical,public
* category,Categories,,post,1,1,1
* post_tag,Tags,,post,1,,1
* nav_menu,"Navigation Menus",,nav_menu_item,,,
* link_category,"Link Categories",,link,1,,
* post_format,Format,,post,,,1
*
* # List all taxonomies with 'post' object type.
* $ wp taxonomy list --object_type=post --fields=name,public
* +-------------+--------+
* | name | public |
* +-------------+--------+
* | category | 1 |
* | post_tag | 1 |
* | post_format | 1 |
* +-------------+--------+
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
$formatter = $this->get_formatter( $assoc_args );
// Check if it's strict mode or not.
$strict = Utils\get_flag_value( $assoc_args, 'strict', false );
unset( $assoc_args['strict'] );
if ( isset( $assoc_args['object_type'] ) ) {
$assoc_args['object_type'] = array( $assoc_args['object_type'] );
$taxonomy_object = $assoc_args['object_type'];
} else {
$taxonomy_object = get_post_types();
}
$fields = $formatter->fields;
$taxonomies = ( isset( $taxonomy_object ) && ! $strict )
? get_object_taxonomies( $taxonomy_object, 'objects' )
: get_taxonomies( $assoc_args, 'objects' );
$counts = [];
if ( count( $taxonomies ) > 0 && in_array( 'count', $fields, true ) ) {
$counts = $this->get_counts( wp_list_pluck( $taxonomies, 'name' ) );
}
$taxonomies = array_map(
function ( $taxonomy ) use ( $counts ) {
// @phpstan-ignore assign.propertyType
$taxonomy->object_type = implode( ', ', $taxonomy->object_type );
// @phpstan-ignore property.notFound
$taxonomy->count = isset( $counts[ $taxonomy->name ] ) ? $counts[ $taxonomy->name ] : 0;
return $taxonomy;
},
$taxonomies
);
$formatter->display_items( $taxonomies );
}
/**
* Gets details about a registered taxonomy.
*
* ## OPTIONS
*
* <taxonomy>
* : Taxonomy slug.
*
* [--field=<field>]
* : Instead of returning the whole taxonomy, 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
*
* These fields will be displayed by default for the specified taxonomy:
*
* * name
* * label
* * description
* * object_type
* * show_tagcloud
* * hierarchical
* * public
* * labels
* * cap
*
* These fields are optionally available:
*
* * count
*
* ## EXAMPLES
*
* # Get details of `category` taxonomy.
* $ wp taxonomy get category --fields=name,label,object_type
* +-------------+------------+
* | Field | Value |
* +-------------+------------+
* | name | category |
* | label | Categories |
* | object_type | ["post"] |
* +-------------+------------+
*
* # Get capabilities of 'post_tag' taxonomy.
* $ wp taxonomy get post_tag --field=cap
* {"manage_terms":"manage_categories","edit_terms":"manage_categories","delete_terms":"manage_categories","assign_terms":"edit_posts"}
*/
public function get( $args, $assoc_args ) {
$taxonomy = get_taxonomy( $args[0] );
if ( ! $taxonomy ) {
WP_CLI::error( "Taxonomy {$args[0]} doesn't exist." );
}
if ( empty( $assoc_args['fields'] ) ) {
$default_fields = array_merge(
$this->fields,
array(
'labels',
'cap',
)
);
$assoc_args['fields'] = $default_fields;
}
$formatter = $this->get_formatter( $assoc_args );
$fields = $formatter->fields;
$count = 0;
if ( in_array( 'count', $fields, true ) ) {
$count = $this->get_counts( [ $taxonomy->name ] );
$count = $count[ $taxonomy->name ];
}
$data = array(
'name' => $taxonomy->name,
'label' => $taxonomy->label,
'description' => $taxonomy->description,
'object_type' => $taxonomy->object_type,
'show_tagcloud' => $taxonomy->show_tagcloud,
'hierarchical' => $taxonomy->hierarchical,
'public' => $taxonomy->public,
'labels' => $taxonomy->labels,
'cap' => $taxonomy->cap,
'count' => $count,
);
$formatter->display_item( $data );
}
private function get_formatter( &$assoc_args ) {
return new Formatter( $assoc_args, $this->fields, 'taxonomy' );
}
}