File size: 11,072 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
/* eslint-disable jsx-a11y/click-events-have-key-events */
/* eslint-disable jsx-a11y/no-static-element-interactions */

import {
	WPCOM_FEATURES_INSTALL_PLUGINS,
	WPCOM_FEATURES_INSTALL_PURCHASED_PLUGINS,
} from '@automattic/calypso-products';
import page from '@automattic/calypso-router';
import { Button, Gridicon, ExternalLink } from '@automattic/components';
import { localizeUrl } from '@automattic/i18n-utils';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component, useRef } from 'react';
import { connect } from 'react-redux';
import InfoPopover from 'calypso/components/info-popover';
import {
	marketplacePlanToAdd,
	getProductSlugByPeriodVariation,
	getSaasRedirectUrl,
} from 'calypso/lib/plugins/utils';
import { getSiteFileModDisableReason } from 'calypso/lib/site/utils';
import { recordGoogleEvent, recordTracksEvent } from 'calypso/state/analytics/actions';
import { getCurrentUserId } from 'calypso/state/current-user/selectors';
import { installPlugin } from 'calypso/state/plugins/installed/actions';
import { removePluginStatuses } from 'calypso/state/plugins/installed/status/actions';
import { getProductsList } from 'calypso/state/products-list/selectors';
import getSiteConnectionStatus from 'calypso/state/selectors/get-site-connection-status';
import isSiteWpcomAtomic from 'calypso/state/selectors/is-site-wpcom-atomic';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import isJetpackSite from 'calypso/state/sites/selectors/is-jetpack-site';
import { isCompatiblePlugin } from '../plugin-compatibility';
import { getPeriodVariationValue } from '../plugin-price';

import './style.scss';

const PluginInstallNotice = ( { isEmbed, warningText, children } ) => {
	const disabledInfoLabel = useRef();
	const infoPopover = useRef();
	const togglePopover = ( event ) => {
		infoPopover.current.handleClick( event );
	};
	return (
		<div className={ clsx( { 'plugin-install-button__install': true, embed: isEmbed } ) }>
			<span
				onClick={ togglePopover }
				ref={ disabledInfoLabel }
				className="plugin-install-button__warning"
			>
				{ warningText }
			</span>
			<InfoPopover
				position="bottom left"
				popoverName="Plugin Action Disabled Install"
				gaEventCategory="Plugins"
				ref={ infoPopover }
				ignoreContext={ disabledInfoLabel.current }
			>
				{ children }
			</InfoPopover>
		</div>
	);
};

export class PluginInstallButton extends Component {
	installAction = () => {
		const {
			isEmbed,
			siteId,
			selectedSite,
			isInstalling,
			plugin,
			canInstallPlugins,
			siteIsWpcomAtomic,
			recordGoogleEvent: recordGAEvent,
			recordTracksEvent: recordEvent,
		} = this.props;

		if ( isInstalling ) {
			return;
		}

		if ( canInstallPlugins && siteIsWpcomAtomic ) {
			this.props.removePluginStatuses( 'completed', 'error', 'up-to-date' );
			this.props.installPlugin( siteId, plugin );
		} else {
			return page( `/plugins/${ plugin.slug }/${ selectedSite.slug }` );
		}

		if ( isEmbed ) {
			recordGAEvent( 'Plugins', 'Install with no selected site', 'Plugin Name', plugin.slug );
			recordEvent( 'calypso_plugin_install_click_from_sites_list', {
				site: siteId,
				plugin: plugin.slug,
			} );
		} else {
			recordGAEvent( 'Plugins', 'Install on selected Site', 'Plugin Name', plugin.slug );
			recordEvent( 'calypso_plugin_install_click_from_plugin_info', {
				site: siteId,
				plugin: plugin.slug,
			} );
		}
	};

	clickSupportLink = () => {
		this.props.recordGoogleEvent(
			'Plugins',
			'Clicked How do I fix disabled plugin installs unresponsive site.'
		);
	};

	clickSiteManagmentLink = () => {
		this.props.recordGoogleEvent( 'Plugins', 'Clicked How do I fix disabled plugin installs' );
	};

