Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | /**
* External dependencies
*/
import { AdminPage, Col, Container, TermsOfService } from '@automattic/jetpack-components';
import { getMyJetpackUrl } from '@automattic/jetpack-script-data';
import { __, sprintf } from '@wordpress/i18n';
import { LinkButton } from '@wordpress/ui';
import clsx from 'clsx';
import { useCallback, useEffect } from 'react';
/**
* Internal dependencies
*/
import { MyJetpackRoutes } from '../../constants';
import useActivatePlugins from '../../data/products/use-activate-plugins';
import useProduct from '../../data/products/use-product';
import useAnalytics from '../../hooks/use-analytics';
import { useGoBack } from '../../hooks/use-go-back';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import useMyJetpackNavigate from '../../hooks/use-my-jetpack-navigate';
import GoBackLink from '../go-back-link';
import { getFeatureCheckoutReturnUrl } from '../my-jetpack-tab-panel/utils';
import ProductDetailCard from '../product-detail-card';
import ProductDetailTable from '../product-detail-table';
import { reloadIfActivationChangesAdminMenu } from './reload-after-activation';
import styles from './style.module.scss';
/**
* Product Interstitial component.
*
* @param {object} props - Component props.
* @param {string} props.slug - Product slug
* @param {string} props.bundle - Bundle including this product
* @param {object} props.children - Product additional content
* @param {string} props.existingLicenseKeyUrl - URL to enter an existing license key (e.g. Akismet)
* @param {boolean} props.installsPlugin - Whether the interstitial button installs a plugin*
* @param {import('react').ReactNode} props.supportingInfo - Complementary links or support/legal text
* @param {boolean} props.preferProductName - Use product name instead of title
* @param {string} props.imageContainerClassName - Append a class to the image container
* @param {string} [props.ctaButtonLabel] - The label for the Call To Action button
* @param {boolean} [props.hideTOS] - Whether to hide the Terms of Service text
* @param {number} [props.quantity] - The quantity of the product to purchase
* @param {number} [props.directCheckout] - Whether to go straight to the checkout page, e.g. for products with usage tiers
* @param {boolean} [props.highlightLastFeature] - Whether to highlight the last feature in the list of features
* @param {object} [props.ctaCallback] - Callback when the product CTA is clicked. Triggered before any activation/checkout process occurs
* @param {string} [props.feature] - The feature to highlight in the product detail card
* @return {object} ProductInterstitial react component.
*/
export default function ProductInterstitial( {
bundle,
existingLicenseKeyUrl = 'admin.php?page=my-jetpack#/add-license',
installsPlugin = false,
slug,
supportingInfo,
preferProductName = false,
children = null,
imageContainerClassName = '',
ctaButtonLabel = null,
hideTOS = false,
quantity = null,
directCheckout = false,
highlightLastFeature = false,
ctaCallback = null,
feature = null,
} ) {
const { detail } = useProduct( slug );
const { detail: proDetail } = useProduct( 'pro' );
const preferredBundle =
proDetail?.pricingForUi?.available === true && ! proDetail?.hasPaidPlanForProduct
? 'pro'
: 'complete';
const offeredBundle = bundle ? preferredBundle : null;
const { detail: bundleDetail } = useProduct( offeredBundle );
const { activate, isPending: isActivating, isSuccess } = useActivatePlugins( slug );
// Get the post activation URL for the product.
let redirectUri = detail?.postActivationUrl || null;
// If the interstitial is highlighting a specific feature, use the post checkout URL for that feature, if available.
if ( feature && detail?.postActivationUrlsByFeature?.[ feature ] ) {
redirectUri = detail.postActivationUrlsByFeature[ feature ];
}
const { isUpgradableByBundle, pricingForUi, isTieredPricing } = detail;
const { recordEvent } = useAnalytics();
const { onClickGoBack } = useGoBack( { slug, fallback: MyJetpackRoutes.Features } );
const myJetpackCheckoutUri = getFeatureCheckoutReturnUrl() || getMyJetpackUrl();
const { siteIsRegistering, handleRegisterSite } = useMyJetpackConnection( {
skipUserConnection: true,
redirectUri,
} );
const showBundledTOS = ! hideTOS && !! bundle && bundleDetail?.pricingForUi?.available === true;
const productName = detail?.title;
const bundleName = bundleDetail?.title;
const bundledTosLabels = [
/* translators: %s is the product name */
sprintf( __( 'Get %s', 'jetpack-my-jetpack' ), productName ),
/* translators: %s is the bundled product name */
sprintf( __( 'Get %s', 'jetpack-my-jetpack' ), bundleName ),
];
useEffect( () => {
recordEvent( 'jetpack_myjetpack_product_interstitial_view', { product: slug } );
}, [ recordEvent, slug ] );
const getProductSlugForTrackEvent = useCallback(
( isFree = false ) => {
if ( isFree ) {
return '';
}
if ( slug === 'crm' ) {
return 'jetpack-crm';
}
if ( pricingForUi?.tiers?.upgraded?.wpcomProductSlug ) {
return pricingForUi.tiers.upgraded.wpcomProductSlug;
}
return pricingForUi?.wpcomProductSlug;
},
[ slug, pricingForUi ]
);
const trackProductOrBundleClick = useCallback(
options => {
const { customSlug = null, isFreePlan = false, ctaText = null } = options || {};
const productSlug = customSlug ? customSlug : ( bundle ?? slug );
recordEvent( 'jetpack_myjetpack_product_interstitial_add_link_click', {
product: productSlug,
product_slug: getProductSlugForTrackEvent( isFreePlan ),
cta_text: ctaText,
} );
},
[ recordEvent, slug, getProductSlugForTrackEvent, bundle ]
);
const navigateToMyJetpackOverviewPage = useMyJetpackNavigate( MyJetpackRoutes.Home );
const clickHandler = useCallback(
( checkout, product, tier ) => {
ctaCallback?.( { slug, product, tier } );
if ( product?.isBundle || directCheckout ) {
// Get straight to the checkout page.
checkout?.();
return;
}
activate(
{ productId: slug },
{
onSettled: activatedProduct => {
let postCheckoutUrl = activatedProduct?.post_checkout_url || myJetpackCheckoutUri;
// If the interstitial is highlighting a specific feature, use the post checkout URL for that feature, if available.
if ( feature && activatedProduct?.post_checkout_urls_by_feature?.[ feature ] ) {
postCheckoutUrl = activatedProduct.post_checkout_urls_by_feature[ feature ];
}
// there is a separate hasRequiredTier, but it is not implemented
const hasPaidPlanForProduct = product?.hasPaidPlanForProduct;
const isFree = tier
? product?.pricingForUi?.tiers?.[ tier ]?.isFree
: product?.pricingForUi?.isFree;
const isUpgradeToHigherTier =
tier && product?.pricingForUi?.tiers?.[ tier ] && ! isFree && product?.isUpgradable;
const needsPurchase = ( ! isFree && ! hasPaidPlanForProduct ) || isUpgradeToHigherTier;
// If the product is CRM, redirect the user to the Jetpack CRM pricing page.
// This is done because CRM is not part of the WP billing system
// and we can't send them to checkout like we can with the rest of the products
if ( product.pluginSlug === 'zero-bs-crm' && ! hasPaidPlanForProduct ) {
window.location.href = 'https://jetpackcrm.com/pricing/';
return;
}
// If no purchase is needed, redirect the user to the product screen.
if ( ! needsPurchase ) {
// for free products, we still initiate the site connection
handleRegisterSite().then( postRegisterRedirectUri => {
if (
! postRegisterRedirectUri &&
! reloadIfActivationChangesAdminMenu( slug, productName )
) {
// Fall back to the My Jetpack overview page.
return navigateToMyJetpackOverviewPage();
}
} );
return;
}
// Redirect to the checkout page.
checkout?.( null, postCheckoutUrl );
},
}
);
},
[
myJetpackCheckoutUri,
feature,
ctaCallback,
slug,
directCheckout,
activate,
handleRegisterSite,
navigateToMyJetpackOverviewPage,
productName,
]
);
return (
<AdminPage
showBackground={ false }
breadcrumbs={
<GoBackLink
onClick={ onClickGoBack }
to={ MyJetpackRoutes.Features }
label={ __( 'My Jetpack', 'jetpack-my-jetpack' ) }
/>
}
actions={
existingLicenseKeyUrl ? (
<LinkButton size="compact" variant="outline" href={ existingLicenseKeyUrl }>
{ __( 'Use license key', 'jetpack-my-jetpack' ) }
</LinkButton>
) : null
}
>
<Container horizontalSpacing={ 3 } horizontalGap={ 3 }>
<Col>
{ isTieredPricing ? (
<ProductDetailTable
slug={ slug }
clickHandler={ clickHandler }
onProductButtonClick={ clickHandler }
trackProductButtonClick={ trackProductOrBundleClick }
preferProductName={ preferProductName }
isFetching={ isActivating || siteIsRegistering }
isFetchingSuccess={ isSuccess }
feature={ feature }
/>
) : (
<Container
className={ ! isUpgradableByBundle ? styles.container : null }
horizontalSpacing={ 0 }
horizontalGap={ 0 }
fluid
>
<Col sm={ 4 } md={ 4 } lg={ 7 }>
<ProductDetailCard
slug={ slug }
trackButtonClick={ trackProductOrBundleClick }
onClick={ installsPlugin ? clickHandler : undefined }
className={ isUpgradableByBundle ? styles.container : null }
supportingInfo={ supportingInfo }
preferProductName={ preferProductName }
ctaButtonLabel={ ctaButtonLabel }
hideTOS={ hideTOS || showBundledTOS }
quantity={ quantity }
highlightLastFeature={ highlightLastFeature }
isFetching={ isActivating || siteIsRegistering }
isFetchingSuccess={ isSuccess }
/>
</Col>
<Col
sm={ 4 }
md={ 4 }
lg={ 5 }
className={ clsx( styles.imageContainer, imageContainerClassName ) }
>
{ bundle ? (
<ProductDetailCard
slug={ offeredBundle }
trackButtonClick={ trackProductOrBundleClick }
onClick={ clickHandler }
className={ isUpgradableByBundle ? styles.container : null }
hideTOS={ hideTOS || showBundledTOS }
quantity={ quantity }
highlightLastFeature={ highlightLastFeature }
isFetching={ isActivating }
isFetchingSuccess={ isSuccess }
isUpsell={ true }
/>
) : (
children
) }
</Col>
</Container>
) }
</Col>
<Col>
{ showBundledTOS && (
<div className={ styles[ 'tos-container' ] }>
<TermsOfService multipleButtons={ bundledTosLabels } />
</div>
) }
</Col>
</Container>
</AdminPage>
);
}
|