Skip to content

Commit bc6035d

Browse files
committed
96: Fix output formats for the theme mod get command
1 parent c125c84 commit bc6035d

File tree

1 file changed

+90
-32
lines changed

1 file changed

+90
-32
lines changed

src/Theme_Mod_Command.php

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,51 +85,109 @@ public function get( $args = array(), $assoc_args = array() ) {
8585
$args = array();
8686
}
8787

88+
// This array will hold the list of theme mods in a format suitable for the WP CLI Formatter.
8889
$list = array();
89-
$mods = get_theme_mods();
90+
91+
// If specific mods are requested, filter out any that aren't requested.
92+
$mods = ! empty( $args ) ? array_intersect_key( get_theme_mods(), array_flip( $args ) ) : get_theme_mods();
9093
if ( ! is_array( $mods ) ) {
91-
// If no mods are set (perhaps new theme), make sure foreach still works.
94+
// If no mods are set (perhaps new theme), make sure array_intersect_key still works.
9295
$mods = array();
9396
}
94-
foreach ( $mods as $k => $v ) {
95-
// If mods were given, skip the others.
96-
if ( ! empty( $args ) && ! in_array( $k, $args, true ) ) {
97-
continue;
98-
}
9997

100-
if ( is_array( $v ) ) {
101-
$list[] = [
102-
'key' => $k,
103-
'value' => '=>',
104-
];
105-
foreach ( $v as $_k => $_v ) {
106-
$list[] = [
107-
'key' => " $_k",
108-
'value' => $_v,
109-
];
110-
}
111-
} else {
112-
$list[] = [
113-
'key' => $k,
114-
'value' => $v,
115-
];
98+
// Generate the list of items ready for output. We use an initial separator that we can replace later depending on format.
99+
$separator = '!!!';
100+
array_walk(
101+
$mods,
102+
function( $value, $key ) use ( &$list, $separator ) {
103+
$this->mod_to_string( $key, $value, $list, $separator );
116104
}
117-
}
105+
);
118106

119-
// For unset mods, show blank value.
120-
foreach ( $args as $mod ) {
121-
if ( ! isset( $mods[ $mod ] ) ) {
122-
$list[] = [
123-
'key' => $mod,
124-
'value' => '',
125-
];
126-
}
107+
// Take our Formatter-friendly list and adjust it according to the requested format.
108+
switch ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ) ) {
109+
// For tables we use a double space to indent child items.
110+
case 'table':
111+
$list = array_map(
112+
function( $item ) {
113+
$parts = explode( '!!!', $item['key'] );
114+
$new_key = array_pop( $parts );
115+
if ( ! empty( $parts ) ) {
116+
$new_key = str_repeat( ' ', count( $parts ) ) . $new_key;
117+
}
118+
return [
119+
'key' => $new_key,
120+
'value' => $item['value'],
121+
];
122+
},
123+
$list
124+
);
125+
break;
126+
127+
// For JSON, CSV, and YAML formats we use dot notation to show the hierarchy.
128+
case 'csv':
129+
case 'yaml':
130+
case 'json':
131+
$list = array_filter( array_map(
132+
function( $item ) {
133+
return [
134+
'key' => str_replace( '!!!', '.', $item['key'] ),
135+
'value' => $item['value'],
136+
];
137+
},
138+
$list
139+
), function( $item ) {
140+
return ! empty( $item['value'] );
141+
} );
142+
break;
127143
}
128144

145+
// Output the list using the WP CLI Formatter.
129146
$formatter = new \WP_CLI\Formatter( $assoc_args, $this->fields, 'thememods' );
130147
$formatter->display_items( $list );
131148
}
132149

150+
/**
151+
* Convert the theme mods to a flattened array with a string representation of the keys.
152+
*
153+
* @param string $key The mod key
154+
* @param mixed $value The value of the mod.
155+
* @param array $list The list so far, passed by reference.
156+
* @param string $separator A string to separate keys to denote their place in the tree.
157+
*/
158+
private function mod_to_string( $key, $value, &$list, $separator ) {
159+
if ( is_array( $value ) ){
160+
if ( empty( $value ) ) {
161+
// Explicitly handle empty arrays to ensure they are displayed.
162+
$list[] = array(
163+
'key' => $key,
164+
'value' => '[empty array]',
165+
);
166+
return;
167+
}
168+
169+
// Arrays get their own entry in the list to allow for sensible table output.
170+
$list[] = array(
171+
'key' => $key,
172+
'value' => '',
173+
);
174+
175+
foreach ( $value as $child_key => $child_value ) {
176+
$this->mod_to_string( $key . $separator . $child_key, $child_value, $list, $separator );
177+
}
178+
} else {
179+
// Explicitly handle false values to ensure they are displayed.
180+
if ( false === $value ) {
181+
$value = '[false]';
182+
}
183+
184+
$list[] = array(
185+
'key' => $key,
186+
'value' => $value,
187+
);
188+
}
189+
}
190+
133191
/**
134192
* Gets a list of theme mods.
135193
*

0 commit comments

Comments
 (0)