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 |
||
| 11 | class Jetpack_IDC { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var Jetpack_IDC |
||
| 15 | **/ |
||
| 16 | private static $instance = null; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The wpcom value of the home URL |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | static $wpcom_home_url; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Has safe mode been confirmed? |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | static $is_safe_mode_confirmed; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The current screen, which is set if the current user is a non-admin and this is an admin page. |
||
| 34 | * |
||
| 35 | * @var WP_Screen |
||
| 36 | */ |
||
| 37 | static $current_screen; |
||
| 38 | |||
| 39 | static function init() { |
||
| 46 | |||
| 47 | private function __construct() { |
||
| 48 | add_action( 'jetpack_sync_processed_actions', array( $this, 'maybe_clear_migrate_option' ) ); |
||
| 49 | if ( false === $urls_in_crisis = Jetpack::check_identity_crisis() ) { |
||
| 50 | return; |
||
| 51 | } |
||
| 52 | |||
| 53 | self::$wpcom_home_url = $urls_in_crisis['wpcom_home']; |
||
| 54 | add_action( 'init', array( $this, 'wordpress_init' ) ); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Gets the link to the support document used to explain Safe Mode to users |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | public static function get_safe_mod_doc_url() { |
||
| 63 | return \Jetpack::build_redirect_url( 'jp-support-safe-mode' ); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * This method loops through the array of processed items from sync and checks if one of the items was the |
||
| 68 | * home_url or site_url callable. If so, then we delete the jetpack_migrate_for_idc option. |
||
| 69 | * |
||
| 70 | * @param $processed_items array Array of processed items that were synced to WordPress.com |
||
| 71 | */ |
||
| 72 | function maybe_clear_migrate_option( $processed_items ) { |
||
| 73 | foreach ( (array) $processed_items as $item ) { |
||
| 74 | |||
| 75 | // First, is this item a jetpack_sync_callable action? If so, then proceed. |
||
| 76 | $callable_args = ( is_array( $item ) && isset( $item[0], $item[1] ) && 'jetpack_sync_callable' === $item[0] ) |
||
| 77 | ? $item[1] |
||
| 78 | : null; |
||
| 79 | |||
| 80 | // Second, if $callable_args is set, check if the callable was home_url or site_url. If so, |
||
| 81 | // clear the migrate option. |
||
| 82 | if ( |
||
| 83 | isset( $callable_args, $callable_args[0] ) |
||
| 84 | && ( 'home_url' === $callable_args[0] || 'site_url' === $callable_args[1] ) |
||
| 85 | ) { |
||
| 86 | Jetpack_Options::delete_option( 'migrate_for_idc' ); |
||
| 87 | break; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | function wordpress_init() { |
||
| 93 | if ( ! current_user_can( 'jetpack_disconnect' ) && is_admin() ) { |
||
| 94 | add_action( 'admin_notices', array( $this, 'display_non_admin_idc_notice' ) ); |
||
| 95 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_idc_notice_files' ) ); |
||
| 96 | add_action( 'current_screen', array( $this, 'non_admins_current_screen_check' ) ); |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ( |
||
| 101 | isset( $_GET['jetpack_idc_clear_confirmation'], $_GET['_wpnonce'] ) && |
||
| 102 | wp_verify_nonce( $_GET['_wpnonce'], 'jetpack_idc_clear_confirmation' ) |
||
| 103 | ) { |
||
| 104 | Jetpack_Options::delete_option( 'safe_mode_confirmed' ); |
||
| 105 | self::$is_safe_mode_confirmed = false; |
||
| 106 | } else { |
||
| 107 | self::$is_safe_mode_confirmed = (bool) Jetpack_Options::get_option( 'safe_mode_confirmed' ); |
||
| 108 | } |
||
| 109 | |||
| 110 | // 121 Priority so that it's the most inner Jetpack item in the admin bar. |
||
| 111 | add_action( 'admin_bar_menu', array( $this, 'display_admin_bar_button' ), 121 ); |
||
| 112 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_bar_css' ) ); |
||
| 113 | |||
| 114 | if ( is_admin() && ! self::$is_safe_mode_confirmed ) { |
||
| 115 | add_action( 'admin_notices', array( $this, 'display_idc_notice' ) ); |
||
| 116 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_idc_notice_files' ) ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | function non_admins_current_screen_check( $current_screen ) { |
||
| 121 | self::$current_screen = $current_screen; |
||
| 122 | if ( isset( $current_screen->id ) && 'toplevel_page_jetpack' == $current_screen->id ) { |
||
| 123 | return null; |
||
| 124 | } |
||
| 125 | |||
| 126 | // If the user has dismissed the notice, and we're not currently on a Jetpack page, |
||
| 127 | // then do not show the non-admin notice. |
||
| 128 | if ( isset( $_COOKIE, $_COOKIE['jetpack_idc_dismiss_notice'] ) ) { |
||
| 129 | remove_action( 'admin_notices', array( $this, 'display_non_admin_idc_notice' ) ); |
||
| 130 | remove_action( 'admin_enqueue_scripts', array( $this, 'enqueue_idc_notice_files' ) ); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | function display_admin_bar_button() { |
||
| 135 | global $wp_admin_bar; |
||
| 136 | |||
| 137 | $href = is_admin() |
||
| 138 | ? add_query_arg( 'jetpack_idc_clear_confirmation', '1' ) |
||
| 139 | : add_query_arg( 'jetpack_idc_clear_confirmation', '1', admin_url() ); |
||
| 140 | |||
| 141 | $href = wp_nonce_url( $href, 'jetpack_idc_clear_confirmation' ); |
||
| 142 | |||
| 143 | $title = sprintf( |
||
| 144 | '<span class="jp-idc-admin-bar">%s %s</span>', |
||
| 145 | '<span class="dashicons dashicons-warning"></span>', |
||
| 146 | esc_html__( 'Jetpack Safe Mode', 'jetpack' ) |
||
| 147 | ); |
||
| 148 | |||
| 149 | $menu = array( |
||
| 150 | 'id' => 'jetpack-idc', |
||
| 151 | 'title' => $title, |
||
| 152 | 'href' => esc_url( $href ), |
||
| 153 | 'parent' => 'top-secondary', |
||
| 154 | ); |
||
| 155 | |||
| 156 | if ( ! self::$is_safe_mode_confirmed ) { |
||
| 157 | $menu['meta'] = array( |
||
| 158 | 'class' => 'hide', |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | $wp_admin_bar->add_node( $menu ); |
||
| 163 | } |
||
| 164 | |||
| 165 | static function prepare_url_for_display( $url ) { |
||
| 166 | return untrailingslashit( Jetpack::normalize_url_protocol_agnostic( $url ) ); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Clears all IDC specific options. This method is used on disconnect and reconnect. |
||
| 171 | */ |
||
| 172 | static function clear_all_idc_options() { |
||
| 173 | // If the site is currently in IDC, let's also clear the VaultPress connection options. |
||
| 174 | // We have to check if the site is in IDC, otherwise we'd be clearing the VaultPress |
||
| 175 | // connection any time the Jetpack connection is cycled. |
||
| 176 | if ( Jetpack::validate_sync_error_idc_option() ) { |
||
| 177 | delete_option( 'vaultpress' ); |
||
| 178 | delete_option( 'vaultpress_auto_register' ); |
||
| 179 | } |
||
| 180 | |||
| 181 | Jetpack_Options::delete_option( |
||
| 182 | array( |
||
| 183 | 'sync_error_idc', |
||
| 184 | 'safe_mode_confirmed', |
||
| 185 | 'migrate_for_idc', |
||
| 186 | ) |
||
| 187 | ); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Does the current admin page have help tabs? |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | function admin_page_has_help_tabs() { |
||
| 196 | if ( ! function_exists( 'get_current_screen' ) ) { |
||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | $current_screen = get_current_screen(); |
||
| 201 | $tabs = $current_screen->get_help_tabs(); |
||
| 202 | |||
| 203 | return ! empty( $tabs ); |
||
| 204 | } |
||
| 205 | |||
| 206 | function display_non_admin_idc_notice() { |
||
| 207 | $classes = 'jp-idc-notice inline is-non-admin notice notice-warning'; |
||
| 208 | if ( isset( self::$current_screen ) && 'toplevel_page_jetpack' != self::$current_screen->id ) { |
||
| 209 | $classes .= ' is-dismissible'; |
||
| 210 | } |
||
| 211 | |||
| 212 | if ( $this->admin_page_has_help_tabs() ) { |
||
| 213 | $classes .= ' has-help-tabs'; |
||
| 214 | } |
||
| 215 | ?> |
||
| 216 | |||
| 217 | <div class="<?php echo $classes; ?>"> |
||
| 218 | <?php $this->render_notice_header(); ?> |
||
| 219 | <div class="jp-idc-notice__content-header"> |
||
| 220 | <h3 class="jp-idc-notice__content-header__lead"> |
||
| 221 | <?php echo $this->get_non_admin_notice_text(); ?> |
||
| 222 | </h3> |
||
| 223 | |||
| 224 | <p class="jp-idc-notice__content-header__explanation"> |
||
| 225 | <?php echo $this->get_non_admin_contact_admin_text(); ?> |
||
| 226 | </p> |
||
| 227 | </div> |
||
| 228 | </div> |
||
| 229 | <?php |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * First "step" of the IDC mitigation. Will provide some messaging and two options/buttons. |
||
| 234 | * "Confirm Staging" - Dismiss the notice and continue on with our lives in staging mode. |
||
| 235 | * "Fix Jetpack Connection" - Will disconnect the site and start the mitigation... |
||
| 236 | */ |
||
| 237 | function display_idc_notice() { |
||
| 238 | $classes = 'jp-idc-notice inline notice notice-warning'; |
||
| 239 | if ( $this->admin_page_has_help_tabs() ) { |
||
| 240 | $classes .= ' has-help-tabs'; |
||
| 241 | } |
||
| 242 | ?> |
||
| 243 | <div class="<?php echo $classes; ?>"> |
||
| 244 | <?php $this->render_notice_header(); ?> |
||
| 245 | <?php $this->render_notice_first_step(); ?> |
||
| 246 | <?php $this->render_notice_second_step(); ?> |
||
| 247 | </div> |
||
| 248 | <?php |
||
| 249 | } |
||
| 250 | |||
| 251 | function enqueue_admin_bar_css() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Enqueue scripts for the notice |
||
| 262 | */ |
||
| 263 | function enqueue_idc_notice_files() { |
||
| 264 | |||
| 265 | wp_enqueue_script( |
||
| 266 | 'jetpack-idc-js', |
||
| 267 | Assets::get_file_url_for_environment( '_inc/build/idc-notice.min.js', '_inc/idc-notice.js' ), |
||
| 268 | array( 'jquery' ), |
||
| 269 | JETPACK__VERSION, |
||
| 270 | true |
||
| 321 | |||
| 322 | function render_notice_header() { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Is a container for the error notices. |
||
| 342 | * Will be shown/controlled by jQuery in idc-notice.js |
||
| 343 | */ |
||
| 344 | function render_error_notice() { |
||
| 366 | |||
| 367 | function render_notice_first_step() { |
||
| 404 | |||
| 405 | function render_notice_second_step() { |
||
| 443 | |||
| 444 | function get_first_step_header_lead() { |
||
| 467 | |||
| 468 | View Code Duplication | function get_first_step_header_explanation() { |
|
| 490 | |||
| 491 | View Code Duplication | function get_confirm_safe_mode_action_explanation() { |
|
| 514 | |||
| 515 | function get_confirm_safe_mode_button_text() { |
||
| 527 | |||
| 528 | View Code Duplication | function get_first_step_fix_connection_action_explanation() { |
|
| 551 | |||
| 552 | function get_first_step_fix_connection_button_text() { |
||
| 564 | |||
| 565 | function get_second_step_header_lead() { |
||
| 584 | |||
| 585 | View Code Duplication | function get_migrate_site_action_explanation() { |
|
| 610 | |||
| 611 | function get_migrate_site_button_text() { |
||
| 623 | |||
| 624 | View Code Duplication | function get_start_fresh_action_explanation() { |
|
| 649 | |||
| 650 | function get_start_fresh_button_text() { |
||
| 662 | |||
| 663 | View Code Duplication | function get_unsure_prompt() { |
|
| 684 | |||
| 685 | View Code Duplication | function get_non_admin_notice_text() { |
|
| 706 | |||
| 707 | function get_non_admin_contact_admin_text() { |
||
| 719 | } |
||
| 720 | |||
| 722 |