File size: 19,668 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* eslint-disable wpcalypso/jsx-classname-namespace */
import { isEnabled } from '@automattic/calypso-config';
import {
	FEATURE_INSTALL_PLUGINS,
	WPCOM_FEATURES_INSTALL_PURCHASED_PLUGINS,
	getPlan,
	PLAN_BUSINESS,
} from '@automattic/calypso-products';
import { Gridicon, Button } from '@automattic/components';
import { localizeUrl } from '@automattic/i18n-utils';
import { fixMe, useTranslate } from 'i18n-calypso';
import { Fragment, useState, useCallback, useMemo } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import QuerySitePurchases from 'calypso/components/data/query-site-purchases';
import { getPluginPurchased, getSoftwareSlug, getSaasRedirectUrl } from 'calypso/lib/plugins/utils';
import { setQueryArgs } from 'calypso/lib/query-args';
import { addQueryArgs } from 'calypso/lib/route';
import { userCan } from 'calypso/lib/site/utils';
import BillingIntervalSwitcher from 'calypso/my-sites/marketplace/components/billing-interval-switcher';
import { ManageSitePluginsDialog } from 'calypso/my-sites/plugins/manage-site-plugins-dialog';
import PluginAutoupdateToggle from 'calypso/my-sites/plugins/plugin-autoupdate-toggle';
import { isCompatiblePlugin } from 'calypso/my-sites/plugins/plugin-compatibility';
import StagingSiteNotice from 'calypso/my-sites/plugins/plugin-details-CTA/staging-site-notice';
import { siteObjectsToSiteIds } from 'calypso/my-sites/plugins/utils';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { getEligibility } from 'calypso/state/automated-transfer/selectors';
import {
	isUserLoggedIn,
	getCurrentUserId,
	getCurrentUserSiteCount,
} from 'calypso/state/current-user/selectors';
import { setBillingInterval } from 'calypso/state/marketplace/billing-interval/actions';
import { getBillingInterval } from 'calypso/state/marketplace/billing-interval/selectors';
import {
	isRequestingForSites,
	isRequestingForAllSites,
	getSiteObjectsWithPlugin,
	getPluginOnSite,
} from 'calypso/state/plugins/installed/selectors';
import { isMarketplaceProduct as isMarketplaceProductSelector } from 'calypso/state/products-list/selectors';
import { getSitePurchases } from 'calypso/state/purchases/selectors';
import getSelectedOrAllSitesWithPlugins from 'calypso/state/selectors/get-selected-or-all-sites-with-plugins';
import isSiteAutomatedTransfer from 'calypso/state/selectors/is-site-automated-transfer';
import isSiteWpcomStaging from 'calypso/state/selectors/is-site-wpcom-staging';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import { getSelectedSite, getSectionName } from 'calypso/state/ui/selectors';
import { PREINSTALLED_PLUGINS } from '../constants';
import { PluginPrice } from '../plugin-price';
import usePreinstalledPremiumPlugin from '../use-preinstalled-premium-plugin';
import CTAButton from './CTA-button';
import { ActivationButton } from './activation-button';
import { ManagePluginMenu } from './manage-plugin-menu';
import PluginDetailsCTAPreinstalledPremiumPlugins from './preinstalled-premium-plugins-CTA';
import './style.scss';

