-
Notifications
You must be signed in to change notification settings - Fork 812
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
elliottprogrammer
merged 18 commits into
trunk
from
add/mj-red-bubble-notice-when-plugins-need-installed
Jan 28, 2025
Merged
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 1f507b8
changelog
elliottprogrammer a56c14d
Handle undefined vars usePaidPlanNeedsPluginInstallActivationNotice h…
elliottprogrammer 9a10d22
Add activate-multiple-plugins endpoint & logic.
elliottprogrammer c7e3eab
Bug fix, missing parens on implode function call.
elliottprogrammer 626e4b9
Allow products endpoint to retrieve selected products only.
elliottprogrammer 1072740
Remove notice after required plugins are installed/activated.
elliottprogrammer 4c7b9b5
Delay last-backup-failed red-bubble until 30 min after purchase. Also…
elliottprogrammer 394c879
Refactor how we derive shown_products & not_shown_products.
elliottprogrammer 7fddba2
Cleanup & add GlobalNotice after successfull activation/installation.
elliottprogrammer dcc271a
Merge branch 'trunk' into add/mj-red-bubble-notice-when-plugins-need-…
elliottprogrammer d7f3782
Add global notice (toast) when plugins installed sucessfully.
elliottprogrammer 496d647
Remove single product endpoints from mj REST_Products.
elliottprogrammer f4ff88e
changelog
elliottprogrammer 749b394
Fix static analysis possibly undeclared $product_slug.
elliottprogrammer d755aa5
fix php unit test.
elliottprogrammer 6882a70
Update activate/install success toast message.
elliottprogrammer 652402b
Filter duplicate plugins allow string or string[] useActivate & useIn…
elliottprogrammer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
projects/packages/my-jetpack/_inc/components/connections-section/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
projects/packages/my-jetpack/_inc/data/products/use-activate-plugins.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
projects/packages/my-jetpack/_inc/data/products/use-activate.ts
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
projects/packages/my-jetpack/_inc/data/products/use-all-products.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) } ), | ||
{} | ||
); | ||
}; |
46 changes: 46 additions & 0 deletions
46
projects/packages/my-jetpack/_inc/data/products/use-install-plugins.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) | ||
) | ||
); | ||
} ); | ||
}, | ||
}, | ||
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; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
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.
There was a problem hiding this comment.
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. 👍🙂
There was a problem hiding this comment.
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). 👍
There was a problem hiding this comment.
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.