Skip to content

Commit c659e1e

Browse files
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.
1 parent 47ff5a1 commit c659e1e

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

‎features/option-set-autoload.feature‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,21 @@ Feature: Set 'autoload' value for an option
9191
"""
9292
off
9393
"""
94+
95+
@require-wp-6.6
96+
Scenario: Updates the cached alloptions in the same request
97+
Given a WP install
98+
And I run `wp option add foo bar --autoload=on`
99+
And I run `wp option add baz qux --autoload=off`
100+
101+
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() ) );'`
102+
Then STDOUT should contain:
103+
"""
104+
bool(false)
105+
"""
106+
107+
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() ) );'`
108+
Then STDOUT should contain:
109+
"""
110+
bool(true)
111+
"""

‎src/Option_Command.php‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,17 @@ public function set_autoload( $args ) {
557557
wp_cache_set( 'notoptions', $notoptions, 'options' );
558558
}
559559

560+
// Refresh the 'alloptions' cache like wp_set_option_autoload_values().
560561
if ( ! defined( 'WP_INSTALLING' ) ) {
561-
$alloptions = wp_load_alloptions( true );
562-
if ( isset( $alloptions[ $option ] ) ) {
563-
$alloptions[ $option ] = $previous->option_value;
564-
wp_cache_set( 'alloptions', $alloptions, 'options' );
562+
if ( in_array( $autoload, array( 'on', 'yes' ), true ) ) {
563+
wp_cache_delete( $option, 'options' );
564+
wp_cache_delete( 'alloptions', 'options' );
565565
} else {
566-
wp_cache_set( $option, $previous->option_value, 'options' );
566+
$alloptions = wp_load_alloptions( true );
567+
if ( isset( $alloptions[ $option ] ) ) {
568+
unset( $alloptions[ $option ] );
569+
wp_cache_set( 'alloptions', $alloptions, 'options' );
570+
}
567571
}
568572
}
569573

0 commit comments

Comments
 (0)