Completed
Push — add/enable-userless ( 8c1b04...497106 )
by
unknown
418:13 queued 408:31
created

enqueue_connect_button_scripts()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

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