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:
Complex classes like Jetpack_IDC often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_IDC, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Jetpack_IDC { |
||
13 | |||
14 | /** |
||
15 | * @var Jetpack_IDC |
||
16 | **/ |
||
17 | private static $instance = null; |
||
18 | |||
19 | /** |
||
20 | * The wpcom value of the home URL |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | static $wpcom_home_url; |
||
25 | |||
26 | /** |
||
27 | * Has safe mode been confirmed? |
||
28 | * |
||
29 | * @var bool |
||
30 | */ |
||
31 | static $is_safe_mode_confirmed; |
||
32 | |||
33 | /** |
||
34 | * The current screen, which is set if the current user is a non-admin and this is an admin page. |
||
35 | * |
||
36 | * @var WP_Screen |
||
37 | */ |
||
38 | static $current_screen; |
||
39 | |||
40 | static function init() { |
||
47 | |||
48 | View Code Duplication | private function __construct() { |
|
49 | add_action( 'jetpack_sync_processed_actions', array( $this, 'maybe_clear_migrate_option' ) ); |
||
50 | if ( false === $urls_in_crisis = Jetpack::check_identity_crisis() ) { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | self::$wpcom_home_url = $urls_in_crisis['wpcom_home']; |
||
55 | add_action( 'init', array( $this, 'wordpress_init' ) ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Gets the link to the support document used to explain Safe Mode to users |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public static function get_safe_mod_doc_url() { |
||
66 | |||
67 | /** |
||
68 | * This method loops through the array of processed items from sync and checks if one of the items was the |
||
69 | * home_url or site_url callable. If so, then we delete the jetpack_migrate_for_idc option. |
||
70 | * |
||
71 | * @param $processed_items array Array of processed items that were synced to WordPress.com |
||
72 | */ |
||
73 | View Code Duplication | function maybe_clear_migrate_option( $processed_items ) { |
|
74 | foreach ( (array) $processed_items as $item ) { |
||
75 | |||
76 | // First, is this item a jetpack_sync_callable action? If so, then proceed. |
||
77 | $callable_args = ( is_array( $item ) && isset( $item[0], $item[1] ) && 'jetpack_sync_callable' === $item[0] ) |
||
78 | ? $item[1] |
||
79 | : null; |
||
80 | |||
81 | // Second, if $callable_args is set, check if the callable was home_url or site_url. If so, |
||
82 | // clear the migrate option. |
||
83 | if ( |
||
84 | isset( $callable_args, $callable_args[0] ) |
||
85 | && ( 'home_url' === $callable_args[0] || 'site_url' === $callable_args[1] ) |
||
86 | ) { |
||
87 | Jetpack_Options::delete_option( 'migrate_for_idc' ); |
||
88 | break; |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | View Code Duplication | function wordpress_init() { |
|
94 | if ( ! current_user_can( 'jetpack_disconnect' ) && is_admin() ) { |
||
95 | add_action( 'admin_notices', array( $this, 'display_non_admin_idc_notice' ) ); |
||
96 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_idc_notice_files' ) ); |
||
97 | add_action( 'current_screen', array( $this, 'non_admins_current_screen_check' ) ); |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | if ( |
||
102 | isset( $_GET['jetpack_idc_clear_confirmation'], $_GET['_wpnonce'] ) && |
||
103 | wp_verify_nonce( $_GET['_wpnonce'], 'jetpack_idc_clear_confirmation' ) |
||
104 | ) { |
||
105 | Jetpack_Options::delete_option( 'safe_mode_confirmed' ); |
||
106 | self::$is_safe_mode_confirmed = false; |
||
107 | } else { |
||
108 | self::$is_safe_mode_confirmed = (bool) Jetpack_Options::get_option( 'safe_mode_confirmed' ); |
||
109 | } |
||
110 | |||
111 | // 121 Priority so that it's the most inner Jetpack item in the admin bar. |
||
112 | add_action( 'admin_bar_menu', array( $this, 'display_admin_bar_button' ), 121 ); |
||
113 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_bar_css' ) ); |
||
114 | |||
115 | if ( is_admin() && ! self::$is_safe_mode_confirmed ) { |
||
116 | add_action( 'admin_notices', array( $this, 'display_idc_notice' ) ); |
||
117 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_idc_notice_files' ) ); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | View Code Duplication | function non_admins_current_screen_check( $current_screen ) { |
|
122 | self::$current_screen = $current_screen; |
||
123 | if ( isset( $current_screen->id ) && 'toplevel_page_jetpack' == $current_screen->id ) { |
||
124 | return null; |
||
125 | } |
||
126 | |||
127 | // If the user has dismissed the notice, and we're not currently on a Jetpack page, |
||
128 | // then do not show the non-admin notice. |
||
129 | if ( isset( $_COOKIE, $_COOKIE['jetpack_idc_dismiss_notice'] ) ) { |
||
130 | remove_action( 'admin_notices', array( $this, 'display_non_admin_idc_notice' ) ); |
||
131 | remove_action( 'admin_enqueue_scripts', array( $this, 'enqueue_idc_notice_files' ) ); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | View Code Duplication | function display_admin_bar_button() { |
|
136 | global $wp_admin_bar; |
||
137 | |||
138 | $href = is_admin() |
||
139 | ? add_query_arg( 'jetpack_idc_clear_confirmation', '1' ) |
||
140 | : add_query_arg( 'jetpack_idc_clear_confirmation', '1', admin_url() ); |
||
141 | |||
142 | $href = wp_nonce_url( $href, 'jetpack_idc_clear_confirmation' ); |
||
143 | |||
144 | $title = sprintf( |
||
145 | '<span class="jp-idc-admin-bar">%s %s</span>', |
||
146 | '<span class="dashicons dashicons-warning"></span>', |
||
147 | esc_html__( 'Jetpack Safe Mode', 'jetpack' ) |
||
148 | ); |
||
149 | |||
150 | $menu = array( |
||
151 | 'id' => 'jetpack-idc', |
||
152 | 'title' => $title, |
||
153 | 'href' => esc_url( $href ), |
||
154 | 'parent' => 'top-secondary', |
||
155 | ); |
||
156 | |||
157 | if ( ! self::$is_safe_mode_confirmed ) { |
||
158 | $menu['meta'] = array( |
||
159 | 'class' => 'hide', |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | $wp_admin_bar->add_node( $menu ); |
||
164 | } |
||
165 | |||
166 | static function prepare_url_for_display( $url ) { |
||
169 | |||
170 | /** |
||
171 | * Clears all IDC specific options. This method is used on disconnect and reconnect. |
||
172 | */ |
||
173 | View Code Duplication | static function clear_all_idc_options() { |
|
174 | // If the site is currently in IDC, let's also clear the VaultPress connection options. |
||
175 | // We have to check if the site is in IDC, otherwise we'd be clearing the VaultPress |
||
176 | // connection any time the Jetpack connection is cycled. |
||
177 | if ( Jetpack::validate_sync_error_idc_option() ) { |
||
178 | delete_option( 'vaultpress' ); |
||
179 | delete_option( 'vaultpress_auto_register' ); |
||
180 | } |
||
181 | |||
182 | Jetpack_Options::delete_option( |
||
183 | array( |
||
184 | 'sync_error_idc', |
||
185 | 'safe_mode_confirmed', |
||
186 | 'migrate_for_idc', |
||
187 | ) |
||
188 | ); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Does the current admin page have help tabs? |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | View Code Duplication | function admin_page_has_help_tabs() { |
|
197 | if ( ! function_exists( 'get_current_screen' ) ) { |
||
198 | return false; |
||
199 | } |
||
200 | |||
201 | $current_screen = get_current_screen(); |
||
202 | $tabs = $current_screen->get_help_tabs(); |
||
203 | |||
204 | return ! empty( $tabs ); |
||
205 | } |
||
206 | |||
207 | function display_non_admin_idc_notice() { |
||
232 | |||
233 | /** |
||
234 | * First "step" of the IDC mitigation. Will provide some messaging and two options/buttons. |
||
235 | * "Confirm Staging" - Dismiss the notice and continue on with our lives in staging mode. |
||
236 | * "Fix Jetpack Connection" - Will disconnect the site and start the mitigation... |
||
237 | */ |
||
238 | View Code Duplication | function display_idc_notice() { |
|
239 | $classes = 'jp-idc-notice inline notice notice-warning'; |
||
240 | if ( $this->admin_page_has_help_tabs() ) { |
||
241 | $classes .= ' has-help-tabs'; |
||
242 | } |
||
243 | ?> |
||
244 | <div class="<?php echo $classes; ?>"> |
||
245 | <?php $this->render_notice_header(); ?> |
||
246 | <?php $this->render_notice_first_step(); ?> |
||
247 | <?php $this->render_notice_second_step(); ?> |
||
248 | </div> |
||
249 | <?php |
||
250 | } |
||
251 | |||
252 | function enqueue_admin_bar_css() { |
||
260 | |||
261 | /** |
||
262 | * Enqueue scripts for the notice |
||
263 | */ |
||
264 | function enqueue_idc_notice_files() { |
||
265 | |||
266 | wp_enqueue_script( |
||
267 | 'jetpack-idc-js', |
||
268 | Assets::get_file_url_for_environment( '_inc/build/idc-notice.min.js', '_inc/idc-notice.js' ), |
||
269 | array( 'jquery' ), |
||
270 | JETPACK__VERSION, |
||
271 | true |
||
272 | ); |
||
273 | |||
274 | wp_localize_script( |
||
275 | 'jetpack-idc-js', |
||
276 | 'idcL10n', |
||
277 | array( |
||
278 | 'apiRoot' => esc_url_raw( rest_url() ), |
||
279 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
||
280 | 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), |
||
281 | 'currentUrl' => remove_query_arg( '_wpnonce', remove_query_arg( 'jetpack_idc_clear_confirmation' ) ), |
||
282 | 'tracksEventData' => array( |
||
283 | 'isAdmin' => current_user_can( 'jetpack_disconnect' ), |
||
284 | 'currentScreen' => self::$current_screen ? self::$current_screen->id : false, |
||
285 | ), |
||
286 | ) |
||
287 | ); |
||
288 | |||
289 | if ( ! wp_style_is( 'jetpack-dops-style' ) ) { |
||
290 | wp_register_style( |
||
291 | 'jetpack-dops-style', |
||
292 | plugins_url( '_inc/build/admin.css', JETPACK__PLUGIN_FILE ), |
||
293 | array(), |
||
294 | JETPACK__VERSION |
||
295 | ); |
||
296 | } |
||
297 | |||
298 | wp_enqueue_style( |
||
299 | 'jetpack-idc-css', |
||
300 | plugins_url( 'css/jetpack-idc.css', JETPACK__PLUGIN_FILE ), |
||
301 | array( 'jetpack-dops-style' ), |
||
302 | JETPACK__VERSION |
||
303 | ); |
||
304 | |||
305 | // Required for Tracks |
||
306 | wp_enqueue_script( |
||
307 | 'jp-tracks', |
||
308 | '//stats.wp.com/w.js', |
||
309 | array(), |
||
310 | gmdate( 'YW' ), |
||
311 | true |
||
312 | ); |
||
313 | |||
314 | wp_enqueue_script( |
||
315 | 'jp-tracks-functions', |
||
316 | plugins_url( '_inc/lib/tracks/tracks-callables.js', JETPACK__PLUGIN_FILE ), |
||
317 | array(), |
||
318 | JETPACK__VERSION, |
||
319 | false |
||
320 | ); |
||
321 | } |
||
322 | |||
323 | function render_notice_header() { |
||
340 | |||
341 | /** |
||
342 | * Is a container for the error notices. |
||
343 | * Will be shown/controlled by jQuery in idc-notice.js |
||
344 | */ |
||
345 | function render_error_notice() { |
||
367 | |||
368 | function render_notice_first_step() { |
||
405 | |||
406 | function render_notice_second_step() { |
||
444 | |||
445 | function get_first_step_header_lead() { |
||
468 | |||
469 | View Code Duplication | function get_first_step_header_explanation() { |
|
491 | |||
492 | function get_confirm_safe_mode_action_explanation() { |
||
493 | $html = wp_kses( |
||
494 | sprintf( |
||
495 | __( |
||
496 | 'Is this website a temporary duplicate of <a href="%1$s">%2$s</a> for the purposes |
||
497 | of testing, staging or development? If so, we recommend keeping it in Safe Mode.', |
||
498 | 'jetpack' |
||
499 | ), |
||
500 | esc_url( untrailingslashit( self::$wpcom_home_url ) ), |
||
501 | self::prepare_url_for_display( esc_url( self::$wpcom_home_url ) ) |
||
502 | ), |
||
503 | array( 'a' => array( 'href' => array() ) ) |
||
504 | ); |
||
505 | |||
506 | /** |
||
507 | * Allows overriding of the default text used to explain the confirm safe mode action. |
||
508 | * |
||
509 | * @since 4.4.0 |
||
510 | * |
||
511 | * @param string $html The HTML to be displayed |
||
512 | */ |
||
513 | return apply_filters( 'jetpack_idc_confirm_safe_mode_explanation', $html ); |
||
514 | } |
||
515 | |||
516 | function get_confirm_safe_mode_button_text() { |
||
528 | |||
529 | function get_first_step_fix_connection_action_explanation() { |
||
530 | $html = wp_kses( |
||
531 | sprintf( |
||
532 | __( |
||
533 | 'If this is a separate and new website, or the new home of <a href="%1$s">%2$s</a>, |
||
534 | we recommend turning Safe Mode off, and re-establishing your connection to WordPress.com.', |
||
535 | 'jetpack' |
||
536 | ), |
||
537 | esc_url( untrailingslashit( self::$wpcom_home_url ) ), |
||
538 | self::prepare_url_for_display( esc_url( self::$wpcom_home_url ) ) |
||
539 | ), |
||
540 | array( 'a' => array( 'href' => array() ) ) |
||
541 | ); |
||
542 | |||
543 | /** |
||
544 | * Allows overriding of the default text used to explain the fix Jetpack connection action. |
||
545 | * |
||
546 | * @since 4.4.0 |
||
547 | * |
||
548 | * @param string $html The HTML to be displayed |
||
549 | */ |
||
550 | return apply_filters( 'jetpack_idc_first_fix_connection_explanation', $html ); |
||
551 | } |
||
552 | |||
553 | function get_first_step_fix_connection_button_text() { |
||
565 | |||
566 | View Code Duplication | function get_second_step_header_lead() { |
|
567 | $string = sprintf( |
||
568 | esc_html__( |
||
569 | 'Is %1$s the new home of %2$s?', |
||
570 | 'jetpack' |
||
571 | ), |
||
572 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( get_home_url() ) ), |
||
573 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( esc_url_raw( self::$wpcom_home_url ) ) ) |
||
574 | ); |
||
575 | |||
576 | /** |
||
577 | * Allows overriding of the default header text in the second step of the Safe Mode notice. |
||
578 | * |
||
579 | * @since 4.4.0 |
||
580 | * |
||
581 | * @param string $html The HTML to be displayed |
||
582 | */ |
||
583 | return apply_filters( 'jetpack_idc_second_step_header_lead', $string ); |
||
584 | } |
||
585 | |||
586 | View Code Duplication | function get_migrate_site_action_explanation() { |
|
587 | $html = wp_kses( |
||
588 | sprintf( |
||
589 | __( |
||
590 | 'Yes. <a href="%1$s">%2$s</a> is replacing <a href="%3$s">%4$s</a>. I would like to |
||
591 | migrate my stats and subscribers from <a href="%3$s">%4$s</a> to <a href="%1$s">%2$s</a>.', |
||
592 | 'jetpack' |
||
593 | ), |
||
594 | esc_url( get_home_url() ), |
||
595 | self::prepare_url_for_display( get_home_url() ), |
||
596 | esc_url( self::$wpcom_home_url ), |
||
597 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( esc_url_raw( self::$wpcom_home_url ) ) ) |
||
598 | ), |
||
599 | array( 'a' => array( 'href' => array() ) ) |
||
600 | ); |
||
601 | |||
602 | /** |
||
603 | * Allows overriding of the default text for explaining the migrate site action. |
||
604 | * |
||
605 | * @since 4.4.0 |
||
606 | * |
||
607 | * @param string $html The HTML to be displayed |
||
608 | */ |
||
609 | return apply_filters( 'jetpack_idc_migrate_site_explanation', $html ); |
||
610 | } |
||
611 | |||
612 | function get_migrate_site_button_text() { |
||
624 | |||
625 | View Code Duplication | function get_start_fresh_action_explanation() { |
|
626 | $html = wp_kses( |
||
627 | sprintf( |
||
628 | __( |
||
629 | 'No. <a href="%1$s">%2$s</a> is a new and different website that\'s separate from |
||
630 | <a href="%3$s">%4$s</a>. It requires a new connection to WordPress.com for new stats and subscribers.', |
||
631 | 'jetpack' |
||
632 | ), |
||
633 | esc_url( get_home_url() ), |
||
634 | self::prepare_url_for_display( get_home_url() ), |
||
635 | esc_url( self::$wpcom_home_url ), |
||
636 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( esc_url_raw( self::$wpcom_home_url ) ) ) |
||
637 | ), |
||
638 | array( 'a' => array( 'href' => array() ) ) |
||
639 | ); |
||
640 | |||
641 | /** |
||
642 | * Allows overriding of the default text for explaining the start fresh action. |
||
643 | * |
||
644 | * @since 4.4.0 |
||
645 | * |
||
646 | * @param string $html The HTML to be displayed |
||
647 | */ |
||
648 | return apply_filters( 'jetpack_idc_start_fresh_explanation', $html ); |
||
649 | } |
||
650 | |||
651 | function get_start_fresh_button_text() { |
||
663 | |||
664 | View Code Duplication | function get_unsure_prompt() { |
|
685 | |||
686 | View Code Duplication | function get_non_admin_notice_text() { |
|
707 | |||
708 | function get_non_admin_contact_admin_text() { |
||
720 | } |
||
721 | |||
722 | add_action( 'plugins_loaded', array( 'Jetpack_IDC', 'init' ) ); |
||
723 |