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 |
||
| 17 | class WPcom_Admin_Menu extends Admin_Menu { |
||
| 18 | /** |
||
| 19 | * WPcom_Admin_Menu constructor. |
||
| 20 | */ |
||
| 21 | protected function __construct() { |
||
| 22 | parent::__construct(); |
||
| 23 | |||
| 24 | add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) ); |
||
| 25 | add_action( 'admin_init', array( $this, 'sync_sidebar_collapsed_state' ) ); |
||
| 26 | add_action( 'admin_menu', array( $this, 'remove_submenus' ), 140 ); // After hookpress hook at 130. |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Sets up class properties for REST API requests. |
||
| 31 | * |
||
| 32 | * @param WP_REST_Response $response Response from the endpoint. |
||
| 33 | */ |
||
| 34 | public function rest_api_init( $response ) { |
||
| 35 | parent::rest_api_init( $response ); |
||
| 36 | |||
| 37 | // Get domain for requested site. |
||
| 38 | $this->domain = ( new Status() )->get_site_suffix(); |
||
| 39 | |||
| 40 | return $response; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Create the desired menu output. |
||
| 45 | */ |
||
| 46 | View Code Duplication | public function reregister_menu_items() { |
|
| 47 | parent::reregister_menu_items(); |
||
| 48 | |||
| 49 | $this->add_my_home_menu(); |
||
| 50 | |||
| 51 | // Not needed outside of wp-admin. |
||
| 52 | if ( ! $this->is_api_request ) { |
||
| 53 | $this->add_browse_sites_link(); |
||
| 54 | $this->add_site_card_menu(); |
||
| 55 | $this->add_new_site_link(); |
||
| 56 | } |
||
| 57 | |||
| 58 | ksort( $GLOBALS['menu'] ); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Adds the site switcher link if user has more than one site. |
||
| 63 | */ |
||
| 64 | public function add_browse_sites_link() { |
||
| 65 | if ( count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | // Add the menu item. |
||
| 70 | add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 ); |
||
| 71 | add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) ); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Adds a custom element class for Site Switcher menu item. |
||
| 76 | * |
||
| 77 | * @param array $menu Associative array of administration menu items. |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 81 | foreach ( $menu as $key => $menu_item ) { |
||
| 82 | if ( 'site-switcher' !== $menu_item[3] ) { |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | |||
| 86 | $menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] ); |
||
| 87 | break; |
||
| 88 | } |
||
| 89 | |||
| 90 | return $menu; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Adds a link to the menu to create a new site. |
||
| 95 | */ |
||
| 96 | public function add_new_site_link() { |
||
| 97 | if ( count( get_blogs_of_user( get_current_user_id() ) ) > 1 ) { |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->add_admin_menu_separator(); |
||
| 102 | add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Adds site card component. |
||
| 107 | */ |
||
| 108 | public function add_site_card_menu() { |
||
| 109 | $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>' ); |
||
| 110 | $icon = get_site_icon_url( 32, $default ); |
||
| 111 | $blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain; |
||
| 112 | |||
| 113 | if ( $default === $icon && blavatar_exists( $this->domain ) ) { |
||
| 114 | $icon = blavatar_url( $this->domain, 'img', 32 ); |
||
| 115 | } |
||
| 116 | |||
| 117 | $badge = ''; |
||
| 118 | if ( is_private_blog() ) { |
||
| 119 | $badge .= sprintf( |
||
| 120 | '<span class="site__badge site__badge-private">%s</span>', |
||
| 121 | wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | |||
| 125 | if ( is_redirected_domain( $this->domain ) ) { |
||
| 126 | $badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>'; |
||
| 127 | } |
||
| 128 | |||
| 129 | if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) { |
||
| 130 | $badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>'; |
||
| 131 | } |
||
| 132 | |||
| 133 | $site_card = ' |
||
| 134 | <div class="site__info"> |
||
| 135 | <div class="site__title">%1$s</div> |
||
| 136 | <div class="site__domain">%2$s</div> |
||
| 137 | %3$s |
||
| 138 | </div>'; |
||
| 139 | |||
| 140 | $site_card = sprintf( |
||
| 141 | $site_card, |
||
| 142 | $blog_name, |
||
| 143 | $this->domain, |
||
| 144 | $badge |
||
| 145 | ); |
||
| 146 | |||
| 147 | add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
||
| 148 | add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Adds a custom element class and id for Site Card's menu item. |
||
| 153 | * |
||
| 154 | * @param array $menu Associative array of administration menu items. |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function set_site_card_menu_class( array $menu ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Adds Stats menu. |
||
| 178 | */ |
||
| 179 | public function add_stats_menu() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Adds Upgrades menu. |
||
| 195 | * |
||
| 196 | * @param string $plan The current WPCOM plan of the blog. |
||
|
|
|||
| 197 | */ |
||
| 198 | public function add_upgrades_menu( $plan = null ) { |
||
| 199 | if ( class_exists( 'WPCOM_Store_API' ) ) { |
||
| 200 | $products = \WPCOM_Store_API::get_current_plan( get_current_blog_id() ); |
||
| 201 | if ( array_key_exists( 'product_name_short', $products ) ) { |
||
| 202 | $plan = $products['product_name_short']; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | parent::add_upgrades_menu( $plan ); |
||
| 206 | |||
| 207 | $last_upgrade_submenu_position = $this->get_submenu_item_count( 'paid-upgrades.php' ); |
||
| 208 | |||
| 209 | add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, $last_upgrade_submenu_position - 1 ); |
||
| 210 | |||
| 211 | /** This filter is already documented in modules/masterbar/admin-menu/class-atomic-admin-menu.php */ |
||
| 212 | View Code Duplication | if ( apply_filters( 'jetpack_show_wpcom_upgrades_email_menu', false ) ) { |
|
| 213 | add_submenu_page( 'paid-upgrades.php', __( 'Emails', 'jetpack' ), __( 'Emails', 'jetpack' ), 'manage_options', 'https://wordpress.com/email/' . $this->domain, null, $last_upgrade_submenu_position ); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Adds Appearance menu. |
||
| 219 | * |
||
| 220 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 221 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 222 | */ |
||
| 223 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
| 224 | // $wp_admin_themes can have a `true` value here if the user has activated the "Show advanced dashboard pages" account setting. |
||
| 225 | // We force $wp_admin_themes to `false` anyways, since Simple sites should always see the Calypso Theme showcase. |
||
| 226 | $wp_admin_themes = false; |
||
| 227 | $customize_url = parent::add_appearance_menu( $wp_admin_themes, $wp_admin_customize ); |
||
| 228 | |||
| 229 | $this->hide_submenu_page( 'themes.php', 'theme-editor.php' ); |
||
| 230 | |||
| 231 | $user_can_customize = current_user_can( 'customize' ); |
||
| 232 | |||
| 233 | if ( $user_can_customize ) { |
||
| 234 | // If the user does not have the custom CSS option then present them with the CSS nudge upsell section instead. |
||
| 235 | $custom_css_section = '1' === get_option( 'custom-design-upgrade' ) ? 'jetpack_custom_css' : 'css_nudge'; //phpcs:ignore |
||
| 236 | $customize_custom_css_url = add_query_arg( array( 'autofocus' => array( 'section' => $custom_css_section ) ), $customize_url ); |
||
| 237 | add_submenu_page( 'themes.php', esc_attr__( 'Additional CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'customize', esc_url( $customize_custom_css_url ), null, 20 ); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Adds Users menu. |
||
| 243 | * |
||
| 244 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 245 | */ |
||
| 246 | public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 247 | if ( current_user_can( 'list_users' ) ) { |
||
| 248 | $submenus_to_update = array( |
||
| 249 | 'users.php' => 'https://wordpress.com/people/team/' . $this->domain, |
||
| 250 | 'grofiles-editor' => 'https://wordpress.com/me', |
||
| 251 | 'grofiles-user-settings' => 'https://wordpress.com/me/account', |
||
| 252 | ); |
||
| 253 | $this->update_submenus( 'users.php', $submenus_to_update ); |
||
| 254 | } else { |
||
| 255 | $submenus_to_update = array( |
||
| 256 | 'grofiles-editor' => 'https://wordpress.com/me', |
||
| 257 | 'grofiles-user-settings' => 'https://wordpress.com/me/account', |
||
| 258 | ); |
||
| 259 | $this->update_submenus( 'profile.php', $submenus_to_update ); |
||
| 260 | } |
||
| 261 | add_submenu_page( 'users.php', esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', 'https://wordpress.com/people/new/' . $this->domain, null, 1 ); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Adds Settings menu. |
||
| 266 | * |
||
| 267 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 268 | */ |
||
| 269 | public function add_options_menu( $wp_admin = false ) { |
||
| 270 | parent::add_options_menu( $wp_admin ); |
||
| 271 | |||
| 272 | add_submenu_page( 'options-general.php', esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 6 ); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Also remove the Gutenberg plugin menu. |
||
| 277 | * |
||
| 278 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 279 | */ |
||
| 280 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Whether to use wp-admin pages rather than Calypso. |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function should_link_to_wp_admin() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Adds Plugins menu. |
||
| 304 | * |
||
| 305 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 306 | */ |
||
| 307 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
| 327 | */ |
||
| 328 | public function ajax_sidebar_state() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Syncs the sidebar collapsed state from Calypso Preferences. |
||
| 351 | */ |
||
| 352 | public function sync_sidebar_collapsed_state() { |
||
| 353 | $calypso_preferences = get_user_attribute( get_current_user_id(), 'calypso_preferences' ); |
||
| 354 | |||
| 355 | $sidebar_collapsed = isset( $calypso_preferences['sidebarCollapsed'] ) ? $calypso_preferences['sidebarCollapsed'] : false; |
||
| 356 | set_user_setting( 'mfold', $sidebar_collapsed ? 'f' : 'o' ); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Removes unwanted submenu items. |
||
| 361 | * |
||
| 362 | * These submenus are added across wp-content and should be removed together with these function calls. |
||
| 363 | */ |
||
| 364 | public function remove_submenus() { |
||
| 393 | } |
||
| 394 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.