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 |
||
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 || Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' ) ) { |
||
176 | $force_variation = 'original'; |
||
177 | } else { |
||
178 | $force_variation = 'in_place'; |
||
179 | } |
||
180 | |||
181 | $tracking = new Automattic\Jetpack\Tracking(); |
||
182 | $identity = $tracking->tracks_get_identity( get_current_user_id() ); |
||
183 | |||
184 | wp_localize_script( |
||
185 | 'jetpack-connect-button', |
||
186 | 'jpConnect', |
||
187 | array( |
||
188 | 'apiBaseUrl' => esc_url_raw( rest_url( 'jetpack/v4' ) ), |
||
189 | 'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ), |
||
190 | 'apiNonce' => wp_create_nonce( 'wp_rest' ), |
||
191 | 'apiSiteDataNonce' => wp_create_nonce( 'wp_rest' ), |
||
192 | 'buttonTextRegistering' => __( 'Loading...', 'jetpack' ), |
||
193 | 'jetpackApiDomain' => $jetpackApiUrl['scheme'] . '://' . $jetpackApiUrl['host'], |
||
194 | 'forceVariation' => $force_variation, |
||
195 | 'connectInPlaceUrl' => Jetpack::admin_url( 'page=jetpack#/setup' ), |
||
196 | 'dashboardUrl' => Jetpack::admin_url( 'page=jetpack#/dashboard' ), |
||
197 | 'plansPromptUrl' => Jetpack::admin_url( 'page=jetpack#/plans-prompt' ), |
||
198 | 'identity' => $identity, |
||
199 | 'preFetchScript' => plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ) . '?ver=' . JETPACK__VERSION, |
||
200 | ) |
||
201 | ); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Renders the new connection banner as of 4.4.0. |
||
206 | * |
||
207 | * @since 7.2 Copy and visual elements reduced to show the new focus of Jetpack on Security and Performance. |
||
208 | * @since 4.4.0 |
||
209 | */ |
||
210 | public function render_banner() { |
||
211 | ?> |
||
212 | <div id="message" class="updated jp-wpcom-connect__container"> |
||
213 | <div class="jp-wpcom-connect__container-top-text"> |
||
214 | <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> |
||
215 | <span> |
||
216 | <?php esc_html_e( 'You’re almost done. Set up Jetpack to enable powerful security and performance tools for WordPress.', 'jetpack' ); ?> |
||
217 | </span> |
||
218 | </div> |
||
219 | <div class="jp-wpcom-connect__inner-container"> |
||
220 | |||
221 | <?php |
||
222 | if ( ! $this->force_display() ) : |
||
223 | ?> |
||
224 | |||
225 | <span |
||
226 | class="notice-dismiss connection-banner-dismiss" |
||
227 | title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>"> |
||
228 | </span> |
||
229 | |||
230 | <?php |
||
231 | endif; |
||
232 | ?> |
||
233 | |||
234 | <div class="jp-wpcom-connect__content-container"> |
||
235 | |||
236 | <!-- slide 1: intro --> |
||
237 | <div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active"> |
||
238 | |||
239 | <div class="jp-wpcom-connect__content-icon jp-connect-illo"> |
||
240 | <?php |
||
241 | $logo = new Logo(); |
||
242 | echo $logo->render(); |
||
243 | ?> |
||
244 | <img |
||
245 | src="<?php echo plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ); ?>" |
||
246 | class="jp-wpcom-connect__hide-phone-and-smaller" |
||
247 | alt=" |
||
248 | <?php |
||
249 | esc_attr_e( |
||
250 | 'Jetpack premium services offer even more powerful performance, security, ' . |
||
251 | 'and revenue tools to help you keep your site safe, fast, and help generate income.', |
||
252 | 'jetpack' |
||
253 | ); |
||
254 | ?> |
||
255 | " |
||
256 | height="auto" |
||
257 | width="225" |
||
258 | /> |
||
259 | </div> |
||
260 | |||
261 | <div class="jp-wpcom-connect__slide-text"> |
||
262 | <h2><?php esc_html_e( 'Simplify your site security and performance with Jetpack', 'jetpack' ); ?></h2> |
||
263 | |||
264 | <p> |
||
265 | <?php |
||
266 | esc_html_e( |
||
267 | 'Jetpack protects you against brute force attacks and unauthorized logins. Basic protection ' . |
||
268 | 'is always free, while premium plans add unlimited backups of your whole site, spam protection, ' . |
||
269 | 'malware scanning, and automated fixes.', |
||
270 | 'jetpack' |
||
271 | ); |
||
272 | ?> |
||
273 | </p> |
||
274 | |||
275 | <p> |
||
276 | <?php |
||
277 | esc_html_e( |
||
278 | 'Activate site accelerator tools and watch your page load times decrease—we’ll ' . |
||
279 | 'optimize your images and serve them from our own powerful global network of servers, ' . |
||
280 | 'and speed up your mobile site to reduce bandwidth usage.', |
||
281 | 'jetpack' |
||
282 | ); |
||
283 | ?> |
||
284 | </p> |
||
285 | |||
286 | <div class="jp-banner__button-container"> |
||
287 | <span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span> |
||
288 | <a |
||
289 | href="<?php echo esc_url( $this->build_connect_url_for_slide( '72' ) ); ?>" |
||
290 | class="dops-button is-primary jp-banner__alt-connect-button"> |
||
291 | <?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?> |
||
292 | </a> |
||
293 | </div> |
||
294 | |||
295 | </div> |
||
296 | </div> <!-- end slide 1 --> |
||
297 | </div> |
||
298 | </div> |
||
299 | </div> |
||
300 | <?php |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Renders the full-screen connection prompt. Only shown once and on plugin activation. |
||
305 | */ |
||
306 | public static function render_connect_prompt_full_screen() { |
||
307 | $current_screen = get_current_screen(); |
||
308 | if ( 'plugins' === $current_screen->base ) { |
||
309 | $bottom_connect_url_from = 'full-screen-prompt'; |
||
310 | } else { |
||
311 | $bottom_connect_url_from = 'landing-page-bottom'; |
||
312 | } |
||
313 | ?> |
||
314 | <div class="jp-connect-full__container"><div class="jp-connect-full__container-card"> |
||
315 | |||
316 | <?php if ( 'plugins' === $current_screen->base ) : ?> |
||
317 | <?php |
||
318 | $logo = new Logo(); |
||
319 | echo $logo->render(); |
||
320 | ?> |
||
321 | |||
322 | <?php |
||
323 | if ( ! self::force_display() ) : |
||
324 | ?> |
||
325 | |||
326 | <div class="jp-connect-full__dismiss"> |
||
327 | <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> |
||
328 | </div> |
||
329 | |||
330 | <?php |
||
331 | endif; |
||
332 | ?> |
||
333 | |||
334 | <?php endif; ?> |
||
335 | |||
336 | <div class="jp-connect-full__step-header"> |
||
337 | <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> |
||
338 | </div> |
||
339 | |||
340 | <p class="jp-connect-full__tos-blurb"> |
||
341 | <?php jetpack_render_tos_blurb(); ?> |
||
342 | </p> |
||
343 | |||
344 | <p class="jp-connect-full__button-container"> |
||
345 | <a href="<?php echo esc_url( Jetpack::init()->build_connect_url( true, false, $bottom_connect_url_from ) ); ?>" |
||
346 | class="dops-button is-primary jp-connect-button"> |
||
347 | <?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?> |
||
348 | </a> |
||
349 | </p> |
||
350 | |||
351 | <div class="jp-connect-full__row" id="jetpack-connection-cards"> |
||
352 | <div class="jp-connect-full__slide"> |
||
353 | <div class="jp-connect-full__slide-card illustration"> |
||
354 | <img |
||
355 | src="<?php echo plugins_url( 'images/security.svg', JETPACK__PLUGIN_FILE ); ?>" |
||
356 | alt="<?php esc_attr_e( 'Security & Backups', 'jetpack' ); ?>" |
||
357 | /> |
||
358 | </div> |
||
359 | <div class="jp-connect-full__slide-card"> |
||
360 | <p> |
||
361 | <?php |
||
362 | esc_html_e( |
||
363 | 'Jetpack protects you against brute force attacks and unauthorized logins. ' . |
||
364 | 'Basic protection is always free, while premium plans add unlimited backups of your whole site, ' . |
||
365 | 'spam protection, malware scanning, and automated fixes.', |
||
366 | 'jetpack' |
||
367 | ); |
||
368 | ?> |
||
369 | </p> |
||
370 | </div> |
||
371 | </div> |
||
372 | <div class="jp-connect-full__slide"> |
||
373 | <div class="jp-connect-full__slide-card illustration"> |
||
374 | <img |
||
375 | src="<?php echo plugins_url( 'images/jetpack-speed.svg', JETPACK__PLUGIN_FILE ); ?>" |
||
376 | alt="<?php esc_attr_e( 'Built-in Performance', 'jetpack' ); ?>" |
||
377 | /> |
||
378 | </div> |
||
379 | <div class="jp-connect-full__slide-card"> |
||
380 | <p> |
||
381 | <?php |
||
382 | esc_html_e( |
||
383 | 'Activate site accelerator tools and watch your page load times decrease—' . |
||
384 | "we'll optimize your images and serve them from our own powerful global network of servers, " . |
||
385 | 'and speed up your mobile site to reduce bandwidth usage.', |
||
386 | 'jetpack' |
||
387 | ); |
||
388 | ?> |
||
389 | </p> |
||
390 | </div> |
||
391 | </div> |
||
392 | </div> |
||
393 | |||
394 | <?php if ( 'plugins' === $current_screen->base ) : ?> |
||
395 | |||
396 | <?php |
||
397 | if ( ! self::force_display() ) : |
||
398 | ?> |
||
399 | |||
400 | <p class="jp-connect-full__dismiss-paragraph"> |
||
401 | <a> |
||
402 | <?php |
||
403 | echo esc_html_x( |
||
404 | 'Not now, thank you.', |
||
405 | 'a link that closes the modal window that offers to connect Jetpack', |
||
406 | 'jetpack' |
||
407 | ); |
||
408 | ?> |
||
409 | </a> |
||
410 | </p> |
||
411 | |||
412 | <?php |
||
413 | endif; |
||
414 | ?> |
||
415 | |||
416 | <?php endif; ?> |
||
417 | </div> |
||
418 | </div> |
||
419 | <?php |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Renders the legacy network connection banner. |
||
424 | */ |
||
425 | function network_connect_notice() { |
||
444 | } |
||
445 |