Completed
Push — add/deactivation-dismiss-modal ( 423334...745af2 )
by Jeremy
185:13 queued 175:17
created

deactivate_dialog_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Assets;
4
use Automattic\Jetpack\Assets\Logo;
5
use Automattic\Jetpack\Constants;
6
use Automattic\Jetpack\Device_Detection\User_Agent_Info;
7
use Automattic\Jetpack\Licensing;
8
use Automattic\Jetpack\Redirect;
9
use Automattic\Jetpack\Status;
10
11
class Jetpack_Connection_Banner {
12
	/**
13
	 * @var Jetpack_Connection_Banner
14
	 **/
15
	private static $instance = null;
16
17
	static function init() {
18
		if ( is_null( self::$instance ) ) {
19
			self::$instance = new Jetpack_Connection_Banner();
20
		}
21
22
		return self::$instance;
23
	}
24
25
	/**
26
	 * Jetpack_Connection_Banner constructor.
27
	 *
28
	 * Since we call the Jetpack_Connection_Banner:init() method from the `Jetpack` class, and after
29
	 * the admin_init action fires, we know that the admin is initialized at this point.
30
	 */
31
	private function __construct() {
32
		add_action( 'current_screen', array( $this, 'maybe_initialize_hooks' ) );
33
	}
34
35
	/**
36
	 * The banner is forcibly displayed.
37
	 *
38
	 * @return bool
39
	 */
40
	public static function force_display() {
41
		/**
42
		 * This is an experiment for partners to test. Allow customization of the behavior of pre-connection banners.
43
		 *
44
		 * @since 8.6.0
45
		 *
46
		 * @param bool $always_show_prompt Should this prompt always appear? Default to false.
47
		 */
48
		return apply_filters( 'jetpack_pre_connection_prompt_helpers', false );
49
	}
50
51
	/**
52
	 * Given a string for the the banner was added, and an int that represents the slide to
53
	 * a URL for, this function returns a connection URL with a from parameter that will
54
	 * support split testing.
55
	 *
56
	 * @since 7.2   Event key format is now banner-connect-banner-72-dashboard or connect-banner-72-plugins.
57
	 *              The param $slide_num was removed since we removed all slides but the first one.
58
	 * @since 4.4.0
59
	 *
60
	 * @param string $jp_version_banner_added A short version of when the banner was added. Ex. 44
61
	 *
62
	 * @return string
63
	 */
64
	function build_connect_url_for_slide( $jp_version_banner_added ) {
65
		global $current_screen;
66
		$url = Jetpack::init()->build_connect_url(
67
			true,
68
			false,
69
			sprintf( 'connect-banner-%s-%s', $jp_version_banner_added, $current_screen->base )
70
		);
71
		return add_query_arg( 'auth_approved', 'true', $url );
72
	}
73
74
	/**
75
	 * Will initialize hooks to display the new (as of 4.4) connection banner if the current user can
76
	 * connect Jetpack, if Jetpack has not been deactivated, and if the current page is the plugins page.
77
	 *
78
	 * This method should not be called if the site is connected to WordPress.com or if the site is in offline mode.
79
	 *
80
	 * @since 4.4.0
81
	 * @since 4.5.0 Made the new (as of 4.4) connection banner display to everyone by default.
82
	 * @since 5.3.0 Running another split test between 4.4 banner and a new one in 5.3.
83
	 * @since 7.2   B test was removed.
84
	 *
85
	 * @param $current_screen
86
	 */
87
	function maybe_initialize_hooks( $current_screen ) {
88
89
		// Kill if banner has been dismissed and the pre-connection helpers filter is not set.
90
		if (
91
			Jetpack_Options::get_option( 'dismissed_connection_banner' ) &&
92
			! $this->force_display()
93
		) {
94
			return;
95
		}
96
97
		// Don't show the connect notice anywhere but the plugins.php after activating
98
		if ( 'plugins' !== $current_screen->base && 'dashboard' !== $current_screen->base ) {
99
			return;
100
		}
101
102
		if ( ! current_user_can( 'jetpack_connect' ) ) {
103
			return;
104
		}
105
106
		if ( ! empty( Licensing::instance()->stored_licenses() ) ) {
107
			add_action( 'admin_notices', array( $this, 'render_license_aware_banner' ) );
108
		} else {
109
			add_action( 'admin_notices', array( $this, 'render_banner' ) );
110
		}
111
112
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) );
113
		add_action( 'admin_print_styles', array( Jetpack::init(), 'admin_banner_styles' ) );
114
115
		if ( 'plugins' === $current_screen->base ) {
116
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_deactivation_modal' ) );
117
		}
