Completed
Push — branch-9.0-built ( 7e9beb...186083 )
by Jeremy
08:28
created

render_license_aware_banner()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 62
rs 8.829
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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