Completed
Push — e2e/e2e-page-actions ( 25d653...1f4d85 )
by
unknown
17:41 queued 06:47
created

Jetpack_Connection_Banner::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
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 ( Jetpack::state( 'network_nag' ) ) {
116
			add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) );
117
		}
118
119
		// Only fires immediately after plugin activation
120
		if ( get_transient( 'activated_jetpack' ) ) {
121
			add_action( 'admin_notices', array( $this, 'render_connect_prompt_full_screen' ) );
122
			delete_transient( 'activated_jetpack' );
123
		}
124
	}
125
126
	/**
127
	 * Enqueues JavaScript for new connection banner.
128
	 *
129
	 * @since 4.4.0
130
	 */
131 View Code Duplication
	public static function enqueue_banner_scripts() {
132
		wp_enqueue_script(
133
			'jetpack-connection-banner-js',
134
			Assets::get_file_url_for_environment(
135
				'_inc/build/jetpack-connection-banner.min.js',
136
				'_inc/jetpack-connection-banner.js'
137
			),
138
			array( 'jquery' ),
139
			JETPACK__VERSION,
140
			true
141
		);
142
143
		wp_localize_script(
144
			'jetpack-connection-banner-js',
145
			'jp_banner',
146
			array(
147
				'ajax_url'              => admin_url( 'admin-ajax.php' ),
148
				'connectionBannerNonce' => wp_create_nonce( 'jp-connection-banner-nonce' ),
149
			)
150
		);
151
	}
152
153
	/**
154
	 * Enqueues JavaScript and CSS for new connect-in-place flow.
155
	 *
156
	 * @since 7.7
157
	 */
158
	public static function enqueue_connect_button_scripts() {
159
		global $is_safari;
160
161
		wp_enqueue_script(
162
			'jetpack-connect-button',
163
			Assets::get_file_url_for_environment(
164
				'_inc/build/connect-button.min.js',
165
				'_inc/connect-button.js'
166
			),
167
			array( 'jquery' ),
168
			JETPACK__VERSION,
169
			true
170
		);
171
172
		wp_enqueue_style(
173
			'jetpack-connect-button',
174
			Assets::get_file_url_for_environment(
175
				'css/jetpack-connect.min.css',
176
				'css/jetpack-connect.css'
177
			)
178
		);
179
180
		$jetpackApiUrl = wp_parse_url( Jetpack::connection()->api_url( '' ) );
181
182
		// Due to the limitation in how 3rd party cookies are handled in Safari and Opera,
183
		// we're falling back to the original flow.
184
		if ( $is_safari || User_Agent_Info::is_opera_desktop() || Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' ) ) {
185
			$force_variation = 'original';
186
		} else {
187
			$force_variation = 'in_place';
188
		}
189
190
		$tracking = new Automattic\Jetpack\Tracking();
191
		$identity = $tracking->tracks_get_identity( get_current_user_id() );
192
193
		wp_localize_script(
194
			'jetpack-connect-button',
195
			'jpConnect',
196
			array(
197
				'apiBaseUrl'            => esc_url_raw( rest_url( 'jetpack/v4' ) ),
198
				'registrationNonce'     => wp_create_nonce( 'jetpack-registration-nonce' ),
199
				'apiNonce'              => wp_create_nonce( 'wp_rest' ),
200
				'apiSiteDataNonce'      => wp_create_nonce( 'wp_rest' ),
201
				'buttonTextRegistering' => __( 'Loading...', 'jetpack' ),
202
				'jetpackApiDomain'      => $jetpackApiUrl['scheme'] . '://' . $jetpackApiUrl['host'],
203
				'forceVariation'        => $force_variation,
204
				'connectInPlaceUrl'     => Jetpack::admin_url( 'page=jetpack#/setup' ),
205
				'dashboardUrl'          => Jetpack::admin_url( 'page=jetpack#/dashboard' ),
206
				'plansPromptUrl'        => Redirect::get_url( 'jetpack-connect-plans' ),
207
				'identity'              => $identity,
208
				'preFetchScript'        => plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ) . '?ver=' . JETPACK__VERSION,
209
			)
210
		);
