Completed
Push — update/contact-info-block-remo... ( a8ae97...02f543 )
by
unknown
34:39 queued 27:24
created

Jetpack_Connection_Banner   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 322
Duplicated Lines 6.52 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 21
loc 322
rs 10
c 0
b 0
f 0
wmc 19
lcom 2
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A __construct() 0 3 1
A build_connect_url_for_slide() 0 9 1
A get_jetpack_logo() 0 10 1
B maybe_initialize_hooks() 0 29 7
A enqueue_banner_scripts() 21 21 1
B render_banner() 0 72 1
B render_connect_prompt_full_screen() 0 81 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
class Jetpack_Connection_Banner {
4
	/**
5
	 * @var Jetpack_Connection_Banner
6
	 **/
7
	private static $instance = null;
8
9
	static function init() {
10
		if ( is_null( self::$instance ) ) {
11
			self::$instance = new Jetpack_Connection_Banner();
12
		}
13
14
		return self::$instance;
15
	}
16
17
	/**
18
	 * Jetpack_Connection_Banner constructor.
19
	 *
20
	 * Since we call the Jetpack_Connection_Banner:init() method from the `Jetpack` class, and after
21
	 * the admin_init action fires, we know that the admin is initialized at this point.
22
	 */
23
	private function __construct() {
24
		add_action( 'current_screen', array( $this, 'maybe_initialize_hooks' ) );
25
	}
26
27
	/**
28
	 * Given a string for the the banner was added, and an int that represents the slide to
29
	 * a URL for, this function returns a connection URL with a from parameter that will
30
	 * support split testing.
31
	 *
32
	 * @since 7.2   Event key format is now banner-connect-banner-72-dashboard or connect-banner-72-plugins.
33
	 *              The param $slide_num was removed since we removed all slides but the first one.
34
	 * @since 4.4.0
35
	 *
36
	 * @param string     $jp_version_banner_added A short version of when the banner was added. Ex. 44
37
	 *
38
	 * @return string
39
	 */
40
	function build_connect_url_for_slide( $jp_version_banner_added ) {
41
		global $current_screen;
42
		$url = Jetpack::init()->build_connect_url(
43
			true,
44
			false,
45
			sprintf( 'connect-banner-%s-%s', $jp_version_banner_added, $current_screen->base )
46
		);
47
		return add_query_arg( 'auth_approved', 'true', $url );
48
	}
49
50
	/**
51
	 * Return an img HTML tag pointing to the Jetpack logo. Includes alt text.
52
	 *
53
	 * @since 7.2
54
	 *
55
	 * @return string
56
	 */
57
	public static function get_jetpack_logo() {
58
		return sprintf(
59
			'<img src="%s" class="jetpack-logo" alt="%s" />',
60
			esc_url( plugins_url( 'images/jetpack-logo-green.svg', JETPACK__PLUGIN_FILE ) ),
61
			esc_attr__(
62
				'Jetpack is a free plugin that utilizes powerful WordPress.com servers to enhance your site and simplify managing it',
63
				'jetpack'
64
			)
65
		);
66
	}
67
68
	/**
69
	 * Will initialize hooks to display the new (as of 4.4) connection banner if the current user can
70
	 * connect Jetpack, if Jetpack has not been deactivated, and if the current page is the plugins page.
71
	 *
72
	 * This method should not be called if the site is connected to WordPress.com or if the site is in development mode.
73
	 *
74
	 * @since 4.4.0
75
	 * @since 4.5.0 Made the new (as of 4.4) connection banner display to everyone by default.
76
	 * @since 5.3.0 Running another split test between 4.4 banner and a new one in 5.3.
77
	 * @since 7.2   B test was removed.
78
	 *
79
	 * @param $current_screen
80
	 */
81
	function maybe_initialize_hooks( $current_screen ) {
82
		// Kill if banner has been dismissed
83
		if ( Jetpack_Options::get_option( 'dismissed_connection_banner' ) ) {
84
			return;
85
		}
86
87
		// Don't show the connect notice anywhere but the plugins.php after activating
88
		if ( 'plugins' !== $current_screen->base && 'dashboard' !== $current_screen->base ) {
89
			return;
90
		}
91
92
		if ( ! current_user_can( 'jetpack_connect' ) ) {
93
			return;
94
		}
95
96
		add_action( 'admin_notices', array( $this, 'render_banner' ) );
97
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) );
98
		add_action( 'admin_print_styles', array( Jetpack::init(), 'admin_banner_styles' ) );
