Skip to content

My Jetpack: Keep the Scan threats notice when the status refresh fails - #55504

Merged
xavier-lc merged 12 commits into
trunkfrom
fix/my-jetpack-threats-refresh-failed
Nov 27, 2026
Merged

xavier-lc merged 12 commits into
trunkfrom
fix/my-jetpack-threats-refresh-failed

Conversation

@xavier-lc

@xavier-lc xavier-lc commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes JETPACK-3939

When the stored Scan status has expired and the refresh from WordPress.com fails, Protect's status comes back as an error with no threats. The "Scan found threats on your site" notice, the protect_has_threats alert and the Protect card's warning then went away, while the Overview and the Protect menu badge kept counting the stored threats.

Stacked on #55499: review that one first. This PR will target trunk once #55499 merges.

Proposed changes

  • When Protect_Status::get_status() returns an error, Protect::does_module_need_attention() falls back to the stored status (get_cached_status()), as Protect's Threat_Count already does. Threats the site fixed since the last scan are still left out. There is no extra request to WordPress.com.
  • With nothing stored, or with a Protect Status package too old to have get_cached_status(), it returns false as before.
  • Any refresh error falls back, site_not_connected included. Nothing new shows on a disconnected site, though: My Jetpack goes to its onboarding step, and the card shows the connection error before it looks at threats.
  • This covers every surface that reads it: the notice, its alert in My Jetpack and the menu bubble, and the Protect card's needs-attention border and "Fix threats" button.
  • Changelog entries for the Jetpack and Protect plugins: Protect's card and the Scan flow only reach sites through one of those two.

Related product discussion/links

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

No new data. For sessions where the Scan refresh fails, the notice now shows, so its two click events (jetpack_my_jetpack_protect_threats_detected_notice_primary_cta_click and …_secondary_cta_click) can fire, with threat_count, critical_threat_count and fixable_threat_ids taken from the stored status.

Testing instructions

  • On a connected site with Scan, let Scan report two threats: one critical (severity 5) and one not.
  • Make the stored status expire (set jetpack_scan_status_timestamp to a time in the past) and make the next request to /sites/<id>/scan fail, for example with a pre_http_request filter that returns a WP_Error for it.
  • Go to Jetpack › My Jetpack. Before: no notice, and the Protect card is a plain active card with "View", although it and the Protect menu item still count 2 threats. After: the red "Scan found threats on your site" notice shows, and the card has a red border and "Fix threats".
  • Mark one threat fixed (put its ID in jetpack_protect_dashboard_resolved_threats with a fixedOn later than the last scan) and reload: it is still left out.
Before After
After a failed Scan refresh, no threats notice, and the Protect card is a plain active card with View although it counts 2 threats After a failed Scan refresh, the red Scan found threats notice shows, and the Protect card has a red border and Fix threats

Checked through the real flow on a local site with Scan's answer mocked: before is #55499's head, after is this branch.

🤖 Generated with Claude Code

https://claude.ai/code/session_01K4t4VZHSSm6LBsfyfae3a2

Xavier Lozano Carreras and others added 10 commits November 24, 2026 16:52
…t item

The Overview's Protect item counted Scan's threat list as is, so a threat
the site fixed itself stayed in the count until the next scan while the
Protect page and menu badge dropped it. The Protect data endpoint now also
sends Protect's own count, and the item shows it when present.

Fixes JETPACK-3888

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4t4VXnhHJgHhu1cewCKNh
The endpoint always sends threatCount, so it is number | null rather than
optional, and the test row for a missing field goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4t4VXnhHJgHhu1cewCKNh
The Protect card counted Scan's threat list as is, so a threat the site
fixed itself stayed in its threat count, critical count and tooltip until
the next scan, while the Protect page and menu badge dropped it. The card
now shows the endpoint's threatCount, and a new criticalThreatCount worked
out on the server, since users without manage_options get no threat IDs.

Fixes JETPACK-3923

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4t4VY7LCrGDeMSLBEXF6e
Protect 0.5 has Dashboard_Threats::without_resolved() but no
get_scanned_at(), and its Threat_Count::get_cached() passes last_checked
instead. Requiring get_scanned_at() left criticalThreatCount null while
threatCount was set, so the card could say "1 threat (2 critical)".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4t4VY7LCrGDeMSLBEXF6e
Both counts read the stored status through Threat_Count::get_cached(),
not Status::$status, so the line only skipped a fetch the test's
pre_http_request filter already blocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4t4VY9A5xjg6HXJuD1qcy
…ct-card-resolved-threats

