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 | /** |
||
| 20 | * Sets up class properties for REST API requests. |
||
| 21 | * |
||
| 22 | * @param WP_REST_Response $response Response from the endpoint. |
||
| 23 | */ |
||
| 24 | public function rest_api_init( $response ) { |
||
| 25 | parent::rest_api_init( $response ); |
||
| 26 | |||
| 27 | // Get domain for requested site. |
||
| 28 | $this->domain = ( new Status() )->get_site_suffix(); |
||
| 29 | |||
| 30 | return $response; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create the desired menu output. |
||
| 35 | */ |
||
| 36 | public function reregister_menu_items() { |
||
| 37 | parent::reregister_menu_items(); |
||
| 38 | |||
| 39 | $wp_admin = $this->should_link_to_wp_admin(); |
||
| 40 | |||
| 41 | $this->add_my_home_menu(); |
||
| 42 | |||
| 43 | // Not needed outside of wp-admin. |
||
| 44 | if ( ! $this->is_api_request ) { |
||
| 45 | $this->add_browse_sites_link(); |
||
| 46 | $this->add_site_card_menu(); |
||
| 47 | $this->add_new_site_link(); |
||
| 48 | } |
||
| 49 | |||
| 50 | $this->add_gutenberg_menus( $wp_admin ); |
||
| 51 | |||
| 52 | ksort( $GLOBALS['menu'] ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Adds the site switcher link if user has more than one site. |
||
| 57 | */ |
||
| 58 | public function add_browse_sites_link() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Adds a custom element class for Site Switcher menu item. |
||
| 70 | * |
||
| 71 | * @param array $menu Associative array of administration menu items. |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Adds a link to the menu to create a new site. |
||
| 89 | */ |
||
| 90 | public function add_new_site_link() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Adds site card component. |
||
| 109 | */ |
||
| 110 | public function add_site_card_menu() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Adds a custom element class and id for Site Card's menu item. |
||
| 154 | * |
||
| 155 | * @param array $menu Associative array of administration menu items. |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public function set_site_card_menu_class( array $menu ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Adds Stats menu. |
||
| 179 | */ |
||
| 180 | public function add_stats_menu() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Adds Upgrades menu. |
||
| 196 | */ |
||
| 197 | View Code Duplication | public function add_upgrades_menu() { |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Adds Appearance menu. |
||
| 207 | * |
||
| 208 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 209 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 210 | */ |
||
| 211 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
| 212 | $customize_url = parent::add_appearance_menu( $wp_admin_themes, $wp_admin_customize ); |
||
| 213 | |||
| 214 | remove_submenu_page( 'themes.php', 'theme-editor.php' ); |
||
| 215 | |||
| 216 | $user_can_customize = current_user_can( 'customize' ); |
||
| 217 | |||
| 218 | if ( $user_can_customize ) { |
||
| 219 | // If the user does not have the custom CSS option then present them with the CSS nudge upsell section instead. |
||
| 220 | $custom_css_section = '1' === get_option( 'custom-design-upgrade' ) ? 'jetpack_custom_css' : 'css_nudge'; //phpcs:ignore |
||
| 221 | $customize_custom_css_url = add_query_arg( array( 'autofocus' => array( 'section' => $custom_css_section ) ), $customize_url ); |
||
| 222 | add_submenu_page( 'themes.php', esc_attr__( 'Edit CSS', 'jetpack' ), __( 'Edit CSS', 'jetpack' ), 'customize', esc_url( $customize_custom_css_url ), null, 20 ); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Adds Users menu. |
||
| 228 | * |
||
| 229 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 230 | */ |
||
| 231 | public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Adds Tools menu. |
||
| 238 | * |
||
| 239 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 240 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 241 | */ |
||
| 242 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Adds Settings menu. |
||
| 249 | * |
||
| 250 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 251 | */ |
||
| 252 | View Code Duplication | public function add_options_menu( $wp_admin = false ) { |
|
| 253 | parent::add_options_menu( $wp_admin ); |
||
| 254 | |||
| 255 | 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 ); |
||
| 256 | |||
| 257 | // Replace sharing menu if it exists. See Publicize_UI::sharing_menu. |
||
| 258 | if ( remove_submenu_page( 'options-general.php', 'sharing' ) ) { |
||
| 259 | add_submenu_page( 'options-general.php', esc_attr__( 'Sharing Settings', 'jetpack' ), __( 'Sharing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/sharing-buttons/' . $this->domain, null, 30 ); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * 1. Remove the Gutenberg plugin menu |
||
| 265 | * 2. Re-add the Site Editor 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_gutenberg_menus( $wp_admin = false ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Whether to use wp-admin pages rather than Calypso. |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | public function should_link_to_wp_admin() { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Adds Plugins menu. |
||
| 314 | * |
||
| 315 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 316 | */ |
||
| 317 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Adds Jetpack menu. |
||
| 324 | */ |
||
| 325 | public function add_jetpack_menu() { |
||
| 329 | } |
||
| 330 |