File size: 11,492 Bytes
1e92f2d |
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
import { isEnabled } from '@automattic/calypso-config';
import {
FEATURE_INSTALL_PLUGINS,
WPCOM_FEATURES_INSTALL_PURCHASED_PLUGINS,
} from '@automattic/calypso-products';
import page from '@automattic/calypso-router';
import { Button } from '@automattic/components';
import { ToggleControl, Modal } from '@wordpress/components';
import { useTranslate } from 'i18n-calypso';
import React, { useCallback, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import EligibilityWarnings from 'calypso/blocks/eligibility-warnings';
import { marketplacePlanToAdd, getProductSlugByPeriodVariation } from 'calypso/lib/plugins/utils';
import useAtomicSiteHasEquivalentFeatureToPlugin from 'calypso/my-sites/plugins/use-atomic-site-has-equivalent-feature-to-plugin';
import { recordGoogleEvent, recordTracksEvent } from 'calypso/state/analytics/actions';
import { getCurrentUserId } from 'calypso/state/current-user/selectors';
import { getBillingInterval } from 'calypso/state/marketplace/billing-interval/selectors';
import { productToBeInstalled } from 'calypso/state/marketplace/purchase-flow/actions';
import { removePluginStatuses } from 'calypso/state/plugins/installed/status/actions';
import { savePreference } from 'calypso/state/preferences/actions';
import { getPreference, hasReceivedRemotePreferences } from 'calypso/state/preferences/selectors';
import {
isMarketplaceProduct as isMarketplaceProductSelector,
getProductsList,
} from 'calypso/state/products-list/selectors';
import getPrimaryDomainBySiteId from 'calypso/state/selectors/get-primary-domain-by-site-id';
import getSiteConnectionStatus from 'calypso/state/selectors/get-site-connection-status';
import isSiteAutomatedTransfer from 'calypso/state/selectors/is-site-automated-transfer';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import { getDomainsBySiteId } from 'calypso/state/sites/domains/selectors';
import { isSiteOnECommerceTrial } from 'calypso/state/sites/plans/selectors';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import { getSelectedSite } from 'calypso/state/ui/selectors';
import { getFirstCategoryFromTags } from '../categories/use-categories';
import { PluginCustomDomainDialog } from '../plugin-custom-domain-dialog';
import { getPeriodVariationValue } from '../plugin-price';
import usePreinstalledPremiumPlugin from '../use-preinstalled-premium-plugin';
export default function CTAButton( { plugin, hasEligibilityMessages, disabled } ) {
const dispatch = useDispatch();
const translate = useTranslate();
const [ showEligibility, setShowEligibility ] = useState( false );
const [ showAddCustomDomain, setShowAddCustomDomain ] = useState( false );
const billingPeriod = useSelector( getBillingInterval );
const isMarketplaceProduct = useSelector( ( state ) =>
isMarketplaceProductSelector( state, plugin.slug )
);
// Site type
const selectedSite = useSelector( getSelectedSite );
const isJetpack = useSelector( ( state ) => isJetpackSite( state, selectedSite?.ID ) );
const isAtomic = useSelector( ( state ) => isSiteAutomatedTransfer( state, selectedSite?.ID ) );
const isJetpackSelfHosted = selectedSite && isJetpack && ! isAtomic;
const pluginFeature = isMarketplaceProduct
? WPCOM_FEATURES_INSTALL_PURCHASED_PLUGINS
: FEATURE_INSTALL_PLUGINS;
const isSiteConnected = useSelector( ( state ) =>
getSiteConnectionStatus( state, selectedSite?.ID )
);
const isECommerceTrial = useSelector( ( state ) =>
isSiteOnECommerceTrial( state, selectedSite?.ID )
);
const shouldUpgrade =
useSelector( ( state ) => ! siteHasFeature( state, selectedSite?.ID, pluginFeature ) ) &&
! isJetpackSelfHosted;
// Keep me updated
const userId = useSelector( getCurrentUserId );
const keepMeUpdatedPreferenceId = `jetpack-self-hosted-keep-updated-${ userId }`;
const keepMeUpdatedPreference = useSelector( ( state ) =>
getPreference( state, keepMeUpdatedPreferenceId )
);
const hasPreferences = useSelector( hasReceivedRemotePreferences );
const primaryDomain = useSelector( ( state ) =>
getPrimaryDomainBySiteId( state, selectedSite?.ID )
);
const pluginRequiresCustomPrimaryDomain =
( primaryDomain?.isWPCOMDomain || primaryDomain?.isWpcomStagingDomain ) && !! plugin?.tags?.seo;
const domains = useSelector( ( state ) => getDomainsBySiteId( state, selectedSite?.ID ) );
const updatedKeepMeUpdatedPreference = useCallback(
( isChecked ) => {
dispatch( savePreference( keepMeUpdatedPreferenceId, isChecked ) );
dispatch(
recordTracksEvent( 'calypso_plugins_availability_jetpack_self_hosted', {
user_id: userId,
value: isChecked,
} )
);
},
[ dispatch, keepMeUpdatedPreferenceId, userId ]
);
const { isPreinstalledPremiumPlugin, preinstalledPremiumPluginProduct } =
usePreinstalledPremiumPlugin( plugin.slug );
// Atomic sites already include features such as Jetpack backup, scan, videopress, publicize, and search. So
// therefore we should prevent users from installing these standalone plugin equivalents.
const atomicSiteHasEquivalentFeatureToPlugin = useAtomicSiteHasEquivalentFeatureToPlugin(
plugin.slug
);
const productsList = useSelector( getProductsList );
const pluginsPlansPageFlag = isEnabled( 'plugins-plans-page' );
const pluginsPlansPage = `/plugins/plans/${ plugin.slug }/yearly/${ selectedSite?.slug }`;
let buttonText = translate( 'Install and activate' );
if ( isMarketplaceProduct || isPreinstalledPremiumPlugin ) {
buttonText = translate( 'Purchase and activate' );
} else if ( isECommerceTrial ) {
buttonText = translate( 'Upgrade your plan' );
} else if ( shouldUpgrade ) {
buttonText = translate( 'Upgrade and activate' );
} else if ( atomicSiteHasEquivalentFeatureToPlugin ) {
buttonText = translate( 'Included with your plan' );
}
const handleDismissEligibilityWarnings = useCallback( () => {
setShowEligibility( false );
setShowAddCustomDomain( false );
}, [ setShowEligibility, setShowAddCustomDomain ] );
return (
<>
<PluginCustomDomainDialog
onProceed={ () => {
if ( hasEligibilityMessages ) {
if ( pluginsPlansPageFlag && shouldUpgrade ) {
return page( pluginsPlansPage );
}
return setShowEligibility( true );
}
onClickInstallPlugin( {
dispatch,
selectedSite,
plugin,
upgradeAndInstall: shouldUpgrade,
isMarketplaceProduct,
billingPeriod,
productsList,
} );
} }
isDialogVisible={ showAddCustomDomain }
plugin={ plugin }
domains={ domains }
closeDialog={ () => setShowAddCustomDomain( false ) }
/>
{ showEligibility && (
<Modal
className="plugin-details-cta__dialog-content"
title={ translate( 'Before you continue' ) }
onRequestClose={ () => setShowEligibility( false ) }
size="medium"
>
<EligibilityWarnings
currentContext="plugin-details"
isMarketplace={ isMarketplaceProduct }
standaloneProceed
onDismiss={ handleDismissEligibilityWarnings }
onProceed={ () =>
onClickInstallPlugin( {
dispatch,
selectedSite,
plugin,
upgradeAndInstall: shouldUpgrade,
isMarketplaceProduct,
billingPeriod,
productsList,
} )
}
/>
</Modal>
) }
<Button
className="plugin-details-cta__install-button"
primary
onClick={ () => {
if ( pluginRequiresCustomPrimaryDomain ) {
return setShowAddCustomDomain( true );
}
if ( isECommerceTrial ) {
return page(
`/plans/${ selectedSite.slug }?feature=${ encodeURIComponent(
FEATURE_INSTALL_PLUGINS
) }`
);
}
if ( hasEligibilityMessages ) {
if ( pluginsPlansPageFlag && shouldUpgrade ) {
return page( pluginsPlansPage );
}
return setShowEligibility( true );
}
onClickInstallPlugin( {
dispatch,
selectedSite,
plugin,
upgradeAndInstall: shouldUpgrade,
isMarketplaceProduct,
billingPeriod,
isPreinstalledPremiumPlugin,
preinstalledPremiumPluginProduct,
productsList,
} );
} }
disabled={
( isJetpackSelfHosted && isMarketplaceProduct ) ||
isSiteConnected === false ||
atomicSiteHasEquivalentFeatureToPlugin ||
disabled
}
>
{ buttonText }
</Button>
{ isJetpackSelfHosted && isMarketplaceProduct && (
<div className="plugin-details-cta__not-available">
<p className="plugin-details-cta__not-available-text">
{ translate( 'Thanks for your interest. ' ) }
{ translate(
'Paid plugins are not yet available for Jetpack Sites but we can notify you when they are ready.'
) }
</p>
{ hasPreferences && (
<ToggleControl
className="plugin-details-cta__follow-toggle"
label={ translate( 'Keep me updated' ) }
checked={ keepMeUpdatedPreference }
onChange={ updatedKeepMeUpdatedPreference }
/>
) }
</div>
) }
</>
);
}
function onClickInstallPlugin( {
dispatch,
selectedSite,
plugin,
upgradeAndInstall,
isMarketplaceProduct,
billingPeriod,
isPreinstalledPremiumPlugin,
preinstalledPremiumPluginProduct,
productsList,
} ) {
const tags = Object.keys( plugin.tags );
dispatch( removePluginStatuses( 'completed', 'error', 'up-to-date' ) );
dispatch(
recordGoogleEvent( 'Plugins', 'Install on selected Site', 'Plugin Name', plugin.slug )
);
dispatch(
recordGoogleEvent( 'calypso_plugin_install_click_from_plugin_info', {
site: selectedSite?.ID,
plugin: plugin.slug,
} )
);
dispatch(
recordTracksEvent( 'calypso_plugin_install_activate_click', {
plugin: plugin.slug,
blog_id: selectedSite?.ID,
marketplace_product: isMarketplaceProduct,
needs_plan_upgrade: upgradeAndInstall,
tags: tags.join( ',' ),
category: getFirstCategoryFromTags( tags ),
} )
);
dispatch( productToBeInstalled( plugin.slug, selectedSite.slug ) );
if ( isMarketplaceProduct ) {
// We need to add the product to the cart.
// Plugin install is handled on the backend by activating the subscription.
const variationPeriod = getPeriodVariationValue( billingPeriod );
const variation = plugin?.variations?.[ variationPeriod ];
const product_slug = getProductSlugByPeriodVariation( variation, productsList );
if ( upgradeAndInstall ) {
// We also need to add a business plan to the cart.
return page(
`/checkout/${ selectedSite.slug }/${ marketplacePlanToAdd(
selectedSite?.plan,
billingPeriod
) },${ product_slug }`
);
}
return page( `/checkout/${ selectedSite.slug }/${ product_slug }#step2` );
}
if ( isPreinstalledPremiumPlugin ) {
const checkoutUrl = `/checkout/${ selectedSite.slug }/${ preinstalledPremiumPluginProduct }`;
const installUrl = `/marketplace/plugin/${ plugin.slug }/install/${ selectedSite.slug }`;
return page( `${ checkoutUrl }?redirect_to=${ installUrl }#step2` );
}
// After buying a plan we need to redirect to the plugin install page.
const installPluginURL = `/marketplace/plugin/${ plugin.slug }/install/${ selectedSite.slug }`;
if ( upgradeAndInstall ) {
// We also need to add a business plan to the cart.
return page(
`/checkout/${ selectedSite.slug }/${ marketplacePlanToAdd(
selectedSite?.plan,
billingPeriod
) }?redirect_to=${ installPluginURL }#step2`
);
}
// No need to go through checkout, go to install page directly.
return page( installPluginURL );
}
|