# Conflicts:
#	projects/packages/my-jetpack/global.d.ts
#	projects/packages/my-jetpack/src/products/class-protect.php
#	projects/packages/my-jetpack/tests/php/Protect_Threat_Count_Test.php
The "Scan found threats" notice and its alert counted Scan's full list, so
they stayed after the site fixed its threats until the next scan. They now
leave out the same fixed threats as Protect's count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4t4Va2xaQbRQPPmv9ogtB
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4t4Va2xaQbRQPPmv9ogtB
When the stored Scan status had expired and refreshing it failed, the
"Scan found threats" notice, its alert and the Protect card's warning went
away, while the Overview and the Protect menu badge kept counting the stored
threats. They now fall back to the stored status too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4t4VZHSSm6LBsfyfae3a2
@github-actions

github-actions Bot commented Nov 26, 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 (Jetpack), and enable the fix/my-jetpack-threats-refresh-failed branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/my-jetpack-threats-refresh-failed

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 added [Package] My Jetpack [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Tests] Includes Tests labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 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!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: December 8, 2026
    • Code freeze: December 7, 2026

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Protect plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@xavier-lc
xavier-lc marked this pull request as ready for review November 26, 2026 15:08

@xavier-lc xavier-lc left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Code-only review: I haven't run it on a site, and CI is still running on d8ce481. No blockers.

Checked:

  • A failed refresh comes back as error => true with no threats. The fallback leaves out threats fixed since the last scan, and returns false when nothing is stored. The 'none' row covers that case.
  • get_cached_status() is @since 0.9.0 in all three status classes, so the comment about older versions is right.
  • It adds no way to crash: get_status() already parses the same stored data on every request while it's fresh.

Nits, both optional:

  1. The new call is guarded with method_exists() (L436-L442), but get_critical_threat_count() calls the same method unguarded (L696). my-jetpack requires protect-status, so either drop the new guard or add the same one at L696.
  2. Any error triggers the fallback, site_not_connected included, so a disconnected site keeps the notice and "Fix threats" for its stored threats. The Overview and menu badge already show those threats through Threat_Count, so this matches them. Consider mentioning it in the description.

@jp-launch-control

jp-launch-control Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/my-jetpack/src/products/class-protect.php 168/173 (97.11%) 0.07% 0 💚

Full summary · PHP report · JS report

@xavier-lc

Copy link
Copy Markdown
Contributor Author

Re nit 1: I'd keep the guard. L696 is only reached once get_threat_count() returns non-null, which needs Threat_Count::get_cached(), and that already calls Status::get_cached_status(). So that call is guarded by the time it runs. The new call has nothing in front of it, and @dev doesn't stop another plugin's autoloader from loading an older protect-status.

Re nit 2: checked locally with the site token removed. The fallback does fire (site_not_connected), but My Jetpack redirects to the onboarding step, so neither the notice nor the cards render. Product::get_status() also returns site_connection_error before it reads needs-attention, so the card never gets "Fix threats". The alert can only reach the menu bubble when there's no Protect page, where it matches the stored count the badge already shows. I'll add a line to the description saying so.

LiamSarsfield
LiamSarsfield previously approved these changes Nov 27, 2026

@LiamSarsfield LiamSarsfield 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.

LGTM 🚀

One non blocking nit (can be a follow up). The two new rows in Protect_Needs_Attention_Test.php reach the fallback through site_not_connected, as the test stores no connection. Is it worth adding a row with a blog ID and token so the Scan fetch itself fails, as that's the case this PR is about?

The failed-refresh rows stopped at site_not_connected, since the test
stored no connection. Connect the site so the refresh reaches the Scan
request, which the blocked HTTP filter fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4t4VZHSSm6LBsfyfae3a2
@xavier-lc

Copy link
Copy Markdown
Contributor Author

Re the test rows: good catch, they stopped at site_not_connected. The test now connects the site in setUp() and resets the connection status in tearDown(), so both failed-refresh rows fail at the Scan request itself (failed_fetching_status). I connected every row rather than adding one, since the fresh rows never fetch. Done in 44e0181.

gmjuhasz
gmjuhasz previously approved these changes Nov 27, 2026

@gmjuhasz gmjuhasz 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.

I tested this on a JN and works per the instructions

Base automatically changed from fix/my-jetpack-threats-notice-resolved to trunk November 27, 2026 12:02
@xavier-lc
xavier-lc dismissed stale reviews from gmjuhasz and LiamSarsfield November 27, 2026 12:02

The base branch was changed.

…ts-refresh-failed

# Conflicts:
#	projects/packages/my-jetpack/src/products/class-protect.php
#	projects/packages/my-jetpack/tests/php/Protect_Needs_Attention_Test.php
#	projects/packages/my-jetpack/tests/php/Protect_Threat_Count_Test.php

@LiamSarsfield LiamSarsfield 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.

LGTM 🚀

@xavier-lc
xavier-lc merged commit e6fa008 into trunk Nov 27, 2026
178 of 180 checks passed
@xavier-lc
xavier-lc deleted the fix/my-jetpack-threats-refresh-failed branch November 27, 2026 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] My Jetpack [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Tests] Includes Tests [Type] Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants