Completed
Push — update/wpcom-premium-block-reg... ( dc3f2a...74d2c1 )
by Jeremy
18:11 queued 10:52
created

network_connect_notice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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