|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use WP_CLI\Utils; |
| 4 | + |
3 | 5 | /** |
4 | 6 | * Sets, gets, and removes theme mods. |
5 | 7 | * |
@@ -80,57 +82,143 @@ class Theme_Mod_Command extends WP_CLI_Command { |
80 | 82 | */ |
81 | 83 | public function get( $args, $assoc_args ) { |
82 | 84 |
|
83 | | - if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) { |
| 85 | + if ( ! Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) { |
84 | 86 | WP_CLI::error( 'You must specify at least one mod or use --all.' ); |
85 | 87 | } |
86 | 88 |
|
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 = []; |
89 | 91 | } |
90 | 92 |
|
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 | + } |
96 | 109 | } |
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 ) { |
98 | 115 | // 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 ) ) { |
100 | 117 | continue; |
101 | 118 | } |
102 | 119 |
|
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 ); |
120 | 121 | } |
121 | 122 |
|
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; |
130 | 174 | } |
131 | 175 |
|
| 176 | + // Output the list using the WP CLI Formatter. |
132 | 177 | $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 | + } |
134 | 222 | } |
135 | 223 |
|
136 | 224 | /** |
@@ -206,11 +294,11 @@ public function list_( $args, $assoc_args ) { |
206 | 294 | */ |
207 | 295 | public function remove( $args, $assoc_args ) { |
208 | 296 |
|
209 | | - if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) { |
| 297 | + if ( ! Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) { |
210 | 298 | WP_CLI::error( 'You must specify at least one mod or use --all.' ); |
211 | 299 | } |
212 | 300 |
|
213 | | - if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) { |
| 301 | + if ( Utils\get_flag_value( $assoc_args, 'all' ) ) { |
214 | 302 | remove_theme_mods(); |
215 | 303 | WP_CLI::success( 'Theme mods removed.' ); |
216 | 304 | return; |
|
0 commit comments