Completed
Push — fix/plans-prompt-url ( 04be99 )
by
unknown
255:28 queued 245:50
created

Jetpack_Connection_Banner   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 557
Duplicated Lines 3.77 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
dl 21
loc 557
rs 9.44
c 0
b 0
f 0
wmc 37
lcom 2
cbo 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A __construct() 0 3 1
A force_display() 0 10 1
B can_be_displayed() 0 36 9
A build_connect_url_for_slide() 0 9 1
A maybe_initialize_hooks() 0 24 5
A enqueue_banner_scripts() 21 21 1
A enqueue_connect_button_scripts() 0 54 4
A get_plans_prompt_url() 0 9 2
B render_banner() 0 92 2
B render_license_aware_banner() 0 62 1
B render_connect_prompt_full_screen() 0 115 7
A network_connect_notice() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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'        => self::get_plans_prompt_url(),
235
				'identity'              => $identity,
236
				'preFetchScript'        => plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ) . '?ver=' . JETPACK__VERSION,
237
			)
238
		);
239
	}
240
241
	/**
242
	 * Gets the Jetpack Redirect url pointing to the Jetpack plans/pricing page.
243
	 *
244
	 * @since 9.8
245
	 *
246
	 * @return string
247
	 */
248
	public function get_plans_prompt_url() {
249
		$query = array();
250
		// If the site's wp-admin url is http only (not https), include a query param indicating this. `&query=https%3Doff`.
251
		// The query param is used on the Calypso plans/pricing page to build the sites's wp-admin url when selecting the Jetapack "Free" plan.
252
		if ( 'http://' === substr( get_site_url(), 0, 7 ) ) {
253
			$query['query'] = 'https=off';
254
		}
255
		return Redirect::get_url( 'jetpack-connect-plans', $query );
256
	}
257
258
	/**
259
	 * Renders the new connection banner as of 4.4.0.
260
	 *
261
	 * @since 7.2   Copy and visual elements reduced to show the new focus of Jetpack on Security and Performance.
262
	 * @since 4.4.0
263
	 */
264
	public function render_banner() {
265
		?>
266
		<div id="message" class="updated jp-wpcom-connect__container">
267
			<div class="jp-wpcom-connect__container-top-text">
268
				<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>
269
				<span>
270
					<?php esc_html_e( 'You’re almost done. Set up Jetpack to enable powerful security and performance tools for WordPress.', 'jetpack' ); ?>
271
				</span>
272
			</div>
273
			<div class="jp-wpcom-connect__inner-container">
274
275
				<?php
276
				if ( ! $this->force_display() ) :
277
					?>
278
279
					<span
280
						class="notice-dismiss connection-banner-dismiss"
281
						title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
282
					</span>
283
284
					<?php
285
				endif;
286
				?>
287
288
				<div class="jp-wpcom-connect__content-container">
289
290
					<!-- slide 1: intro -->
291
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
292
293
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
294
							<?php
295
							$logo = new Logo();
296
							echo $logo->render();
297
							?>
298
							<img
299
								src="<?php echo plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ); ?>"
300
								class="jp-wpcom-connect__hide-phone-and-smaller"
301
								alt="
302
								<?php
303
								esc_attr_e(
304
									'Jetpack premium services offer even more powerful performance, security, ' .
305
									'and revenue tools to help you keep your site safe, fast, and help generate income.',
306
									'jetpack'
307
								);
308
								?>
309
								"
310
								height="auto"
311
								width="225"
312
								/>
313
						</div>
314
315
						<div class="jp-wpcom-connect__slide-text">
316
							<h2><?php esc_html_e( 'Simplify your site security and performance with Jetpack', 'jetpack' ); ?></h2>
317
318
							<p>
319
								<?php
320
								esc_html_e(
321
									'Jetpack protects you against brute force attacks and unauthorized logins. Basic protection ' .
322
									'is always free, while premium plans add unlimited backups of your whole site, spam protection, ' .
323
									'malware scanning, and automated fixes.',
324
									'jetpack'
325
								);
326
								?>
327
							</p>
328
329
							<p>
330
								<?php
331
								esc_html_e(
332
									'Activate site accelerator tools and watch your page load times decrease—we’ll ' .
333
									'optimize your images and serve them from our own powerful global network of servers, ' .
334
									'and speed up your mobile site to reduce bandwidth usage.',
335
									'jetpack'
336
								);
337
								?>
338
							</p>
339
340
							<div class="jp-banner__button-container">
341
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
342
								<a
343
										href="<?php echo esc_url( $this->build_connect_url_for_slide( '72' ) ); ?>"
344
										class="dops-button is-primary jp-banner__alt-connect-button">
345
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
346
								</a>
347
							</div>
348
349
						</div>
350
					</div> <!-- end slide 1 -->
351
				</div>
352
			</div>
353
		</div>
354
		<?php
355
	}