118
119
		if ( Jetpack::state( 'network_nag' ) ) {
120
			add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) );
121
		}
122
123
		// Only fires immediately after plugin activation.
124
		if ( get_transient( 'activated_jetpack' ) ) {
125
			add_action( 'admin_notices', array( $this, 'render_connect_prompt_full_screen' ) );
126
			delete_transient( 'activated_jetpack' );
127
		}
128
	}
129
130
	/**
131
	 * Enqueues JavaScript for new connection banner.
132
	 *
133
	 * @since 4.4.0
134
	 */
135
	public static function enqueue_banner_scripts() {
136
		wp_enqueue_script(
137
			'jetpack-connection-banner-js',
138
			Assets::get_file_url_for_environment(
139
				'_inc/build/jetpack-connection-banner.min.js',
140
				'_inc/jetpack-connection-banner.js'
141
			),
142
			array( 'jquery' ),
143
			JETPACK__VERSION,
144
			true
145
		);
146
147
		wp_localize_script(
148
			'jetpack-connection-banner-js',
149
			'jp_banner',
150
			array(
151
				'ajax_url'              => admin_url( 'admin-ajax.php' ),
152
				'connectionBannerNonce' => wp_create_nonce( 'jp-connection-banner-nonce' ),
153
				'deactivate_title'      => __( 'Deactivate Jetpack', 'jetpack' ),
154
			)
155
		);
156
	}
157
158
	/**
159
	 * Enqueue elements for the Deactivation dialog
160
	 * (appearing when hitting dismiss in the connection banner).
161
	 *
162
	 * @since 9.7.0
163
	 */
164
	public function enqueue_deactivation_modal() {
165
		add_thickbox();
166
167
		add_action( 'admin_footer', array( $this, 'deactivate_dialog_content' ) );
168
169
		wp_enqueue_style(
170
			'jetpack-deactivate-dialog',
171
			plugins_url( 'css/jetpack-deactivate-dialog.css', JETPACK__PLUGIN_FILE ),
172
			array(),
173
			JETPACK__VERSION
174
		);
175
	}
176
177
	/**
178
	 * Enqueues JavaScript and CSS for new connect-in-place flow.
179
	 *
180
	 * @since 7.7
181
	 */
182
	public static function enqueue_connect_button_scripts() {
183
		global $is_safari;
184
185
		wp_enqueue_script(
186
			'jetpack-connect-button',
187
			Assets::get_file_url_for_environment(
188
				'_inc/build/connect-button.min.js',
189
				'_inc/connect-button.js'
190
			),
191
			array( 'jquery' ),
192
			JETPACK__VERSION,
193
			true
194
		);
195
196
		wp_enqueue_style(
197
			'jetpack-connect-button',
198
			Assets::get_file_url_for_environment(
199
				'css/jetpack-connect.min.css',
200
				'css/jetpack-connect.css'
201
			)
202
		);
203
204
		$jetpackApiUrl = wp_parse_url( Jetpack::connection()->api_url( '' ) );
205
206
		// Due to the limitation in how 3rd party cookies are handled in Safari and Opera,
207
		// we're falling back to the original flow.
208
		if ( $is_safari || User_Agent_Info::is_opera_desktop() || Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' ) ) {
209
			$force_variation = 'original';
210
		} else {
211
			$force_variation = 'in_place';
212
		}
213
214
		$tracking = new Automattic\Jetpack\Tracking();
215
		$identity = $tracking->tracks_get_identity( get_current_user_id() );
216
217
		wp_localize_script(
218
			'jetpack-connect-button',
219
			'jpConnect',
220
			array(
221
				'apiBaseUrl'            => esc_url_raw( rest_url( 'jetpack/v4' ) ),
222
				'registrationNonce'     => wp_create_nonce( 'jetpack-registration-nonce' ),
223
				'apiNonce'              => wp_create_nonce( 'wp_rest' ),
224
				'apiSiteDataNonce'      => wp_create_nonce( 'wp_rest' ),
225
				'buttonTextRegistering' => __( 'Loading...', 'jetpack' ),
226
				'jetpackApiDomain'      => $jetpackApiUrl['scheme'] . '://' . $jetpackApiUrl['host'],
227
				'forceVariation'        => $force_variation,
228
				'connectInPlaceUrl'     => Jetpack::admin_url( 'page=jetpack#/setup' ),
229
				'dashboardUrl'          => Jetpack::admin_url( 'page=jetpack#/dashboard' ),
230
				'plansPromptUrl'        => Redirect::get_url( 'jetpack-connect-plans' ),
231
				'identity'              => $identity,
232
				'preFetchScript'        => plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ) . '?ver=' . JETPACK__VERSION,
233
				'isUserless'            => ( new Status() )->is_no_user_testing_mode() && ! Jetpack::connection()->has_connected_owner(),
234
			)
235
		);
