Skip to content

Commit 35b0e2f

Browse files
authored
Include auto autoload values in option list --autoload filter (#671)
Since WordPress 6.6, options added without an explicit autoload value are stored as 'auto', 'auto-on' or 'auto-off'. WordPress autoloads 'auto' and 'auto-on' options, but the --autoload filter of wp option list only matched 'on'/'yes' and 'off'/'no', so these options were missing from both lists. Use wp_autoload_values_to_autoload() to decide which values count as autoloaded, like wp_load_alloptions() does.
1 parent f9dd1b9 commit 35b0e2f

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎features/option-list.feature‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,30 @@ Feature: List WordPress options
235235
"""
236236
sample_value_four
237237
"""
238+
239+
@require-wp-6.6
240+
Scenario: Using the `--autoload` flag with the values WordPress picks automatically
241+
Given a WP install
242+
And I run `wp option update sample_auto_option 'sample_value_auto'`
243+
And I run `wp eval 'add_filter( "wp_default_autoload_value", "__return_true" ); add_option( "sample_auto_on_option", "sample_value_auto_on" );'`
244+
And I run `wp eval 'add_filter( "wp_default_autoload_value", "__return_false" ); add_option( "sample_auto_off_option", "sample_value_auto_off" );'`
245+
246+
When I run `wp option list --search="sample_auto*" --fields=option_name,autoload --orderby=option_name`
247+
Then STDOUT should be a table containing rows:
248+
| option_name | autoload |
249+
| sample_auto_off_option | auto-off |
250+
| sample_auto_on_option | auto-on |
251+
| sample_auto_option | auto |
252+
253+
When I run `wp option list --autoload=on --search="sample_auto*" --field=option_name --orderby=option_name`
254+
Then STDOUT should be:
255+
"""
256+
sample_auto_on_option
257+
sample_auto_option
258+
"""
259+
260+
When I run `wp option list --autoload=off --search="sample_auto*" --field=option_name`
261+
Then STDOUT should be:
262+
"""
263+
sample_auto_off_option
264+
"""

‎src/Option_Command.php‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function add( $args, $assoc_args ) {
168168
* : Pattern to exclude. Use wildcards ( * and ? ) to match option name.
169169
*
170170
* [--autoload=<value>]
171-
* : Match only autoload options when value is on, and only not-autoload option when off.
171+
* : Match only autoload options when value is on, and only not-autoload option when off. On WordPress 6.6 and later, options with the `auto` and `auto-on` values count as autoload options.
172172
*
173173
* [--transients]
174174
* : List only transients. Use `--no-transients` to ignore all transients.
@@ -293,10 +293,22 @@ public function list_( $args, $assoc_args ) {
293293

294294
if ( isset( $assoc_args['autoload'] ) ) {
295295
$autoload = $assoc_args['autoload'];
296+
297+
// Since WordPress 6.6, options set to 'auto' or 'auto-on' are autoloaded too.
298+
$autoload_values = function_exists( 'wp_autoload_values_to_autoload' )
299+
? wp_autoload_values_to_autoload()
300+
: [ 'on', 'yes' ];
301+
296302
if ( 'on' === $autoload || 'yes' === $autoload ) {
297-
$autoload_query = " AND (autoload='on' OR autoload='yes')";
303+
$autoload_query = $wpdb->prepare(
304+
' AND autoload IN (' . implode( ', ', array_fill( 0, count( $autoload_values ), '%s' ) ) . ')',
305+
$autoload_values
306+
);
298307
} elseif ( 'off' === $autoload || 'no' === $autoload ) {
299-
$autoload_query = " AND (autoload='off' OR autoload='no')";
308+
$autoload_query = $wpdb->prepare(
309+
' AND autoload NOT IN (' . implode( ', ', array_fill( 0, count( $autoload_values ), '%s' ) ) . ')',
310+
$autoload_values
311+
);
300312
} else {
301313
WP_CLI::error( "Value of '--autoload' should be 'on', 'off', 'yes', or 'no'." );
302314
}

0 commit comments

Comments
 (0)