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() { |
||
| 59 | if ( count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | // Add the menu item. |
||
| 64 | add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 ); |
||
| 65 | add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) ); |
||
| 66 | } |
||
| 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 ) { |
|
| 75 | foreach ( $menu as $key => $menu_item ) { |
||
| 76 | if ( 'site-switcher' !== $menu_item[3] ) { |
||
| 77 | continue; |
||
| 78 | } |
||
| 79 | |||
| 80 | $menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] ); |
||
| 81 | break; |
||
| 82 | } |
||
| 83 | |||
| 84 | return $menu; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Adds a link to the menu to create a new site. |
||
| 89 | */ |
||
| 90 | public function add_new_site_link() { |
||
| 91 | global $menu; |
||
| 92 | |||
| 93 | if ( count( get_blogs_of_user( get_current_user_id() ) ) > 1 ) { |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | // Attempt to get last position. |
||
| 98 | $position = 1000; |
||
| 99 | while ( isset( $menu[ $position ] ) ) { |
||
| 100 | $position++; |
||
| 101 | } |
||
| 102 | |||
| 103 | $this->add_admin_menu_separator( ++$position ); |
||
| 104 | add_menu_page( __( 'Add new site', 'jetpack' ), __( 'Add new site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt', ++$position ); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Adds site card component. |
||
| 109 | */ |
||
| 110 | public function add_site_card_menu() { |
||
| 111 | $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>' ); |
||
| 112 | $icon = get_site_icon_url( 32, $default ); |
||
| 113 | |||
| 114 | if ( $default === $icon && blavatar_exists( $this->domain ) ) { |
||
| 115 | $icon = blavatar_url( $this->domain, 'img', 32 ); |
||
| 116 | } |
||
| 117 | |||
| 118 | $badge = ''; |
||
| 119 | if ( is_private_blog() ) { |
||
| 120 | $badge .= sprintf( |
||
| 121 | '<span class="site__badge site__badge-private">%s</span>', |
||
| 122 | wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | if ( is_redirected_domain( $this->domain ) ) { |
||
| 127 | $badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>'; |
||
| 128 | } |
||
| 129 | |||
| 130 | if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) { |
||
| 131 | $badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>'; |
||
| 132 | } |
||
| 133 | |||
| 134 | $site_card = ' |
||
| 135 | <div class="site__info"> |
||
| 136 | <div class="site__title">%1$s</div> |
||
| 137 | <div class="site__domain">%2$s</div> |
||
| 138 | %3$s |
||
| 139 | </div>'; |
||
| 140 | |||
| 141 | $site_card = sprintf( |
||
| 142 | $site_card, |
||
| 143 | get_option( 'blogname' ), |
||
| 144 | $this->domain, |
||
| 145 | $badge |
||
| 146 | ); |
||
| 147 | |||
| 148 | add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
||
| 149 | add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
||
| 150 | } |
||
| 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 ) { |
||
| 159 | foreach ( $menu as $key => $menu_item ) { |
||
| 160 | if ( 'site-card' !== $menu_item[3] ) { |
||
| 161 | continue; |
||
| 162 | } |
||
| 163 | |||
| 164 | $classes = ' toplevel_page_site-card'; |
||
| 165 | if ( blavatar_exists( $this->domain ) ) { |
||
| 166 | $classes .= ' has-site-icon'; |
||
| 167 | } |
||
| 168 | |||
| 169 | $menu[ $key ][4] = $menu_item[4] . $classes; |
||
| 170 | $menu[ $key ][5] = 'toplevel_page_site_card'; |
||
| 171 | break; |
||
| 172 | } |
||
| 173 | |||
| 174 | return $menu; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Adds Stats menu. |
||
| 179 | */ |
||
| 180 | public function add_stats_menu() { |
||
| 181 | $menu_title = __( 'Stats', 'jetpack' ); |
||
| 182 | |||
| 183 | if ( ! $this->is_api_request ) { |
||
| 184 | $menu_title .= sprintf( |
||
| 185 | '<img class="sidebar-unified__sparkline" width="80" height="20" src="%1$s" alt="%2$s">', |
||
| 186 | esc_url( site_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ), |
||
| 187 | esc_attr__( 'Hourly views', 'jetpack' ) |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | add_menu_page( __( 'Stats', 'jetpack' ), $menu_title, 'edit_posts', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 ); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Adds Upgrades menu. |
||
| 196 | */ |
||
| 197 | public function add_upgrades_menu() { |
||
| 198 | parent::add_upgrades_menu(); |
||
| 199 | |||
| 200 | add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, 10 ); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Adds Appearance menu. |
||
| 205 | * |
||
| 206 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 207 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 208 | */ |
||
| 209 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
| 210 | $customize_url = parent::add_appearance_menu( $wp_admin_themes, $wp_admin_customize ); |
||
| 211 | |||
| 212 | remove_submenu_page( 'themes.php', 'theme-editor.php' ); |
||
| 213 | |||
| 214 | $user_can_customize = current_user_can( 'customize' ); |
||
| 215 | |||
| 216 | if ( $user_can_customize ) { |
||
| 217 | // If the user does not have the custom CSS option then present them with the CSS nudge upsell section instead. |
||
| 218 | $custom_css_section = '1' === get_option( 'custom-design-upgrade' ) ? 'jetpack_custom_css' : 'css_nudge'; //phpcs:ignore |
||
| 219 | $customize_custom_css_url = add_query_arg( array( 'autofocus' => array( 'section' => $custom_css_section ) ), $customize_url ); |
||
| 220 | add_submenu_page( 'themes.php', esc_attr__( 'Edit CSS', 'jetpack' ), __( 'Edit CSS', 'jetpack' ), 'customize', esc_url( $customize_custom_css_url ), null, 20 ); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Adds Users menu. |
||
| 226 | * |
||
| 227 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 228 | */ |
||
| 229 | public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 230 | if ( current_user_can( 'list_users' ) ) { |
||
| 231 | $submenus_to_update = array( |
||
| 232 | 'users.php' => 'https://wordpress.com/people/team/' . $this->domain, |
||
| 233 | 'grofiles-editor' => 'https://wordpress.com/me', |
||
| 234 | 'grofiles-user-settings' => 'https://wordpress.com/me/account', |
||
| 235 | ); |
||
| 236 | $this->update_submenus( 'users.php', $submenus_to_update ); |
||
| 237 | } else { |
||
| 238 | $submenus_to_update = array( |
||
| 239 | 'grofiles-editor' => 'https://wordpress.com/me', |
||
| 240 | 'grofiles-user-settings' => 'https://wordpress.com/me/account', |
||
| 241 | ); |
||
| 242 | $this->update_submenus( 'profile.php', $submenus_to_update ); |
||
| 243 | } |
||
| 244 | add_submenu_page( 'users.php', esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', 'https://wordpress.com/people/new/' . $this->domain, null, 1 ); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Adds Tools menu. |
||
| 249 | * |
||
| 250 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 251 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 252 | */ |
||
| 253 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Adds Settings menu. |
||
| 260 | * |
||
| 261 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function add_options_menu( $wp_admin = false ) { |
|
| 264 | parent::add_options_menu( $wp_admin ); |
||
| 265 | |||
| 266 | 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 ); |
||
| 267 | |||
| 268 | // Replace sharing menu if it exists. See Publicize_UI::sharing_menu. |
||
| 273 | |||
| 274 | /** |
||
| 275 | * 1. Remove the Gutenberg plugin menu |
||
| 276 | * 2. Re-add the Site Editor 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 ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Whether to use wp-admin pages rather than Calypso. |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | public function should_link_to_wp_admin() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Adds Plugins menu. |
||
| 325 | * |
||
| 326 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 327 | */ |
||
| 328 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 345 | } |
||
| 346 |