From 80a0e024f03a966f854506111d71baeceb5e03a0 Mon Sep 17 00:00:00 2001 From: wpalani Date: Thu, 25 Apr 2024 14:42:57 -0700 Subject: [PATCH 1/4] Add 'bluehost-app-nav' context to notification api and component --- assets/js/components/notifications/index.js | 40 ++++++++++++++------- includes/Notification.php | 1 + includes/NotificationsApi.php | 3 +- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/assets/js/components/notifications/index.js b/assets/js/components/notifications/index.js index a0dfe5c..8dd708e 100644 --- a/assets/js/components/notifications/index.js +++ b/assets/js/components/notifications/index.js @@ -37,15 +37,15 @@ const Notifications = ({methods, constants, ...props}) => { ) }).then( ( response ) => { setAllNotifications(response); - }); - }, [] ); + }); + }, [] ); // on update notifications, context or page calculate active notifications methods.useEffect(() => { setActiveNotifications( filterNotifications(allNotifications) ); - }, [allNotifications, constants.page]); + }, [allNotifications, constants.page]); /** * Wrapper method to filter notifications @@ -73,7 +73,7 @@ const Notifications = ({methods, constants, ...props}) => { * @returns Array of filtered notifications - removes expired notifications */ const filterByExpiry = (notifications) => { - const now = Date.now(); + const now = Math.round(Date.now() / 1000); // console.log( 'Now: ' + now ); // filter out expired notifications return methods.filter(notifications, @@ -150,19 +150,33 @@ const Notifications = ({methods, constants, ...props}) => { ); } - return ( -
- {activeNotifications.map(notification => ( + if ('bluehost-app-nav' === constants.context && activeNotifications.length > 0) { + return ( +
- ))} -
- ) +
+ ); + } else { + return ( +
+ {activeNotifications.map(notification => ( + + ))} +
+ ); + } }; diff --git a/includes/Notification.php b/includes/Notification.php index debdaf5..5cf9caa 100644 --- a/includes/Notification.php +++ b/includes/Notification.php @@ -121,6 +121,7 @@ public function shouldShow( $context, array $contextData ) { } case container()->plugin()->id . '-plugin': + case container()->plugin()->id . '-app-nav': // The current page $current_page = Arr::get( $contextData, 'page' ); diff --git a/includes/NotificationsApi.php b/includes/NotificationsApi.php index 12f5d2c..c61af6e 100644 --- a/includes/NotificationsApi.php +++ b/includes/NotificationsApi.php @@ -52,6 +52,7 @@ function ( Notification $notification ) { 'validate_callback' => function ( $value ) { return is_string( $value ) && in_array( $value, array( container()->plugin()->id . '-plugin', + container()->plugin()->id . '-app-nav', 'wp-admin-notice', 'wp-admin-prime' ), true ); @@ -61,7 +62,7 @@ function ( Notification $notification ) { 'required' => false, 'validate_callback' => function ( $value, \WP_REST_Request $request ) { $context = $request->get_param( 'context' ); - if ( container()->plugin()->id . '-plugin' === $context || 'wp-admin-notice' === $context ) { + if ( container()->plugin()->id . '-plugin' === $context || container()->plugin()->id . '-app-nav' === $context || 'wp-admin-notice' === $context ) { return is_string( $value ); } From 5d04eb101f2292419edc55b3dd82599c657eb777 Mon Sep 17 00:00:00 2001 From: wpalani Date: Thu, 25 Apr 2024 16:48:02 -0700 Subject: [PATCH 2/4] Cypress test step --- tests/cypress/integration/notifications.cy.js | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/tests/cypress/integration/notifications.cy.js b/tests/cypress/integration/notifications.cy.js index aaaaec5..8674bd4 100644 --- a/tests/cypress/integration/notifications.cy.js +++ b/tests/cypress/integration/notifications.cy.js @@ -8,7 +8,7 @@ const notifications = [ pages: '#/settings', }, ], - expiration: 2748863456503, + expiration: 2748820256, content: '