	getDisabledInfo() {
		const { translate, selectedSite, siteId } = this.props;
		if ( ! selectedSite ) {
			// we don't have enough info
			return null;
		}

		if ( selectedSite.options.is_multi_network ) {
			return translate(
				'%(site)s is part of a multi-network installation, which is not currently supported.',
				{
					args: { site: selectedSite.title },
				}
			);
		}

		if ( ! selectedSite.canUpdateFiles && selectedSite.options.file_mod_disabled ) {
			const reasons = getSiteFileModDisableReason( selectedSite, 'modifyFiles' );
			const html = [];

			if ( reasons.length > 1 ) {
				html.push(
					<p key="reason-shell">
						{ translate( 'Plugin install is not available for %(site)s:', {
							args: { site: selectedSite.title },
						} ) }
					</p>
				);
				const list = reasons.map( ( reason, i ) => (
					<li key={ 'reason-i' + i + '-' + siteId }>{ reason }</li>
				) );
				html.push(
					// eslint-disable-next-line wpcalypso/jsx-classname-namespace
					<ul className="plugin-action__disabled-info-list" key="reason-shell-list">
						{ list }
					</ul>
				);
			} else {
				html.push(
					<p key="reason-shell">
						{ translate( 'Plugin install is not available for %(site)s. %(reason)s', {
							args: { site: selectedSite.title, reason: reasons[ 0 ] },
						} ) }
					</p>
				);
			}
			html.push(
				<ExternalLink
					key="external-link"
					onClick={ this.clickSiteManagmentLink }
					href="https://jetpack.me/support/site-management/#file-update-disabled"
				>
					{ translate( 'How do I fix this?' ) }
				</ExternalLink>
			);

			return html;
		}
		return null;
	}

	renderMarketplaceButton() {
		const {
			translate,
			selectedSite,
			siteId,
			userId,
			plugin,
			billingPeriod,
			canInstallPurchasedPlugins,
			productsList,
		} = this.props;
		const variationPeriod = getPeriodVariationValue( billingPeriod );
		const variation = plugin?.variations?.[ variationPeriod ];
		const product_slug = getProductSlugByPeriodVariation( variation, productsList );

		if ( plugin.isSaasProduct ) {
			const saasRedirectUrl = getSaasRedirectUrl( plugin, userId, siteId );
			return (
				<span className="plugin-install-button__install embed">
					<Button href={ saasRedirectUrl }>
						{ translate( 'Get started' ) }
						<Gridicon icon="external" size={ 18 } />
					</Button>
				</span>
			);
		}

		const buttonLink = canInstallPurchasedPlugins
			? `/checkout/${ selectedSite.slug }/${ product_slug }`
			: `/checkout/${ selectedSite.slug }/${ marketplacePlanToAdd(
					selectedSite?.plan,
					billingPeriod
			  ) },${ product_slug }#step2`;

		return (
			<span className="plugin-install-button__install embed">
				<Button href={ buttonLink }>
					<Gridicon key="plus-icon" icon="plus-small" size={ 18 } />
					{ canInstallPurchasedPlugins ? (
						<Gridicon icon="cart" size={ 18 } />
					) : (
						<Gridicon icon="plugins" size={ 18 } />
					) }
					{ canInstallPurchasedPlugins
						? translate( 'Purchase and activate' )
						: translate( 'Upgrade and activate' ) }
				</Button>
			</span>
		);
	}

	renderButton() {
		const {
			translate,
			isInstalling,
			isEmbed,
			disabled,
			isJetpackCloud,
			canInstallPlugins,
			siteIsWpcomAtomic,
		} = this.props;
		const label = isInstalling ? translate( 'Installing…' ) : translate( 'Install' );

		if ( isEmbed ) {
			return (
				<span className="plugin-install-button__install embed">
					{ isInstalling ? (
						<span className="plugin-install-button__installing">{ label }</span>
					) : (
						<Button compact onClick={ this.installAction } disabled={ disabled }>
							{ ! isJetpackCloud && (
								<>
									<Gridicon key="plus-icon" icon="plus-small" size={ 18 } />
									<Gridicon icon="plugins" size={ 18 } />
								</>
							) }
							{ canInstallPlugins && siteIsWpcomAtomic
								? translate( 'Install' )
								: translate( 'Go to plugin page' ) }
						</Button>
					) }
				</span>
			);
		}

		return (
			<span className="plugin-install-button__install">
				<Button onClick={ this.installAction } primary disabled={ isInstalling || disabled }>
					{ label }
				</Button>
			</span>
		);
	}

