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 WPcom_Admin_Menu 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 WPcom_Admin_Menu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class WPcom_Admin_Menu extends Admin_Menu { |
||
| 19 | /** |
||
| 20 | * WPcom_Admin_Menu constructor. |
||
| 21 | */ |
||
| 22 | protected function __construct() { |
||
| 23 | parent::__construct(); |
||
| 24 | |||
| 25 | add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) ); |
||
| 26 | add_action( 'admin_init', array( $this, 'sync_sidebar_collapsed_state' ) ); |
||
| 27 | add_action( 'admin_menu', array( $this, 'remove_submenus' ), 140 ); // After hookpress hook at 130. |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Sets up class properties for REST API requests. |
||
| 32 | * |
||
| 33 | * @param WP_REST_Response $response Response from the endpoint. |
||
| 34 | */ |
||
| 35 | public function rest_api_init( $response ) { |
||
| 36 | parent::rest_api_init( $response ); |
||
| 37 | |||
| 38 | // Get domain for requested site. |
||
| 39 | $this->domain = ( new Status() )->get_site_suffix(); |
||
| 40 | |||
| 41 | return $response; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Create the desired menu output. |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function reregister_menu_items() { |
|
| 48 | parent::reregister_menu_items(); |
||
| 49 | |||
| 50 | $this->add_my_home_menu(); |
||
| 51 | |||
| 52 | // Not needed outside of wp-admin. |
||
| 53 | if ( ! $this->is_api_request ) { |
||
| 54 | $this->add_browse_sites_link(); |
||
| 55 | $this->add_site_card_menu(); |
||
| 56 | $nudge = $this->get_upsell_nudge(); |
||
| 57 | if ( $nudge ) { |
||
| 58 | parent::add_upsell_nudge( $nudge ); |
||
| 59 | } |
||
| 60 | $this->add_new_site_link(); |
||
| 61 | } |
||
| 62 | |||
| 63 | ksort( $GLOBALS['menu'] ); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Adds the site switcher link if user has more than one site. |
||
| 68 | */ |
||
| 69 | public function add_browse_sites_link() { |
||
| 70 | if ( count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) { |
||
| 71 | return; |
||
| 72 | } |
||
| 73 | |||
| 74 | // Add the menu item. |
||
| 75 | add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 ); |
||
| 76 | add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) ); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Adds a custom element class for Site Switcher menu item. |
||
| 81 | * |
||
| 82 | * @param array $menu Associative array of administration menu items. |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 86 | foreach ( $menu as $key => $menu_item ) { |
||
| 87 | if ( 'site-switcher' !== $menu_item[3] ) { |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | |||
| 91 | $menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] ); |
||
| 92 | break; |
||
| 93 | } |
||
| 94 | |||
| 95 | return $menu; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Adds a link to the menu to create a new site. |
||
| 100 | */ |
||
| 101 | public function add_new_site_link() { |
||
| 102 | if ( count( get_blogs_of_user( get_current_user_id() ) ) > 1 ) { |
||
| 103 | return; |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->add_admin_menu_separator(); |
||
| 107 | add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' ); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Adds site card component. |
||
| 112 | */ |
||
| 113 | public function add_site_card_menu() { |
||
| 114 | $default = 'data:image/svg+xml,' . rawurlencode( '<svg class="gridicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Globe</title><rect fill-opacity="0" x="0" width="24" height="24"/><g><path fill="#fff" d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm0 18l2-2 1-1v-2h-2v-1l-1-1H9v3l2 2v1.93c-3.94-.494-7-3.858-7-7.93l1 1h2v-2h2l3-3V6h-2L9 5v-.41C9.927 4.21 10.94 4 12 4s2.073.212 3 .59V6l-1 1v2l1 1 3.13-3.13c.752.897 1.304 1.964 1.606 3.13H18l-2 2v2l1 1h2l.286.286C18.03 18.06 15.24 20 12 20z"/></g></svg>' ); |
||
| 115 | $icon = get_site_icon_url( 32, $default ); |
||
| 116 | $blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain; |
||
| 117 | |||
| 118 | if ( $default === $icon && blavatar_exists( $this->domain ) ) { |
||
| 119 | $icon = blavatar_url( $this->domain, 'img', 32 ); |
||
| 120 | } |
||
| 121 | |||
| 122 | $badge = ''; |
||
| 123 | if ( is_private_blog() ) { |
||
| 124 | $badge .= sprintf( |
||
| 125 | '<span class="site__badge site__badge-private">%s</span>', |
||
| 126 | wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ( function_exists( 'is_simple_site_redirect' ) && is_simple_site_redirect( $this->domain ) ) { |
||
| 131 | $badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>'; |
||
| 132 | } |
||
| 133 | |||
| 134 | if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) { |
||
| 135 | $badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>'; |
||
| 136 | } |
||
| 137 | |||
| 138 | $site_card = ' |
||
| 139 | <div class="site__info"> |
||
| 140 | <div class="site__title">%1$s</div> |
||
| 141 | <div class="site__domain">%2$s</div> |
||
| 142 | %3$s |
||
| 143 | </div>'; |
||
| 144 | |||
| 145 | $site_card = sprintf( |
||
| 146 | $site_card, |
||
| 147 | $blog_name, |
||
| 148 | $this->domain, |
||
| 149 | $badge |
||
| 150 | ); |
||
| 151 | |||
| 152 | add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
||
| 153 | add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Adds a custom element class and id for Site Card's menu item. |
||
| 158 | * |
||
| 159 | * @param array $menu Associative array of administration menu items. |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | public function set_site_card_menu_class( array $menu ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the first available upsell nudge. |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function get_upsell_nudge() { |
||
| 187 | require_lib( 'jetpack-jitm/jitm-engine' ); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Adds Stats menu. |
||
| 217 | */ |
||
| 218 | public function add_stats_menu() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Adds Upgrades menu. |
||
| 234 | * |
||
| 235 | * @param string $plan The current WPCOM plan of the blog. |
||
| 236 | */ |
||
| 237 | public function add_upgrades_menu( $plan = null ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Adds Appearance menu. |
||
| 258 | * |
||
| 259 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 260 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 261 | */ |
||
| 262 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Adds Users menu. |
||
| 282 | * |
||
| 283 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 284 | */ |
||
| 285 | public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Adds Settings menu. |
||
| 305 | * |
||
| 306 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 307 | */ |
||
| 308 | public function add_options_menu( $wp_admin = false ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Also remove the Gutenberg plugin menu. |
||
| 316 | * |
||
| 317 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 318 | */ |
||
| 319 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Whether to use wp-admin pages rather than Calypso. |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | public function should_link_to_wp_admin() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Adds Plugins menu. |
||
| 343 | * |
||
| 344 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 345 | */ |
||
| 346 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
| 366 | */ |
||
| 367 | public function ajax_sidebar_state() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Syncs the sidebar collapsed state from Calypso Preferences. |
||
| 390 | */ |
||
| 391 | public function sync_sidebar_collapsed_state() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Removes unwanted submenu items. |
||
| 400 | * |
||
| 401 | * These submenus are added across wp-content and should be removed together with these function calls. |
||
| 402 | */ |
||
| 403 | public function remove_submenus() { |
||
| 432 | } |
||
| 433 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.