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 and notice when paid plan needs plugin install or activation. #41013

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
294b6f9
Add redbubble and notice when paid plan is missing plugin(s).
elliottprogrammer Jan 14, 2025
1f507b8
changelog
elliottprogrammer Jan 14, 2025
a56c14d
Handle undefined vars usePaidPlanNeedsPluginInstallActivationNotice h…
elliottprogrammer Jan 14, 2025
9a10d22
Add activate-multiple-plugins endpoint & logic.
elliottprogrammer Jan 15, 2025
c7e3eab
Bug fix, missing parens on implode function call.
elliottprogrammer Jan 15, 2025
626e4b9
Allow products endpoint to retrieve selected products only.
elliottprogrammer Jan 15, 2025
1072740
Remove notice after required plugins are installed/activated.
elliottprogrammer Jan 15, 2025
4c7b9b5
Delay last-backup-failed red-bubble until 30 min after purchase. Also…
elliottprogrammer Jan 16, 2025
394c879
Refactor how we derive shown_products & not_shown_products.
elliottprogrammer Jan 17, 2025
7fddba2
Cleanup & add GlobalNotice after successfull activation/installation.
elliottprogrammer Jan 23, 2025
dcc271a
Merge branch 'trunk' into add/mj-red-bubble-notice-when-plugins-need-…
elliottprogrammer Jan 23, 2025
d7f3782
Add global notice (toast) when plugins installed sucessfully.
elliottprogrammer Jan 23, 2025
496d647
Remove single product endpoints from mj REST_Products.
elliottprogrammer Jan 24, 2025
f4ff88e
changelog
elliottprogrammer Jan 24, 2025
749b394
Fix static analysis possibly undeclared $product_slug.
elliottprogrammer Jan 24, 2025
d755aa5
fix php unit test.
elliottprogrammer Jan 24, 2025
6882a70
Update activate/install success toast message.
elliottprogrammer Jan 27, 2025
652402b
Filter duplicate plugins allow string or string[] useActivate & useIn…
elliottprogrammer Jan 28, 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 @@ -8,8 +8,8 @@ import { useCallback, useEffect } from 'react';
* Internal dependencies
*/
import { MyJetpackRoutes, PRODUCT_STATUSES } from '../../constants';
import useActivate from '../../data/products/use-activate';
import useInstallStandalonePlugin from '../../data/products/use-install-standalone-plugin';
import useActivatePlugins from '../../data/products/use-activate-plugins';
import useInstallPlugins from '../../data/products/use-install-plugins';
import useProduct from '../../data/products/use-product';
import useAnalytics from '../../hooks/use-analytics';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
Expand Down Expand Up @@ -54,9 +54,8 @@ const ConnectedProductCard: FC< ConnectedProductCardProps > = ( {
const { isRegistered, isUserConnected } = useMyJetpackConnection();
const { recordEvent } = useAnalytics();

const { install: installStandalonePlugin, isPending: isInstalling } =
useInstallStandalonePlugin( slug );
const { activate, isPending: isActivating } = useActivate( slug );
const { install: installStandalonePlugin, isPending: isInstalling } = useInstallPlugins( slug );
const { activate, isPending: isActivating } = useActivatePlugins( slug );
const { detail, refetch, isLoading: isProductDataLoading } = useProduct( slug );
const {
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { __, sprintf } from '@wordpress/i18n';
import { Icon, info, check, lockOutline } from '@wordpress/icons';
import clsx from 'clsx';
import { useState, useCallback, useMemo } from 'react';
import { useAllProducts } from '../../data/products/use-product';
import { useAllProducts } from '../../data/products/use-all-products';
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
import getProductSlugsThatRequireUserConnection from '../../data/utils/get-product-slugs-that-require-user-connection';
import useAnalytics from '../../hooks/use-analytics';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MyJetpackRoutes } from '../../constants';
import { useAllProducts } from '../../data/products/use-product';
import { useAllProducts } from '../../data/products/use-all-products';
import getProductSlugsThatRequireUserConnection from '../../data/utils/get-product-slugs-that-require-user-connection';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import useMyJetpackNavigate from '../../hooks/use-my-jetpack-navigate';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ZendeskChat,
useBreakpointMatch,
ActionButton,
GlobalNotices,
} from '@automattic/jetpack-components';
import { __, _x } from '@wordpress/i18n';
import clsx from 'clsx';
Expand Down Expand Up @@ -165,6 +166,7 @@ export default function MyJetpackScreen() {
<hr className={ styles.separator } />

<IDCModal />
<GlobalNotices />
{ ! isNewUser && (
<Container horizontalSpacing={ 0 }>
<Col>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@
flex-grow: 1;
display: flex;

a {
color: var( --jp-black );
&:hover, &:focus {
text-decoration: underline;
text-decoration-thickness: 2px;
}
}

& :global(.terms-of-service) {
font-size: var(--font-body-small);
color: var( --jp-gray-70 );
Expand Down Expand Up @@ -120,6 +128,15 @@
background-color: #f1f1f1
}
}

& :global(.plugins-list) {
& :global(.plugin-item) {
> span {
font-size: calc( var( --font-body-extra-small ) + 1px );
margin-inline-start: 5px;
}
}
}
}

.separator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import clsx from 'clsx';
import { useCallback } from 'react';
import { MyJetpackRoutes, PRODUCT_STATUSES } from '../../constants';
import { QUERY_PURCHASES_KEY, REST_API_SITE_PURCHASES_ENDPOINT } from '../../data/constants';
import { useAllProducts } from '../../data/products/use-product';
import { useAllProducts } from '../../data/products/use-all-products';
import useSimpleQuery from '../../data/use-simple-query';
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
import useAnalytics from '../../hooks/use-analytics';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useProductCheckoutWorkflow } from '@automattic/jetpack-connection';
import { __ } from '@wordpress/i18n';
import { useCallback } from 'react';
import { PRODUCT_STATUSES } from '../../constants';
import useActivate from '../../data/products/use-activate';
import useInstallStandalonePlugin from '../../data/products/use-install-standalone-plugin';
import useActivatePlugins from '../../data/products/use-activate-plugins';
import useInstallPlugins from '../../data/products/use-install-plugins';
import useProduct from '../../data/products/use-product';
import { ProductCamelCase } from '../../data/types';
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
Expand Down Expand Up @@ -138,11 +138,11 @@ const usePricingData = ( slug: string ) => {
const { wpcomProductSlug, wpcomFreeProductSlug, ...data } = parsePricingData(
detail.pricingForUi
);
const { install: installPlugin, isPending: isInstalling } = useInstallStandalonePlugin( slug );
const { install: installPlugin, isPending: isInstalling } = useInstallPlugins( slug );

const { isUserConnected } = useMyJetpackConnection();
const { myJetpackUrl, siteSuffix } = getMyJetpackWindowInitialState();
const { activate, isPending: isActivating } = useActivate( slug );
const { activate, isPending: isActivating } = useActivatePlugins( slug );
const { run: runCheckout } = useProductCheckoutWorkflow( {
from: 'my-jetpack',
productSlug: wpcomProductSlug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import React, { useCallback, useEffect } from 'react';
*/
import { useParams } from 'react-router-dom';
import { MyJetpackRoutes } from '../../constants';
import useActivate from '../../data/products/use-activate';
import useActivatePlugins from '../../data/products/use-activate-plugins';
import useProduct from '../../data/products/use-product';
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state';
import useAnalytics from '../../hooks/use-analytics';
Expand Down Expand Up @@ -78,7 +78,7 @@ export default function ProductInterstitial( {
} ) {
const { detail } = useProduct( slug );
const { detail: bundleDetail } = useProduct( bundle );
const { activate, isPending: isActivating, isSuccess } = useActivate( slug );
const { activate, isPending: isActivating, isSuccess } = useActivatePlugins( slug );

// Get the post activation URL for the product.
let redirectUri = detail?.postActivationUrl || null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { useGlobalNotices } from '@automattic/jetpack-components';
import { __, sprintf } from '@wordpress/i18n';
import useAnalytics from '../../hooks/use-analytics';
import { REST_API_SITE_PRODUCTS_ENDPOINT, QUERY_ACTIVATE_PRODUCT_KEY } from '../constants';
import useSimpleMutation from '../use-simple-mutation';
import { getMyJetpackWindowInitialState } from '../utils/get-my-jetpack-window-state';
import useProducts from './use-products';
import type { ProductCamelCase, ProductSnakeCase } from '../types';

const setPluginActiveState = ( productId: string ) => {
const { items } = getMyJetpackWindowInitialState( 'products' );
if ( items[ productId ]?.plugin_slug === 'jetpack' ) {
return;
}
if ( items[ productId ]?.standalone_plugin_info.has_standalone_plugin ) {
window.myJetpackInitialState.products.items[
productId
].standalone_plugin_info.is_standalone_active = true;
window.myJetpackInitialState.products.items[
productId
].standalone_plugin_info.is_standalone_installed = true;
}
};

const getIsPluginAlreadyActive = ( detail: ProductCamelCase ) => {
const { standalonePluginInfo, isPluginActive } = detail;

if ( standalonePluginInfo?.hasStandalonePlugin ) {
return standalonePluginInfo?.isStandaloneActive;
}

return isPluginActive;
};

const useActivatePlugins = ( productSlugs: string | string[] ) => {
const productIds = Array.isArray( productSlugs ) ? productSlugs : [ productSlugs ];

const { products, refetch } = useProducts( productIds );
const { recordEvent } = useAnalytics();
const { createSuccessNotice } = useGlobalNotices();

const {
mutate: activate,
isPending,
isSuccess,
} = useSimpleMutation< { [ key: string ]: ProductSnakeCase } >( {
name: QUERY_ACTIVATE_PRODUCT_KEY,
query: {
path: `${ REST_API_SITE_PRODUCTS_ENDPOINT }/activate`,
method: 'POST',
data: { products: productIds },
},
options: {
onSuccess: () => {
products?.forEach( product => {
if ( ! getIsPluginAlreadyActive( product ) ) {
recordEvent( 'jetpack_myjetpack_product_activated', {
product: product.slug,
} );

// This is to handle an edge case where a user is redirected somewhere after activation
// and goes back in the browser and "activates" the product again. This will manually update
// the window state so that the tracking is not recorded twice for one activation.
setPluginActiveState( product.slug );
}
} );
refetch().then( () => {
createSuccessNotice(
sprintf(
/* translators: %s is either the product name, i.e.- "Jetpack Backup" or the word "Plugins". */
__( '%s activated successfully!', 'jetpack-my-jetpack' ),
products?.length === 1 ? products[ 0 ].title : __( 'Plugins', 'jetpack-my-jetpack' )
)
);
} );
},
},
errorMessage: sprintf(
// translators: %s is the Jetpack product name or comma seperated list of multiple Jetpack product names.
__( 'There was a problem activating %s.', 'jetpack-my-jetpack' ),
products?.map( product => product?.name ).join( ', ' )
),
} );

return {
activate,
isPending,
isSuccess,
};
};

export default useActivatePlugins;
74 changes: 0 additions & 74 deletions projects/packages/my-jetpack/_inc/data/products/use-activate.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getMyJetpackWindowInitialState } from '../utils/get-my-jetpack-window-state';
import { prepareProductData } from '../utils/prepare-product-data';
import type { ProductCamelCase } from '../types';

export const useAllProducts = (): { [ key: string ]: ProductCamelCase } => {
const { items: products } = getMyJetpackWindowInitialState( 'products' );

if ( ! products ) {
return {};
}

return Object.entries( products ).reduce(
( acc, [ key, product ] ) => ( { ...acc, [ key ]: prepareProductData( product ) } ),
{}
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useGlobalNotices } from '@automattic/jetpack-components';
import { __, sprintf } from '@wordpress/i18n';
import { REST_API_SITE_PRODUCTS_ENDPOINT, QUERY_INSTALL_PRODUCT_KEY } from '../constants';
import useSimpleMutation from '../use-simple-mutation';
import useProducts from './use-products';

const useInstallPlugins = ( productSlugs: string | string[] ) => {
const productIds = Array.isArray( productSlugs ) ? productSlugs : [ productSlugs ];

const { products, refetch } = useProducts( productIds );
const { createSuccessNotice } = useGlobalNotices();

const { mutate: install, isPending } = useSimpleMutation( {
name: QUERY_INSTALL_PRODUCT_KEY,
query: {
path: `${ REST_API_SITE_PRODUCTS_ENDPOINT }/install`,
method: 'POST',
data: { products: productIds },
},
options: {
onSuccess: () => {
refetch().then( () => {
createSuccessNotice(
sprintf(
/* translators: %s is the word "Plugin" or "Pluigns" (singular or plural). */
__( '%s installed successfully!', 'jetpack-my-jetpack' ),
products?.length === 1 ? products[ 0 ].title : __( 'Plugins', 'jetpack-my-jetpack' )
Copy link

@emilyaudela emilyaudela Feb 11, 2025

Choose a reason for hiding this comment

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

Would it be possible to update the plural strings to account for localization? Instead of "'%s installed successfully!", we would prefer 2 strings or a proper plural:

  • plugin installed successfully!
  • plugins installed successfully!

In many languages, the words will need to change depending on whether the word "plugin" is singular or plural.

There are many instances of this in this PR. If it's not changed, some languages will not read correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @emilyaudela 👋!
Thanks for your feedback! Yes! Thank you! I'll get it fixed asap. 👍🙂

Copy link
Contributor Author

@elliottprogrammer elliottprogrammer Feb 11, 2025

Choose a reason for hiding this comment

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

Oh wait... yeah the translation comment is incorrect. The %s is not the word "Plugin" or "Plugins", but rather %s is the Jetpack product name, i.e.- VaultPress Backup or Boost or Social or Search or VideoPress, etc. if it's in the singular context, or %s is the word "Plugins" if it's in the plural context.
So yeah, anyway still, I'll fix the translation comment and make it 2 separate strings (or proper plural, whichever seems to be the best approach). 👍

Copy link
Contributor Author

@elliottprogrammer elliottprogrammer Feb 12, 2025

Choose a reason for hiding this comment

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

I've created PR #41756 which separates the above translations into separate strings.

)
);
} );
},
},
errorMessage: sprintf(
// translators: %s is the Jetpack product name or comma seperated list of multiple Jetpack product names.
__( 'There was a problem installing and activating %s.', 'jetpack-my-jetpack' ),
products?.map( product => product?.name ).join( ', ' )
),
} );

return {
install,
isPending,
};
};

export default useInstallPlugins;
Loading
Loading