From 8c1905b2f5011c8bc61d96a941d2de8a95b7fb9c Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 15 Nov 2023 16:43:55 -0500 Subject: [PATCH] Added a 'raw' format to the option get command that returns the value as stored in the database --- features/option.feature | 9 +++++++++ src/Option_Command.php | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/features/option.feature b/features/option.feature index ec7312808..6fd54c101 100644 --- a/features/option.feature +++ b/features/option.feature @@ -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: diff --git a/src/Option_Command.php b/src/Option_Command.php index 34310af0f..b57b339e8 100644 --- a/src/Option_Command.php +++ b/src/Option_Command.php @@ -47,6 +47,7 @@ class Option_Command extends WP_CLI_Command { * - var_export * - json * - yaml + * - raw * --- * * ## EXAMPLES @@ -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 ); }