236
	}
237
238
	/**
239
	 * Renders the new connection banner as of 4.4.0.
240
	 *
241
	 * @since 7.2   Copy and visual elements reduced to show the new focus of Jetpack on Security and Performance.
242
	 * @since 4.4.0
243
	 */
244
	public function render_banner() {
245
		?>
246
		<div id="message" class="updated jp-wpcom-connect__container">
247
			<div class="jp-wpcom-connect__container-top-text">
248
				<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="0" fill="none" width="24" height="24"/><g><path d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm1 15h-2v-2h2v2zm0-4h-2l-.5-6h3l-.5 6z"/></g></svg>
249
				<span>
250
					<?php esc_html_e( 'You’re almost done. Set up Jetpack to enable powerful security and performance tools for WordPress.', 'jetpack' ); ?>
251
				</span>
252
			</div>
253
			<div class="jp-wpcom-connect__inner-container">
254
255
				<?php
256
				if ( ! $this->force_display() ) :
257
					?>
258
259
					<a
260
						class="notice-dismiss connection-banner-dismiss"
261
						title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
262
					</a>
263
264
					<?php
265
				endif;
266
				?>
267
268
				<div class="jp-wpcom-connect__content-container">
269
270
					<!-- slide 1: intro -->
271
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
272
273
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
274
							<?php
275
							$logo = new Logo();
276
							echo $logo->render();
277
							?>
278
							<img
279
								src="<?php echo plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ); ?>"
280
								class="jp-wpcom-connect__hide-phone-and-smaller"
281
								alt="
282
								<?php
283
								esc_attr_e(
284
									'Jetpack premium services offer even more powerful performance, security, ' .
285
									'and revenue tools to help you keep your site safe, fast, and help generate income.',
286
									'jetpack'
287
								);
288
								?>
289
								"
290
								height="auto"
291
								width="225"
292
								/>
293
						</div>
294
295
						<div class="jp-wpcom-connect__slide-text">
296
							<h2><?php esc_html_e( 'Simplify your site security and performance with Jetpack', 'jetpack' ); ?></h2>
297
298
							<p>
299
								<?php
300
								esc_html_e(
301
									'Jetpack protects you against brute force attacks and unauthorized logins. Basic protection ' .
302
									'is always free, while premium plans add unlimited backups of your whole site, spam protection, ' .
303
									'malware scanning, and automated fixes.',
304
									'jetpack'
305
								);
306
								?>
307
							</p>
308
309
							<p>
310
								<?php
311
								esc_html_e(
312
									'Activate site accelerator tools and watch your page load times decrease—we’ll ' .
313
									'optimize your images and serve them from our own powerful global network of servers, ' .
314
									'and speed up your mobile site to reduce bandwidth usage.',
315
									'jetpack'
316
								);
317
								?>
318
							</p>
319
320
							<div class="jp-banner__button-container">
321
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
322
								<a
323
										href="<?php echo esc_url( $this->build_connect_url_for_slide( '72' ) ); ?>"
324
										class="dops-button is-primary jp-banner__alt-connect-button">
325
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
326
								</a>
327
							</div>
328
329
						</div>
330
					</div> <!-- end slide 1 -->
331
				</div>
332
			</div>
333
		</div>
334
		<?php
335
	}
336
337
	/**
338
	 * Renders the license-away version of the connection banner.
339
	 *
340
	 * @since 9.0.0
341
	 */
