Completed
Push — add/enable-userless ( 8c1b04 )
by
unknown
375:58 queued 366:19
created

enqueue_connect_button_scripts()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

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