Skip to content

Connection: Add hooks for external storage provider registration#48022

Open
bindlegirl wants to merge 3 commits intotrunkfrom
add/external-storage-init-hook
Open

Connection: Add hooks for external storage provider registration#48022
bindlegirl wants to merge 3 commits intotrunkfrom
add/external-storage-init-hook

Conversation

@bindlegirl
Copy link
Copy Markdown
Contributor

Proposed changes

  • Add jetpack_external_storage_init action that fires before the first external storage read in External_Storage::get_value(). This gives mu-plugins (like CIAB/Garden) a guaranteed-safe point to register their storage provider via add_action() — no Jetpack classes need to exist at hook-registration time.
  • Add jetpack_external_storage_provider_registered action that fires after External_Storage::register_provider() is called. Manager::add_connection_status_invalidation_hooks() listens for this and resets the memoized is_connected() result, so a provider registered "too late" still gets picked up on the next check.

Why

On CIAB/Garden sites, connection data comes from external storage (Atomic Persistent Data), not the database. The storage provider is registered from a mu-plugin that loads before Jetpack. However, there was no guaranteed moment tween "Jetpack classes are available" and "Jetpack's first is_connected() call." A recent early call to is_connected() during Jetpack's load (newsletter module) caused the result to be memoized as false before the provider was registered, breaking Big Sky's assembler on garden sites.

These two hooks solve the problem from both directions:

  • The init hook prevents the stale result by letting providers register at exactly the right time (before the first read)
  • The invalidation hook is a safety net — if registration happens after a read, the cached status is cleared

Both are backward compatible. Existing consumers (wpcomsh/Atomic) register at include time before any reads; the new actions fire harmlessly with no effect.

Related product discussion/links

  • CIAB external storage timing issue with is_connected() memoization

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

No.

Testing instructions

  • Run the connection package PHP tests: jetpack test php packes/connection
  • All 630 tests should pass, including 4 new tests:
    • test_init_action_fires_on_first_get_value — verifies the init action fires exactly once
    • test_init_action_allows_late_provider_registration — verifies a provider registered during the init action serves the triggering read
    • test_provider_registered_action_fires — verifies the registered action fires with the correct provider instance
    • test_registering_provider_invalidates_stale_is_connected — verifies registering a provider after is_connected() returned false clears the cached result

@bindlegirl bindlegirl requested a review from a team as a code owner April 9, 2026 10:52
@bindlegirl bindlegirl added the [Status] Needs Review This PR is ready for review. label Apr 9, 2026
@bindlegirl bindlegirl self-assigned this Apr 9, 2026
@bindlegirl bindlegirl requested a review from WPprodigy April 9, 2026 10:53
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

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 (Jetpack or WordPress.com Site Helper), and enable the add/external-storage-init-hook branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/external-storage-init-hook
bin/jetpack-downloader test jetpack-mu-wpcom-plugin add/external-storage-init-hook

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
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

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!

@jp-launch-control
Copy link
Copy Markdown

jp-launch-control bot commented Apr 9, 2026

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/packages/connection/src/class-manager.php 608/1015 (59.90%) -0.06% 1 ❤️‍🩹
projects/packages/connection/src/class-external-storage.php 39/74 (52.70%) 2.70% 0 💚

Full summary · PHP report · JS report

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds two WordPress actions around external storage provider registration/first-read to make provider registration timing reliable, and ensures Jetpack connection status caching is invalidated when a provider is registered after an initial is_connected() evaluation.

Changes:

  • Fire jetpack_external_storage_init once, before the first External_Storage::get_value() read, to allow “late” provider registration.
  • Fire jetpack_external_storage_provider_registered after External_Storage::register_provider() and invalidate memoized connection status in Manager.
  • Add/extend integration and unit tests covering the new hooks and cache invalidation behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
projects/packages/connection/src/class-external-storage.php Adds one-time init hook before first read; adds provider-registered hook after registration.
projects/packages/connection/src/class-manager.php Resets memoized is_connected() when an external storage provider is registered.
projects/packages/connection/tests/php/External_Storage_Test.php Adds tests for init/registered hooks; updates teardown to reset new static state.
projects/packages/connection/tests/php/ManagerIntegrationTest.php Adds integration test verifying stale is_connected() cache is invalidated after provider registration.
projects/packages/connection/changelog/add-external-storage-provider-hooks Documents the newly added hooks in the connection package changelog.

Copy link
Copy Markdown
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

Nice work here, Ana!

I only have a small recommendation: I believe that the cleanup logic in the unit tests you added should happen in tearDown or at least before the assertions.

Plus in test_registering_provider_invalidates_stale_is_connected shouldn't we also reset init_fired same way we do with provider?

- Move action hook cleanup (remove_all_filters) into tearDown() in
  External_Storage_Test so cleanup is guaranteed even if assertions fail.
- Wrap assertions in try/finally in ManagerIntegrationTest to guarantee
  provider and init_fired cleanup.
- Reset init_fired in ManagerIntegrationTest cleanup (per fgiannar).

Made-with: Cursor
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

reset_connection_status() takes no parameters; explicitly pass 0
accepted_args so WordPress does not forward the provider argument.

Made-with: Cursor
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.

3 participants