356
357
	/**
358
	 * Renders the license-away version of the connection banner.
359
	 *
360
	 * @since 9.0.0
361
	 */
362
	public function render_license_aware_banner() {
363
		?>
364
		<div id="message" class="updated jp-wpcom-connect__container">
365
			<div class="jp-wpcom-connect__inner-container">
366
				<div class="jp-wpcom-connect__content-container">
367
					<!-- slide 1: intro -->
368
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
369
370
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
371
							<?php echo ( new Logo() )->render(); ?>
372
							<img
373
								src="<?php echo esc_url( plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ) ); ?>"
374
								class="jp-wpcom-connect__hide-phone-and-smaller"
375
								alt="
376
								<?php
377
								esc_attr_e(
378
									'Jetpack premium services offer even more powerful performance, security, and revenue tools to help you keep your site safe, fast, and help generate income.',
379
									'jetpack'
380
								);
381
								?>
382
								"
383
								height="auto"
384
								width="225"
385
								/>
386
						</div>
387
388
						<div class="jp-wpcom-connect__slide-text">
389
							<h2 class="jp-wpcom-connect__quest">
390
								<svg class="gridicon gridicons-notice jp-wpcom-connect__quest-marker" height="38" width="38" viewBox="0 0 24 24">
391
									<g>
392
										<rect x="8" y="6" width="8" height="12" style="fill:#000000" />
393
										<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>
394
									</g>
395
								</svg>
396
								<?php esc_html_e( 'Your Jetpack purchase needs completion! Please set up the plugin for your subscription.', 'jetpack' ); ?>
397
							</h2>
398
399
							<p>
400
								<?php
401
								esc_html_e(
402
									'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!',
403
									'jetpack'
404
								);
405
								?>
406
							</p>
407
408
							<div class="jp-banner__button-container">
409
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
410
								<a
411
									href="<?php echo esc_url( $this->build_connect_url_for_slide( '90' ) ); ?>"
412
									class="dops-button is-primary jp-banner__alt-connect-button">
413
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
414
								</a>
415
							</div>
416
417
						</div>
418
					</div> <!-- end slide 1 -->
419
				</div>
420
			</div>
421
		</div>
422
		<?php
423
	}
424
425
	/**
426
	 * Renders the full-screen connection prompt.  Only shown once and on plugin activation.
427
	 */
