Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My Jetpack: Add red bubble & notice when Protect threats detected #40719

Merged
merged 47 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
7987f1f
Add red bubble alert if Backups failing.
elliottprogrammer Dec 4, 2024
e964759
changelog
elliottprogrammer Dec 4, 2024
418542a
Add 'needs-attention' status when Backup fails.
elliottprogrammer Dec 5, 2024
249de73
Remove Red bubble alert code, for use in followup PR.
elliottprogrammer Dec 5, 2024
edd0ccd
Clean up, remove testing/dev code.
elliottprogrammer Dec 5, 2024
938dcd1
Fix doctype return value.
elliottprogrammer Dec 5, 2024
7f6615a
Phan suppress PhanTypeSuspiciousNonTraversableForeach warning.
elliottprogrammer Dec 5, 2024
0a23bd0
Update changelog entry - incorrect description.
elliottprogrammer Dec 7, 2024
4d6fde1
Add redbubble & notice when backups are failing.
elliottprogrammer Dec 8, 2024
fdee436
Call Products\Backup class directly. fix phan TypeError.
elliottprogrammer Dec 8, 2024
bbcd4e0
Add changelog.
elliottprogrammer Dec 8, 2024
fd266cb
Adjust the red bubble notice priorities (reverse order).
elliottprogrammer Dec 9, 2024
e90eb26
Fix Notice styling for mobile view.
elliottprogrammer Dec 10, 2024
3719ce8
Fix last Backup date: Apply local timezone.
elliottprogrammer Dec 10, 2024
32ca2d3
changelog
elliottprogrammer Dec 10, 2024
2ad22a6
Add backup failed message in product card.
elliottprogrammer Dec 11, 2024
0ac3116
Merge branch 'trunk' into add/backup-needs-attention-status
elliottprogrammer Dec 11, 2024
adba171
Merge branch 'add/backup-needs-attention-status' into add/backup-redb…
elliottprogrammer Dec 11, 2024
1b9c547
Moar fixes to Notice mobile view.
elliottprogrammer Dec 11, 2024
87597d4
Improve detection of failed backup.
elliottprogrammer Dec 12, 2024
0c818a7
Add 'source' property to needs-attention status data.
elliottprogrammer Dec 14, 2024
b35bbc1
Protect product card: Fix Tooltips not displaying correctly in Threat…
elliottprogrammer Dec 16, 2024
bbe1f2c
Add a error/warning status and a "Fix threats" CTA when Protect has t…
elliottprogrammer Dec 16, 2024
84479e2
Merge branch 'trunk' into add/protect-fix-threats-status
elliottprogrammer Dec 19, 2024
a8f0df5
changelog
elliottprogrammer Dec 19, 2024
c1b9526
Remove scan/threats tooltip changes. (Changes completed in a seperate…
elliottprogrammer Dec 20, 2024
d04a1ba
Merge branch 'trunk' into add/protect-fix-threats-status
elliottprogrammer Dec 20, 2024
ace58eb
Fix i18n minification issue
robertsreberski Dec 20, 2024
2850dea
Maybe fix PhanTypeArraySuspicious TypeError.
elliottprogrammer Dec 20, 2024
feff403
Maybe fix PhanTypeArraySuspicious TypeError.
elliottprogrammer Dec 20, 2024
fd97fdc
Add comment explaining Protect status exception, per feedback.
elliottprogrammer Dec 20, 2024
a1d51a7
Merge branch 'trunk' into add/protect-fix-threats-status
elliottprogrammer Dec 20, 2024
b70b4b5
Rename slug prop to productSlug, for more context.
elliottprogrammer Dec 20, 2024
b4cfce6
Status card, rename prop to suppressNeedsAttention, per feedback.
elliottprogrammer Dec 20, 2024
804c991
Refactor the primaryActionOverride property code, per feedback.
elliottprogrammer Dec 20, 2024
4b89d88
Merge branch 'trunk' into add/protect-fix-threats-status
elliottprogrammer Dec 21, 2024
24cd0c0
Add redbubble & notice when protect threats are detected.
elliottprogrammer Dec 21, 2024
11435da
Changelog.
elliottprogrammer Dec 21, 2024
514b27a
Fix method of checking for standalone plugin.
elliottprogrammer Dec 21, 2024
4b584fe
Fix method how active standalone plugin is detected.
elliottprogrammer Dec 21, 2024
5da9a3a
Merge branch 'add/protect-fix-threats-status' into add/protect-redbub…
elliottprogrammer Dec 21, 2024
4d8e1e7
Refactor 'status' destructure from redBubbleAlerts data.
elliottprogrammer Dec 22, 2024
f2e4337
Merge branch 'add/protect-fix-threats-status' into add/protect-redbub…
elliottprogrammer Dec 22, 2024
5ec9cb8
Change notice priority to medium.
elliottprogrammer Dec 22, 2024
49cdd6a
Merge branch 'trunk' into add/protect-fix-threats-status
elliottprogrammer Jan 3, 2025
23264ac
Merge branch 'add/protect-fix-threats-status' into add/protect-redbub…
elliottprogrammer Jan 3, 2025
f26c6ff
Merge branch 'trunk' into add/protect-redbubble-and-notice
elliottprogrammer Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import useBadInstallNotice from './use-bad-install-notice';
import useConnectionErrorsNotice from './use-connection-errors-notice';
import useDeprecateFeatureNotice from './use-deprecate-feature-notice';
import useExpiringPlansNotice from './use-expiring-plans-notice';
import useProtectThreatsDetectedNotice from './use-protect-threats-detected-notice';
import useSiteConnectionNotice from './use-site-connection-notice';

const useNotificationWatcher = () => {
const { redBubbleAlerts } = getMyJetpackWindowInitialState();

useProtectThreatsDetectedNotice( redBubbleAlerts );
useExpiringPlansNotice( redBubbleAlerts );
useBackupNeedsAttentionNotice( redBubbleAlerts );
useDeprecateFeatureNotice( redBubbleAlerts );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { Col, getRedirectUrl, Text } from '@automattic/jetpack-components';
import { __, sprintf } from '@wordpress/i18n';
import { useContext, useEffect, useCallback } from 'react';
import { NOTICE_PRIORITY_MEDIUM } from '../../context/constants';
import { NoticeContext } from '../../context/notices/noticeContext';
import useProduct from '../../data/products/use-product';
import preventWidows from '../../utils/prevent-widows';
import useAnalytics from '../use-analytics';
import type { NoticeOptions } from '../../context/notices/types';

type RedBubbleAlerts = Window[ 'myJetpackInitialState' ][ 'redBubbleAlerts' ];

const useProtectThreatsDetectedNotice = ( redBubbleAlerts: RedBubbleAlerts ) => {
const { recordEvent } = useAnalytics();
const { setNotice } = useContext( NoticeContext );
const { detail } = useProduct( 'protect' );
const {
hasPaidPlanForProduct,
standalonePluginInfo,
manageUrl: protectDashboardUrl,
} = detail || {};
const { isStandaloneActive } = standalonePluginInfo || {};

const {
type,
data: {
threat_count: threatCount,
critical_threat_count: criticalThreatCount,
fixable_threat_ids: fixableThreatIds,
},
} = redBubbleAlerts?.protect_has_threats || { type: 'warning', data: {} };

const fixThreatsLearnMoreUrl = getRedirectUrl( 'protect-footer-learn-more-scan', {
anchor: 'how-do-i-fix-threats',
} );

const noticeTitle = sprintf(
// translators: %s is the product name. Can be either "Scan" or "Protect".
__( '%s found threats on your site', 'jetpack-my-jetpack' ),
hasPaidPlanForProduct && isStandaloneActive ? 'Protect' : 'Scan'
);

const onPrimaryCtaClick = useCallback( () => {
window.open( protectDashboardUrl );
recordEvent( 'jetpack_my_jetpack_protect_threats_detected_notice_primary_cta_click', {
threat_count: threatCount,
critical_threat_count: criticalThreatCount,
fixable_threat_ids: fixableThreatIds,
} );
}, [ criticalThreatCount, fixableThreatIds, protectDashboardUrl, recordEvent, threatCount ] );

const onSecondaryCtaClick = useCallback( () => {
window.open( fixThreatsLearnMoreUrl );
recordEvent( 'jetpack_my_jetpack_protect_threats_detected_notice_secondary_cta_click', {
threat_count: threatCount,
critical_threat_count: criticalThreatCount,
fixable_threat_ids: fixableThreatIds,
} );
}, [ criticalThreatCount, fixThreatsLearnMoreUrl, fixableThreatIds, recordEvent, threatCount ] );

useEffect( () => {
if ( ! redBubbleAlerts?.protect_has_threats ) {
return;
}

const noticeMessage = (
<Col>
<Text mb={ 1 }>
{ preventWidows(
__(
'We’ve detected some security threats that need your attention.',
'jetpack-my-jetpack'
)
) }
</Text>
<Text mb={ 1 }>
{ preventWidows(
sprintf(
// translators: %s is the product name. Can be either "Scan" or "Protect".
__(
'Visit the %s dashboard to view threat details, auto-fix threats, and keep your site safe.',
'jetpack-my-jetpack'
),
hasPaidPlanForProduct && isStandaloneActive ? 'Protect' : 'Scan'
)
) }
</Text>
</Col>
);

const noticeOptions: NoticeOptions = {
id: 'protect-threats-detected-notice',
level: type,
actions: [
{
label: __( 'Fix threats', 'jetpack-my-jetpack' ),
onClick: onPrimaryCtaClick,
noDefaultClasses: true,
},
{
label: __( 'Learn more', 'jetpack-my-jetpack' ),
onClick: onSecondaryCtaClick,
isExternalLink: true,
},
],
priority: NOTICE_PRIORITY_MEDIUM,
};

setNotice( {
title: noticeTitle,
message: noticeMessage,
options: noticeOptions,
} );
}, [
hasPaidPlanForProduct,
isStandaloneActive,
noticeTitle,
onPrimaryCtaClick,
onSecondaryCtaClick,
redBubbleAlerts?.protect_has_threats,
setNotice,
type,
] );
};

export default useProtectThreatsDetectedNotice;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

My Jetpack: Adds a red bubble and notice when Protect threats are detected.
8 changes: 8 additions & 0 deletions projects/packages/my-jetpack/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ interface Window {
manage_url?: string;
products_effected?: string[];
};
protect_has_threats?: {
type: 'warning' | 'error';
data: {
threat_count: number;
critical_threat_count: number;
fixable_threat_ids: number[];
};
};
};
recommendedModules: {
modules: JetpackModule[] | null;
Expand Down
23 changes: 22 additions & 1 deletion projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,8 @@ public static function add_red_bubble_alerts( array $red_bubble_slugs ) {
return array_merge(
self::alert_if_missing_connection( $red_bubble_slugs ),
self::alert_if_last_backup_failed( $red_bubble_slugs ),
self::alert_if_paid_plan_expiring( $red_bubble_slugs )
self::alert_if_paid_plan_expiring( $red_bubble_slugs ),
self::alert_if_protect_has_threats( $red_bubble_slugs )
);
}
}
Expand Down Expand Up @@ -1056,4 +1057,24 @@ public static function alert_if_last_backup_failed( array $red_bubble_slugs ) {

return $red_bubble_slugs;
}

/**
* Add an alert slug if Protect has scan threats/vulnerabilities.
*
* @param array $red_bubble_slugs - slugs that describe the reasons the red bubble is showing.
* @return array
*/
public static function alert_if_protect_has_threats( array $red_bubble_slugs ) {
// Make sure we're dealing with the Protect product only
if ( ! Products\Protect::has_paid_plan_for_product() ) {
return $red_bubble_slugs;
}

$protect_threats_status = Products\Protect::does_module_need_attention();
if ( $protect_threats_status ) {
$red_bubble_slugs['protect_has_threats'] = $protect_threats_status;
}

return $red_bubble_slugs;
}
}
Loading