-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathTheme_Mod_Command.php
More file actions
321 lines (293 loc) · 8.45 KB
/
Theme_Mod_Command.php
File metadata and controls
321 lines (293 loc) · 8.45 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
<?php
use WP_CLI\Utils;
/**
* Sets, gets, and removes theme mods.
*
* ## EXAMPLES
*
* # Set the 'background_color' theme mod to '000000'.
* $ wp theme mod set background_color 000000
* Success: Theme mod background_color set to 000000.
*
* # Get single theme mod in JSON format.
* $ wp theme mod get background_color --format=json
* [{"key":"background_color","value":"dd3333"}]
*
* # Remove all theme mods.
* $ wp theme mod remove --all
* Success: Theme mods removed.
*/
class Theme_Mod_Command extends WP_CLI_Command {
private $fields = [ 'key', 'value' ];
/**
* Gets one or more theme mods.
*
* ## OPTIONS
*
* [<mod>...]
* : One or more mods to get.
*
* [--field=<field>]
* : Returns the value of a single field.
*
* [--all]
* : List all theme mods
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - json
* - csv
* - yaml
* ---
*
* ## EXAMPLES
*
* # Get all theme mods.
* $ wp theme mod get --all
* +------------------+---------+
* | key | value |
* +------------------+---------+
* | background_color | dd3333 |
* | link_color | #dd9933 |
* | main_text_color | #8224e3 |
* +------------------+---------+
*
* # Get single theme mod in JSON format.
* $ wp theme mod get background_color --format=json
* [{"key":"background_color","value":"dd3333"}]
*
* # Get value of a single theme mod.
* $ wp theme mod get background_color --field=value
* dd3333
*
* # Get multiple theme mods.
* $ wp theme mod get background_color header_textcolor
* +------------------+--------+
* | key | value |
* +------------------+--------+
* | background_color | dd3333 |
* | header_textcolor | |
* +------------------+--------+
*
* @param string[] $args Positional arguments.
* @param array{field?: string, all?: bool, format: string} $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
if ( ! Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) {
WP_CLI::error( 'You must specify at least one mod or use --all.' );
}
if ( Utils\get_flag_value( $assoc_args, 'all' ) ) {
$args = array();
}
// This array will hold the list of theme mods in a format suitable for the WP CLI Formatter.
$mod_list = array();
// If specific mods are requested, filter out any that aren't requested.
$mods = ! empty( $args ) ? array_intersect_key( get_theme_mods(), array_flip( $args ) ) : get_theme_mods();
// Generate the list of items ready for output. We use an initial separator that we can replace later depending on format.
$separator = '\t';
array_walk(
$mods,
function ( $value, $key ) use ( &$mod_list, $separator ) {
$this->mod_to_string( $key, $value, $mod_list, $separator );
}
);
// Take our Formatter-friendly list and adjust it according to the requested format.
switch ( Utils\get_flag_value( $assoc_args, 'format' ) ) {
// For tables we use a double space to indent child items.
case 'table':
$mod_list = array_map(
static function ( $item ) use ( $separator ) {
$parts = explode( $separator, $item['key'] );
$new_key = array_pop( $parts );
if ( ! empty( $parts ) ) {
$new_key = str_repeat( ' ', count( $parts ) ) . $new_key;
}
return [
'key' => $new_key,
'value' => $item['value'],
];
},
$mod_list
);
break;
// For JSON, CSV, and YAML formats we use dot notation to show the hierarchy.
case 'csv':
case 'yaml':
case 'json':
$mod_list = array_filter(
array_map(
static function ( $item ) use ( $separator ) {
return [
'key' => str_replace( $separator, '.', $item['key'] ),
'value' => $item['value'],
];
},
$mod_list
),
function ( $item ) {
return '' !== $item['value'] && null !== $item['value'];
}
);
break;
}
// Output the list using the WP CLI Formatter.
$formatter = new \WP_CLI\Formatter( $assoc_args, $this->fields, 'thememods' );
$formatter->display_items( $mod_list );
}
/**
* Convert the theme mods to a flattened array with a string representation of the keys.
*
* @param string $key The mod key
* @param mixed $value The value of the mod.
* @param array $mod_list The list so far, passed by reference.
* @param string $separator A string to separate keys to denote their place in the tree.
*/
private function mod_to_string( $key, $value, &$mod_list, $separator ) {
if ( is_array( $value ) || is_object( $value ) ) {
// Convert objects to arrays for easier handling.
$value = (array) $value;
// Explicitly handle empty arrays to ensure they are displayed.
if ( empty( $value ) ) {
$mod_list[] = array(
'key' => $key,
'value' => '[empty array]',
);
return;
}
// Arrays get their own entry in the list to allow for sensible table output.
$mod_list[] = array(
'key' => $key,
'value' => '',
);
foreach ( $value as $child_key => $child_value ) {
$this->mod_to_string( $key . $separator . $child_key, $child_value, $mod_list, $separator );
}
} else {
// Explicitly handle boolean values to ensure they are displayed correctly.
if ( is_bool( $value ) ) {
$value = $value ? '[true]' : '[false]';
}
$mod_list[] = array(
'key' => $key,
'value' => $value,
);
}
}
/**
* Gets a list of theme mods.
*
* ## OPTIONS
*
* [--field=<field>]
* : Returns the value of a single field.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - json
* - csv
* - yaml
* ---
*
* ## EXAMPLES
*
* # Gets a list of theme mods.
* $ wp theme mod list
* +------------------+---------+
* | key | value |
* +------------------+---------+
* | background_color | dd3333 |
* | link_color | #dd9933 |
* | main_text_color | #8224e3 |
* +------------------+---------+
*
* @subcommand list
*
* @param string[] $args Positional arguments. Unused.
* @param array{field?: string, format: string} $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
$assoc_args['all'] = true;
$this->get( $args, $assoc_args );
}
/**
* Removes one or more theme mods.
*
* ## OPTIONS
*
* [<mod>...]
* : One or more mods to remove.
*
* [--all]
* : Remove all theme mods.
*
* ## EXAMPLES
*
* # Remove all theme mods.
* $ wp theme mod remove --all
* Success: Theme mods removed.
*
* # Remove single theme mod.
* $ wp theme mod remove background_color
* Success: 1 mod removed.
*
* # Remove multiple theme mods.
* $ wp theme mod remove background_color header_textcolor
* Success: 2 mods removed.
*
* @param string[] $args Positional arguments.
* @param array{all?: bool} $assoc_args Associative arguments.
*/
public function remove( $args, $assoc_args ) {
if ( ! Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) {
WP_CLI::error( 'You must specify at least one mod or use --all.' );
}
if ( Utils\get_flag_value( $assoc_args, 'all' ) ) {
remove_theme_mods();
WP_CLI::success( 'Theme mods removed.' );
return;
}
foreach ( $args as $mod ) {
remove_theme_mod( $mod );
}
$count = count( $args );
$success_message = ( 1 === $count ) ? '%d mod removed.' : '%d mods removed.';
WP_CLI::success( sprintf( $success_message, $count ) );
}
/**
* Sets the value of a theme mod.
*
* ## OPTIONS
*
* <mod>
* : The name of the theme mod to set or update.
*
* <value>
* : The new value.
*
* ## EXAMPLES
*
* # Set theme mod
* $ wp theme mod set background_color 000000
* Success: Theme mod background_color set to 000000.
*
* @param array{0: string, 1: string} $args Positional arguments.
* @param array $assoc_args Associative arguments. Unused.
*/
public function set( $args, $assoc_args ) {
list( $mod, $value ) = $args;
set_theme_mod( $mod, $value );
if ( get_theme_mod( $mod ) === $value ) {
WP_CLI::success( "Theme mod {$mod} set to {$value}." );
} else {
WP_CLI::success( "Could not update theme mod {$mod}." );
}
}
}