Skip to content

Commit 437e645

Browse files
authored
Merge pull request #456 from philipjohn/fix/96-theme-mod-get-format
Fix output formats for the `theme mod get` command
2 parents 8c3bfd7 + c00e855 commit 437e645

2 files changed

Lines changed: 242 additions & 38 deletions

File tree

‎features/theme-mod.feature‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ Feature: Manage WordPress theme mods
77
Then STDOUT should be a table containing rows:
88
| key | value |
99

10+
When I run `wp theme mod get --all --format=csv`
11+
Then STDOUT should be CSV containing:
12+
| key | value |
13+
14+
When I run `wp theme mod get --all --format=json`
15+
Then STDOUT should be:
16+
"""
17+
[]
18+
"""
19+
20+
When I run `wp theme mod get --all --format=yaml`
21+
Then STDOUT should be:
22+
"""
23+
---
24+
"""
25+
1026
When I try `wp theme mod get`
1127
Then STDERR should contain:
1228
"""
@@ -21,19 +37,119 @@ Feature: Manage WordPress theme mods
2137
| key | value |
2238
| background_color | 123456 |
2339

40+
When I run `wp theme mod get --all --format=csv`
41+
Then STDOUT should be CSV containing:
42+
| key | value |
43+
| background_color | 123456 |
44+
45+
46+
When I run `wp theme mod get --all --format=json`
47+
Then STDOUT should be JSON containing:
48+
"""
49+
[{"key":"background_color","value":"123456"}]
50+
"""
51+
52+
When I run `wp theme mod get --all --format=yaml`
53+
Then STDOUT should be YAML containing:
54+
"""
55+
---
56+
--
57+
key: background_color
58+
value: "123456"
59+
"""
60+
2461
When I run `wp theme mod get background_color --field=value`
2562
Then STDOUT should be:
2663
"""
2764
123456
2865
"""
2966

67+
When I run `wp theme mod get background_color --field=value --format=csv`
68+
Then STDOUT should be:
69+
"""
70+
123456
71+
"""
72+
73+
When I run `wp theme mod get background_color --field=value --format=json`
74+
Then STDOUT should be:
75+
"""
76+
["123456"]
77+
"""
78+
79+
When I run `wp theme mod get background_color --field=value --format=yaml`
80+
Then STDOUT should be YAML containing:
81+
"""
82+
---
83+
- "123456"
84+
"""
85+
3086
When I run `wp theme mod set background_color 123456`
3187
And I run `wp theme mod get background_color header_textcolor`
3288
Then STDOUT should be a table containing rows:
3389
| key | value |
3490
| background_color | 123456 |
3591
| header_textcolor | |
3692

93+
When I run `wp theme mod get background_color header_textcolor --format=csv`
94+
Then STDOUT should be CSV containing:
95+
| key | value |
96+
| background_color | 123456 |
97+
| header_textcolor | |
98+
99+
When I run `wp theme mod get background_color header_textcolor --format=json`
100+
Then STDOUT should be JSON containing:
101+
"""
102+
[{"key":"background_color","value":"123456"},{"key":"header_textcolor","value":null}]
103+
"""
104+
105+
When I run `wp theme mod get background_color header_textcolor --format=yaml`
106+
Then STDOUT should be YAML containing:
107+
"""
108+
---
109+
--
110+
key: background_color
111+
value: "123456"
112+
--
113+
key: header_textcolor
114+
value: null
115+
"""
116+
117+
Scenario: Getting theme mods keeps native value types for machine-readable formats
118+
Given a WP install
119+
And a set-mods.php file:
120+
"""
121+
<?php
122+
set_theme_mod( 'flag_off', false );
123+
set_theme_mod( 'flag_on', true );
124+
set_theme_mod( 'empty_thing', [] );
125+
set_theme_mod( 'nested', [ 'child' => 'x' ] );
126+
"""
127+
128+
When I run `wp eval-file set-mods.php`
129+
130+
# The table format is for humans, so types are shown as readable placeholders.
131+
And I run `wp theme mod get --all`
132+
Then STDOUT should be a table containing rows:
133+
| key | value |
134+
| flag_off | [false] |
135+
| flag_on | [true] |
136+
| empty_thing | [empty array] |
137+
138+
# Machine-readable formats keep the native value types.
139+
When I run `wp theme mod get --all --format=json`
140+
Then STDOUT should be JSON containing:
141+
"""
142+
[{"key":"flag_off","value":false},{"key":"flag_on","value":true},{"key":"empty_thing","value":[]},{"key":"nested","value":""},{"key":"nested.child","value":"x"}]
143+
"""
144+
145+
When I run `wp theme mod get --all --format=csv`
146+
Then STDOUT should be CSV containing:
147+
| key | value |
148+
| flag_off | false |
149+
| flag_on | true |
150+
| empty_thing | [] |
151+
| nested.child | x |
152+
37153
Scenario: Setting theme mods
38154
Given a WP install
39155

‎src/Theme_Mod_Command.php‎

Lines changed: 126 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use WP_CLI\Utils;
4+
35
/**
46
* Sets, gets, and removes theme mods.
57
*
@@ -80,57 +82,143 @@ class Theme_Mod_Command extends WP_CLI_Command {
8082
*/
8183
public function get( $args, $assoc_args ) {
8284

83-
if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) {
85+
if ( ! Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) {
8486
WP_CLI::error( 'You must specify at least one mod or use --all.' );
8587
}
8688

87-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) {
88-
$args = array();
89+
if ( Utils\get_flag_value( $assoc_args, 'all' ) ) {
90+
$args = [];
8991
}
9092

91-
$list = array();
92-
$mods = get_theme_mods();
93-
if ( ! is_array( $mods ) ) {
94-
// If no mods are set (perhaps new theme), make sure foreach still works.
95-
$mods = array();
93+
// This array will hold the list of theme mods in a format suitable for the WP CLI Formatter.
94+
$mod_list = [];
95+
96+
// If specific mods are requested, fetch only those, setting missing mods to null. Otherwise, fetch all mods.
97+
$mods = [];
98+
if ( ! empty( $args ) ) {
99+
foreach ( $args as $mod ) {
100+
$mods[ $mod ] = get_theme_mod( $mod, null );
101+
}
102+
} else {
103+
$mods = get_theme_mods();
104+
105+
// A theme with no mods set returns false rather than an empty array.
106+
if ( ! is_array( $mods ) ) {
107+
$mods = [];
108+
}
96109
}
97-
foreach ( $mods as $k => $v ) {
110+
111+
// Generate the list of items ready for output. We use an actual tab as an
112+
// initial separator that we can replace later depending on format.
113+
$separator = "\t";
114+
foreach ( $mods as $key => $value ) {
98115
// If mods were given, skip the others.
99-
if ( ! empty( $args ) && ! in_array( $k, $args, true ) ) {
116+
if ( ! empty( $args ) && ! in_array( $key, $args, true ) ) {
100117
continue;
101118
}
102119

103-
if ( is_array( $v ) ) {
104-
$list[] = [
105-
'key' => $k,
106-
'value' => '=>',
107-
];
108-
foreach ( $v as $_k => $_v ) {
109-
$list[] = [
110-
'key' => " $_k",
111-
'value' => $_v,
112-
];
113-
}
114-
} else {
115-
$list[] = [
116-
'key' => $k,
117-
'value' => $v,
118-
];
119-
}
120+
$this->mod_to_string( $key, $value, $mod_list, $separator );
120121
}
121122

122-
// For unset mods, show blank value.
123-
foreach ( $args as $mod ) {
124-
if ( ! isset( $mods[ $mod ] ) ) {
125-
$list[] = [
126-
'key' => $mod,
127-
'value' => '',
128-
];
129-
}
123+
// Take our Formatter-friendly list and adjust it according to the requested format.
124+
$format = Utils\get_flag_value( $assoc_args, 'format', 'table' );
125+
switch ( $format ) {
126+
// For tables we use a double space to indent child items, and render
127+
// types like booleans and empty arrays as readable placeholders.
128+
case 'table':
129+
$mod_list = array_map(
130+
static function ( $item ) use ( $separator ) {
131+
$parts = explode( $separator, $item['key'] );
132+
$new_key = array_pop( $parts );
133+
if ( ! empty( $parts ) ) {
134+
$new_key = str_repeat( ' ', count( $parts ) ) . $new_key;
135+
}
136+
137+
$value = $item['value'];
138+
if ( is_bool( $value ) ) {
139+
$value = $value ? '[true]' : '[false]';
140+
} elseif ( is_array( $value ) ) {
141+
$value = '[empty array]';
142+
}
143+
144+
return [
145+
'key' => $new_key,
146+
'value' => $value,
147+
];
148+
},
149+
$mod_list
150+
);
151+
break;
152+
153+
// For JSON, CSV, and YAML formats we use dot notation to show the
154+
// hierarchy and keep the native value types. CSV is a flat format
155+
// that cannot represent an array, so those are JSON-encoded.
156+
case 'csv':
157+
case 'yaml':
158+
case 'json':
159+
$mod_list = array_map(
160+
static function ( $item ) use ( $separator, $format ) {
161+
$value = $item['value'];
162+
if ( 'csv' === $format && is_array( $value ) ) {
163+
$value = (string) wp_json_encode( $value );
164+
}
165+
166+
return [
167+
'key' => str_replace( $separator, '.', $item['key'] ),
168+
'value' => $value,
169+
];
170+
},
171+
$mod_list
172+
);
173+
break;
130174
}
131175

176+
// Output the list using the WP CLI Formatter.
132177
$formatter = new \WP_CLI\Formatter( $assoc_args, $this->fields, 'thememods' );
133-
$formatter->display_items( $list );
178+
$formatter->display_items( $mod_list );
179+
}
180+
181+
/**
182+
* Convert the theme mods to a flattened array with a string representation of the keys.
183+
*
184+
* @param string $key The mod key
185+
* @param mixed $value The value of the mod.
186+
* @param array $mod_list The list so far, passed by reference.
187+
* @param string $separator A string to separate keys to denote their place in the tree.
188+
*/
189+
private function mod_to_string( $key, $value, &$mod_list, $separator ) {
190+
if ( is_array( $value ) || is_object( $value ) ) {
191+
// Convert objects to arrays for easier handling.
192+
$value = (array) $value;
193+
194+
// Explicitly handle empty arrays to ensure they are displayed. The
195+
// value is kept native here and only rendered as a placeholder for
196+
// the table format.
197+
if ( empty( $value ) ) {
198+
$mod_list[] = [
199+
'key' => $key,
200+
'value' => [],
201+
];
202+
return;
203+
}
204+
205+
// Arrays get their own entry in the list to allow for sensible table output.
206+
$mod_list[] = [
207+
'key' => $key,
208+
'value' => '',
209+
];
210+
211+
foreach ( $value as $child_key => $child_value ) {
212+
$this->mod_to_string( $key . $separator . $child_key, $child_value, $mod_list, $separator );
213+
}
214+
} else {
215+
// Scalars (including booleans) are kept as their native type here.
216+
// The table format renders booleans as readable placeholders later.
217+
$mod_list[] = [
218+
'key' => $key,
219+
'value' => $value,
220+
];
221+
}
134222
}
135223

136224
/**
@@ -206,11 +294,11 @@ public function list_( $args, $assoc_args ) {
206294
*/
207295
public function remove( $args, $assoc_args ) {
208296

209-
if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) {
297+
if ( ! Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) {
210298
WP_CLI::error( 'You must specify at least one mod or use --all.' );
211299
}
212300

213-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) {
301+
if ( Utils\get_flag_value( $assoc_args, 'all' ) ) {
214302
remove_theme_mods();
215303
WP_CLI::success( 'Theme mods removed.' );
216304
return;

0 commit comments

Comments
 (0)