@automattic/jetpack-shared-extension-utils 0.17.4 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.18.0] - 2025-03-12
|
|
9
|
+
### Added
|
|
10
|
+
- Add `shouldUseInternalLinks()`. [#42000]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
- Update package dependencies. [#42384]
|
|
14
|
+
|
|
15
|
+
## [0.17.5] - 2025-03-10
|
|
16
|
+
### Added
|
|
17
|
+
- Social: Move useUpgradeFlow from Jetpack plugin to the shared package. [#41836]
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
- Update dependencies. [#42222]
|
|
21
|
+
|
|
8
22
|
## [0.17.4] - 2025-03-03
|
|
9
23
|
### Changed
|
|
10
24
|
- Update package dependencies. [#42163]
|
|
@@ -544,6 +558,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
544
558
|
### Changed
|
|
545
559
|
- Core: prepare utility for release
|
|
546
560
|
|
|
561
|
+
[0.18.0]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.5...0.18.0
|
|
562
|
+
[0.17.5]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.4...0.17.5
|
|
547
563
|
[0.17.4]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.3...0.17.4
|
|
548
564
|
[0.17.3]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.2...0.17.3
|
|
549
565
|
[0.17.2]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.17.1...0.17.2
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './src/block-icons';
|
|
2
2
|
export { default as getJetpackData, JETPACK_DATA_PATH } from './src/get-jetpack-data';
|
|
3
3
|
export { default as getSiteFragment } from './src/get-site-fragment';
|
|
4
|
+
export { default as shouldUseInternalLinks } from './src/should-use-internal-links';
|
|
4
5
|
export * from './src/site-type-utils';
|
|
5
6
|
export { default as getJetpackExtensionAvailability } from './src/get-jetpack-extension-availability';
|
|
6
7
|
export { default as registerJetpackPlugin } from './src/register-jetpack-plugin';
|
|
@@ -25,3 +26,4 @@ export { default as getJetpackBlocksVariation } from './src/get-jetpack-blocks-v
|
|
|
25
26
|
export * from './src/modules-state';
|
|
26
27
|
export { default as isMyJetpackAvailable } from './src/is-my-jetpack-available';
|
|
27
28
|
export * from './src/libs';
|
|
29
|
+
export { default as useUpgradeFlow } from './src/hooks/use-upgrade-flow';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automattic/jetpack-shared-extension-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "Utility functions used by the block editor extensions",
|
|
5
5
|
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/shared-extension-utils/#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -21,14 +21,16 @@
|
|
|
21
21
|
"@automattic/color-studio": "4.0.0",
|
|
22
22
|
"@automattic/jetpack-analytics": "^0.1.36",
|
|
23
23
|
"@automattic/jetpack-base-styles": "^0.6.44",
|
|
24
|
-
"@automattic/jetpack-components": "^0.68.
|
|
25
|
-
"@automattic/jetpack-connection": "^0.
|
|
24
|
+
"@automattic/jetpack-components": "^0.68.2",
|
|
25
|
+
"@automattic/jetpack-connection": "^0.39.1",
|
|
26
|
+
"@automattic/jetpack-script-data": "^0.2.1",
|
|
26
27
|
"@wordpress/api-fetch": "7.19.0",
|
|
27
28
|
"@wordpress/block-editor": "14.14.0",
|
|
28
29
|
"@wordpress/components": "29.5.0",
|
|
29
30
|
"@wordpress/compose": "7.19.0",
|
|
30
31
|
"@wordpress/data": "10.19.0",
|
|
31
32
|
"@wordpress/element": "6.19.0",
|
|
33
|
+
"@wordpress/hooks": "4.19.0",
|
|
32
34
|
"@wordpress/i18n": "5.19.0",
|
|
33
35
|
"@wordpress/plugins": "7.19.0",
|
|
34
36
|
"@wordpress/url": "4.19.0",
|
|
@@ -41,7 +43,7 @@
|
|
|
41
43
|
"@babel/core": "7.26.0",
|
|
42
44
|
"@babel/plugin-transform-react-jsx": "7.25.9",
|
|
43
45
|
"@babel/preset-react": "7.26.3",
|
|
44
|
-
"@babel/runtime": "7.26.
|
|
46
|
+
"@babel/runtime": "7.26.10",
|
|
45
47
|
"@testing-library/dom": "10.4.0",
|
|
46
48
|
"@testing-library/react": "16.2.0",
|
|
47
49
|
"@testing-library/user-event": "14.6.1",
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
## useUpgradeFlow hook
|
|
2
|
+
|
|
3
|
+
Use this hook when you need to implement a component that leads the user to the checkout page.
|
|
4
|
+
|
|
5
|
+
### Usage
|
|
6
|
+
|
|
7
|
+
```es6
|
|
8
|
+
/**
|
|
9
|
+
* Internal dependencies
|
|
10
|
+
*/
|
|
11
|
+
import { useUpgradeFlow } from '@automattic/jetpack-shared-extension-utils';
|
|
12
|
+
|
|
13
|
+
const myUPgradeComponent = () => {
|
|
14
|
+
const [ checkoutUrl, goToCheckoutPage, isRedirecting ] = useUpgradeFlow( 'business-bundle' );
|
|
15
|
+
return (
|
|
16
|
+
<Button href={ checkoutUrl } onClick={ goToCheckoutPage } isBusy={ isRedirecting }>
|
|
17
|
+
CheckOut!
|
|
18
|
+
</Button>
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### API
|
|
24
|
+
|
|
25
|
+
`const [ checkoutUrl, goToCheckoutPage, isRedirecting ] = useUpgradeFlow( planSlug, onRedirect );`
|
|
26
|
+
|
|
27
|
+
#### Arguments
|
|
28
|
+
|
|
29
|
+
The hook accepts two arguments.
|
|
30
|
+
|
|
31
|
+
- `planSlug` (`string`) - Slug of plan to purchase
|
|
32
|
+
- _(optional)_ `onRedirect` (`(string) => void`) - callback function that will
|
|
33
|
+
be run when the redirect process triggers. The new URL is passed.
|
|
34
|
+
|
|
35
|
+
### Return Values
|
|
36
|
+
|
|
37
|
+
The hook returns an array with three items.
|
|
38
|
+
|
|
39
|
+
- `checkoutUrl` (`string`): The checkout URL. You can use this value to set the href of an anchor element.
|
|
40
|
+
- `goToCheckoutPage` (`(event) => void`): Callback to be used in an onClick event.
|
|
41
|
+
|
|
42
|
+
Redirects the user to the checkout URL, checking before whether the current
|
|
43
|
+
post/page/etc has changes to save. If so, it saves them before to redirect.
|
|
44
|
+
|
|
45
|
+
- `isRedirecting` (`bool`): If the component is in the process of redirecting the
|
|
46
|
+
user. It may be waiting for a save to complete before redirecting. Use
|
|
47
|
+
this to set a button as busy or in a loading state.
|
|
48
|
+
|
|
49
|
+
- `planData` (`object`) An object with full data about the plan.
|
|
50
|
+
|
|
51
|
+
This data is provided by the `wordpress-com/plan` store registered by the [upgrade-nudge](../upgrade-nudge/store.js) shared component, which is populated by the response of the `https://public-api.wordpress.com/rest/v1.5/plans` request.
|
|
52
|
+
The following is a quite accurate shape of the personal plan:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
{
|
|
56
|
+
product_id: 2005,
|
|
57
|
+
product_name: "Jetpack Personal",
|
|
58
|
+
meta: null,
|
|
59
|
+
bd_slug: "jetpack-personal",
|
|
60
|
+
bd_variation_slug: "jetpack-personal-yearly",
|
|
61
|
+
sale_coupon_applied: false,
|
|
62
|
+
sale_coupon: null,
|
|
63
|
+
multi: 0,
|
|
64
|
+
cost: 39,
|
|
65
|
+
blog_id: null,
|
|
66
|
+
product_slug: "jetpack_personal",
|
|
67
|
+
description: "",
|
|
68
|
+
bill_period: 365,
|
|
69
|
+
product_type: "bundle",
|
|
70
|
+
available: "yes",
|
|
71
|
+
outer_slug: null,
|
|
72
|
+
extra: null,
|
|
73
|
+
capability: "manage_options",
|
|
74
|
+
product_name_short: "Personal",
|
|
75
|
+
icon: "https://s0.wordpress.com/i/store/plan-business.png",
|
|
76
|
+
icon_active: "https://s0.wordpress.com/i/store/plan-business-active.png",
|
|
77
|
+
bill_period_label: "per year",
|
|
78
|
+
price: "$39",
|
|
79
|
+
formatted_price: "$39",
|
|
80
|
+
raw_price: 39,
|
|
81
|
+
tagline: null,
|
|
82
|
+
currency_code: "USD"
|
|
83
|
+
}
|
|
84
|
+
```
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useSelect } from '@wordpress/data';
|
|
2
|
+
import { doAction, hasAction } from '@wordpress/hooks';
|
|
3
|
+
import { getUpgradeUrl } from '../../plan-utils';
|
|
4
|
+
import useAutosaveAndRedirect from '../use-autosave-and-redirect';
|
|
5
|
+
|
|
6
|
+
const noop = () => {};
|
|
7
|
+
|
|
8
|
+
const HOOK_OPEN_CHECKOUT_MODAL = 'a8c.wpcom-block-editor.openCheckoutModal';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Use this hook when you need to implement a component that leads the user to the checkout page.
|
|
12
|
+
* @param {string} planSlug - The plan slug to upgrade to.
|
|
13
|
+
* @param {Function} onRedirect - A callback function to be called when the user is redirected to the checkout page.
|
|
14
|
+
*
|
|
15
|
+
* @return {Array} - An array with the following elements:
|
|
16
|
+
*/
|
|
17
|
+
export default function useUpgradeFlow( planSlug, onRedirect = noop ) {
|
|
18
|
+
const { checkoutUrl, planData } = useSelect(
|
|
19
|
+
select => {
|
|
20
|
+
const editorSelector = select( 'core/editor' );
|
|
21
|
+
const planSelector = select( 'wordpress-com/plans' );
|
|
22
|
+
|
|
23
|
+
const { id: postId, type: postType } = editorSelector.getCurrentPost();
|
|
24
|
+
const plan = planSelector && planSelector.getPlan( planSlug );
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
checkoutUrl: getUpgradeUrl( { plan, planSlug, postId, postType } ),
|
|
28
|
+
planData: plan,
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
[ planSlug ]
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const { autosave, autosaveAndRedirect, isRedirecting } = useAutosaveAndRedirect(
|
|
35
|
+
checkoutUrl,
|
|
36
|
+
onRedirect
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const goToCheckoutPage = async event => {
|
|
40
|
+
event.preventDefault();
|
|
41
|
+
|
|
42
|
+
// If this action is available, the feature is enabled to open the checkout
|
|
43
|
+
// in a modal rather than redirect the user there, away from the editor.
|
|
44
|
+
if ( hasAction( HOOK_OPEN_CHECKOUT_MODAL ) ) {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
|
|
47
|
+
autosave( event );
|
|
48
|
+
|
|
49
|
+
doAction( HOOK_OPEN_CHECKOUT_MODAL, { products: [ planData ] } );
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
autosaveAndRedirect( event );
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
return [ checkoutUrl, goToCheckoutPage, isRedirecting, planData ];
|
|
57
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { getScriptData } from '@automattic/jetpack-script-data';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Return whether to use internal Jetpack admin links or not.
|
|
5
|
+
*
|
|
6
|
+
* @return {boolean} True if Jetpack admin links are available and should be used, false otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export default function shouldUseInternalLinks() {
|
|
9
|
+
const { connectedPlugins, connectionStatus } = getScriptData()?.connection ?? {};
|
|
10
|
+
return (
|
|
11
|
+
// Some admin pages require the site to be connected (e.g. Privacy)
|
|
12
|
+
connectionStatus?.isActive &&
|
|
13
|
+
// Admin pages are part of the Jetpack plugin and require it to be installed
|
|
14
|
+
connectedPlugins?.some( ( { slug } ) => 'jetpack' === slug )
|
|
15
|
+
);
|
|
16
|
+
}
|