211
	}
212
213
	/**
214
	 * Renders the new connection banner as of 4.4.0.
215
	 *
216
	 * @since 7.2   Copy and visual elements reduced to show the new focus of Jetpack on Security and Performance.
217
	 * @since 4.4.0
218
	 */
219
	public function render_banner() {
220
		?>
221
		<div id="message" class="updated jp-wpcom-connect__container">
222
			<div class="jp-wpcom-connect__container-top-text">
223
				<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>
224
				<span>
225
					<?php esc_html_e( 'You’re almost done. Set up Jetpack to enable powerful security and performance tools for WordPress.', 'jetpack' ); ?>
226
				</span>
227
			</div>
228
			<div class="jp-wpcom-connect__inner-container">
229
230
				<?php
231
				if ( ! $this->force_display() ) :
232
					?>
233
234
					<span
235
						class="notice-dismiss connection-banner-dismiss"
236
						title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
237
					</span>
238
239
					<?php
240
				endif;
241
				?>
242
243
				<div class="jp-wpcom-connect__content-container">
244
245
					<!-- slide 1: intro -->
246
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
247
248
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
249
							<?php
250
							$logo = new Logo();
251
							echo $logo->render();
252
							?>
253
							<img
254
								src="<?php echo plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ); ?>"
255
								class="jp-wpcom-connect__hide-phone-and-smaller"
256
								alt="
257
								<?php
258
								esc_attr_e(
259
									'Jetpack premium services offer even more powerful performance, security, ' .
260
									'and revenue tools to help you keep your site safe, fast, and help generate income.',
261
									'jetpack'
262
								);
263
								?>
264
								"
265
								height="auto"
266
								width="225"
267
								/>
268
						</div>
269
270
						<div class="jp-wpcom-connect__slide-text">
271
							<h2><?php esc_html_e( 'Simplify your site security and performance with Jetpack', 'jetpack' ); ?></h2>
272
273
							<p>
274
								<?php
275
								esc_html_e(
276
									'Jetpack protects you against brute force attacks and unauthorized logins. Basic protection ' .
277
									'is always free, while premium plans add unlimited backups of your whole site, spam protection, ' .
278
									'malware scanning, and automated fixes.',
279
									'jetpack'
280
								);
281
								?>
282
							</p>
283
284
							<p>
285
								<?php
286
								esc_html_e(
287
									'Activate site accelerator tools and watch your page load times decrease—we’ll ' .
288
									'optimize your images and serve them from our own powerful global network of servers, ' .
289
									'and speed up your mobile site to reduce bandwidth usage.',
290
									'jetpack'
291
								);
292
								?>
293
							</p>
294
295
							<div class="jp-banner__button-container">
296
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
297
								<a
298
										href="<?php echo esc_url( $this->build_connect_url_for_slide( '72' ) ); ?>"
299
										class="dops-button is-primary jp-banner__alt-connect-button">
300
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
301
								</a>
302
							</div>
303
304
						</div>
305
					</div> <!-- end slide 1 -->
306
				</div>
307
			</div>
308
		</div>
309
		<?php
310
	}
311
312
	/**
313
	 * Renders the license-away version of the connection banner.
314
	 *
315
	 * @since 9.0.0
316
	 */
317
	public function render_license_aware_banner() {
318
		?>
319
		<div id="message" class="updated jp-wpcom-connect__container">
320
			<div class="jp-wpcom-connect__inner-container">
321
				<div class="jp-wpcom-connect__content-container">
322
					<!-- slide 1: intro -->
323
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
324
325
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
326
							<?php echo ( new Logo() )->render(); ?>
327
							<img
328
								src="<?php echo esc_url( plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ) ); ?>"
329
								class="jp-wpcom-connect__hide-phone-and-smaller"
330
								alt="
331
								<?php
332
								esc_attr_e(
333
									'Jetpack premium services offer even more powerful performance, security, and revenue tools to help you keep your site safe, fast, and help generate income.',
334
									'jetpack'
335
								);
336
								?>
337
								"
338
								height="auto"
339
								width="225"
340
								/>
341
						</div>
342
343
						<div class="jp-wpcom-connect__slide-text">
344
							<h2 class="jp-wpcom-connect__quest">
345
								<svg class="gridicon gridicons-notice jp-wpcom-connect__quest-marker" height="38" width="38" viewBox="0 0 24 24">
346
									<g>
347
										<rect x="8" y="6" width="8" height="12" style="fill:#000000" />
348
										<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>
349
									</g>
350
								</svg>
351
								<?php esc_html_e( 'Your Jetpack purchase needs completion! Please set up the plugin for your subscription.', 'jetpack' ); ?>
352
							</h2>
353
354
							<p>
355
								<?php
356
								esc_html_e(
357
									'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!',
358
									'jetpack'
359
								);
360
								?>
361
							</p>
362
363
							<div class="jp-banner__button-container">
364
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
365
								<a
366
									href="<?php echo esc_url( $this->build_connect_url_for_slide( '90' ) ); ?>"
367
									class="dops-button is-primary jp-banner__alt-connect-button">
368
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
369
								</a>
370
							</div>
371
372
						</div>
373
					</div> <!-- end slide 1 -->
374
				</div>
375
			</div>
376
		</div>
377
		<?php
378
	}
379
380
	/**
381
	 * Renders the full-screen connection prompt.  Only shown once and on plugin activation.
382
	 */
