From c659e1e80c0c1cef1dfa22314ebbbb7f931bba16 Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Fri, 6 Nov 2026 10:48:39 +0530 Subject: [PATCH] Keep the alloptions cache in sync in `wp option set-autoload` The command refreshed the caches like a value change in update_option(), so the option stayed in, or out of, the cached alloptions after its autoload value changed. Refresh the cache like wp_set_option_autoload_values() instead. Fixes #687. --- features/option-set-autoload.feature | 18 ++++++++++++++++++ src/Option_Command.php | 14 +++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/features/option-set-autoload.feature b/features/option-set-autoload.feature index 8e893d4da..6a70c816b 100644 --- a/features/option-set-autoload.feature +++ b/features/option-set-autoload.feature @@ -91,3 +91,21 @@ Feature: Set 'autoload' value for an option """ off """ + + @require-wp-6.6 + Scenario: Updates the cached alloptions in the same request + Given a WP install + And I run `wp option add foo bar --autoload=on` + And I run `wp option add baz qux --autoload=off` + + When I run `wp eval 'wp_load_alloptions(); WP_CLI::runcommand( "option set-autoload foo off", array( "launch" => false ) ); var_dump( array_key_exists( "foo", wp_load_alloptions() ) );'` + Then STDOUT should contain: + """ + bool(false) + """ + + When I run `wp eval 'wp_load_alloptions(); get_option( "baz" ); WP_CLI::runcommand( "option set-autoload baz on", array( "launch" => false ) ); var_dump( array_key_exists( "baz", wp_load_alloptions() ) );'` + Then STDOUT should contain: + """ + bool(true) + """ diff --git a/src/Option_Command.php b/src/Option_Command.php index e4cd076f2..51be29687 100644 --- a/src/Option_Command.php +++ b/src/Option_Command.php @@ -557,13 +557,17 @@ public function set_autoload( $args ) { wp_cache_set( 'notoptions', $notoptions, 'options' ); } + // Refresh the 'alloptions' cache like wp_set_option_autoload_values(). if ( ! defined( 'WP_INSTALLING' ) ) { - $alloptions = wp_load_alloptions( true ); - if ( isset( $alloptions[ $option ] ) ) { - $alloptions[ $option ] = $previous->option_value; - wp_cache_set( 'alloptions', $alloptions, 'options' ); + if ( in_array( $autoload, array( 'on', 'yes' ), true ) ) { + wp_cache_delete( $option, 'options' ); + wp_cache_delete( 'alloptions', 'options' ); } else { - wp_cache_set( $option, $previous->option_value, 'options' ); + $alloptions = wp_load_alloptions( true ); + if ( isset( $alloptions[ $option ] ) ) { + unset( $alloptions[ $option ] ); + wp_cache_set( 'alloptions', $alloptions, 'options' ); + } } }