	renderNoticeOrButton() {
		const {
			plugin,
			isEmbed,
			selectedSite,
			siteIsConnected,
			siteIsJetpackSite,
			siteIsWpcomAtomic,
			translate,
			canInstallPurchasedPlugins,
		} = this.props;

		if ( siteIsConnected === false ) {
			return (
				<PluginInstallNotice warningText={ translate( 'Site unreachable' ) } isEmbed={ isEmbed }>
					<div>
						<p>
							{ translate( '%(site)s is unresponsive.', { args: { site: selectedSite.title } } ) }
						</p>
						<ExternalLink
							key="external-link"
							onClick={ this.clickSupportLink }
							href={ 'https://jetpack.me/support/debug/?url=' + selectedSite.URL }
						>
							{ translate( 'Debug site!' ) }
						</ExternalLink>
					</div>
				</PluginInstallNotice>
			);
		}

		if ( ! isCompatiblePlugin( plugin.slug ) && siteIsWpcomAtomic ) {
			return (
				<PluginInstallNotice warningText={ translate( 'Incompatible Plugin' ) } isEmbed={ isEmbed }>
					<div>
						<p>{ translate( 'This plugin is not supported on WordPress.com.' ) }</p>
						<ExternalLink
							key="external-link"
							href={ localizeUrl( 'https://en.support.wordpress.com/incompatible-plugins' ) }
						>
							{ translate( 'Learn more.' ) }
						</ExternalLink>
					</div>
				</PluginInstallNotice>
			);
		}

		const disabledInfo = this.getDisabledInfo();
		if ( ! selectedSite.canUpdateFiles && disabledInfo ) {
			return disabledInfo ? (
				<PluginInstallNotice warningText={ translate( 'Install Disabled' ) } isEmbed={ isEmbed }>
					{ disabledInfo }
				</PluginInstallNotice>
			) : null;
		}

		if ( siteIsJetpackSite && ! siteIsWpcomAtomic && plugin.isMarketplaceProduct ) {
			return (
				<PluginInstallNotice
					warningText={
						canInstallPurchasedPlugins
							? translate( 'Purchase disabled' )
							: translate( 'Upgrade disabled' )
					}
					isEmbed={ isEmbed }
				>
					<div>
						<p>{ translate( 'Paid plugins are not yet available for Jetpack Sites.' ) }</p>
					</div>
				</PluginInstallNotice>
			);
		}

		if ( ! plugin.isMarketplaceProduct ) {
			return this.renderButton();
		}

		return this.renderMarketplaceButton();
	}

	render() {
		return <div>{ this.renderNoticeOrButton() }</div>;
	}
}

PluginInstallButton.propTypes = {
	selectedSite: PropTypes.object.isRequired,
	plugin: PropTypes.object.isRequired,
	isEmbed: PropTypes.bool,
	isInstalling: PropTypes.bool,
	disabled: PropTypes.bool,
};

export default connect(
	( state, { selectedSite } ) => {
		const siteId = selectedSite && selectedSite.ID;

		return {
			siteId,
			userId: getCurrentUserId( state ),
			siteIsConnected: getSiteConnectionStatus( state, siteId ),
			siteIsWpcomAtomic: isSiteWpcomAtomic( state, siteId ),
			siteIsJetpackSite: isJetpackSite( state, siteId ),
			canInstallPurchasedPlugins: siteHasFeature(
				state,
				siteId,
				WPCOM_FEATURES_INSTALL_PURCHASED_PLUGINS
			),
			canInstallPlugins: siteHasFeature( state, siteId, WPCOM_FEATURES_INSTALL_PLUGINS ),
			productsList: getProductsList( state ),
		};
	},
	{
		installPlugin,
		removePluginStatuses,
		recordGoogleEvent,
		recordTracksEvent,
	}
)( localize( PluginInstallButton ) );