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() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Adds a custom element class and id for Site Card's menu item. |
||
| 155 | * |
||
| 156 | * @param array $menu Associative array of administration menu items. |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function set_site_card_menu_class( array $menu ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Adds Stats menu. |
||
| 180 | */ |
||
| 181 | public function add_stats_menu() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Adds Upgrades menu. |
||
| 197 | */ |
||
| 198 | public function add_upgrades_menu() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Adds Appearance menu. |
||
| 206 | * |
||
| 207 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 208 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 209 | */ |
||
| 210 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Adds Users menu. |
||
| 227 | * |
||
| 228 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 229 | */ |
||
| 230 | public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Adds Tools menu. |
||
| 250 | * |
||
| 251 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 252 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 253 | */ |
||
| 254 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Adds Settings menu. |
||
| 261 | * |
||
| 262 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 263 | */ |
||
| 264 | View Code Duplication | public function add_options_menu( $wp_admin = false ) { |
|
| 274 | |||
| 275 | /** |
||
| 276 | * 1. Remove the Gutenberg plugin menu |
||
| 277 | * 2. Re-add the Site Editor menu |
||
| 278 | * |
||
| 279 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 280 | */ |
||
| 281 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Whether to use wp-admin pages rather than Calypso. |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function should_link_to_wp_admin() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Adds Plugins menu. |
||
| 326 | * |
||
| 327 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 328 | */ |
||
| 329 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 346 | } |
||
| 347 |