-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathTaxonomy_Command.php
More file actions
199 lines (183 loc) · 5.11 KB
/
Taxonomy_Command.php
File metadata and controls
199 lines (183 loc) · 5.11 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
<?php
/**
* Manage 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',
);
public function __construct() {
if ( \WP_CLI\Utils\wp_version_compare( 3.7, '<' ) ) {
// remove description for wp <= 3.7
$this->fields = array_values( array_diff( $this->fields, array( 'description' ) ) );
}
parent::__construct();
}
/**
* List 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
* * public
* * hierarchical
*
* There are no optionally available fields.
*
* ## 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 );
if ( isset( $assoc_args['object_type'] ) ) {
$assoc_args['object_type'] = array( $assoc_args['object_type'] );
}
$taxonomies = get_taxonomies( $assoc_args, 'objects' );
$taxonomies = array_map( function( $taxonomy ) {
$taxonomy->object_type = implode( ', ', $taxonomy->object_type );
return $taxonomy;
}, $taxonomies );
$formatter->display_items( $taxonomies );
}
/**
* Get 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
* ---
*
* ## 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;
}
$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,
);
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_item( $data );
}
private function get_formatter( &$assoc_args ) {
return new \WP_CLI\Formatter( $assoc_args, $this->fields, 'taxonomy' );
}
}