Skip to content

Commit

Permalink
Added a 'raw' format to the option get command that returns the value…
Browse files Browse the repository at this point in the history
… as stored in the database
  • Loading branch information
shawnhooper committed Nov 15, 2023
1 parent fde2586 commit 8c1905b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions features/option.feature
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ Feature: Manage WordPress options
[1,2]
"""

# Raw values
When I run `wp option set raw_option '[ 1, 2 ]' --format=json`
Then STDOUT should not be empty

When I run `wp option get raw_option --format=raw`
Then STDOUT should be:
"""
a:2:{i:0;i:1;i:1;i:2;}
"""

# Reading from files
Given a value.json file:
Expand Down
11 changes: 11 additions & 0 deletions src/Option_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Option_Command extends WP_CLI_Command {
* - var_export
* - json
* - yaml
* - raw
* ---
*
* ## EXAMPLES
Expand Down Expand Up @@ -80,6 +81,16 @@ public function get( $args, $assoc_args ) {
WP_CLI::error( "Could not get '{$key}' option. Does it exist?" );
}

if ( 'raw' === Utils\get_flag_value( $assoc_args, 'format' ) ) {
global $wpdb;
$value = $wpdb->get_var(
$wpdb->prepare(
"SELECT option_value FROM $wpdb->options WHERE option_name = %s",
$key
)
);
}

WP_CLI::print_value( $value, $assoc_args );
}

Expand Down

0 comments on commit 8c1905b

Please sign in to comment.