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 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 Admin_Menu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Admin_Menu { |
||
| 17 | /** |
||
| 18 | * Holds class instance. |
||
| 19 | * |
||
| 20 | * @var Admin_Menu |
||
| 21 | */ |
||
| 22 | private static $instance; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Whether the current request is a REST API request. |
||
| 26 | * |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | private $is_api_request; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Admin_Menu constructor. |
||
| 33 | */ |
||
| 34 | private function __construct() { |
||
| 35 | if ( jetpack_is_atomic_site() ) { |
||
| 36 | add_action( |
||
| 37 | 'admin_menu', |
||
| 38 | function () { |
||
| 39 | remove_action( 'admin_menu', 'gutenberg_menu', 9 ); |
||
| 40 | }, |
||
| 41 | 0 |
||
| 42 | ); |
||
| 43 | } |
||
| 44 | |||
| 45 | add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 ); |
||
| 46 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
||
| 47 | add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); |
||
| 48 | add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Returns class instance. |
||
| 53 | * |
||
| 54 | * @return Admin_Menu |
||
| 55 | */ |
||
| 56 | public static function get_instance() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Create the desired menu output. |
||
| 66 | */ |
||
| 67 | public function reregister_menu_items() { |
||
| 68 | $this->is_api_request = ( defined( 'REST_API_PLUGINS' ) && REST_API_PLUGINS ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ); |
||
| 69 | |||
| 70 | $domain = ( new Status() )->get_site_suffix(); |
||
| 71 | |||
| 72 | // Not needed outside of wp-admin. |
||
| 73 | if ( ! $this->is_api_request && ( $this->is_wpcom_site() || jetpack_is_atomic_site() ) ) { |
||
| 74 | $this->add_browse_sites_link(); |
||
| 75 | $this->add_site_card_menu( $domain ); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Whether links should point to Calypso or wp-admin. |
||
| 80 | * |
||
| 81 | * Options: |
||
| 82 | * true - Calypso. |
||
| 83 | * false - wp-admin. |
||
| 84 | * |
||
| 85 | * @module masterbar |
||
| 86 | * @since 9.3.0 |
||
| 87 | * |
||
| 88 | * @param bool $calypso Whether menu item URLs should point to Calypso. |
||
| 89 | */ |
||
| 90 | $calypso = apply_filters( 'jetpack_admin_menu_use_calypso_links', true ); |
||
| 91 | |||
| 92 | // Remove separators. |
||
| 93 | remove_menu_page( 'separator1' ); |
||
| 94 | |||
| 95 | $this->add_my_home_menu( $domain, $calypso ); |
||
| 96 | $this->add_stats_menu( $domain ); |
||
| 97 | $this->add_upgrades_menu( $domain ); |
||
| 98 | $this->add_posts_menu( $domain, $calypso ); |
||
| 99 | $this->add_media_menu( $domain, $calypso ); |
||
| 100 | $this->add_page_menu( $domain, $calypso ); |
||
| 101 | $this->add_comments_menu( $domain, $calypso ); |
||
| 102 | $this->add_jetpack_menu( $domain ); |
||
| 103 | $this->add_appearance_menu( $domain, $calypso ); |
||
| 104 | $this->add_plugins_menu( $domain ); |
||
| 105 | $this->add_users_menu( $domain, $calypso ); |
||
| 106 | $this->add_tools_menu( $domain, $calypso ); |
||
| 107 | $this->add_options_menu( $domain ); |
||
| 108 | |||
| 109 | ksort( $GLOBALS['menu'] ); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Adds the site switcher link if user has more than one site. |
||
| 114 | */ |
||
| 115 | public function add_browse_sites_link() { |
||
| 116 | if ( jetpack_is_atomic_site() ) { |
||
| 117 | $wpcom_user_data = ( new Connection_Manager() )->get_connected_user_data(); |
||
| 118 | |||
| 119 | if ( $wpcom_user_data['site_count'] < 2 ) { |
||
| 120 | return; |
||
| 121 | } |
||
| 122 | } elseif ( ! is_multisite() || count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) { |
||
| 123 | return; |
||
| 124 | } |
||
| 125 | |||
| 126 | // Add the menu item. |
||
| 127 | add_menu_page( __( 'Browse sites', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 ); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Adds site card component. |
||
| 132 | * |
||
| 133 | * @param string $domain Site domain. |
||
| 134 | */ |
||
| 135 | public function add_site_card_menu( $domain ) { |
||
| 136 | $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>' ); |
||
| 137 | $icon = get_site_icon_url( 32, $default ); |
||
| 138 | |||
| 139 | if ( $default === $icon && function_exists( 'blavatar_exists' ) && blavatar_exists( $domain ) && function_exists( 'blavatar_url' ) ) { |
||
| 140 | $icon = blavatar_url( $domain, 'img', 32 ); |
||
| 141 | } |
||
| 142 | |||
| 143 | $badge = ''; |
||
| 144 | if ( $this->is_wpcom_site() ) { |
||
| 145 | View Code Duplication | if ( function_exists( 'is_private_blog' ) && function_exists( 'wpcom_is_coming_soon' ) && is_private_blog() ) { |
|
| 146 | $badge .= sprintf( |
||
| 147 | '<span class="site__badge site__badge-private">%s</span>', |
||
| 148 | wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | |||
| 152 | if ( function_exists( 'is_redirected_domain' ) && is_redirected_domain( $domain ) ) { |
||
| 153 | $badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>'; |
||
| 154 | } |
||
| 155 | |||
| 156 | if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) { |
||
| 157 | $badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>'; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | View Code Duplication | if ( jetpack_is_atomic_site() ) { |
|
| 162 | if ( function_exists( 'site_is_private' ) && function_exists( 'site_is_coming_soon' ) && site_is_private() ) { |
||
| 163 | $badge .= sprintf( |
||
| 164 | '<span class="site__badge site__badge-private">%s</span>', |
||
| 165 | site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | $site_card = ' |
||
| 171 | <div class="site__info"> |
||
| 172 | <div class="site__title">%1$s</div> |
||
| 173 | <div class="site__domain">%2$s</div> |
||
| 174 | %3$s |
||
| 175 | </div>'; |
||
| 176 | |||
| 177 | $site_card = sprintf( |
||
| 178 | $site_card, |
||
| 179 | get_option( 'blogname' ), |
||
| 180 | $domain, |
||
| 181 | $badge |
||
| 182 | ); |
||
| 183 | |||
| 184 | add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
||
| 185 | add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Adds a custom element class and id for Site Card's menu item. |
||
| 190 | * |
||
| 191 | * @param array $menu Associative array of administration menu items. |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function set_site_card_menu_class( array $menu ) { |
||
| 195 | foreach ( $menu as $key => $menu_item ) { |
||
| 196 | if ( 'site-card' !== $menu_item[3] ) { |
||
| 197 | continue; |
||
| 198 | } |
||
| 199 | |||
| 200 | $classes = ' toplevel_page_site-card'; |
||
| 201 | |||
| 202 | // webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon. |
||
| 203 | if ( |
||
| 204 | ( function_exists( 'blavatar_exists' ) && blavatar_exists( ( new Status() )->get_site_suffix() ) ) || |
||
| 205 | ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) |
||
| 206 | ) { |
||
| 207 | $classes .= ' has-site-icon'; |
||
| 208 | } |
||
| 209 | |||
| 210 | $menu[ $key ][4] = $menu_item[4] . $classes; |
||
| 211 | $menu[ $key ][5] = 'toplevel_page_site_card'; |
||
| 212 | break; |
||
| 213 | } |
||
| 214 | |||
| 215 | return $menu; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Adds My Home menu. |
||
| 220 | * |
||
| 221 | * @param string $domain Site domain. |
||
| 222 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 223 | */ |
||
| 224 | public function add_my_home_menu( $domain, $calypso = true ) { |
||
| 225 | global $submenu; |
||
| 226 | |||
| 227 | $menu_slug = $calypso ? 'https://wordpress.com/home/' . $domain : 'index.php'; |
||
| 228 | |||
| 229 | remove_menu_page( 'index.php' ); |
||
| 230 | remove_submenu_page( 'index.php', 'index.php' ); |
||
| 231 | |||
| 232 | add_menu_page( __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 'dashicons-admin-home', 2 ); |
||
| 233 | |||
| 234 | // Only add submenu when there are other submenu items. |
||
| 235 | if ( ! empty( $submenu['index.php'] ) ) { |
||
| 236 | add_submenu_page( $menu_slug, __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 1 ); |
||
| 237 | } |
||
| 238 | |||
| 239 | $this->migrate_submenus( 'index.php', $menu_slug ); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Adds Stats menu. |
||
| 244 | * |
||
| 245 | * @param string $domain Site domain. |
||
| 246 | */ |
||
| 247 | public function add_stats_menu( $domain ) { |
||
| 248 | $menu_title = __( 'Stats', 'jetpack' ); |
||
| 249 | |||
| 250 | if ( $this->is_wpcom_site() && ! $this->is_api_request ) { |
||
| 251 | $menu_title .= sprintf( |
||
| 252 | '<img class="sidebar-unified__sparkline" width="80" height="20" src="%1$s" alt="%2$s">', |
||
| 253 | esc_url( home_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ), |
||
| 254 | esc_attr__( 'Hourly views', 'jetpack' ) |
||
| 255 | ); |
||
| 256 | } |
||
| 257 | |||
| 258 | add_menu_page( __( 'Stats', 'jetpack' ), $menu_title, 'edit_posts', 'https://wordpress.com/stats/day/' . $domain, null, 'dashicons-chart-bar', 3 ); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Adds Upgrades menu. |
||
| 263 | * |
||
| 264 | * @param string $domain Site domain. |
||
| 265 | */ |
||
| 266 | public function add_upgrades_menu( $domain ) { |
||
| 267 | remove_menu_page( 'paid-upgrades.php' ); |
||
| 268 | |||
| 269 | $menu_slug = 'https://wordpress.com/plans/' . $domain; |
||
| 270 | |||
| 271 | add_menu_page( __( 'Upgrades', 'jetpack' ), __( 'Upgrades', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-cart', 4 ); |
||
| 272 | add_submenu_page( $menu_slug, __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', $menu_slug, null, 1 ); |
||
| 273 | add_submenu_page( $menu_slug, __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $domain, null, 2 ); |
||
| 274 | add_submenu_page( $menu_slug, __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $domain, null, 3 ); |
||
| 275 | |||
| 276 | $this->migrate_submenus( 'paid-upgrades.php', 'https://wordpress.com/plans/' . $domain ); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Adds Posts menu. |
||
| 281 | * |
||
| 282 | * @param string $domain Site domain. |
||
| 283 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 284 | */ |
||
| 285 | View Code Duplication | public function add_posts_menu( $domain, $calypso = true ) { |
|
| 286 | if ( ! $calypso ) { |
||
| 287 | return; |
||
| 288 | } |
||
| 289 | |||
| 290 | $ptype_obj = get_post_type_object( 'post' ); |
||
| 291 | $menu_slug = 'https://wordpress.com/posts/' . $domain; |
||
| 292 | |||
| 293 | remove_menu_page( 'edit.php' ); |
||
| 294 | remove_submenu_page( 'edit.php', 'edit.php' ); |
||
| 295 | remove_submenu_page( 'edit.php', 'post-new.php' ); |
||
| 296 | |||
| 297 | add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, 'dashicons-admin-post', $ptype_obj->menu_position ); |
||
| 298 | add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 ); |
||
| 299 | add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/post/' . $domain, null, 10 ); |
||
| 300 | |||
| 301 | $this->migrate_submenus( 'edit.php', $menu_slug ); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Adds Media menu. |
||
| 306 | * |
||
| 307 | * @param string $domain Site domain. |
||
| 308 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 309 | */ |
||
| 310 | public function add_media_menu( $domain, $calypso = true ) { |
||
| 311 | remove_submenu_page( 'upload.php', 'upload.php' ); |
||
| 312 | remove_submenu_page( 'upload.php', 'media-new.php' ); |
||
| 313 | |||
| 314 | if ( $calypso ) { |
||
| 315 | $menu_slug = 'https://wordpress.com/media/' . $domain; |
||
| 316 | |||
| 317 | remove_menu_page( 'upload.php' ); |
||
| 318 | add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', $menu_slug, null, 'dashicons-admin-media', 10 ); |
||
| 319 | $this->migrate_submenus( 'upload.php', $menu_slug ); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Adds Page menu. |
||
| 325 | * |
||
| 326 | * @param string $domain Site domain. |
||
| 327 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 328 | */ |
||
| 329 | View Code Duplication | public function add_page_menu( $domain, $calypso = true ) { |
|
| 330 | if ( ! $calypso ) { |
||
| 331 | return; |
||
| 332 | } |
||
| 333 | |||
| 334 | $ptype_obj = get_post_type_object( 'page' ); |
||
| 335 | $menu_slug = 'https://wordpress.com/pages/' . $domain; |
||
| 336 | |||
| 337 | remove_menu_page( 'edit.php?post_type=page' ); |
||
| 338 | remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' ); |
||
| 339 | remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' ); |
||
| 340 | |||
| 341 | add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, 'dashicons-admin-page', $ptype_obj->menu_position ); |
||
| 342 | add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 ); |
||
| 343 | add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/page/' . $domain, null, 10 ); |
||
| 344 | $this->migrate_submenus( 'edit.php?post_type=page', $menu_slug ); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Adds Comments menu. |
||
| 349 | * |
||
| 350 | * @param string $domain Site domain. |
||
| 351 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 352 | */ |
||
| 353 | public function add_comments_menu( $domain, $calypso = true ) { |
||
| 354 | if ( ! $calypso || ! current_user_can( 'edit_posts' ) ) { |
||
| 355 | return; |
||
| 356 | } |
||
| 357 | |||
| 358 | $awaiting_mod = wp_count_comments(); |
||
| 359 | $awaiting_mod = $awaiting_mod->moderated; |
||
| 360 | $awaiting_mod_i18n = number_format_i18n( $awaiting_mod ); |
||
| 361 | /* translators: %s: Number of comments. */ |
||
| 362 | $awaiting_mod_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod, 'jetpack' ), $awaiting_mod_i18n ); |
||
| 363 | |||
| 364 | /* translators: %s: Number of comments. */ |
||
| 365 | $menu_title = sprintf( __( 'Comments %s', 'jetpack' ), '<span class="awaiting-mod count-' . absint( $awaiting_mod ) . '"><span class="pending-count" aria-hidden="true">' . $awaiting_mod_i18n . '</span><span class="comments-in-moderation-text screen-reader-text">' . $awaiting_mod_text . '</span></span>' ); |
||
| 366 | $menu_slug = 'https://wordpress.com/comments/all/' . $domain; |
||
| 367 | |||
| 368 | remove_menu_page( 'edit-comments.php' ); |
||
| 369 | remove_submenu_page( 'edit-comments.php', 'edit-comments.php' ); |
||
| 370 | |||
| 371 | add_menu_page( esc_attr__( 'Comments', 'jetpack' ), $menu_title, 'edit_posts', $menu_slug, null, 'dashicons-admin-comments', 25 ); |
||
| 372 | $this->migrate_submenus( 'edit-comments.php', $menu_slug ); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Adds Jetpack menu. |
||
| 377 | * |
||
| 378 | * @param string $domain Site domain. |
||
| 379 | */ |
||
| 380 | public function add_jetpack_menu( $domain ) { |
||
| 381 | if ( ! $this->is_wpcom_site() && ! jetpack_is_atomic_site() ) { |
||
| 382 | return; |
||
| 383 | } |
||
| 384 | |||
| 385 | global $menu; |
||
| 386 | |||
| 387 | $position = 50; |
||
| 388 | while ( isset( $menu[ $position ] ) ) { |
||
| 389 | $position++; |
||
| 390 | } |
||
| 391 | |||
| 392 | // TODO: Replace with proper SVG data url. |
||
| 393 | $jetpack_icon = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 32 32' %3E%3Cpath fill='%23a0a5aa' d='M16,0C7.2,0,0,7.2,0,16s7.2,16,16,16s16-7.2,16-16S24.8,0,16,0z'%3E%3C/path%3E%3Cpolygon fill='%23fff' points='15,19 7,19 15,3 '%3E%3C/polygon%3E%3Cpolygon fill='%23fff' points='17,29 17,13 25,13 '%3E%3C/polygon%3E%3C/svg%3E"; |
||
| 394 | $jetpack_slug = 'https://wordpress.com/activity-log/' . $domain; |
||
| 395 | |||
| 396 | $this->add_admin_menu_separator( $position++, 'manage_options' ); |
||
| 397 | add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', $jetpack_slug, null, $jetpack_icon, $position ); |
||
| 398 | |||
| 399 | // Maintain id for jQuery selector. |
||
| 400 | $menu[ $position ][5] = 'toplevel_page_jetpack'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
||
| 401 | |||
| 402 | remove_menu_page( 'jetpack' ); |
||
| 403 | $this->migrate_submenus( 'jetpack', $jetpack_slug ); |
||
| 404 | |||
| 405 | add_submenu_page( $jetpack_slug, esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $domain, null, 5 ); |
||
| 406 | |||
| 407 | add_filter( 'parent_file', array( $this, 'jetpack_parent_file' ) ); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Filters the parent file of an admin menu sub-menu item. |
||
| 412 | * |
||
| 413 | * @param string $parent_file The parent file. |
||
| 414 | * @return string Updated parent file. |
||
| 415 | */ |
||
| 416 | public function jetpack_parent_file( $parent_file ) { |
||
| 417 | if ( 'jetpack' === $parent_file ) { |
||
| 418 | $parent_file = 'https://wordpress.com/activity-log/' . ( new Status() )->get_site_suffix(); |
||
| 419 | } |
||
| 420 | |||
| 421 | return $parent_file; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Adds Appearance menu. |
||
| 426 | * |
||
| 427 | * @param string $domain Site domain. |
||
| 428 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 429 | */ |
||
| 430 | public function add_appearance_menu( $domain, $calypso = true ) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Adds Plugins menu. |
||
| 482 | * |
||
| 483 | * @param string $domain Site domain. |
||
| 484 | */ |
||
| 485 | public function add_plugins_menu( $domain ) { |
||
| 486 | remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); |
||
| 487 | |||
| 488 | if ( $this->is_wpcom_site() ) { |
||
| 489 | remove_menu_page( 'plugins.php' ); |
||
| 490 | |||
| 491 | if ( $this->is_api_request ) { |
||
| 492 | remove_submenu_page( 'plugins.php', 'plugins.php' ); |
||
| 493 | } |
||
| 494 | |||
| 495 | $count = ''; |
||
| 496 | if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) { |
||
| 497 | $update_data = wp_get_update_data(); |
||
| 498 | $count = sprintf( |
||
| 499 | '<span class="update-plugins count-%s"><span class="plugin-count">%s</span></span>', |
||
| 500 | $update_data['counts']['plugins'], |
||
| 501 | number_format_i18n( $update_data['counts']['plugins'] ) |
||
| 502 | ); |
||
| 503 | } |
||
| 504 | |||
| 505 | /* translators: %s: Number of pending plugin updates. */ |
||
| 506 | add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), sprintf( __( 'Plugins %s', 'jetpack' ), $count ), 'activate_plugins', 'https://wordpress.com/plugins/' . $domain, null, 'dashicons-admin-plugins', 65 ); |
||
| 507 | $this->migrate_submenus( 'plugins.php', 'https://wordpress.com/plugins/' . $domain ); |
||
| 508 | } |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Adds Users menu. |
||
| 513 | * |
||
| 514 | * @param string $domain Site domain. |
||
| 515 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 516 | */ |
||
| 517 | public function add_users_menu( $domain, $calypso = true ) { |
||
| 518 | $users_slug = $calypso ? 'https://wordpress.com/people/team/' . $domain : 'users.php'; |
||
| 519 | $add_new_slug = 'https://wordpress.com/people/new/' . $domain; |
||
| 520 | $profile_slug = $this->is_wpcom_site() ? 'grofiles-editor' : 'profile.php'; |
||
| 521 | $profile_slug = $calypso ? 'https://wordpress.com/me' : $profile_slug; |
||
| 522 | $account_slug = $this->is_wpcom_site() && ! $calypso ? 'grofiles-user-settings' : 'https://wordpress.com/me/account'; |
||
| 523 | |||
| 524 | if ( current_user_can( 'list_users' ) ) { |
||
| 525 | remove_menu_page( 'users.php' ); |
||
| 526 | remove_submenu_page( 'users.php', 'users.php' ); |
||
| 527 | remove_submenu_page( 'users.php', 'user-new.php' ); |
||
| 528 | remove_submenu_page( 'users.php', 'profile.php' ); |
||
| 529 | remove_submenu_page( 'users.php', 'grofiles-editor' ); |
||
| 530 | remove_submenu_page( 'users.php', 'grofiles-user-settings' ); |
||
| 531 | |||
| 532 | add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 ); |
||
| 533 | add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 ); |
||
| 534 | add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 ); |
||
| 535 | add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 ); |
||
| 536 | add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 20 ); |
||
| 537 | $this->migrate_submenus( 'users.php', $users_slug ); |
||
| 538 | } elseif ( $calypso && $this->is_wpcom_site() ) { |
||
| 539 | remove_menu_page( 'profile.php' ); |
||
| 540 | remove_submenu_page( 'profile.php', 'grofiles-editor' ); |
||
| 541 | remove_submenu_page( 'profile.php', 'grofiles-user-settings' ); |
||
| 542 | |||
| 543 | add_menu_page( esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 'dashicons-admin-users', 70 ); |
||
| 544 | add_submenu_page( $profile_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 5 ); |
||
| 545 | $this->migrate_submenus( 'profile.php', $profile_slug ); |
||
| 546 | } |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Adds Tools menu. |
||
| 551 | * |
||
| 552 | * @param string $domain Site domain. |
||
| 553 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 554 | */ |
||
| 555 | public function add_tools_menu( $domain, $calypso = true ) { |
||
| 556 | $admin_slug = 'tools.php'; |
||
| 557 | $menu_slug = $calypso ? 'https://wordpress.com/marketing/tools/' . $domain : $admin_slug; |
||
| 558 | |||
| 559 | if ( $this->is_wpcom_site() || jetpack_is_atomic_site() ) { |
||
| 560 | add_submenu_page( $menu_slug, esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'manage_options', 'https://wordpress.com/marketing/tools/' . $domain, null, 5 ); |
||
| 561 | add_submenu_page( $menu_slug, esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $domain, null, 10 ); |
||
| 562 | } |
||
| 563 | |||
| 564 | if ( $calypso ) { |
||
| 565 | remove_menu_page( $admin_slug ); |
||
| 566 | remove_submenu_page( $admin_slug, $admin_slug ); |
||
| 567 | remove_submenu_page( $admin_slug, 'import.php' ); |
||
| 568 | remove_submenu_page( $admin_slug, 'export.php' ); |
||
| 569 | remove_submenu_page( $admin_slug, 'delete-blog' ); |
||
| 570 | |||
| 571 | add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-admin-tools', 75 ); |
||
| 572 | add_submenu_page( $menu_slug, esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', 'https://wordpress.com/import/' . $domain, null, 15 ); |
||
| 573 | add_submenu_page( $menu_slug, esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', 'https://wordpress.com/export/' . $domain, null, 20 ); |
||
| 574 | |||
| 575 | $this->migrate_submenus( $admin_slug, $menu_slug ); |
||
| 576 | } |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Adds Settings menu. |
||
| 581 | * |
||
| 582 | * @param string $domain Site domain. |
||
| 583 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 584 | */ |
||
| 585 | public function add_options_menu( $domain, $calypso = true ) { |
||
| 586 | if ( $calypso ) { |
||
| 587 | remove_submenu_page( 'options-general.php', 'options-discussion.php' ); |
||
| 588 | remove_submenu_page( 'options-general.php', 'options-writing.php' ); |
||
| 589 | } |
||
| 590 | |||
| 591 | if ( $this->is_wpcom_site() || jetpack_is_atomic_site() ) { |
||
| 592 | add_options_page( esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $domain, null, 6 ); |
||
| 593 | } |
||
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs. |
||
| 598 | * |
||
| 599 | * @param string $old_slug WP-Admin menu slug. |
||
| 600 | * @param string $new_slug Calypso menu slug. (Calypso URL). |
||
| 601 | */ |
||
| 602 | public function migrate_submenus( $old_slug, $new_slug ) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Adds a menu separator. |
||
| 619 | * |
||
| 620 | * @param int $position The position in the menu order this item should appear. |
||
| 621 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. |
||
| 622 | * Default: 'read'. |
||
| 623 | */ |
||
| 624 | public function add_admin_menu_separator( $position, $cap = 'read' ) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Enqueues scripts and styles. |
||
| 641 | */ |
||
| 642 | public function enqueue_scripts() { |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Dequeues unnecessary scripts. |
||
| 660 | */ |
||
| 661 | public function dequeue_scripts() { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Whether we're in a WordPress.com context. |
||
| 667 | * |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | private function is_wpcom_site() { |
||
| 671 | /** |
||
| 672 | * Filters whether this request is executed in a WordPress.com environment. |
||
| 673 | * |
||
| 674 | * Filterable to make it easier to unit test other parts of this class. |
||
| 675 | * |
||
| 676 | * @module masterbar |
||
| 677 | * |
||
| 678 | * @since 9.3.0 |
||
| 684 | } |
||
| 685 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: