Completed
Push — fix/mailchimp-styling-for-fse ( ea89d1...c645b0 )
by
unknown
20:37 queued 09:41
created

Jetpack_Connection_Banner::can_be_displayed()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

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