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..cebee1cc7 --- /dev/null +++ b/features/option-set-autoload.feature @@ -0,0 +1,58 @@ +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 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: + """ + no + """ diff --git a/src/Option_Command.php b/src/Option_Command.php index b2e6542a5..241906f15 100644 --- a/src/Option_Command.php +++ b/src/Option_Command.php @@ -436,6 +436,100 @@ 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. + * + * + * : 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 ( ! defined( '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. *