Skip to content

Keep the alloptions cache in sync in wp option set-autoload - #688

Open
rahultank-spaceo wants to merge 2 commits into
wp-cli:mainfrom
rahultank-spaceo:fix/687-set-autoload-cache
Open

rahultank-spaceo wants to merge 2 commits into
wp-cli:mainfrom
rahultank-spaceo:fix/687-set-autoload-cache

Conversation

@rahultank-spaceo

@rahultank-spaceo rahultank-spaceo commented Nov 6, 2026 •

Copy link
Copy Markdown

Fixes #687.

wp option set-autoload refreshed the caches the way update_option() does for a value change. So after the autoload value changed, the option stayed in, or out of, the cached alloptions. With a persistent object cache, an option set to off kept loading on every request, and a later delete_option() left its old value behind.

The command now refreshes the cache the same way core's wp_set_option_autoload_values() does:

  • to on or yes: it deletes the option's own cache and alloptions, so alloptions is rebuilt on the next load;
  • to off or no: it removes the option from the cached alloptions.

I kept the command's own query instead of calling wp_set_option_autoload(). That way there is no version check or fallback for WordPress before 6.4 (see the comment on the issue), and yes/no are still stored as given, as the existing scenarios expect. Core would store on/off.

Testing

The new scenario reproduces the bug without a persistent object cache. It runs the command in the same process with WP_CLI::runcommand( ..., array( 'launch' => false ) ), so the in-memory cache keeps alloptions between the two calls. With SQLite and WordPress 7.1.4, features/option-set-autoload.feature passes on this branch (4 scenarios). With the old src/Option_Command.php, the new scenario fails: the first var_dump() prints bool(true). PHPCS and the linter pass on the change.

I also ran the old and new set_autoload() code on WordPress 7.2-beta4, in one request, with the option cached first:

Change Before After
on → off still in cached alloptions removed
off → on missing from cached alloptions added
yes → no still in cached alloptions removed
no → yes missing from cached alloptions added
delete_option() after off get_option() returns the old value returns the default

AI assistance: Yes. An AI assistant helped write the change and this description. I reviewed the code and the test results.

Summary by CodeRabbit

  • Bug Fixes
    • Changing an option’s autoload setting now refreshes the relevant cached option data during the same request, helping prevent stale cache contents after the change.

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 wp-cli#687.
@rahultank-spaceo
rahultank-spaceo requested a review from a team as a code owner November 6, 2026 05:39
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Hello! 👋

Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project.

Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

Here are some useful Composer commands to get you started:

  • composer install: Install dependencies.
  • composer test: Run the full test suite.
  • composer phpcs: Check for code style violations.
  • composer phpcbf: Automatically fix code style violations.
  • composer phpunit: Run unit tests.
  • composer behat: Run behavior-driven tests.

To run a single Behat test, you can use the following command:

# Run all tests in a single file
composer behat features/some-feature.feature

# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123

You can find a list of all available Behat steps in our handbook.

@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

option set-autoload now updates option caches according to the new autoload value. A WordPress 6.6+ scenario checks that switching foo off removes it from alloptions, and switching baz on adds it.

Changes: Option autoload cache

Layer / File(s) Summary
Refresh and verify option caches
src/Option_Command.php, features/option-set-autoload.feature
When WordPress is not installing, autoload values on and yes cause the command to delete the option-specific cache entry and alloptions. Other values cause it to load alloptions and remove the option if present. The WordPress 6.6+ scenario checks both transitions.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • wp-cli/entity-command#689: This PR calls the same set_autoload() logic when wp option update receives --autoload and the value is unchanged.

Suggested reviewers: danielbachhuber, mrsdizzie, swissspidy

Merge Risk: Minimal · up to 61695

The command now keeps the cached alloptions in sync when an option's autoload status changes. No actionable merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: the alloptions cache is kept in sync when wp option set-autoload runs. The raw summary shows Option_Command::set_autoload() now updates the alloptions cache (deleting…
Linked Issues check Passed [#687] The only source change in src/Option_Command.php is in set_autoload(), after the DB update. When WP_INSTALLING is not defined, the code now branches on the new autoload value. For on or…
Out of Scope Changes check Passed [#687] The source change is limited to the cache refresh block in set_autoload(). The added scenario in features/option-set-autoload.feature tests that same behavior. Neither change touches the `o…
Full details: Title check

Explanation

The title states the main change: the alloptions cache is kept in sync when wp option set-autoload runs. The raw summary shows Option_Command::set_autoload() now updates the alloptions cache (deleting or removing the option entry depending on the new autoload value), and a feature scenario checks this within one request. The title is specific and matches the linked issue's goal of keeping alloptions consistent with autoload status.

Full details: Linked Issues check

Explanation

[#687] The only source change in src/Option_Command.php is in set_autoload(), after the DB update. When WP_INSTALLING is not defined, the code now branches on the new autoload value. For on or yes, it calls wp_cache_delete() on the option's own cache key and on alloptions, so alloptions is rebuilt on the next load. For other values (off, no), it loads alloptions with wp_load_alloptions( true ) and calls unset() on the option key if present, then writes alloptions back. This matches the issue's expected outcome: an off option leaves alloptions, and an on option is rebuilt into alloptions on the next load. The old restore-previous-value logic is removed, which addresses the stale value after delete_option(). The new scenario in features/option-set-autoload.feature covers both transitions in one process. The PR does not follow the issue's suggested delegation to wp_set_option_autoload(). The issue lists that as a option, not a requirement, and the PR states its reason. Unverified: the branch assumes $autoload holds the normalized on/off/yes/no value at this point. The diff hunk does not show the validation code.

Full details: Out of Scope Changes check

Explanation

[#687] The source change is limited to the cache refresh block in set_autoload(). The added scenario in features/option-set-autoload.feature tests that same behavior. Neither change touches the option list --autoload handling named in #670, which the issue marks as separate. No unrelated changes appear in the reviewed whole-PR diff for src/Option_Command.php. The reviewed diff for the feature file was taken from the supplied summary and was not re-read in full.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cd410459-3c0b-44ad-8e81-4abdc8debae5
Commits

Reviewing files that changed from the base of the PR and between c659e1e and 61695c9.

Files selected for processing: 1

  • src/Option_Command.php

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@github-actions github-actions Bot added bug command:option-set-autoload Related to 'option set-autoload' command labels Nov 6, 2026
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:option-set-autoload Related to 'option set-autoload' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

With a persistent object cache, "wp option set-autoload <name> off" updates the DB row but writes the option back into the cached "alloptions".

2 participants