Completed
Push — story-block/add/more-media-opt... ( 53fda1...42cfc5 )
by
unknown
107:17 queued 96:49
created

maybe_initialize_hooks()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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