const PluginDetailsCTA = ( { plugin, isPlaceholder } ) => {
	const translate = useTranslate();
	const dispatch = useDispatch();

	const selectedSite = useSelector( getSelectedSite );
	const billingPeriod = useSelector( getBillingInterval );

	const currentUserId = useSelector( getCurrentUserId );

	const currentUserSiteCount = useSelector( getCurrentUserSiteCount );

	const isMarketplaceProduct = useSelector( ( state ) =>
		isMarketplaceProductSelector( state, plugin.slug )
	);
	const softwareSlug = getSoftwareSlug( plugin, isMarketplaceProduct );
	const purchases = useSelector( ( state ) => getSitePurchases( state, selectedSite?.ID ) );
	const currentPurchase = getPluginPurchased( plugin, purchases );

	// Site type
	const sitesWithPlugins = useSelector( getSelectedOrAllSitesWithPlugins );
	const siteIds = [ ...new Set( siteObjectsToSiteIds( sitesWithPlugins ) ) ];

	const isJetpack = useSelector( ( state ) => isJetpackSite( state, selectedSite?.ID ) );
	const isAtomic = useSelector( ( state ) => isSiteAutomatedTransfer( state, selectedSite?.ID ) );
	const isJetpackSelfHosted = selectedSite && isJetpack && ! isAtomic;
	const isWpcomStaging = useSelector( ( state ) => isSiteWpcomStaging( state, selectedSite?.ID ) );
	const isDisabledForWpcomStaging = isWpcomStaging && isMarketplaceProduct;
	const pluginFeature = isMarketplaceProduct
		? WPCOM_FEATURES_INSTALL_PURCHASED_PLUGINS
		: FEATURE_INSTALL_PLUGINS;
	const incompatiblePlugin = ! isJetpackSelfHosted && ! isCompatiblePlugin( softwareSlug );
	const userCantManageTheSite = ! userCan( 'manage_options', selectedSite );
	const isLoggedIn = useSelector( isUserLoggedIn );
	const sitePlugin = useSelector( ( state ) =>
		getPluginOnSite( state, selectedSite?.ID, softwareSlug )
	);

	const shouldUpgrade =
		useSelector( ( state ) => ! siteHasFeature( state, selectedSite?.ID, pluginFeature ) ) &&
		! isJetpackSelfHosted;

	const requestingPluginsForSites = useSelector( ( state ) =>
		isRequestingForSites( state, siteIds )
	);

	const isPluginInstalledOnsite =
		sitesWithPlugins.length && ! requestingPluginsForSites ? !! sitePlugin : false;
	const isPluginInstalledOnsiteWithSubscription =
		isPluginInstalledOnsite && ! isMarketplaceProduct ? true : currentPurchase?.active;
	const sitesWithPlugin = useSelector( ( state ) =>
		getSiteObjectsWithPlugin( state, siteIds, softwareSlug )
	);
	const installedOnSitesQuantity = sitesWithPlugin.length;

	// Eligibilities for Simple Sites.
	// eslint-disable-next-line prefer-const
	let { eligibilityHolds, eligibilityWarnings } = useSelector( ( state ) =>
		getEligibility( state, selectedSite?.ID )
	);

	const upgradeToBusinessHref = useMemo( () => {
		const pluginsPlansPageFlag = isEnabled( 'plugins-plans-page' );

		const siteSlug = selectedSite?.slug;

		const pluginsPlansPage = `/plugins/plans/yearly/${ siteSlug }`;
		const checkoutPage = siteSlug ? `/checkout/${ siteSlug }/business` : `/checkout/business`;
		return pluginsPlansPageFlag ? pluginsPlansPage : checkoutPage;
	}, [ selectedSite?.slug ] );

	const saasRedirectHRef = useMemo( () => {
		return getSaasRedirectUrl( plugin, currentUserId, selectedSite?.ID );
	}, [ currentUserId, plugin, selectedSite?.ID ] );
	/*
	 * Remove 'NO_BUSINESS_PLAN' holds if the INSTALL_PURCHASED_PLUGINS feature is present.
	 *
	 * Starter plans do not have the ATOMIC feature, but they have the
	 * INSTALL_PURCHASED_PLUGINS feature which allows them to buy marketplace
	 * addons (which do have the ATOMIC feature).
	 *
	 * This means a Starter plan about to purchase a marketplace addon might get a
	 * 'NO_BUSINESS_PLAN' hold on atomic transfer; however, if we're about to buy a
	 * marketplace addon which provides the ATOMIC feature, then we can ignore this
	 * hold.
	 */
	if ( typeof eligibilityHolds !== 'undefined' && isMarketplaceProduct && ! shouldUpgrade ) {
		eligibilityHolds = eligibilityHolds.filter( ( hold ) => hold !== 'NO_BUSINESS_PLAN' );
	}

	const hasEligibilityMessages =
		! isAtomic && ! isJetpack && ( eligibilityHolds?.length || eligibilityWarnings?.length );

	const { isPreinstalledPremiumPlugin } = usePreinstalledPremiumPlugin( plugin.slug );

	const onIntervalSwitcherChange = useCallback(
		( interval ) => {
			setQueryArgs( { interval: interval?.toLowerCase() }, true );
			dispatch( setBillingInterval( interval ) );
		},
		[ dispatch ]
	);

	// Activation and deactivation translations.
	const activeText = translate( '{{span}}active{{/span}}', {
		components: {
			span: <span className="plugin-details-cta__installed-text-active"></span>,
		},
	} );
	const inactiveText = translate( '{{span}}deactivated{{/span}}', {
		components: {
			span: <span className="plugin-details-cta__installed-text-inactive"></span>,
		},
	} );

	const getTermsAndConditions = () => {
		const translateArgs = {
			components: {
				a: (
					<a
						target="_blank"
						rel="noopener noreferrer"
						href={ localizeUrl( 'https://wordpress.com/tos/' ) }
					/>
				),
				thirdPartyTos: (
					<a
						target="_blank"
						rel="noopener noreferrer"
						href="https://wordpress.com/third-party-plugins-terms/"
					/>
				),
			},
		};

		return fixMe( {
			text: 'By installing, you agree to {{a}}WordPress.com Terms of Service{{/a}} and {{thirdPartyTos}}Third-Party plugin Terms{{/thirdPartyTos}}.',
			newCopy: translate(
				'By installing, you agree to {{a}}WordPress.com Terms of Service{{/a}} and {{thirdPartyTos}}Third-Party plugin Terms{{/thirdPartyTos}}.',
				translateArgs
			),
			oldCopy: translate(
				'By installing, you agree to {{a}}WordPress.com’s Terms of Service{{/a}} and the {{thirdPartyTos}}Third-Party plugin Terms{{/thirdPartyTos}}.',
				translateArgs
			),
		} );
	};

	// If we cannot retrieve plugin status through jetpack ( ! isJetpack ) and plugin is preinstalled.
	if ( ! isJetpack && PREINSTALLED_PLUGINS.includes( plugin.slug ) ) {
		return (
			<div className="plugin-details-cta__container">
				{ selectedSite ? (
					<div className="plugin-details-cta__installed-text">
						{ translate( 'Installed and {{activation /}}', {
							components: {
								activation: activeText,
							},
						} ) }
					</div>
				) : (
					<div className="plugin-details-cta__price">{ translate( 'Free' ) }</div>
				) }
				<span className="plugin-details-cta__preinstalled">
					<p>{ translate( '%s is automatically managed for you.', { args: plugin.name } ) }</p>
					{ selectedSite && shouldUpgrade && (
						<p>
							{ translate(
								'Upgrade your plan and get access to another 50,000 WordPress plugins to extend functionality for your site.'
							) }
						</p>
					) }
				</span>

				{ selectedSite && shouldUpgrade && (
					<Button
						href={ upgradeToBusinessHref }
						className="plugin-details-cta__install-button"
						primary
					>
						{ translate( 'Upgrade my plan' ) }
					</Button>
				) }
				{ ( ! isLoggedIn || ! currentUserSiteCount ) && (
					<GetStartedButton
						plugin={ plugin }
						isMarketplaceProduct={ isMarketplaceProduct }
						onClick={ () => {
							dispatch(
								recordTracksEvent( 'calypso_plugin_details_get_started_click', {
									plugin: plugin?.slug,
									is_logged_in: isLoggedIn,
								} )
							);
						} }
					/>
				) }
			</div>
		);
	}

	// Some plugins can be preinstalled on WPCOM and available as standalone on WPORG,
	// but require a paid upgrade to function.
	if ( selectedSite && isPreinstalledPremiumPlugin ) {
		return (
			<div className="plugin-details-cta__container">
				<PluginDetailsCTAPreinstalledPremiumPlugins
					isPluginInstalledOnsite={ isPluginInstalledOnsiteWithSubscription }
					plugin={ plugin }
				/>
			</div>
		);
	}

	if ( isPlaceholder || requestingPluginsForSites ) {
		return <PluginDetailsCTAPlaceholder />;
	}

	if ( isPluginInstalledOnsiteWithSubscription && sitePlugin ) {
		// Check if already instlaled on the site
		const { active } = sitePlugin;

		return (
			<div className="plugin-details-cta__container">
				<div className="plugin-details-cta__container-header">
					<div className="plugin-details-cta__installed-text">
						{ translate( 'Installed and {{activation /}}', {
							components: {
								activation: active ? activeText : inactiveText,
							},
						} ) }
					</div>
					<div className="plugin-details-cta__manage-plugin-menu">
						<ManagePluginMenu plugin={ plugin } />
					</div>
				</div>

				<ActivationButton plugin={ plugin } active={ active } />

				<PluginAutoupdateToggle
					site={ selectedSite }
					plugin={ sitePlugin }
					label={
						<span className="plugin-details-cta__autoupdate-text">
							<span className="plugin-details-cta__autoupdate-text-main">
								{ translate( 'Enable autoupdates.' ) }
							</span>
							{ sitePlugin.version && (
								<span className="plugin-details-cta__autoupdate-text-version">
									{ translate( ' Currently %(version)s', {
										args: { version: sitePlugin.version },
									} ) }
								</span>
							) }
						</span>
					}
					isMarketplaceProduct={ plugin.isMarketplaceProduct }
					productPurchase={ currentPurchase }
					wporg
				/>
			</div>
		);
	}

	return (
		<Fragment>
			<QuerySitePurchases siteId={ selectedSite?.ID } />
			<div className="plugin-details-cta__container">
				{ isPluginInstalledOnsite && sitePlugin && (
					<div className="plugin-details-cta__manage-plugin-menu-new-purchase">
						<ManagePluginMenu plugin={ plugin } />
					</div>
				) }
				{ ! plugin.isSaasProduct && (
					<div className="plugin-details-cta__price">
						<PluginPrice plugin={ plugin } billingPeriod={ billingPeriod }>
							{ ( { isFetching, price, period } ) =>
								isFetching ? (
									<div className="plugin-details-cta__price-placeholder">...</div>
								) : (
									<>
										{ price ? (
											<>
												{ price }
												<span className="plugin-details-cta__period">{ period }</span>
											</>
										) : (
											<FreePrice shouldUpgrade={ shouldUpgrade } />
										) }
									</>
								)
							}
						</PluginPrice>
					</div>
				) }
				{ isMarketplaceProduct && ! plugin.isSaasProduct && (
					<BillingIntervalSwitcher
						billingPeriod={ billingPeriod }
						onChange={ onIntervalSwitcherChange }
						plugin={ plugin }
					/>
				) }
				<div className="plugin-details-cta__install">
					<PrimaryButton
						isLoggedIn={ isLoggedIn }
						selectedSite={ selectedSite }
						currentUserSiteCount={ currentUserSiteCount }
						shouldUpgrade={ shouldUpgrade }
						hasEligibilityMessages={ hasEligibilityMessages }
						incompatiblePlugin={ incompatiblePlugin }
						userCantManageTheSite={ userCantManageTheSite }
						translate={ translate }
						plugin={ plugin }
						saasRedirectHRef={ saasRedirectHRef }
						isWpcomStaging={ isWpcomStaging }
						sitesWithPlugins={ sitesWithPlugins }
						installedOnSitesQuantity={ installedOnSitesQuantity }
					/>
				</div>
				{ isDisabledForWpcomStaging && <StagingSiteNotice plugin={ plugin } /> }
				{ ! isJetpackSelfHosted && ! isMarketplaceProduct && (
					<div className="plugin-details-cta__t-and-c">{ getTermsAndConditions() }</div>
				) }
				{ plugin.isSaasProduct && shouldUpgrade && isLoggedIn && selectedSite && (
					<div className="plugin-details-cta__upgrade-required-card">
						<Button
							href={ upgradeToBusinessHref }
							className="plugin-details-cta__install-button"
							primary
							onClick={ () => {} }
						>
							{ translate( 'Upgrade to %(planName)s', {
								args: { planName: getPlan( PLAN_BUSINESS )?.getTitle() ?? '' },
							} ) }
						</Button>
					</div>
				) }
			</div>
		</Fragment>
	);
};