342
	public function render_license_aware_banner() {
343
		?>
344
		<div id="message" class="updated jp-wpcom-connect__container">
345
			<div class="jp-wpcom-connect__inner-container">
346
				<div class="jp-wpcom-connect__content-container">
347
					<!-- slide 1: intro -->
348
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
349
350
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
351
							<?php echo ( new Logo() )->render(); ?>
352
							<img
353
								src="<?php echo esc_url( plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ) ); ?>"
354
								class="jp-wpcom-connect__hide-phone-and-smaller"
355
								alt="
356
								<?php
357
								esc_attr_e(
358
									'Jetpack premium services offer even more powerful performance, security, and revenue tools to help you keep your site safe, fast, and help generate income.',
359
									'jetpack'
360
								);
361
								?>
362
								"
363
								height="auto"
364
								width="225"
365
								/>
366
						</div>
367
368
						<div class="jp-wpcom-connect__slide-text">
369
							<h2 class="jp-wpcom-connect__quest">
370
								<svg class="gridicon gridicons-notice jp-wpcom-connect__quest-marker" height="38" width="38" viewBox="0 0 24 24">
371
									<g>
372
										<rect x="8" y="6" width="8" height="12" style="fill:#000000" />
373
										<path d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm1 15h-2v-2h2v2zm0-4h-2l-.5-6h3l-.5 6z"></path>
374
									</g>
375
								</svg>
376
								<?php esc_html_e( 'Your Jetpack purchase needs completion! Please set up the plugin for your subscription.', 'jetpack' ); ?>
377
							</h2>
378
379
							<p>
380
								<?php
381
								esc_html_e(
382
									'Jetpack offers security, performance, and marketing tools made for WordPress sites by the WordPress experts. Set up Jetpack to enable new features for this site; don\'t let your subscription go to waste!',
383
									'jetpack'
384
								);
385
								?>
386
							</p>
387
388
							<div class="jp-banner__button-container">
389
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
390
								<a
391
									href="<?php echo esc_url( $this->build_connect_url_for_slide( '90' ) ); ?>"
392
									class="dops-button is-primary jp-banner__alt-connect-button">
393
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
394
								</a>
395
							</div>
396
397
						</div>
398
					</div> <!-- end slide 1 -->
399
				</div>
400
			</div>
401
		</div>
402
		<?php
403
	}
404
405
	/**
406
	 * Renders the full-screen connection prompt.  Only shown once and on plugin activation.
407
	 */
408
	public static function render_connect_prompt_full_screen() {
409
		$current_screen = get_current_screen();
410
		if ( 'plugins' === $current_screen->base ) {
411
			$bottom_connect_url_from = 'full-screen-prompt';
412
		} else {
413
			$bottom_connect_url_from = 'landing-page-bottom';
414
		}
415
416
		$is_no_user_testing_mode = ( new Status() )->is_no_user_testing_mode() && ! Jetpack::connection()->has_connected_owner();
417
		?>
418
		<div class="jp-connect-full__container <?php echo $is_no_user_testing_mode ? 'jp-jetpack-connect__userless' : ''; ?>"><div class="jp-connect-full__container-card">
419
420
				<?php if ( 'plugins' === $current_screen->base ) : ?>
421
					<?php
422
					$logo = new Logo();
423
					echo $logo->render();
424
					?>
425
426
					<?php
427
					if ( ! self::force_display() ) :
428
						?>
429
430
						<div class="jp-connect-full__dismiss">
431
							<svg class="jp-connect-full__svg-dismiss" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><title>Dismiss Jetpack Connection Window</title><rect x="0" fill="none" /><g><path d="M17.705 7.705l-1.41-1.41L12 10.59 7.705 6.295l-1.41 1.41L10.59 12l-4.295 4.295 1.41 1.41L12 13.41l4.295 4.295 1.41-1.41L13.41 12l4.295-4.295z"/></g></svg>
432
						</div>
433
434
						<?php
435
					endif;
436
					?>
437
438
				<?php endif; ?>
439
440
				<div id="jp-connect-full__step1-header" class="jp-connect-full__step-header">
441
					<h2 class="jp-connect-full__step-header-title"><?php esc_html_e( 'Activate essential WordPress security and performance tools by setting up Jetpack', 'jetpack' ); ?></h2>
442
				</div>
443
444
				<div id="jp-connect-full__step2-header" class="jp-connect-full__step-header">
445
					<h2 class="jp-connect-full__step-header-title"><?php esc_html_e( 'Jetpack is activated!', 'jetpack' ); ?><br /><?php esc_html_e( 'Unlock more amazing features by connecting a user account', 'jetpack' ); ?></h2>
446
				</div>
447
448
				<p class="jp-connect-full__tos-blurb">
449
					<?php jetpack_render_tos_blurb(); ?>
450
				</p>
451
452
				<p class="jp-connect-full__button-container">
453
					<a href="<?php echo esc_url( Jetpack::init()->build_connect_url( true, false, $bottom_connect_url_from ) ); ?>"
454
					   class="dops-button is-primary jp-connect-button">
455
						<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
456
					</a>
457
				</p>
458
459
				<div class="jp-connect-full__row" id="jetpack-connection-cards">
460
					<div class="jp-connect-full__slide">
461
						<div class="jp-connect-full__slide-card illustration">
462
							<img
463
									src="<?php echo plugins_url( 'images/jetpack-connection-security.svg', JETPACK__PLUGIN_FILE ); ?>"
464
									alt="<?php esc_attr_e( 'Security & Backups', 'jetpack' ); ?>"
465
							/>
466
						</div>
467
						<div class="jp-connect-full__slide-card">
468
							<h3><?php esc_html_e( 'Always-on Security', 'jetpack' ); ?></h3>
469
							<ul>
470
								<li><?php esc_html_e( 'Stay one step ahead of security threats with automatic scanning, one-click fixes, and spam protection.', 'jetpack' ); ?></li>
471
								<li><?php esc_html_e( 'Real-time backups save every change and one-click restores get you back online quickly.', 'jetpack' ); ?></li>
472
								<li><?php esc_html_e( 'Free protection against brute force attacks and instant notifications if your site goes down.', 'jetpack' ); ?></li>
473
							</ul>
474
						</div>
475
					</div>
476
					<div class="jp-connect-full__slide">
477
						<div class="jp-connect-full__slide-card illustration">
478
							<img
479
									src="<?php echo plugins_url( 'images/jetpack-connection-performance.svg', JETPACK__PLUGIN_FILE ); ?>"
480
									alt="<?php esc_attr_e( 'Built-in Performance', 'jetpack' ); ?>"
481
							/>
482
						</div>
483
						<div class="jp-connect-full__slide-card">
484
							<h3><?php esc_html_e( 'Built-in Performance', 'jetpack' ); ?></h3>
485
							<ul>
486
								<li><?php esc_html_e( 'Keep people on your site longer with lightning-fast page load times through our free global CDN.', 'jetpack' ); ?></li>
487
								<li><?php esc_html_e( 'Speed up your mobile site and reduce bandwidth usage automatically.', 'jetpack' ); ?></li>
488
								<li><?php esc_html_e( 'Improve visitor engagement and sales with a customized search experience.', 'jetpack' ); ?></li>
489
							</ul>
490
						</div>
491
					</div>
492
				</div>
493
494
				<h2 class="jp-connect-full__testimonial"><?php esc_html_e( 'More than 5 million WordPress sites trust Jetpack for their website security and performance.', 'jetpack' ); ?></h2>
495
496
				<?php if ( 'plugins' === $current_screen->base ) : ?>
497
498
					<?php
499
					if ( ! self::force_display() ) :
500
						?>
501
502
						<p class="jp-connect-full__dismiss-paragraph">
503
							<a>
504
								<?php
505
								echo esc_html_x(
506
									'Not now, thank you.',
507
									'a link that closes the modal window that offers to connect Jetpack',
508
									'jetpack'
509
								);
510
								?>
511
							</a>
512
						</p>
513
514
						<?php
515
						endif;
516
					?>
517
518
				<?php endif; ?>
519
			</div>
520
		</div>
521
		<?php
522
	}
