From 8bdd517adc8976b707a940fb872653aa0cba2b24 Mon Sep 17 00:00:00 2001 From: Isla Waters Date: Thu, 25 Apr 2024 16:35:26 -0400 Subject: [PATCH] Support new autoload options Fixes #491 See: https://core.trac.wordpress.org/changeset/57920 --- features/option-get-autoload.feature | 17 +++++++++++++- features/option-set-autoload.feature | 35 ++++++++++++++++++++++++++++ features/option.feature | 32 +++++++++++++++++++++---- src/Option_Command.php | 17 ++++++++++---- 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/features/option-get-autoload.feature b/features/option-get-autoload.feature index 82708e5e2..30a3c0d1b 100644 --- a/features/option-get-autoload.feature +++ b/features/option-get-autoload.feature @@ -8,7 +8,7 @@ Feature: Get 'autoload' value for an option """ Error: Could not get 'foo' option. Does it exist? """ - + @less-than-wp-6.6 Scenario: Displays 'autoload' value Given a WP install @@ -23,3 +23,18 @@ Feature: Get 'autoload' value for an option """ yes """ + @require-wp-6.6 + 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: + """ + on + """ diff --git a/features/option-set-autoload.feature b/features/option-set-autoload.feature index cebee1cc7..8e893d4da 100644 --- a/features/option-set-autoload.feature +++ b/features/option-set-autoload.feature @@ -24,6 +24,7 @@ Feature: Set 'autoload' value for an option Error: Invalid value specified for positional arg. """ + @less-than-wp-6.6 Scenario: Successfully updates autoload value Given a WP install @@ -56,3 +57,37 @@ Feature: Set 'autoload' value for an option """ no """ + + @require-wp-6.6 + 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: + """ + on + """ + + When I run `wp option set-autoload foo off` + 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: + """ + off + """ diff --git a/features/option.feature b/features/option.feature index ec7312808..795147610 100644 --- a/features/option.feature +++ b/features/option.feature @@ -140,6 +140,7 @@ Feature: Manage WordPress options """ @require-wp-4.2 + @less-than-wp-6.6 Scenario: Update autoload value for custom option Given a WP install And I run `wp option add hello world --autoload=no` @@ -160,7 +161,30 @@ Feature: Manage WordPress options | option_name | option_value | autoload | | hello | island | yes | + @require-wp-6.6 + Scenario: Update autoload value for custom option + Given a WP install + And I run `wp option add hello world --autoload=off` + + When I run `wp option update hello universe` + Then STDOUT should not be empty + + When I run `wp option list --search='hello' --fields=option_name,option_value,autoload` + Then STDOUT should be a table containing rows: + | option_name | option_value | autoload | + | hello | universe | off | + + When I run `wp option update hello island --autoload=on` + Then STDOUT should not be empty + + When I run `wp option list --search='hello' --fields=option_name,option_value,autoload` + Then STDOUT should be a table containing rows: + | option_name | option_value | autoload | + | hello | island | on | + + @require-wp-4.2 + @less-than-wp-6.6 Scenario: Managed autoloaded options Given a WP install @@ -248,11 +272,11 @@ Feature: Manage WordPress options """ And the return code should be 0 - When I try `wp option list --search='auto_opt' --autoload=no` + When I try `wp option list --search='auto_opt' --autoload=nope` Then STDOUT should be empty - And STDERR should be: + And STDERR should contain: """ - Error: Value of '--autoload' should be on or off. + Error: Value of '--autoload' should be """ And the return code should be 1 @@ -264,7 +288,7 @@ Feature: Manage WordPress options """ And the return code should be 0 - When I try `wp option add str_opt_foo 'bar' --autoload=off` + When I try `wp option add str_opt_foo 'bar' --autoload=bad` Then STDOUT should be empty And STDERR should contain: """ diff --git a/src/Option_Command.php b/src/Option_Command.php index 9a947f211..072abca43 100644 --- a/src/Option_Command.php +++ b/src/Option_Command.php @@ -109,6 +109,8 @@ public function get( $args, $assoc_args ) { * : Should this option be automatically loaded. * --- * options: + * - 'on' + * - 'off' * - 'yes' * - 'no' * --- @@ -125,7 +127,7 @@ public function add( $args, $assoc_args ) { $value = WP_CLI::get_value_from_arg_or_stdin( $args, 1 ); $value = WP_CLI::read_value( $value, $assoc_args ); - if ( Utils\get_flag_value( $assoc_args, 'autoload' ) === 'no' ) { + if ( in_array( Utils\get_flag_value( $assoc_args, 'autoload' ), [ 'no', 'off' ], true ) ) { $autoload = 'no'; } else { $autoload = 'yes'; @@ -271,12 +273,13 @@ public function list_( $args, $assoc_args ) { } if ( isset( $assoc_args['autoload'] ) ) { - if ( 'on' === $assoc_args['autoload'] ) { + $autoload = $assoc_args['autoload']; + if ( 'on' === $autoload || 'yes' === $autoload ) { $autoload_query = " AND autoload='yes'"; - } elseif ( 'off' === $assoc_args['autoload'] ) { + } elseif ( 'off' === $autoload || 'no' === $autoload ) { $autoload_query = " AND autoload='no'"; } else { - WP_CLI::error( "Value of '--autoload' should be on or off." ); + WP_CLI::error( "Value of '--autoload' should be 'on', 'off', 'yes', or 'no'." ); } } @@ -360,6 +363,8 @@ function ( $a, $b ) use ( $orderby, $order ) { * : Requires WP 4.2. Should this option be automatically loaded. * --- * options: + * - 'on' + * - 'off' * - 'yes' * - 'no' * --- @@ -413,7 +418,7 @@ public function update( $args, $assoc_args ) { $value = WP_CLI::read_value( $value, $assoc_args ); $autoload = Utils\get_flag_value( $assoc_args, 'autoload' ); - if ( ! in_array( $autoload, [ 'yes', 'no' ], true ) ) { + if ( ! in_array( $autoload, [ 'on', 'off', 'yes', 'no' ], true ) ) { $autoload = null; } @@ -479,6 +484,8 @@ public function get_autoload( $args ) { * : Should this option be automatically loaded. * --- * options: + * - 'on' + * - 'off' * - 'yes' * - 'no' * ---