Completed
Push — fix/deactivated-publicize ( d23d2d...fa3324 )
by Jeremy
22:10 queued 13:54
created

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