Skip to content

Feature Flags: key the wpcom override cache by blog - #52558

Merged
louwie17 merged 5 commits into
trunkfrom
fix/wpcom-feature-flags-by-blog-cache
Sep 22, 2026
Merged

louwie17 merged 5 commits into
trunkfrom
fix/wpcom-feature-flags-by-blog-cache

Conversation

@louwie17

@louwie17 louwie17 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Alternative to #52555, for the same bug. Wpcom_Feature_Flags memoizes a site's flag overrides in a static property the first time any flag is resolved, and never re-reads them. On WordPress.com Simple, the public API process resolves flags before it switches to the requested site (premium-analytics-dashboard-composition is checked on init), so the empty map cached then answers for the requested site too. Overrides set on Tools → Feature Flags therefore apply in that site's wp-admin but not to its wpcom/v2 REST responses.

#52555 resets the cache on switch_blog. This PR keys it by blog instead:

  • The cache is one slot plus the blog it was read for, checked against get_current_blog_id(). A resolution on a different blog re-reads that blog's option; a repeat on the same blog answers from the cache. Memory stays bounded — a CLI process that walks many blogs holds one map, not one per blog.
  • No switch_blog hook, so the fix does not depend on that action firing, or on firing before another callback that resolves a flag. Re-reads match a reset-on-switch_blog whenever flags resolve inside the switched scope; the only saving is a switch_to_blog()/restore_current_blog() pair that resolves no flag, and after the first read that saving is an object-cache lookup, not a query.
  • get_current_blog_id() is always 1 on single-site, so Atomic and self-hosted sites behave as before. get_overrides(), save_overrides() and reset_overrides_cache() keep their signatures.
  • Tests: one visits blog 1, blog 2 and blog 1 again, each with its own override, never fires switch_blog, and asserts each blog's exact override map, so an empty map cannot pass (it fails against Feature Flags: re-read wpcom overrides after switch_to_blog() #52555's version). The WorDBless suite is single-site, so it moves get_current_blog_id() through $GLOBALS['blog_id'] and serves each blog's option through a pre_option_ filter instead of calling switch_to_blog(). A second test checks that repeat resolutions on one blog read the option once.

Behaviour change for reviewers: any process that resolves flags on more than one blog (the public API, cron, wp-cli walking sites) now gets each blog's own overrides, where it used to get the first blog's map for the whole process. That is the fix, but it reaches more than the REST path described here, while the changelog entry is patch/fixed.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No.

Testing instructions

Automated:

  • jp test php packages/jetpack-mu-wpcom --filter Wpcom_Feature_Flags

On a WordPress.com Simple sandbox, with a site whose Premium Analytics preview is switched on through its own opt-in and no jetpack-premium-analytics override (Traffic and Insights only), and with #52549 applied:

  1. Sandbox both the site's domain and public-api.wordpress.com.
  2. As an Automattician, open Tools → Feature Flags (a8c), force premium-analytics-a11n-all-sections on, and save.
  3. Open https://public-api.wordpress.com/wpcom/v2/sites/<blog-id>/dashboards/jetpack-premium-analytics_dashboard/sections.
  4. On trunk, the response still lists only traffic and insights. With this PR, it lists every section the site qualifies for.
  5. Set the flag back to Default. The response returns to the two preview tabs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb

@github-actions

github-actions Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (WordPress.com Site Helper), and enable the fix/wpcom-feature-flags-by-blog-cache branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack-mu-wpcom-plugin fix/wpcom-feature-flags-by-blog-cache

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions

github-actions Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions Bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Sep 21, 2026
@jp-launch-control

jp-launch-control Bot commented Sep 21, 2026

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/jetpack-mu-wpcom/src/features/wpcom-feature-flags/class-wpcom-feature-flags.php 217/228 (95.18%) 0.09% 0 💚

Full summary · PHP report · JS report

@louwie17
louwie17 marked this pull request as ready for review September 22, 2026 07:59
@louwie17 louwie17 added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Status] In Progress labels Sep 22, 2026
@louwie17
louwie17 requested a review from dognose24 September 22, 2026 08:00
dognose24 and others added 5 commits September 22, 2026 10:47
Wpcom_Feature_Flags memoized the override map on the first resolution and
kept it for the rest of the request, even after switch_to_blog(). On
WordPress.com's public API a flag is resolved before the request switches
to the requested site, so that site's overrides never applied to its REST
responses. Drop the cache on switch_blog and pin it with a test.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ing on switch_blog

Replace the switch_blog reset with a single cache slot guarded by
get_current_blog_id(). It does not depend on switch_blog firing or on hook
priority, and a switch/restore pair no longer re-reads the non-autoloaded
option twice.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb
…h blog's exact map

Address review: the saving over a switch_blog reset only applies to a
switch/restore pair that resolves no flag, so the docblock now argues from
hook reliance instead. The blog test asserts each blog's exact override map,
so an empty map can no longer satisfy it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb
tear_down() restored blog_id to a hardcoded 1; it now restores the value the
test started with, and the blog-switching test sets its starting blog itself.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb
Phan flagged the repeated is_enabled() assertion as a duplicate adjacent
statement; the second read now goes through get_overrides().

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb
@louwie17
louwie17 force-pushed the fix/wpcom-feature-flags-by-blog-cache branch from 31136cd to 654df59 Compare September 22, 2026 08:48

@dognose24 dognose24 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both threads are resolved in the follow-up commits: the docblock now argues from correctness rather than a saving that a single slot cannot deliver, and the blog-switching test pins each blog's exact map with blog 1 overriding to true, so an empty map fails every step. Merged onto trunk locally and ran the jetpack-mu-wpcom suite: 1225 tests pass.

@louwie17
louwie17 merged commit 6d78f18 into trunk Sep 22, 2026
72 checks passed
@louwie17
louwie17 deleted the fix/wpcom-feature-flags-by-blog-cache branch September 22, 2026 14:54
@github-actions github-actions Bot removed the [Status] Needs Review This PR is ready for review. label Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants