Repository navigation
My Jetpack: Keep the Scan threats notice when the status refresh fails - #55504
Conversation
…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
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
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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:
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:
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
left a comment
There was a problem hiding this comment.
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 => truewith no threats. The fallback leaves out threats fixed since the last scan, and returnsfalsewhen nothing is stored. The'none'row covers that case. get_cached_status()is@since 0.9.0in 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:
- The new call is guarded with
method_exists()(L436-L442), butget_critical_threat_count()calls the same method unguarded (L696).my-jetpackrequiresprotect-status, so either drop the new guard or add the same one at L696. - Any error triggers the fallback,
site_not_connectedincluded, so a disconnected site keeps the notice and "Fix threats" for its stored threats. The Overview and menu badge already show those threats throughThreat_Count, so this matches them. Consider mentioning it in the description.
Code Coverage SummaryCoverage changed in 1 file.
|
|
Re nit 1: I'd keep the guard. L696 is only reached once Re nit 2: checked locally with the site token removed. The fallback does fire ( |
LiamSarsfield
left a comment
There was a problem hiding this comment.
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
|
Re the test rows: good catch, they stopped at |
gmjuhasz
left a comment
There was a problem hiding this comment.
I tested this on a JN and works per the instructions
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
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_threatsalert 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
Protect_Status::get_status()returns an error,Protect::does_module_need_attention()falls back to the stored status (get_cached_status()), as Protect'sThreat_Countalready does. Threats the site fixed since the last scan are still left out. There is no extra request to WordPress.com.get_cached_status(), it returnsfalseas before.site_not_connectedincluded. 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.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_clickand…_secondary_cta_click) can fire, withthreat_count,critical_threat_countandfixable_threat_idstaken from the stored status.Testing instructions
jetpack_scan_status_timestampto a time in the past) and make the next request to/sites/<id>/scanfail, for example with apre_http_requestfilter that returns aWP_Errorfor it.jetpack_protect_dashboard_resolved_threatswith afixedOnlater than the last scan) and reload: it is still left out.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