-
Notifications
You must be signed in to change notification settings - Fork 85
Fix output formats for the theme mod get command
#456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bc6035d
84891e7
d80db8f
e354614
561cfc8
4b0aa30
26b9aff
5864145
76025d7
efb8947
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||
| <?php | ||||||
|
|
||||||
| use \WP_CLI\Utils; | ||||||
|
|
||||||
| /** | ||||||
| * Sets, gets, and removes theme mods. | ||||||
| * | ||||||
|
|
@@ -80,57 +82,117 @@ | |||||
| */ | ||||||
| public function get( $args, $assoc_args ) { | ||||||
|
|
||||||
| if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $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 ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) { | ||||||
| if ( Utils\get_flag_value( $assoc_args, 'all' ) ) { | ||||||
| $args = array(); | ||||||
| } | ||||||
|
|
||||||
| $list = array(); | ||||||
| $mods = get_theme_mods(); | ||||||
| if ( ! is_array( $mods ) ) { | ||||||
| // If no mods are set (perhaps new theme), make sure foreach still works. | ||||||
| $mods = array(); | ||||||
| } | ||||||
| foreach ( $mods as $k => $v ) { | ||||||
| // If mods were given, skip the others. | ||||||
| if ( ! empty( $args ) && ! in_array( $k, $args, true ) ) { | ||||||
| continue; | ||||||
| } | ||||||
| // This array will hold the list of theme mods in a format suitable for the WP CLI Formatter. | ||||||
| $mod_list = array(); | ||||||
|
|
||||||
| if ( is_array( $v ) ) { | ||||||
| $list[] = [ | ||||||
| 'key' => $k, | ||||||
| 'value' => '=>', | ||||||
| ]; | ||||||
| foreach ( $v as $_k => $_v ) { | ||||||
| $list[] = [ | ||||||
| 'key' => " $_k", | ||||||
| 'value' => $_v, | ||||||
| ]; | ||||||
| } | ||||||
| } else { | ||||||
| $list[] = [ | ||||||
| 'key' => $k, | ||||||
| 'value' => $v, | ||||||
| ]; | ||||||
| } | ||||||
| } | ||||||
| // 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(); | ||||||
|
|
||||||
| // For unset mods, show blank value. | ||||||
| foreach ( $args as $mod ) { | ||||||
| if ( ! isset( $mods[ $mod ] ) ) { | ||||||
| $list[] = [ | ||||||
| 'key' => $mod, | ||||||
| 'value' => '', | ||||||
| ]; | ||||||
| // 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 ) { | ||||||
|
Comment on lines
+96
to
+103
|
||||||
| $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 ! empty( $item['value'] ); | ||||||
|
||||||
| return ! empty( $item['value'] ); | |
| return $item['value'] !== '' && $item['value'] !== null; |
Copilot
AI
Feb 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new flattening and dot-notation logic for CSV/YAML/JSON formats introduces non-trivial behavior but currently has no dedicated acceptance tests (unlike features/theme-mod-list.feature, which covers those formats for the list subcommand). Given that issue #96 was caused by malformed non-tabular output, it would be valuable to add Behat coverage for wp theme mod get --all --format=csv|json|yaml with nested theme-mod structures to ensure this formatter behavior is exercised and guarded against regressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import here uses a leading backslash, whereas other commands in this repository consistently use
use WP_CLI\Utils;without it (for example,src/Theme_Command.php:5andsrc/Plugin_Command.php:5). For consistency with established codebase conventions, it would be better to drop the leading backslash from thisusestatement.