function PrimaryButton( {
	isLoggedIn,
	selectedSite,
	currentUserSiteCount,
	shouldUpgrade,
	hasEligibilityMessages,
	incompatiblePlugin,
	userCantManageTheSite,
	translate,
	plugin,
	saasRedirectHRef,
	isWpcomStaging,
	sitesWithPlugins,
	installedOnSitesQuantity,
} ) {
	const dispatch = useDispatch();

	const isMarketplaceProduct = useSelector( ( state ) =>
		isMarketplaceProductSelector( state, plugin.slug )
	);
	const isAtomic = useSelector( ( state ) => isSiteAutomatedTransfer( state, selectedSite?.ID ) );
	const isDisabledForWpcomStaging = isWpcomStaging && isMarketplaceProduct;
	const isIncompatibleForAtomic = isAtomic && 'vaultpress' === plugin.slug;

	const onClick = useCallback( () => {
		dispatch(
			recordTracksEvent( 'calypso_plugin_details_get_started_click', {
				plugin: plugin?.slug,
				is_logged_in: isLoggedIn,
				is_saas_product: plugin?.isSaasProduct,
			} )
		);
	}, [ dispatch, plugin, isLoggedIn ] );

	if ( isLoggedIn && currentUserSiteCount > 0 && sitesWithPlugins.length > 0 && ! selectedSite ) {
		return (
			<ManageSitesButton plugin={ plugin } installedOnSitesQuantity={ installedOnSitesQuantity } />
		);
	}

	if ( ! isLoggedIn || ! selectedSite ) {
		return (
			<GetStartedButton
				onClick={ onClick }
				plugin={ plugin }
				isMarketplaceProduct={ isMarketplaceProduct }
			/>
		);
	}
	if ( plugin.isSaasProduct ) {
		return (
			<Button
				className="plugin-details-cta__install-button"
				primary={ ! shouldUpgrade }
				href={ saasRedirectHRef }
				onClick={ onClick }
			>
				{ translate( 'Get started' ) }
				<Gridicon icon="external" />
			</Button>
		);
	}

	return (
		<CTAButton
			plugin={ plugin }
			hasEligibilityMessages={ hasEligibilityMessages }
			disabled={
				incompatiblePlugin ||
				userCantManageTheSite ||
				isDisabledForWpcomStaging ||
				isIncompatibleForAtomic
			}
		/>
	);
}

