Completed
Push — update/recurring-payments-use-... ( 75b0c5...161bad )
by Bernhard
07:10
created

Jetpack_Connection_Banner   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 395
Duplicated Lines 5.32 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 21
loc 395
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 6

10 Methods

Rating   Name   Duplication   Size   Complexity  
A enqueue_banner_scripts() 21 21 1
A init() 0 7 2
A __construct() 0 4 1
A build_connect_url_for_slide() 0 14 2
B maybe_initialize_hooks() 0 30 7
A enqueue_connect_button_scripts() 0 48 4
A get_ab_banner_top_bar() 0 18 4
B render_banner() 0 73 1
B render_connect_prompt_full_screen() 0 88 4
A network_connect_notice() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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