383
	public static function render_connect_prompt_full_screen() {
384
		$current_screen = get_current_screen();
385
		if ( 'plugins' === $current_screen->base ) {
386
			$bottom_connect_url_from = 'full-screen-prompt';
387
		} else {
388
			$bottom_connect_url_from = 'landing-page-bottom';
389
		}
390
391
		$is_no_user_testing_mode = ( new Status() )->is_no_user_testing_mode();
392
		?>
393
		<div class="jp-connect-full__container <?php echo $is_no_user_testing_mode ? 'jp-jetpack-connect__userless' : ''; ?>"><div class="jp-connect-full__container-card">
394
395
				<?php if ( 'plugins' === $current_screen->base ) : ?>
396
					<?php
397
					$logo = new Logo();
398
					echo $logo->render();
399
					?>
400
401
					<?php
402
					if ( ! self::force_display() ) :
403
						?>
404
405
						<div class="jp-connect-full__dismiss">
406
							<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>
407
						</div>
408
409
						<?php
410
					endif;
411
					?>
412
413
				<?php endif; ?>
414
415
				<div class="jp-connect-full__step-header">
416
					<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>
417
				</div>
418
419
				<p class="jp-connect-full__tos-blurb">
420
					<?php jetpack_render_tos_blurb(); ?>
421
				</p>
422
423
				<p class="jp-connect-full__button-container">
424
					<a href="<?php echo esc_url( Jetpack::init()->build_connect_url( true, false, $bottom_connect_url_from ) ); ?>"
425
					   class="dops-button is-primary jp-connect-button">
426
						<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
427
					</a>
428
				</p>
429
430
				<?php if ( $is_no_user_testing_mode ) : ?>
431
					<div id="jp-authenticate-no_user_test_mode">
432
						<h2><?php esc_html_e( 'Or connect without an account', 'jetpack' ); ?></h2>
433
						<p><?php esc_html_e( 'Jump in to enjoy Jetpack right away. Some features will not be immediately available, but you will be able to connect your account later to unlock them.', 'jetpack' ); ?></p>
434
						<a class="dops-button jp-no-user-mode-button" href="<?php echo esc_url( Redirect::get_url( 'jetpack-connect-plans', array( 'unlinked' => '1' ) ) ); ?>"><?php esc_html_e( 'Continue without signing in', 'jetpack' ); ?></a>
435
						<a class="jp-no-user-all-features" target="_blank" href="https://jetpack.com/support/features/">
436
							<?php esc_html_e( 'See all Jetpack features', 'jetpack' ); ?>
437
							<svg width="16" height="16" viewBox="0 0 24 24" class="gridicon gridicons-external">
438
								<g><path d="M19 13v6c0 1.105-.895 2-2 2H5c-1.105 0-2-.895-2-2V7c0-1.105.895-2 2-2h6v2H5v12h12v-6h2zM13 3v2h4.586l-7.793 7.793 1.414 1.414L19 6.414V11h2V3h-8z"></path></g>
439
							</svg>
440
						</a>
441
					</div>
442
				<?php endif; ?>
443
444
				<div class="jp-connect-full__row" id="jetpack-connection-cards">
445
					<div class="jp-connect-full__slide">
446
						<div class="jp-connect-full__slide-card illustration">
447
							<img
448
									src="<?php echo plugins_url( 'images/jetpack-connection-security.svg', JETPACK__PLUGIN_FILE ); ?>"
449
									alt="<?php esc_attr_e( 'Security & Backups', 'jetpack' ); ?>"
450
							/>
451
						</div>
452
						<div class="jp-connect-full__slide-card">
453
							<h3><?php esc_html_e( 'Always-on Security', 'jetpack' ); ?></h3>
454
							<ul>
455
								<li><?php esc_html_e( 'Stay one step ahead of security threats with automatic scanning, one-click fixes, and spam protection.', 'jetpack' ); ?></li>
456
								<li><?php esc_html_e( 'Real-time backups save every change and one-click restores get you back online quickly.', 'jetpack' ); ?></li>
457
								<li><?php esc_html_e( 'Free protection against brute force attacks and instant notifications if your site goes down.', 'jetpack' ); ?></li>
458
							</ul>
459
						</div>
460
					</div>
461
					<div class="jp-connect-full__slide">
462
						<div class="jp-connect-full__slide-card illustration">
463
							<img
464
									src="<?php echo plugins_url( 'images/jetpack-connection-performance.svg', JETPACK__PLUGIN_FILE ); ?>"
465
									alt="<?php esc_attr_e( 'Built-in Performance', 'jetpack' ); ?>"
466
							/>
467
						</div>
468
						<div class="jp-connect-full__slide-card">
469
							<h3><?php esc_html_e( 'Built-in Performance', 'jetpack' ); ?></h3>
470
							<ul>
471
								<li><?php esc_html_e( 'Keep people on your site longer with lightning-fast page load times through our free global CDN.', 'jetpack' ); ?></li>
472
								<li><?php esc_html_e( 'Speed up your mobile site and reduce bandwidth usage automatically.', 'jetpack' ); ?></li>
473
								<li><?php esc_html_e( 'Improve visitor engagement and sales with a customized search experience.', 'jetpack' ); ?></li>
474
							</ul>
475
						</div>
476
					</div>
477
				</div>
478
479
				<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>
480
481
				<?php if ( 'plugins' === $current_screen->base ) : ?>
482
483
					<?php
484
					if ( ! self::force_display() ) :
485
						?>
486
487
						<p class="jp-connect-full__dismiss-paragraph">
488
							<a>
489
								<?php
490
								echo esc_html_x(
491
									'Not now, thank you.',
492
									'a link that closes the modal window that offers to connect Jetpack',
493
									'jetpack'
494
								);
495
								?>
496
							</a>
497
						</p>
498
499
						<?php
500
						endif;
501
					?>
502
503
				<?php endif; ?>
504
			</div>
505
		</div>
506
		<?php
507
	}
508
509
	/**
510
	 * Renders the legacy network connection banner.
511
	 */
512
	function network_connect_notice() {
513
		?>
514
		<div id="message" class="updated jetpack-message">
515
			<div class="squeezer">
516
				<h2>
517
					<?php
518
						echo wp_kses(
519
							__(
520
								'<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.',
521
								'jetpack'
522
							),
523
							array( 'strong' => array() )
524
						);
525
					?>
526
				</h2>
527
			</div>
528
		</div>
529
		<?php
530
	}
531
}
532