function GetStartedButton( { onClick, plugin, isMarketplaceProduct, startFreeTrial = false } ) {
	const translate = useTranslate();
	const sectionName = useSelector( getSectionName );
	const billingPeriod = useSelector( getBillingInterval );
	const buttonText = startFreeTrial
		? translate( 'Start your free trial' )
		: translate( 'Get started' );
	const startUrl = addQueryArgs(
		{
			ref: sectionName + '-lp',
			plugin: plugin.slug,
			billing_period: isMarketplaceProduct ? billingPeriod : '',
		},
		startFreeTrial ? 'start/hosting' : '/start/with-plugin'
	);
	return (
		<Button
			type="a"
			className="plugin-details-cta__install-button"
			primary
			onClick={ onClick }
			href={ startUrl }
		>
			{ buttonText }
		</Button>
	);
}

function ManageSitesButton( { plugin, installedOnSitesQuantity } ) {
	const translate = useTranslate();
	const [ displayManageSitePluginsModal, setDisplayManageSitePluginsModal ] = useState( false );
	const isRequestingPlugins = useSelector( ( state ) => isRequestingForAllSites( state ) );

	const toggleDisplayManageSitePluginsModal = useCallback( () => {
		setDisplayManageSitePluginsModal( ( value ) => ! value );
	}, [] );

	return (
		<>
			<ManageSitePluginsDialog
				plugin={ plugin }
				isVisible={ displayManageSitePluginsModal }
				onClose={ () => setDisplayManageSitePluginsModal( false ) }
			/>
			{ !! installedOnSitesQuantity && (
				<div className="plugin-details-cta__installed-text">
					{ translate(
						'Installed on {{span}}%d site{{/span}}',
						'Installed on {{span}}%d sites{{/span}}',
						{
							args: [ installedOnSitesQuantity ],
							installedOnSitesQuantity,
							components: {
								span: <span className="plugin-details-cta__installed-text-quantity"></span>,
							},
							count: installedOnSitesQuantity,
						}
					) }
				</div>
			) }
			<Button
				className="plugin-details-cta__manage-button"
				onClick={ toggleDisplayManageSitePluginsModal }
				busy={ isRequestingPlugins }
			>
				{ translate( 'Manage sites' ) }
			</Button>
		</>
	);
}

function FreePrice( { shouldUpgrade } ) {
	const translate = useTranslate();
	const isLoggedIn = useSelector( isUserLoggedIn );
	const selectedSite = useSelector( getSelectedSite );

	return (
		<>
			{ translate( 'Free' ) }
			{ ( ! isLoggedIn || ! selectedSite || shouldUpgrade ) && (
				<span className="plugin-details-cta__notice">
					{ translate(
						// Translators: %(planName)s is the name of a plan (e.g. Creator or Business)
						'on %(planName)s plan',
						{
							args: { planName: getPlan( PLAN_BUSINESS )?.getTitle() ?? '' },
						}
					) }
				</span>
			) }
		</>
	);
}

function PluginDetailsCTAPlaceholder() {
	return (
		<div className="plugin-details-cta__container is-placeholder">
			<div className="plugin-details-cta__price">...</div>
			<div className="plugin-details-cta__install">...</div>
			<div className="plugin-details-cta__t-and-c">...</div>
		</div>
	);
}

export default PluginDetailsCTA;