Notice should only display on plugin app settings page

', }, @@ -20,7 +20,7 @@ const notifications = [ pages: [ '#/home/onboarding', '#/home' ], }, ], - expiration: 2749860279240, + expiration: 2749860279, content: '

Here is a plugin notice it should display on home and onboarding screens only!

', }, @@ -32,7 +32,7 @@ const notifications = [ pages: [ '#/marketplace' ], }, ], - expiration: 2749860279240, + expiration: 2749860279, content: '

Here is a plugin notice it should display on marketplace themes screen only!

', }, @@ -48,10 +48,22 @@ const notifications = [ pages: 'all', }, ], - expiration: 2749860279240, + expiration: 2749860279, content: '

Here is a notice it should display everywhere in the app and in wp-admin!

', }, + { + id: 'test-5', + locations: [ + { + context: Cypress.env( 'pluginId' ) + '-app-nav', + pages: 'all', + }, + ], + expiration: 2749860279, + content: + '

Notice should display in the app side nav

', + }, { id: 'test-expired', locations: [ @@ -60,7 +72,7 @@ const notifications = [ pages: 'all', }, ], - expiration: 1649860279240, + expiration: 1649817079, content: '

Here is an expired notice it should never display anywhere even though it has location `all`

', }, @@ -70,7 +82,7 @@ describe( 'Notifications', () => { const appClass = '.' + Cypress.env( 'appId' ); before( () => { - cy.exec( 'npx wp-env run cli wp transient delete newfold_notifications' ); + cy.exec( 'npx wp-env run cli wp transient delete newfold_notifications', {failOnNonZeroExit: false} ); cy.visit( '/wp-admin/index.php' ); cy.intercept( { @@ -131,6 +143,18 @@ describe( 'Notifications', () => { ).contains( 'display on plugin app settings page' ); } ); + // notification renders on the side nav + it( 'Test notification displays in app side nav', () => { + cy.get( '.newfold-nav-notifications-wrapper #notification-test-5' ) + .should( 'be.visible' ) + .should( 'have.attr', 'data-id' ) + .and( 'equal', 'test-5' ); + + cy.get( + '.newfold-nav-notifications-wrapper #notification-test-5' + ).contains( 'display in the app side nav' ); + } ); + // expired notification should not show it( 'Test expired notification does not display in plugin app', () => { cy.get( From 08adc10cbf5cf02d977fe7015e9a9c5cf4786c9c Mon Sep 17 00:00:00 2001 From: wpalani Date: Fri, 26 Apr 2024 09:52:20 -0700 Subject: [PATCH 3/4] Point bluehost test branch in workflow --- .github/workflows/brand-plugin-test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/brand-plugin-test.yml b/.github/workflows/brand-plugin-test.yml index f31d801..326ac8a 100644 --- a/.github/workflows/brand-plugin-test.yml +++ b/.github/workflows/brand-plugin-test.yml @@ -31,6 +31,7 @@ jobs: module-repo: ${{ github.repository }} module-branch: ${{ needs.setup.outputs.branch }} plugin-repo: 'bluehost/bluehost-wordpress-plugin' + plugin-branch: 'add/app-nav-notifications' secrets: inherit hostgator: From ec0e7dc989bc7909efb5e2fa7eb507dde5c5d9cf Mon Sep 17 00:00:00 2001 From: wpalani Date: Fri, 26 Apr 2024 12:50:02 -0700 Subject: [PATCH 4/4] Fix context check to be dynamic --- assets/js/components/notifications/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/components/notifications/index.js b/assets/js/components/notifications/index.js index 8dd708e..6d6c0fd 100644 --- a/assets/js/components/notifications/index.js +++ b/assets/js/components/notifications/index.js @@ -150,7 +150,7 @@ const Notifications = ({methods, constants, ...props}) => { ); } - if ('bluehost-app-nav' === constants.context && activeNotifications.length > 0) { + if (`${window.NewfoldRuntime.plugin.brand}-app-nav` === constants.context && activeNotifications.length > 0) { return (