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