From 6f2a6111d79a5d818a7ced903e8c31f45666bdac Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 1 Nov 2022 15:44:46 -0700 Subject: [PATCH 1/5] Add `wp option get-autoload` and `wp option set-autoload` --- features/option-get-autoload.feature | 25 ++++++++ features/option-set-autoload.feature | 52 ++++++++++++++++ src/Option_Command.php | 92 ++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 features/option-get-autoload.feature create mode 100644 features/option-set-autoload.feature diff --git a/features/option-get-autoload.feature b/features/option-get-autoload.feature new file mode 100644 index 000000000..82708e5e2 --- /dev/null +++ b/features/option-get-autoload.feature @@ -0,0 +1,25 @@ +Feature: Get 'autoload' value for an option + + Scenario: Option doesn't exist + Given a WP install + + When I try `wp option get-autoload foo` + Then STDERR should be: + """ + Error: Could not get 'foo' option. Does it exist? + """ + + Scenario: Displays 'autoload' value + Given a WP install + + When I run `wp option add foo bar` + Then STDOUT should contain: + """ + Success: + """ + + When I run `wp option get-autoload foo` + Then STDOUT should be: + """ + yes + """ diff --git a/features/option-set-autoload.feature b/features/option-set-autoload.feature new file mode 100644 index 000000000..8f1cde173 --- /dev/null +++ b/features/option-set-autoload.feature @@ -0,0 +1,52 @@ +Feature: Set 'autoload' value for an option + + Scenario: Option doesn't exist + Given a WP install + + When I try `wp option set-autoload foo yes` + Then STDERR should be: + """ + Error: Could not get 'foo' option. Does it exist? + """ + + Scenario: Invalid 'autoload' value provided + Given a WP install + + When I run `wp option add foo bar` + Then STDOUT should contain: + """ + Success: + """ + + When I try `wp option set-autoload foo invalid` + Then STDERR should be: + """ + Error: Invalid value specified for positional arg. + """ + + Scenario: Successfully updates autoload value + Given a WP install + + When I run `wp option add foo bar` + Then STDOUT should contain: + """ + Success: + """ + + When I run `wp option get-autoload foo` + Then STDOUT should be: + """ + yes + """ + + When I run `wp option set-autoload foo no` + Then STDOUT should be: + """ + Success: Updated autoload value for 'foo' option. + """ + + When I run `wp option get-autoload foo` + Then STDOUT should be: + """ + no + """ diff --git a/src/Option_Command.php b/src/Option_Command.php index b2e6542a5..e8fc5ef70 100644 --- a/src/Option_Command.php +++ b/src/Option_Command.php @@ -436,6 +436,98 @@ public function update( $args, $assoc_args ) { } } + /** + * Gets the 'autoload' value for an option. + * + * ## OPTIONS + * + * + * : The name of the option to get 'autoload' of. + * + * @subcommand get-autoload + */ + public function get_autoload( $args ) { + global $wpdb; + + list( $option ) = $args; + + $existing = $wpdb->get_row( + $wpdb->prepare( + "SELECT autoload FROM $wpdb->options WHERE option_name=%s", + $option + ) + ); + if ( ! $existing ) { + WP_CLI::error( "Could not get '{$option}' option. Does it exist?" );; + } + WP_CLI::log( $existing->autoload ); + } + + /** + * Sets the 'autoload' value for an option. + * + * ## OPTIONS + * + * + * : The name of the option to set 'autoload' for. + * + * + * : Requires WP 4.2. Should this option be automatically loaded. + * --- + * options: + * - 'yes' + * - 'no' + * --- + * + * @subcommand set-autoload + */ + public function set_autoload( $args ) { + global $wpdb; + + list( $option, $autoload ) = $args; + + $previous = $wpdb->get_row( + $wpdb->prepare( + "SELECT autoload, option_value FROM $wpdb->options WHERE option_name=%s", + $option + ) + ); + if ( ! $previous ) { + WP_CLI::error( "Could not get '{$option}' option. Does it exist?" );; + } + + if ( $previous->autoload === $autoload ) { + WP_CLI::success( "Autoload value passed for '{$option}' option is unchanged." ); + return; + } + + $wpdb->update( + $wpdb->options, + array( 'autoload' => $autoload ), + array( 'option_name' => $option ) + ); + + // Recreate cache refreshing from update_option(). + $notoptions = wp_cache_get( 'notoptions', 'options' ); + + if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { + unset( $notoptions[ $option ] ); + wp_cache_set( 'notoptions', $notoptions, 'options' ); + } + + if ( ! wp_installing() ) { + $alloptions = wp_load_alloptions( true ); + if ( isset( $alloptions[ $option ] ) ) { + $alloptions[ $option ] = $previous->option_value; + wp_cache_set( 'alloptions', $alloptions, 'options' ); + } else { + wp_cache_set( $option, $previous->option_value, 'options' ); + } + } + + WP_CLI::success( "Updated autoload value for '{$option}' option." ); + } + /** * Deletes an option. * From 60ef1d7f68dad59c04ce0b53bb964e7c48d8c589 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 1 Nov 2022 15:52:21 -0700 Subject: [PATCH 2/5] Add a step for running the same command again --- features/option-set-autoload.feature | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/features/option-set-autoload.feature b/features/option-set-autoload.feature index 8f1cde173..cebee1cc7 100644 --- a/features/option-set-autoload.feature +++ b/features/option-set-autoload.feature @@ -45,6 +45,12 @@ Feature: Set 'autoload' value for an option Success: Updated autoload value for 'foo' option. """ + When I run the previous command again + Then STDOUT should be: + """ + Success: Autoload value passed for 'foo' option is unchanged. + """ + When I run `wp option get-autoload foo` Then STDOUT should be: """ From 89a5e6b451da6e44be4fa1027c33d2fb81a2bd91 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 1 Nov 2022 15:55:08 -0700 Subject: [PATCH 3/5] Clean up PHPCS issues --- src/Option_Command.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Option_Command.php b/src/Option_Command.php index e8fc5ef70..2ec3b6627 100644 --- a/src/Option_Command.php +++ b/src/Option_Command.php @@ -458,7 +458,8 @@ public function get_autoload( $args ) { ) ); if ( ! $existing ) { - WP_CLI::error( "Could not get '{$option}' option. Does it exist?" );; + WP_CLI::error( "Could not get '{$option}' option. Does it exist?" ); + } WP_CLI::log( $existing->autoload ); } @@ -493,7 +494,8 @@ public function set_autoload( $args ) { ) ); if ( ! $previous ) { - WP_CLI::error( "Could not get '{$option}' option. Does it exist?" );; + WP_CLI::error( "Could not get '{$option}' option. Does it exist?" ); + } if ( $previous->autoload === $autoload ) { From d25be6eb1e377573367f0514115d3867983d579e Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 4 Nov 2022 04:31:16 -0700 Subject: [PATCH 4/5] Use the constant instead for WP 3.7 compat --- src/Option_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Option_Command.php b/src/Option_Command.php index 2ec3b6627..fbdc8d678 100644 --- a/src/Option_Command.php +++ b/src/Option_Command.php @@ -517,7 +517,7 @@ public function set_autoload( $args ) { wp_cache_set( 'notoptions', $notoptions, 'options' ); } - if ( ! wp_installing() ) { + if ( ! defined( 'WP_INSTALLING' ) ) { $alloptions = wp_load_alloptions( true ); if ( isset( $alloptions[ $option ] ) ) { $alloptions[ $option ] = $previous->option_value; From 929f0206be2c49d45719976d65bf824e951259b5 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 10 Nov 2022 13:52:23 -0700 Subject: [PATCH 5/5] Remove unnecessary confusion from comment --- src/Option_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Option_Command.php b/src/Option_Command.php index fbdc8d678..241906f15 100644 --- a/src/Option_Command.php +++ b/src/Option_Command.php @@ -473,7 +473,7 @@ public function get_autoload( $args ) { * : The name of the option to set 'autoload' for. * * - * : Requires WP 4.2. Should this option be automatically loaded. + * : Should this option be automatically loaded. * --- * options: * - 'yes'