Completed
Push — add/bail-on-unsupported-wp ( c702b0...29ae98 )
by
unknown
14:04 queued 07:10
created

Jetpack_Connection_Banner   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 318
Duplicated Lines 6.6 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 21
loc 318
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 68 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 boost your site performance and unlock powerful customization, marketing, and security tools.', '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 free protects your site against brute force attacks and unauthorized logins. Our premium security services also include unlimited backups of your entire site, spam protection, malware scanning, and automated fixes.',
183
									'jetpack'
184
								);
185
								?>
186
							</p>
187
188
							<p>
189
								<?php
190
								esc_html_e(
191
									'Activate Jetpack’s site accelerator to load pages faster, optimize your images, and serve your images and static files (like CSS and JavaScript) from our global network of WordPress.com servers. Speed up your site for mobile viewers and reduce bandwidth usage, which may lead to lower hosting costs.',
192
									'jetpack'
193
								);
194
								?>
195
							</p>
196
197
							<div class="jp-banner__button-container">
198
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
199
								<a
200
									href="<?php echo esc_url( $this->build_connect_url_for_slide( '72' ) ); ?>"
201
									class="dops-button is-primary">
202
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
203
								</a>
204
							</div>
205
						</div>
206
					</div> <!-- end slide 1 -->
207
				</div>
208
			</div>
209
		</div>
210
		<?php
211
	}
212
213
	/**
214
	 * Renders the full-screen connection prompt.  Only shown once and on plugin activation.
215
	 */
216
	public static function render_connect_prompt_full_screen() {
217
		$current_screen = get_current_screen();
218
		if ( 'plugins' === $current_screen->base ) {
219
			$bottom_connect_url_from = 'full-screen-prompt';
220
		} else {
221
			$bottom_connect_url_from = 'landing-page-bottom';
222
		}
223
		?>
224
		<div class="jp-connect-full__container"><div class="jp-connect-full__container-card">
225
226
				<?php if ( 'plugins' === $current_screen->base ) : ?>
227
					<?php echo self::get_jetpack_logo(); ?>
228
229
					<div class="jp-connect-full__dismiss">
230
						<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>
231
					</div>
232
				<?php endif; ?>
233
234
				<div class="jp-connect-full__step-header">
235
					<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>
236
				</div>
237
238
				<div class="jp-connect-full__row">
239
					<div class="jp-connect-full__slide">
240
						<div class="jp-connect-full__slide-card illustration">
241
							<img
242
									src="<?php echo plugins_url( 'images/security.svg', JETPACK__PLUGIN_FILE ); ?>"
243
									alt="<?php esc_attr_e( 'Security & Backups', 'jetpack' ); ?>"
244
							/>
245
						</div>
246
						<div class="jp-connect-full__slide-card">
247
							<p><?php
248
								esc_html_e(
249
									'Jetpack protects you against brute force attacks and unauthorized logins. ' .
250
									'Basic protection is always free, while premium plans add unlimited backups of your whole site, ' .
251
									'spam protection, malware scanning, and automated fixes.',
252
									'jetpack'
253
								);
254
								?></p>
255
						</div>
256
					</div>
257
					<div class="jp-connect-full__slide">
258
						<div class="jp-connect-full__slide-card illustration">
259
							<img
260
									src="<?php echo plugins_url( 'images/jetpack-speed.svg', JETPACK__PLUGIN_FILE ); ?>"
261
									alt="<?php esc_attr_e( 'Built-in Performance', 'jetpack' ); ?>"
262
							/>
263
						</div>
264
						<div class="jp-connect-full__slide-card">
265
							<p><?php
266
								esc_html_e(
267
									"Activate site accelerator tools and watch your page load times and hosting costs drop—" .
268
									"we'll optimize your images and serve them from our own powerful global network of servers, " .
269
									"and speed up your mobile site to reduce bandwidth usage.",
270
									'jetpack'
271
								);
272
								?></p>
273
						</div>
274
					</div>
275
				</div>
276
277
				<p class="jp-connect-full__tos-blurb">
278
					<?php jetpack_render_tos_blurb(); ?>
279
				</p>
280
				<p class="jp-connect-full__button-container">
281
					<a href="<?php echo esc_url( Jetpack::init()->build_connect_url( true, false, $bottom_connect_url_from ) ); ?>" class="dops-button is-primary">
282
						<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
283
					</a>
284
				</p>
285
				<?php if ( 'plugins' === $current_screen->base ) : ?>
286
					<p class="jp-connect-full__dismiss-paragraph">
287
						<a>
288
							<?php echo esc_html_x(
289
								'Not now, thank you.', 'a link that closes the modal window that offers to connect Jetpack', 'jetpack'
290
							); ?>
291
						</a>
292
					</p>
293
				<?php endif; ?>
294
			</div></div>
295
		<?php
296
	}
297
298
	/**
299
	 * Renders the legacy network connection banner.
300
	 */
301
	function network_connect_notice() {
302
		?>
303
		<div id="message" class="updated jetpack-message">
304
			<div class="squeezer">
305
				<h2>
306
					<?php
307
						echo wp_kses(
308
							__(
309
								'<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.',
310
								'jetpack'
311
							),
312
							array( 'strong' => array() )
313
						);
314
					?>
315
				</h2>
316
			</div>
317
		</div>
318
		<?php
319
	}
320
}
321