Skip to content

Commit b7140d6

Browse files
committed
Send cookies with red bubble request so backend can check for dismissal cookies
1 parent 357f75b commit b7140d6

12 files changed

+76
-48
lines changed

projects/packages/my-jetpack/_inc/components/my-jetpack-screen/index.jsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ import {
2525
REST_API_CHAT_AVAILABILITY_ENDPOINT,
2626
QUERY_CHAT_AVAILABILITY_KEY,
2727
QUERY_CHAT_AUTHENTICATION_KEY,
28-
QUERY_RED_BUBBLE_ALERTS_KEY,
29-
REST_API_RED_BUBBLE_ALERTS,
3028
} from '../../data/constants';
3129
import useEvaluationRecommendations from '../../data/evaluation-recommendations/use-evaluation-recommendations';
3230
import useUpdateHistoricallyActiveModules from '../../data/products/use-update-historically-active-modules';
31+
import useRedBubbleQuery from '../../data/use-red-bubble-query';
3332
import useSimpleQuery from '../../data/use-simple-query';
3433
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
3534
import onKeyDownCallback from '../../data/utils/onKeyDownCallback';
@@ -120,10 +119,7 @@ export default function MyJetpackScreen() {
120119
data: redBubbleAlerts,
121120
isLoading: isRedBubbleAlertsLoading,
122121
isError: isRedBubbleAlertsError,
123-
} = useSimpleQuery( {
124-
name: QUERY_RED_BUBBLE_ALERTS_KEY,
125-
query: { path: REST_API_RED_BUBBLE_ALERTS },
126-
} );
122+
} = useRedBubbleQuery();
127123

128124
const updateHistoricallyActiveModules = useUpdateHistoricallyActiveModules();
129125

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { QUERY_RED_BUBBLE_ALERTS_KEY, REST_API_RED_BUBBLE_ALERTS } from './constants';
2+
import useSimpleQuery from './use-simple-query';
3+
4+
const useRedBubbleQuery = () => {
5+
const dismissalCookies = document.cookie
6+
.split( ';' )
7+
.map( cookie => cookie.trim() )
8+
.filter( cookie => cookie.includes( '_dismissed' ) );
9+
10+
const { data, isLoading, isError } = useSimpleQuery< RedBubbleAlerts >( {
11+
name: QUERY_RED_BUBBLE_ALERTS_KEY,
12+
query: {
13+
path: REST_API_RED_BUBBLE_ALERTS,
14+
method: 'POST',
15+
data: { dismissal_cookies: dismissalCookies },
16+
},
17+
} );
18+
19+
return {
20+
data,
21+
isLoading,
22+
isError,
23+
};
24+
};
25+
26+
export default useRedBubbleQuery;

projects/packages/my-jetpack/_inc/data/welcome-banner/use-welcome-banner.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ import { useValueStore } from '../../context/value-store/valueStoreContext';
44
import {
55
QUERY_DISMISS_WELCOME_BANNER_KEY,
66
REST_API_SITE_DISMISS_BANNER,
7-
QUERY_RED_BUBBLE_ALERTS_KEY,
8-
REST_API_RED_BUBBLE_ALERTS,
97
} from '../../data/constants';
8+
import useRedBubbleQuery from '../use-red-bubble-query';
109
import useSimpleMutation from '../use-simple-mutation';
11-
import useSimpleQuery from '../use-simple-query';
10+
1211
const useWelcomeBanner = () => {
1312
const {
1413
data: redBubbleAlerts,
1514
isLoading: isRedBubbleAlertsLoading,
1615
isError: isRedBubbleAlertsError,
17-
} = useSimpleQuery( {
18-
name: QUERY_RED_BUBBLE_ALERTS_KEY,
19-
query: { path: REST_API_RED_BUBBLE_ALERTS },
20-
} );
16+
} = useRedBubbleQuery();
2117

