Completed
Push — renovate/wordpress-monorepo ( 8cdadc...6203b6 )
by
unknown
552:54 queued 542:45
created

enqueue_connect_button_scripts()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

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