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 a Protect 'needs-attention' status when Threats are detected. #40628

Open
wants to merge 37 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
37 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
4b584fe
Fix method how active standalone plugin is detected.
elliottprogrammer Dec 21, 2024
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 @@ -216,18 +216,62 @@ const ActionButton: FC< ActionButtonProps > = ( {
PRODUCT_STATUSES.EXPIRED in primaryActionOverride &&
primaryActionOverride[ PRODUCT_STATUSES.EXPIRED ] ),
};
case PRODUCT_STATUSES.NEEDS_ATTENTION:
return {
case PRODUCT_STATUSES.NEEDS_ATTENTION__ERROR: {
const defaultButton: Partial< SecondaryButtonProps > = {
...buttonState,
href: manageUrl,
variant: 'primary',
label: __( 'Troubleshoot', 'jetpack-my-jetpack' ),
...( primaryActionOverride &&
PRODUCT_STATUSES.NEEDS_ATTENTION__ERROR in primaryActionOverride &&
primaryActionOverride[ PRODUCT_STATUSES.NEEDS_ATTENTION__ERROR ] ),
elliottprogrammer marked this conversation as resolved.
Show resolved Hide resolved
};
switch ( slug ) {
case 'backup':
return {
...defaultButton,
href: troubleshootBackupsUrl,
};
case 'protect':
return {
...defaultButton,
label: __( 'Fix threats', 'jetpack-my-jetpack' ),
};
default:
return {
...defaultButton,
};
elliottprogrammer marked this conversation as resolved.
Show resolved Hide resolved
}
}
case PRODUCT_STATUSES.NEEDS_ATTENTION__WARNING: {
const defaultButton: Partial< SecondaryButtonProps > = {
...buttonState,
href: troubleshootBackupsUrl,
href: manageUrl,
variant: 'primary',
label: __( 'Troubleshoot', 'jetpack-my-jetpack' ),
...( primaryActionOverride &&
PRODUCT_STATUSES.NEEDS_ATTENTION in primaryActionOverride &&
primaryActionOverride[ PRODUCT_STATUSES.NEEDS_ATTENTION ] ),
PRODUCT_STATUSES.NEEDS_ATTENTION__WARNING in primaryActionOverride &&
primaryActionOverride[ PRODUCT_STATUSES.NEEDS_ATTENTION__WARNING ] ),
};
switch ( slug ) {
case 'protect':
return {
...defaultButton,
label: __( 'Fix threats', 'jetpack-my-jetpack' ),
};
default:
return {
...defaultButton,
};
}
}
default:
return null;
return {
...buttonState,
href: purchaseUrl || `#/add-${ slug }`,
label: __( 'Learn more', 'jetpack-my-jetpack' ),
onClick: onAdd,
};
}
}, [
status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ const ProductCard: FC< ProductCardProps > = props => {
const isOwned = ownedProducts?.includes( slug );

const isError =
status === PRODUCT_STATUSES.EXPIRED || status === PRODUCT_STATUSES.NEEDS_ATTENTION;
const isWarning = status === PRODUCT_STATUSES.EXPIRING_SOON;
status === PRODUCT_STATUSES.EXPIRED || status === PRODUCT_STATUSES.NEEDS_ATTENTION__ERROR;
const isWarning =
status === PRODUCT_STATUSES.EXPIRING_SOON ||
status === PRODUCT_STATUSES.NEEDS_ATTENTION__WARNING;
const isAbsent =
status === PRODUCT_STATUSES.ABSENT || status === PRODUCT_STATUSES.ABSENT_WITH_PLAN;
const isPurchaseRequired = status === PRODUCT_STATUSES.NEEDS_PLAN;
Expand Down Expand Up @@ -219,6 +221,7 @@ const ProductCard: FC< ProductCardProps > = props => {
) }
</div>
<Status
slug={ slug }
elliottprogrammer marked this conversation as resolved.
Show resolved Hide resolved
status={ status }
isFetching={ isLoading }
isInstallingStandalone={ isInstallingStandalone }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ import styles from './style.module.scss';
import type { FC } from 'react';

interface StatusProps {
slug: JetpackModule;
status: ProductStatus;
isFetching: boolean;
isInstallingStandalone: boolean;
isOwned: boolean;
}

type StatusStateFunction = ( status: ProductStatus, isOwned: boolean ) => string;
type StatusStateFunction = (
slug: JetpackModule,
status: ProductStatus,
isOwned: boolean
) => string;

const getStatusLabel: StatusStateFunction = ( status, isOwned ) => {
const getStatusLabel: StatusStateFunction = ( slug, status, isOwned ) => {
switch ( status ) {
case PRODUCT_STATUSES.ACTIVE:
case PRODUCT_STATUSES.CAN_UPGRADE:
Expand All @@ -40,14 +45,18 @@ const getStatusLabel: StatusStateFunction = ( status, isOwned ) => {
const inactiveText = __( 'Inactive', 'jetpack-my-jetpack' );
return isOwned ? needsPlanText : inactiveText;
}
case PRODUCT_STATUSES.NEEDS_ATTENTION:
case PRODUCT_STATUSES.NEEDS_ATTENTION__WARNING:
case PRODUCT_STATUSES.NEEDS_ATTENTION__ERROR:
if ( slug === 'protect' ) {
return __( 'Active', 'jetpack-my-jetpack' );
}
return __( 'Needs attention', 'jetpack-my-jetpack' );
Copy link
Contributor

Choose a reason for hiding this comment

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

I ask to understand more: are there no cases we need to actually display "Needs attention" in Protect card?

Copy link
Contributor

Choose a reason for hiding this comment

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

If so, I could make use of some comment here, cause it's a bit difficult to understand from the context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ask to understand more: are there no cases we need to actually display "Needs attention" in Protect card?

As I understand it, for the Protect card, even when it has a NEEDS_ATTENTION__{WARNING | ERROR} status (which means threats have been detected), we still want to show the card status as 'Active' (per the designs: v1IWiVmOAvI8yT8CCa4b1I-fi-4398_9423).
I believe this is to portray to the user that Protect is still operating correctly/normally as it should (however threats were detected).
I left a comment in the code (as you suggested) to explain there also.

default:
return __( 'Inactive', 'jetpack-my-jetpack' );
}
};

const getStatusClassName: StatusStateFunction = ( status, isOwned ) => {
const getStatusClassName: StatusStateFunction = ( slug, status, isOwned ) => {
switch ( status ) {
case PRODUCT_STATUSES.ACTIVE:
case PRODUCT_STATUSES.CAN_UPGRADE:
Expand All @@ -64,16 +73,30 @@ const getStatusClassName: StatusStateFunction = ( status, isOwned ) => {
case PRODUCT_STATUSES.NEEDS_PLAN:
return isOwned ? styles.warning : styles.inactive;
case PRODUCT_STATUSES.EXPIRED:
case PRODUCT_STATUSES.NEEDS_ATTENTION:
case PRODUCT_STATUSES.NEEDS_ATTENTION__WARNING:
if ( slug === 'protect' ) {
return styles.active;
}
return styles.warning;
case PRODUCT_STATUSES.NEEDS_ATTENTION__ERROR:
if ( slug === 'protect' ) {
return styles.active;
}
return styles.error;
default:
return styles.inactive;
}
};

const Status: FC< StatusProps > = ( { status, isFetching, isInstallingStandalone, isOwned } ) => {
const flagLabel = getStatusLabel( status, isOwned );
const statusClassName = clsx( styles.status, getStatusClassName( status, isOwned ), {
const Status: FC< StatusProps > = ( {
slug,
status,
isFetching,
isInstallingStandalone,
isOwned,
} ) => {
const flagLabel = getStatusLabel( slug, status, isOwned );
const statusClassName = clsx( styles.status, getStatusClassName( slug, status, isOwned ), {
[ styles[ 'is-fetching' ] ]: isFetching || isInstallingStandalone,
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const BackupCard = props => {
return (
// eslint-disable-next-line react/jsx-no-bind
<ProductCard slug={ productSlug } Description={ noDescription } { ...props }>
{ status === PRODUCT_STATUSES.NEEDS_ATTENTION && backupFailure && (
{ status === PRODUCT_STATUSES.NEEDS_ATTENTION__ERROR && backupFailure && (
<div className={ styles.backupErrorContainer }>
<div className={ styles.iconContainer }>
<Gridicon icon="notice" size={ 16 } className={ styles.iconError } />
Expand Down
3 changes: 2 additions & 1 deletion projects/packages/my-jetpack/_inc/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ export const PRODUCT_STATUSES = {
CAN_UPGRADE: 'can_upgrade',
EXPIRING_SOON: 'expiring',
EXPIRED: 'expired',
NEEDS_ATTENTION: 'needs_attention',
NEEDS_ATTENTION__ERROR: 'needs_attention_error',
NEEDS_ATTENTION__WARNING: 'needs_attention_warning',
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const useBackupNeedsAttentionNotice = ( redBubbleAlerts: RedBubbleAlerts ) => {
const { recordEvent } = useAnalytics();
const { setNotice } = useContext( NoticeContext );

const { status, last_updated: lastUpdated } = redBubbleAlerts?.backup_failure || {};
const {
type,
data: { status, last_updated: lastUpdated },
} = redBubbleAlerts?.backup_failure || { type: 'error', data: {} };
const { text: errorDescription } = useGetReadableFailedBackupReason() || {};

const {
Expand Down Expand Up @@ -79,7 +82,7 @@ const useBackupNeedsAttentionNotice = ( redBubbleAlerts: RedBubbleAlerts ) => {

const noticeOptions: NoticeOptions = {
id: 'backup-needs-attention-notice',
level: 'error',
level: type,
actions: [
{
label: __( 'Read troubleshooting guide', 'jetpack-my-jetpack' ),
Expand Down Expand Up @@ -108,6 +111,7 @@ const useBackupNeedsAttentionNotice = ( redBubbleAlerts: RedBubbleAlerts ) => {
onSecondaryCtaClick,
noticeTitle,
backupStatusLastUpdatedDate,
type,
errorDescription,
] );
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

My Jetpack: Added a new status for when Protect detects threats on the site.
9 changes: 6 additions & 3 deletions projects/packages/my-jetpack/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,12 @@ interface Window {
};
};
backup_failure?: {
source: 'rewind' | 'last_backup';
status: RewindStatus | BackupStatus;
last_updated: string;
type: 'warning' | 'error';
data: {
source: 'rewind' | 'last_backup';
status: RewindStatus | BackupStatus;
last_updated: string;
};
};
[ key: `${ string }--plan_expired` ]: {
product_slug: string;
Expand Down
5 changes: 2 additions & 3 deletions projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Automattic\Jetpack\Licensing;
use Automattic\Jetpack\Modules;
use Automattic\Jetpack\Plugins_Installer;
use Automattic\Jetpack\Protect_Status\Status as Protect_Status;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Status\Host as Status_Host;
use Automattic\Jetpack\Sync\Functions as Sync_Functions;
Expand Down Expand Up @@ -234,7 +233,7 @@ public static function enqueue_scripts() {
$previous_score = $speed_score_history->latest( 1 );
}
$latest_score['previousScores'] = $previous_score['scores'] ?? array();
$scan_data = Protect_Status::get_status();
$scan_data = Products\Protect::get_protect_data();
self::update_historically_active_jetpack_modules();

$waf_config = array();
Expand Down Expand Up @@ -1045,7 +1044,7 @@ public static function alert_if_paid_plan_expiring( array $red_bubble_slugs ) {
* @return array
*/
public static function alert_if_last_backup_failed( array $red_bubble_slugs ) {
// Make sure we're dealing with the backup product only
// Make sure we're dealing with the Backup product only
if ( ! Products\Backup::has_paid_plan_for_product() ) {
return $red_bubble_slugs;
}
Expand Down
9 changes: 6 additions & 3 deletions projects/packages/my-jetpack/src/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Products {
const STATUS_NEEDS_PLAN = 'needs_plan';
const STATUS_NEEDS_ACTIVATION = 'needs_activation';
const STATUS_NEEDS_FIRST_SITE_CONNECTION = 'needs_first_site_connection';
const STATUS_NEEDS_ATTENTION = 'needs_attention';
const STATUS_NEEDS_ATTENTION__WARNING = 'needs_attention_warning';
const STATUS_NEEDS_ATTENTION__ERROR = 'needs_attention_error';

/**
* List of statuses that display the module as disabled
Expand Down Expand Up @@ -67,7 +68,8 @@ class Products {
self::STATUS_USER_CONNECTION_ERROR,
self::STATUS_PLUGIN_ABSENT_WITH_PLAN,
self::STATUS_NEEDS_PLAN,
self::STATUS_NEEDS_ATTENTION,
self::STATUS_NEEDS_ATTENTION__ERROR,
self::STATUS_NEEDS_ATTENTION__WARNING,
);

/**
Expand Down Expand Up @@ -109,7 +111,8 @@ class Products {
self::STATUS_NEEDS_PLAN,
self::STATUS_NEEDS_ACTIVATION,
self::STATUS_NEEDS_FIRST_SITE_CONNECTION,
self::STATUS_NEEDS_ATTENTION,
self::STATUS_NEEDS_ATTENTION__WARNING,
self::STATUS_NEEDS_ATTENTION__ERROR,
);

/**
Expand Down
18 changes: 12 additions & 6 deletions projects/packages/my-jetpack/src/products/class-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,12 @@ public static function does_module_need_attention() {
if ( ! is_wp_error( $rewind_state ) ) {
if ( $rewind_state->state !== 'active' && $rewind_state->state !== 'provisioning' && $rewind_state->state !== 'awaiting_credentials' ) {
$backup_failed_status = array(
'source' => 'rewind',
'status' => isset( $rewind_state->reason ) && ! empty( $rewind_state->reason ) ? $rewind_state->reason : $rewind_state->state,
'last_updated' => $rewind_state->last_updated,
'type' => 'error',
'data' => array(
'source' => 'rewind',
'status' => isset( $rewind_state->reason ) && ! empty( $rewind_state->reason ) ? $rewind_state->reason : $rewind_state->state,
'last_updated' => $rewind_state->last_updated,
),
);
}
}
Expand All @@ -270,9 +273,12 @@ public static function does_module_need_attention() {
if ( $last_backup && isset( $last_backup->status ) ) {
if ( $last_backup->status !== 'started' && ! preg_match( '/-will-retry$/', $last_backup->status ) && $last_backup->status !== 'finished' ) {
$backup_failed_status = array(
'source' => 'last_backup',
'status' => $last_backup->status,
'last_updated' => $last_backup->last_updated,
'type' => 'error',
'data' => array(
'source' => 'last_backup',
'status' => $last_backup->status,
'last_updated' => $last_backup->last_updated,
),
);
}
}
Expand Down
9 changes: 6 additions & 3 deletions projects/packages/my-jetpack/src/products/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Modules;
use Automattic\Jetpack\My_Jetpack\Products\Backup;
use Automattic\Jetpack\Plugins_Installer;
use Automattic\Jetpack\Status;
use Jetpack_Options;
Expand Down Expand Up @@ -717,8 +716,12 @@
} elseif ( static::$requires_user_connection && ! ( new Connection_Manager() )->has_connected_owner() ) {
$status = Products::STATUS_USER_CONNECTION_ERROR;
} elseif ( static::has_paid_plan_for_product() ) {
if ( static::$slug === 'backup' && Backup::does_module_need_attention() ) {
$status = Products::STATUS_NEEDS_ATTENTION;
$needs_attention = static::does_module_need_attention();
if ( $needs_attention ) {
$status = Products::STATUS_NEEDS_ATTENTION__WARNING;
if ( isset( $needs_attention['type'] ) && 'error' === $needs_attention['type'] ) {

Check failure on line 722 in projects/packages/my-jetpack/src/products/class-product.php

View workflow job for this annotation

GitHub Actions / Static analysis

TypeError PhanTypeArraySuspicious Suspicious array access to $needs_attention of type true
$status = Products::STATUS_NEEDS_ATTENTION__ERROR;
}
}
if ( static::is_paid_plan_expired() ) {
$status = Products::STATUS_EXPIRED;
Expand Down
Loading
Loading