2218
const redBubbleAlertKeys = useMemo( () => {
2319
if ( isRedBubbleAlertsError || isRedBubbleAlertsLoading ) {

projects/packages/my-jetpack/_inc/hooks/use-notification-watcher/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { QUERY_RED_BUBBLE_ALERTS_KEY, REST_API_RED_BUBBLE_ALERTS } from '../../data/constants';
2-
import useSimpleQuery from '../../data/use-simple-query';
1+
import useRedBubbleQuery from '../../data/use-red-bubble-query';
32
import useBackupNeedsAttentionNotice from './use-backup-needs-attention-notice';
43
import useBadInstallNotice from './use-bad-install-notice';
54
import useConnectionErrorsNotice from './use-connection-errors-notice';
@@ -9,10 +8,7 @@ import useProtectThreatsDetectedNotice from './use-protect-threats-detected-noti
98
import useSiteConnectionNotice from './use-site-connection-notice';
109

1110
const useNotificationWatcher = () => {
12-
const { data: redBubbleAlerts, isLoading } = useSimpleQuery< RedBubbleAlerts >( {
13-
name: QUERY_RED_BUBBLE_ALERTS_KEY,
14-
query: { path: REST_API_RED_BUBBLE_ALERTS },
15-
} );
11+
const { isLoading, data: redBubbleAlerts } = useRedBubbleQuery();
1612

1713
usePaidPlanNeedsPluginInstallActivationNotice( redBubbleAlerts, isLoading );
1814
useProtectThreatsDetectedNotice( redBubbleAlerts, isLoading );

projects/packages/my-jetpack/_inc/hooks/use-notification-watcher/use-backup-needs-attention-notice.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { useContext, useEffect, useCallback } from 'react';
55
import { NOTICE_PRIORITY_HIGH } from '../../context/constants';
66
import { NoticeContext } from '../../context/notices/noticeContext';
77
import { applyTimezone } from '../../utils/apply-timezone';
8-
import checkForCookie from '../../utils/cookies/check-for-cookie';
9-
import createCookie from '../../utils/cookies/create-cookie';
8+
import createCookie from '../../utils/create-cookie';
109
import preventWidows from '../../utils/prevent-widows';
1110
import useAnalytics from '../use-analytics';
1211
import { useGetReadableFailedBackupReason } from './use-get-readable-failed-backup-reason';
@@ -108,7 +107,7 @@ const useBackupNeedsAttentionNotice: NoticeHookType = ( redBubbleAlerts, isLoadi
108107
priority: NOTICE_PRIORITY_HIGH,
109108
};
110109

111-
if ( ! isLoading && ! checkForCookie( 'backup_failure_dismissed' ) ) {
110+
if ( ! isLoading ) {
112111
setNotice( {
113112
title: noticeTitle,
114113
message: noticeMessage,

projects/packages/my-jetpack/_inc/hooks/use-notification-watcher/use-expiring-plans-notice.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { __ } from '@wordpress/i18n';
22
import { useContext, useEffect, useCallback, useMemo } from 'react';
33
import { NOTICE_PRIORITY_MEDIUM } from '../../context/constants';
44
import { NoticeContext } from '../../context/notices/noticeContext';
5-
import checkForCookie from '../../utils/cookies/check-for-cookie';
6-
import createCookie from '../../utils/cookies/create-cookie';
5+
import createCookie from '../../utils/create-cookie';
76
import useAnalytics from '../use-analytics';
87
import { useGetExpiringNoticeContent } from './use-get-expiring-notice-content';
98
import type { NoticeHookType } from './types';
@@ -114,7 +113,7 @@ const useExpiringPlansNotice: NoticeHookType = ( redBubbleAlerts, isLoading ) =>
114113
priority: NOTICE_PRIORITY_MEDIUM,
115114
};
116115

117-
if ( ! isLoading && ! checkForCookie( `${ cookieKey }_dismissed` ) ) {
116+
if ( ! isLoading ) {
118117
setNotice( {
119118
title: noticeTitle,
120119
message: noticeMessage,

projects/packages/my-jetpack/_inc/hooks/use-notification-watcher/use-get-readable-failed-backup-reason.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { __, sprintf } from '@wordpress/i18n';
22
import { useMemo, type ReactElement } from 'react';
3-
import { QUERY_RED_BUBBLE_ALERTS_KEY, REST_API_RED_BUBBLE_ALERTS } from '../../data/constants';
4-
import useSimpleQuery from '../../data/use-simple-query';
3+
import useRedBubbleQuery from '../../data/use-red-bubble-query';
54

65
export type ReasonContent = {
76
reasonContent: {
@@ -17,11 +16,7 @@ export type ReasonContent = {
1716
* @return {ReasonContent} An object containing each tooltip's title and text content.
1817
*/
1918
export function useGetReadableFailedBackupReason(): ReasonContent {
20-
const { data: redBubbleAlerts, isLoading: isRedBubbleAlertsLoading } =
21-
useSimpleQuery< RedBubbleAlerts >( {
22-
name: QUERY_RED_BUBBLE_ALERTS_KEY,
23-
query: { path: REST_API_RED_BUBBLE_ALERTS },
24-
} );
19+
const { data: redBubbleAlerts, isLoading: isRedBubbleAlertsLoading } = useRedBubbleQuery();
2520

2621
const { backup_failure: backupFailure } = redBubbleAlerts || {};
2722
const {

projects/packages/my-jetpack/_inc/hooks/use-notification-watcher/use-paid-plan-needs-plugin-install-activation-notice.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import useInstallPlugins from '../../data/products/use-install-plugins';
1010
import useSimpleQuery from '../../data/use-simple-query';
1111
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
1212
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
13-
import checkForCookie from '../../utils/cookies/check-for-cookie';
14-
import createCookie from '../../utils/cookies/create-cookie';
13+
import createCookie from '../../utils/create-cookie';
1514
import useAnalytics from '../use-analytics';
1615
import { useGetPaidPlanNeedsPluginsContent } from './get-paid-plan-needs-plugins-content';
1716
import type { NoticeHookType } from './types';
@@ -289,7 +288,7 @@ const usePaidPlanNeedsPluginInstallActivationNotice: NoticeHookType = (
289288
priority: NOTICE_PRIORITY_MEDIUM + ( isInstallingOrActivating ? 1 : 0 ),
290289
};
291290

292-
if ( ! isLoading && ! checkForCookie( `${ planSlug }--plugins_needing_installed_dismissed` ) ) {
291+
if ( ! isLoading ) {
293292
setNotice( {
294293
title: noticeTitle,
295294
message: noticeContent,

projects/packages/my-jetpack/_inc/hooks/use-notification-watcher/use-protect-threats-detected-notice.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { useContext, useEffect, useCallback } from 'react';
44
import { NOTICE_PRIORITY_MEDIUM } from '../../context/constants';
55
import { NoticeContext } from '../../context/notices/noticeContext';
66
import useProduct from '../../data/products/use-product';
7-
import checkForCookie from '../../utils/cookies/check-for-cookie';
8-
import createCookie from '../../utils/cookies/create-cookie';
7+
import createCookie from '../../utils/create-cookie';
98
import preventWidows from '../../utils/prevent-widows';
109
import useAnalytics from '../use-analytics';
1110
import type { NoticeHookType } from './types';
@@ -115,7 +114,7 @@ const useProtectThreatsDetectedNotice: NoticeHookType = ( redBubbleAlerts, isLoa
115114
priority: NOTICE_PRIORITY_MEDIUM,
116115
};
117116

118-
if ( ! isLoading && ! checkForCookie( 'protect_threats_detected_dismissed' ) ) {
117+
if ( ! isLoading ) {
119118
setNotice( {
120119
title: noticeTitle,
121120
message: noticeMessage,

projects/packages/my-jetpack/_inc/utils/cookies/check-for-cookie.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

projects/packages/my-jetpack/src/class-red-bubble-notifications.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
1111
use Jetpack_Options;
1212
use WP_Error;
13+
use WP_REST_Request;
1314
use WP_REST_Response;
1415

1516
/**
@@ -30,9 +31,22 @@ public static function register_rest_endpoints() {
3031
'my-jetpack/v1',
3132
'red-bubble-notifications',
3233
array(
33-
'methods' => \WP_REST_Server::READABLE,
34+
'methods' => \WP_REST_Server::CREATABLE,
3435
'callback' => __CLASS__ . '::rest_api_get_red_bubble_alerts',
3536
'permission_callback' => __CLASS__ . '::permissions_callback',
37+
'args' => array(
38+
'dismissal_cookies' => array(
39+
'type' => 'array',
40+
'description' => 'Array of dismissal cookies to set for the red bubble notifications.',
41+
'required' => false,
42+
'items' => array(
43+
'type' => 'string',
44+
),
45+
'sanitize_callback' => function ( $param ) {
46+
return array_map( 'sanitize_text_field', $param );
47+
},
48+
),
49+
),
3650
)
3751
);
3852
}
@@ -357,13 +371,30 @@ public static function get_red_bubble_alerts( bool $bypass_cache = false ) {
357371
/**
358372
* Get the red bubble alerts, bypassing cache when called via the REST API
359373
*
374+
* @param WP_REST_Request $request The REST API request object.
375+
*
360376
* @return WP_Error|WP_REST_Response
361377
*/
362-
public static function rest_api_get_red_bubble_alerts() {
378+
public static function rest_api_get_red_bubble_alerts( $request ) {
363379
// Initialize products to ensure all products data is available during REST API call.
364380
Products::initialize_products();
365381
add_filter( 'my_jetpack_red_bubble_notification_slugs', array( __CLASS__, 'add_red_bubble_alerts' ) );
366382

383+
$cookies = $request->get_param( 'dismissal_cookies' );
384+
385+
// Update $_COOKIE superglobal with the provided cookies
386+
if ( ! empty( $cookies ) && is_array( $cookies ) ) {
387+
foreach ( $cookies as $cookie_string ) {
388+
// Parse cookie string in format "name=value"
389+
$parts = explode( '=', $cookie_string, 2 );
390+
if ( count( $parts ) === 2 ) {
391+
$name = trim( $parts[0] );
392+
$value = trim( $parts[1] );
393+
$_COOKIE[ $name ] = $value;
394+
}
395+
}
396+
}
397+
367398
$red_bubble_alerts = self::get_red_bubble_alerts( true );
368399
return rest_ensure_response( $red_bubble_alerts );
369400
}

0 commit comments

Comments
 (0)