523
524
	/**
525
	 * Renders the legacy network connection banner.
526
	 */
527
	function network_connect_notice() {
528
		?>
529
		<div id="message" class="updated jetpack-message">
530
			<div class="squeezer">
531
				<h2>
532
					<?php
533
						echo wp_kses(
534
							__(
535
								'<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.',
536
								'jetpack'
537
							),
538
							array( 'strong' => array() )
539
						);
540
					?>
541
				</h2>
542
			</div>
543
		</div>
544
		<?php
545
	}
546
547
	/**
548
	 * Outputs the content of the deactivation modal.
549
	 * appearing when you click on the Dismiss button in a connection banner.
550
	 *
551
	 * @return void
552
	 */
553
	public function deactivate_dialog_content() {
554
		?>
555
		<div id="jetpack_deactivation_dialog">
556
			<div class="jetpack_deactivation_dialog_content">
557
				<p>
558
					<?php esc_html_e( 'Are you sure you want to deactivate the Jetpack plugin?', 'jetpack' ); ?>
559
					<br/>
560
					<?php esc_html_e( 'You can reactivate later under Plugins > Installed Plugins.', 'jetpack' ); ?>
561
				</p>
562
			</div>
563
564
			<div class="jetpack_deactivation_dialog_content__buttons-row-container">
565
				<div class="jetpack_deactivation_dialog_content__buttons-row">
566
					<div class="jetpack_deactivation_dialog_content__buttons">
567
						<button type="button" id="jetpack_deactivation_dialog_content__button-cancel"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></button>
568
						<button type="button" id="jetpack_deactivation_dialog_content__button-deactivate"><?php esc_html_e( 'Deactivate', 'jetpack' ); ?></button>
569
					</div>
570
				</div>
571
			</div>
572
		</div>
573
		<?php
574
	}
575
}
576