99
100
		if ( Jetpack::state( 'network_nag' ) ) {
101
			add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) );
102
		}
103
104
		// Only fires immediately after plugin activation
105
		if ( get_transient( 'activated_jetpack' ) ) {
106
			add_action( 'admin_notices', array( $this, 'render_connect_prompt_full_screen' ) );
107
			delete_transient( 'activated_jetpack' );
108
		}
109
	}
110
111
	/**
112
	 * Enqueues JavaScript for new connection banner.
113
	 *
114
	 * @since 4.4.0
115
	 */
116 View Code Duplication
	public static function enqueue_banner_scripts() {
117
		wp_enqueue_script(
118
			'jetpack-connection-banner-js',
119
			Jetpack::get_file_url_for_environment(
120
				'_inc/build/jetpack-connection-banner.min.js',
121
				'_inc/jetpack-connection-banner.js'
122
			),
123
			array( 'jquery' ),
124
			JETPACK__VERSION,
125
			true
126
		);
127
128
		wp_localize_script(
129
			'jetpack-connection-banner-js',
130
			'jp_banner',
131
			array(
132
				'ajax_url' => admin_url( 'admin-ajax.php' ),
133
				'connectionBannerNonce' => wp_create_nonce( 'jp-connection-banner-nonce' ),
134
			)
135
		);
136
	}
137
138
	/**
139
	 * Renders the new connection banner as of 4.4.0.
140
	 *
141
	 * @since 7.2   Copy and visual elements reduced to show the new focus of Jetpack on Security and Performance.
142
	 * @since 4.4.0
143
	 */
144
	function render_banner() { ?>
145
		<div id="message" class="updated jp-wpcom-connect__container">
146
			<div class="jp-wpcom-connect__container-top-text">
147
				<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>
148
				<span><?php esc_html_e( 'You’re almost done. Set up Jetpack to enable powerful security and performance tools for WordPress.', 'jetpack' ); ?></span>
149
			</div>
150
			<div class="jp-wpcom-connect__inner-container">
151
				<span
152
					class="notice-dismiss connection-banner-dismiss"
153
					title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
154
				</span>
155
156
				<div class="jp-wpcom-connect__content-container">
157
158
					<!-- slide 1: intro -->
159
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
160
161
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
162
							<?php echo self::get_jetpack_logo(); ?>
163
							<img
164
								src="<?php echo plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ); ?>"
165
								class="jp-wpcom-connect__hide-phone-and-smaller"
166
								alt="<?php esc_attr_e(
167
									'Jetpack premium services offer even more powerful performance, security, ' .
168
									'and revenue tools to help you keep your site safe, fast, and help generate income.',
169
									'jetpack'
170
								); ?>"
171
								height="auto"
172
								width="225"
173
								/>
174
						</div>
175
176
						<div class="jp-wpcom-connect__slide-text">
177
							<h2><?php esc_html_e( 'Simplify your site security and performance with Jetpack', 'jetpack' ) ?></h2>
178
179
							<p>
180
								<?php
181
								esc_html_e(
182
									'Jetpack protects you against brute force attacks and unauthorized logins. Basic protection ' .
183
									'is always free, while premium plans add unlimited backups of your whole site, spam protection, ' .
184
									'malware scanning, and automated fixes.',
185
									'jetpack'
186
								);
187
								?>
188
							</p>
189
190
							<p>
191
								<?php
192
								esc_html_e(
193
									'Activate site accelerator tools and watch your page load times and hosting costs drop – we’ll ' .
194
									'optimize your images and serve them from our own powerful global network of servers, ' .
195
									'and speed up your mobile site to reduce bandwidth usage.',
196
									'jetpack'
197
								);
198
								?>
199
							</p>
200
201
							<div class="jp-banner__button-container">
202
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
203
								<a
204
									href="<?php echo esc_url( $this->build_connect_url_for_slide( '72' ) ); ?>"
205
									class="dops-button is-primary">