428
	public static function render_connect_prompt_full_screen() {
429
		$current_screen = get_current_screen();
430
		if ( 'plugins' === $current_screen->base ) {
431
			$bottom_connect_url_from = 'full-screen-prompt';
432
		} else {
433
			$bottom_connect_url_from = 'landing-page-bottom';
434
		}
435
436
		$has_no_owner = ! Jetpack::connection()->has_connected_owner();
437
		?>
438
		<div class="jp-connect-full__container <?php echo $has_no_owner ? 'jp-jetpack-connect__userless' : ''; ?>"><div class="jp-connect-full__container-card">
439
440
				<?php if ( 'plugins' === $current_screen->base ) : ?>
441
					<?php
442
					$logo = new Logo();
443
					echo $logo->render();
444
					?>
445
446
					<?php
447
					if ( ! self::force_display() ) :
448
						?>
449
450
						<div class="jp-connect-full__dismiss">
451
							<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>
452
						</div>
453
454
						<?php
455
					endif;
456
					?>
457
458
				<?php endif; ?>
459
460
				<div id="jp-connect-full__step1-header" class="jp-connect-full__step-header">
461
					<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>
462
				</div>
463
464
				<div id="jp-connect-full__step2-header" class="jp-connect-full__step-header">
465
					<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>
466
				</div>
467
468
				<p class="jp-connect-full__tos-blurb">
469
					<?php jetpack_render_tos_blurb(); ?>
470
				</p>
471
472
				<p class="jp-connect-full__button-container">
473
					<a href="<?php echo esc_url( Jetpack::init()->build_connect_url( true, false, $bottom_connect_url_from ) ); ?>"
474
					   class="dops-button is-primary jp-connect-button">
475
						<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
476
					</a>
477
				</p>
478
479
				<div class="jp-connect-full__row" id="jetpack-connection-cards">
480
					<div class="jp-connect-full__slide">
481
						<div class="jp-connect-full__slide-card illustration">
482
							<img
483
									src="<?php echo plugins_url( 'images/jetpack-connection-security.svg', JETPACK__PLUGIN_FILE ); ?>"
484
									alt="<?php esc_attr_e( 'Security & Backups', 'jetpack' ); ?>"
485
							/>
486
						</div>
487
						<div class="jp-connect-full__slide-card">
488
							<h3><?php esc_html_e( 'Always-on Security', 'jetpack' ); ?></h3>
489
							<ul>
490
								<li><?php esc_html_e( 'Stay one step ahead of security threats with automatic scanning, one-click fixes, and spam protection.', 'jetpack' ); ?></li>
491
								<li><?php esc_html_e( 'Real-time backups save every change and one-click restores get you back online quickly.', 'jetpack' ); ?></li>
492
								<li><?php esc_html_e( 'Free protection against brute force attacks and instant notifications if your site goes down.', 'jetpack' ); ?></li>
493
							</ul>
494
						</div>
495
					</div>
496
					<div class="jp-connect-full__slide">
497
						<div class="jp-connect-full__slide-card illustration">
498
							<img
499
									src="<?php echo plugins_url( 'images/jetpack-connection-performance.svg', JETPACK__PLUGIN_FILE ); ?>"
500
									alt="<?php esc_attr_e( 'Built-in Performance', 'jetpack' ); ?>"
501
							/>
502
						</div>
503
						<div class="jp-connect-full__slide-card">
504
							<h3><?php esc_html_e( 'Built-in Performance', 'jetpack' ); ?></h3>
505
							<ul>
506
								<li><?php esc_html_e( 'Keep people on your site longer with lightning-fast page load times through our free global CDN.', 'jetpack' ); ?></li>
507
								<li><?php esc_html_e( 'Speed up your mobile site and reduce bandwidth usage automatically.', 'jetpack' ); ?></li>
508
								<li><?php esc_html_e( 'Improve visitor engagement and sales with a customized search experience.', 'jetpack' ); ?></li>
509
							</ul>
510
						</div>
511
					</div>
512
				</div>
513
514
				<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>
515
516
				<?php if ( 'plugins' === $current_screen->base ) : ?>
517
518
					<?php
519
					if ( ! self::force_display() ) :
520
						?>
521
522
						<p class="jp-connect-full__dismiss-paragraph">
523
							<a>
524
								<?php
525
								echo esc_html_x(
526
									'Not now, thank you.',
527
									'a link that closes the modal window that offers to connect Jetpack',
528
									'jetpack'
529
								);
530
								?>
531
							</a>
532
						</p>
533
534
						<?php
535
						endif;
536
					?>
537
538
				<?php endif; ?>
539
			</div>
540
		</div>
541
		<?php
542
	}
543
544
	/**
545
	 * Renders the legacy network connection banner.
546
	 */
547
	function network_connect_notice() {
548
		?>
549
		<div id="message" class="updated jetpack-message">
550
			<div class="squeezer">
551
				<h2>
552
					<?php
553
						echo wp_kses(
554
							__(
555
								'<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.',
556
								'jetpack'
557
							),
558
							array( 'strong' => array() )
559
						);
560
					?>
561
				</h2>
562
			</div>
563
		</div>
564
		<?php
565
	}
566
}
567