206
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
207
								</a>
208
							</div>
209
						</div>
210
					</div> <!-- end slide 1 -->
211
				</div>
212
			</div>
213
		</div>
214
		<?php
215
	}
216
217
	/**
218
	 * Renders the full-screen connection prompt.  Only shown once and on plugin activation.
219
	 */
220
	public static function render_connect_prompt_full_screen() {
221
		$current_screen = get_current_screen();
222
		if ( 'plugins' === $current_screen->base ) {
223
			$bottom_connect_url_from = 'full-screen-prompt';
224
		} else {
225
			$bottom_connect_url_from = 'landing-page-bottom';
226
		}
227
		?>
228
		<div class="jp-connect-full__container"><div class="jp-connect-full__container-card">
229
230
				<?php if ( 'plugins' === $current_screen->base ) : ?>
231
					<?php echo self::get_jetpack_logo(); ?>
232
233
					<div class="jp-connect-full__dismiss">
234
						<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>
235
					</div>
236
				<?php endif; ?>
237
238
				<div class="jp-connect-full__step-header">
239
					<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>
240
				</div>
241
242
				<div class="jp-connect-full__row">
243
					<div class="jp-connect-full__slide">
244
						<div class="jp-connect-full__slide-card illustration">
245
							<img
246
									src="<?php echo plugins_url( 'images/security.svg', JETPACK__PLUGIN_FILE ); ?>"
247
									alt="<?php esc_attr_e( 'Security & Backups', 'jetpack' ); ?>"
248
							/>
249
						</div>
250
						<div class="jp-connect-full__slide-card">
251
							<p><?php
252
								esc_html_e(
253
									'Jetpack protects you against brute force attacks and unauthorized logins. ' .
254
									'Basic protection is always free, while premium plans add unlimited backups of your whole site, ' .
255
									'spam protection, malware scanning, and automated fixes.',
256
									'jetpack'
257
								);
258
								?></p>
259
						</div>
260
					</div>
261
					<div class="jp-connect-full__slide">
262
						<div class="jp-connect-full__slide-card illustration">
263
							<img
264
									src="<?php echo plugins_url( 'images/jetpack-speed.svg', JETPACK__PLUGIN_FILE ); ?>"
265
									alt="<?php esc_attr_e( 'Built-in Performance', 'jetpack' ); ?>"
266
							/>
267
						</div>
268
						<div class="jp-connect-full__slide-card">
269
							<p><?php
270
								esc_html_e(
271
									"Activate site accelerator tools and watch your page load times and hosting costs drop—" .
272
									"we'll optimize your images and serve them from our own powerful global network of servers, " .
273
									"and speed up your mobile site to reduce bandwidth usage.",
274
									'jetpack'
275
								);
276
								?></p>
277
						</div>
278
					</div>
279
				</div>
280
281
				<p class="jp-connect-full__tos-blurb">
282
					<?php jetpack_render_tos_blurb(); ?>
283
				</p>
284
				<p class="jp-connect-full__button-container">
285
					<a href="<?php echo esc_url( Jetpack::init()->build_connect_url( true, false, $bottom_connect_url_from ) ); ?>" class="dops-button is-primary">
286
						<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
287
					</a>
288
				</p>
289
				<?php if ( 'plugins' === $current_screen->base ) : ?>
290
					<p class="jp-connect-full__dismiss-paragraph">
291
						<a>
292
							<?php echo esc_html_x(
293
								'Not now, thank you.', 'a link that closes the modal window that offers to connect Jetpack', 'jetpack'
294
							); ?>
295
						</a>
296
					</p>
297
				<?php endif; ?>
298
			</div></div>
299
		<?php
300
	}
301
302
	/**
303
	 * Renders the legacy network connection banner.
304
	 */
305
	function network_connect_notice() {
306
		?>
307
		<div id="message" class="updated jetpack-message">
308
			<div class="squeezer">
309
				<h2>
310
					<?php
311
						echo wp_kses(
312
							__(
313
								'<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.',
314
								'jetpack'
315
							),
316
							array( 'strong' => array() )
317
						);
318
					?>
319
				</h2>
320
			</div>
321
		</div>
322
		<?php
323
	}
324
}
325