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 |
||
| 17 | class Admin_Menu { |
||
| 18 | /** |
||
| 19 | * Holds class instance. |
||
| 20 | * |
||
| 21 | * @var Admin_Menu |
||
| 22 | */ |
||
| 23 | private static $instance; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Whether the current request is a REST API request. |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $is_api_request; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Admin_Menu constructor. |
||
| 34 | */ |
||
| 35 | private function __construct() { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Returns class instance. |
||
| 54 | * |
||
| 55 | * @return Admin_Menu |
||
| 56 | */ |
||
| 57 | public static function get_instance() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Create the desired menu output. |
||
| 67 | */ |
||
| 68 | public function reregister_menu_items() { |
||
| 69 | $this->is_api_request = ( defined( 'REST_API_PLUGINS' ) && REST_API_PLUGINS ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ); |
||
| 70 | |||
| 71 | $domain = ( new Status() )->get_site_suffix(); |
||
| 72 | |||
| 73 | // Not needed outside of wp-admin. |
||
| 74 | if ( ! $this->is_api_request && ( $this->is_wpcom_site() || jetpack_is_atomic_site() ) ) { |
||
| 75 | $this->add_browse_sites_link(); |
||
| 76 | $this->add_site_card_menu( $domain ); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Whether links should point to Calypso or wp-admin. |
||
| 81 | * |
||
| 82 | * Options: |
||
| 83 | * true - Calypso. |
||
| 84 | * false - wp-admin. |
||
| 85 | * |
||
| 86 | * @module masterbar |
||
| 87 | * @since 9.3.0 |
||
| 88 | * |
||
| 89 | * @param bool $calypso Whether menu item URLs should point to Calypso. |
||
| 90 | */ |
||
| 91 | $calypso = apply_filters( 'jetpack_admin_menu_use_calypso_links', true ); |
||
| 92 | |||
| 93 | // Remove separators. |
||
| 94 | remove_menu_page( 'separator1' ); |
||
| 95 | |||
| 96 | $this->add_my_home_menu( $domain, $calypso ); |
||
| 97 | $this->add_stats_menu( $domain ); |
||
| 98 | $this->add_purchases_menu( $domain ); |
||
| 99 | $this->add_posts_menu( $domain, $calypso ); |
||
| 100 | $this->add_media_menu( $domain, $calypso ); |
||
| 101 | $this->add_page_menu( $domain, $calypso ); |
||
| 102 | $this->add_comments_menu( $domain, $calypso ); |
||
| 103 | $this->add_jetpack_menu( $domain ); |
||
| 104 | $this->add_appearance_menu( $domain, $calypso ); |
||
| 105 | $this->add_plugins_menu( $domain ); |
||
| 106 | $this->add_users_menu( $domain, $calypso ); |
||
| 107 | $this->add_tools_menu( $domain, $calypso ); |
||
| 108 | $this->add_options_menu( $domain ); |
||
| 109 | |||
| 110 | ksort( $GLOBALS['menu'] ); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Adds the site switcher link if user has more than one site. |
||
| 115 | */ |
||
| 116 | public function add_browse_sites_link() { |
||
| 117 | if ( jetpack_is_atomic_site() ) { |
||
| 118 | $wpcom_user_data = ( new Connection_Manager() )->get_connected_user_data(); |
||
| 119 | |||
| 120 | if ( $wpcom_user_data['site_count'] < 2 ) { |
||
| 121 | return; |
||
| 122 | } |
||
| 123 | } elseif ( ! is_multisite() || count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) { |
||
| 124 | return; |
||
| 125 | } |
||
| 126 | |||
| 127 | // Add the menu item. |
||
| 128 | add_menu_page( __( 'Browse sites', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Adds site card component. |
||
| 133 | * |
||
| 134 | * @param string $domain Site domain. |
||
| 135 | */ |
||
| 136 | public function add_site_card_menu( $domain ) { |
||
| 137 | $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>' ); |
||
| 138 | $icon = get_site_icon_url( 32, $default ); |
||
| 139 | |||
| 140 | if ( $default === $icon && function_exists( 'blavatar_exists' ) && blavatar_exists( $domain ) && function_exists( 'blavatar_url' ) ) { |
||
| 141 | $icon = blavatar_url( $domain, 'img', 32 ); |
||
| 142 | } |
||
| 143 | |||
| 144 | $badge = ''; |
||
| 145 | if ( $this->is_wpcom_site() ) { |
||
| 146 | View Code Duplication | if ( function_exists( 'is_private_blog' ) && function_exists( 'wpcom_is_coming_soon' ) && is_private_blog() ) { |
|
| 147 | $badge .= sprintf( |
||
| 148 | '<span class="site__badge site__badge-private">%s</span>', |
||
| 149 | wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ( function_exists( 'is_redirected_domain' ) && is_redirected_domain( $domain ) ) { |
||
| 154 | $badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>'; |
||
| 155 | } |
||
| 156 | |||
| 157 | if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) { |
||
| 158 | $badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>'; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | if ( jetpack_is_atomic_site() ) { |
||
| 163 | View Code Duplication | if ( function_exists( 'site_is_private' ) && function_exists( 'site_is_coming_soon' ) && site_is_private() ) { |
|
| 164 | $badge .= sprintf( |
||
| 165 | '<span class="site__badge site__badge-private">%s</span>', |
||
| 166 | site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | $site_card = ' |
||
| 172 | <div class="site__info"> |
||
| 173 | <div class="site__title">%1$s</div> |
||
| 174 | <div class="site__domain">%2$s</div> |
||
| 175 | %3$s |
||
| 176 | </div>'; |
||
| 177 | |||
| 178 | $site_card = sprintf( |
||
| 179 | $site_card, |
||
| 180 | get_option( 'blogname' ), |
||
| 181 | $domain, |
||
| 182 | $badge |
||
| 183 | ); |
||
| 184 | |||
| 185 | add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
||
| 186 | add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Adds a custom element class and id for Site Card's menu item. |
||
| 191 | * |
||
| 192 | * @param array $menu Associative array of administration menu items. |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function set_site_card_menu_class( array $menu ) { |
||
| 196 | foreach ( $menu as $key => $menu_item ) { |
||
| 197 | if ( 'site-card' !== $menu_item[3] ) { |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | |||
| 201 | $classes = ' toplevel_page_site-card'; |
||
| 202 | |||
| 203 | // webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon. |
||
| 204 | if ( |
||
| 205 | ( function_exists( 'blavatar_exists' ) && blavatar_exists( ( new Status() )->get_site_suffix() ) ) || |
||
| 206 | ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) |
||
| 207 | ) { |
||
| 208 | $classes .= ' has-site-icon'; |
||
| 209 | } |
||
| 210 | |||
| 211 | $menu[ $key ][4] = $menu_item[4] . $classes; |
||
| 212 | $menu[ $key ][5] = 'toplevel_page_site_card'; |
||
| 213 | break; |
||
| 214 | } |
||
| 215 | |||
| 216 | return $menu; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Adds My Home menu. |
||
| 221 | * |
||
| 222 | * @param string $domain Site domain. |
||
| 223 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 224 | */ |
||
| 225 | public function add_my_home_menu( $domain, $calypso = true ) { |
||
| 226 | global $submenu; |
||
| 227 | |||
| 228 | $menu_slug = $calypso ? 'https://wordpress.com/home/' . $domain : 'index.php'; |
||
| 229 | |||
| 230 | remove_menu_page( 'index.php' ); |
||
| 231 | remove_submenu_page( 'index.php', 'index.php' ); |
||
| 232 | |||
| 233 | add_menu_page( __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 'dashicons-admin-home', 2 ); |
||
| 234 | |||
| 235 | // Only add submenu when there are other submenu items. |
||
| 236 | if ( ! empty( $submenu['index.php'] ) ) { |
||
| 237 | add_submenu_page( $menu_slug, __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 1 ); |
||
| 238 | } |
||
| 239 | |||
| 240 | $this->migrate_submenus( 'index.php', $menu_slug ); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Adds Stats menu. |
||
| 245 | * |
||
| 246 | * @param string $domain Site domain. |
||
| 247 | */ |
||
| 248 | public function add_stats_menu( $domain ) { |
||
| 249 | $menu_title = __( 'Stats', 'jetpack' ); |
||
| 250 | |||
| 251 | if ( $this->is_wpcom_site() && ! $this->is_api_request ) { |
||
| 252 | $menu_title .= sprintf( |
||
| 253 | '<img class="sidebar-unified__sparkline" width="80" height="20" src="%1$s" alt="%2$s">', |
||
| 254 | esc_url( home_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ), |
||
| 255 | esc_attr__( 'Hourly views', 'jetpack' ) |
||
| 256 | ); |
||
| 257 | } |
||
| 258 | |||
| 259 | add_menu_page( __( 'Stats', 'jetpack' ), $menu_title, 'edit_posts', 'https://wordpress.com/stats/day/' . $domain, null, 'dashicons-chart-bar', 3 ); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Adds Purchases menu. |
||
| 264 | * |
||
| 265 | * @param string $domain Site domain. |
||
| 266 | */ |
||
| 267 | public function add_purchases_menu( $domain ) { |
||
| 268 | remove_menu_page( 'paid-upgrades.php' ); |
||
| 269 | add_menu_page( __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/plans/' . $domain, null, 'dashicons-cart', 4 ); |
||
| 270 | $this->migrate_submenus( 'paid-upgrades.php', 'https://wordpress.com/plans/' . $domain ); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Adds Posts menu. |
||
| 275 | * |
||
| 276 | * @param string $domain Site domain. |
||
| 277 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 278 | */ |
||
| 279 | View Code Duplication | public function add_posts_menu( $domain, $calypso = true ) { |
|
| 280 | if ( ! $calypso ) { |
||
| 281 | return; |
||
| 282 | } |
||
| 283 | |||
| 284 | $ptype_obj = get_post_type_object( 'post' ); |
||
| 285 | $menu_slug = 'https://wordpress.com/posts/' . $domain; |
||
| 286 | |||
| 287 | remove_menu_page( 'edit.php' ); |
||
| 288 | remove_submenu_page( 'edit.php', 'edit.php' ); |
||
| 289 | remove_submenu_page( 'edit.php', 'post-new.php' ); |
||
| 290 | |||
| 291 | 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 ); |
||
| 292 | add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 ); |
||
| 293 | 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 ); |
||
| 294 | |||
| 295 | $this->migrate_submenus( 'edit.php', $menu_slug ); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Adds Media menu. |
||
| 300 | * |
||
| 301 | * @param string $domain Site domain. |
||
| 302 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 303 | */ |
||
| 304 | public function add_media_menu( $domain, $calypso = true ) { |
||
| 305 | remove_submenu_page( 'upload.php', 'upload.php' ); |
||
| 306 | remove_submenu_page( 'upload.php', 'media-new.php' ); |
||
| 307 | |||
| 308 | if ( $calypso ) { |
||
| 309 | $menu_slug = 'https://wordpress.com/media/' . $domain; |
||
| 310 | |||
| 311 | remove_menu_page( 'upload.php' ); |
||
| 312 | add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', $menu_slug, null, 'dashicons-admin-media', 10 ); |
||
| 313 | $this->migrate_submenus( 'upload.php', $menu_slug ); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Adds Page menu. |
||
| 319 | * |
||
| 320 | * @param string $domain Site domain. |
||
| 321 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 322 | */ |
||
| 323 | View Code Duplication | public function add_page_menu( $domain, $calypso = true ) { |
|
| 324 | if ( ! $calypso ) { |
||
| 325 | return; |
||
| 326 | } |
||
| 327 | |||
| 328 | $ptype_obj = get_post_type_object( 'page' ); |
||
| 329 | $menu_slug = 'https://wordpress.com/pages/' . $domain; |
||
| 330 | |||
| 331 | remove_menu_page( 'edit.php?post_type=page' ); |
||
| 332 | remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' ); |
||
| 333 | remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' ); |
||
| 334 | |||
| 335 | 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 ); |
||
| 336 | add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 ); |
||
| 337 | 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 ); |
||
| 338 | $this->migrate_submenus( 'edit.php?post_type=page', $menu_slug ); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Adds Comments menu. |
||
| 343 | * |
||
| 344 | * @param string $domain Site domain. |
||
| 345 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 346 | */ |
||
| 347 | public function add_comments_menu( $domain, $calypso = true ) { |
||
| 348 | if ( ! $calypso || ! current_user_can( 'edit_posts' ) ) { |
||
| 349 | return; |
||
| 350 | } |
||
| 351 | |||
| 352 | $awaiting_mod = wp_count_comments(); |
||
| 353 | $awaiting_mod = $awaiting_mod->moderated; |
||
| 354 | $awaiting_mod_i18n = number_format_i18n( $awaiting_mod ); |
||
| 355 | /* translators: %s: Number of comments. */ |
||
| 356 | $awaiting_mod_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod, 'jetpack' ), $awaiting_mod_i18n ); |
||
| 357 | |||
| 358 | /* translators: %s: Number of comments. */ |
||
| 359 | $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>' ); |
||
| 360 | $menu_slug = 'https://wordpress.com/comments/all/' . $domain; |
||
| 361 | |||
| 362 | remove_menu_page( 'edit-comments.php' ); |
||
| 363 | remove_submenu_page( 'edit-comments.php', 'edit-comments.php' ); |
||
| 364 | |||
| 365 | add_menu_page( esc_attr__( 'Comments', 'jetpack' ), $menu_title, 'edit_posts', $menu_slug, null, 'dashicons-admin-comments', 25 ); |
||
| 366 | $this->migrate_submenus( 'edit-comments.php', $menu_slug ); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Adds Jetpack menu. |
||
| 371 | * |
||
| 372 | * @param string $domain Site domain. |
||
| 373 | */ |
||
| 374 | public function add_jetpack_menu( $domain ) { |
||
| 375 | if ( ! $this->is_wpcom_site() && ! jetpack_is_atomic_site() ) { |
||
| 376 | return; |
||
| 377 | } |
||
| 378 | |||
| 379 | global $menu; |
||
| 380 | |||
| 381 | $position = 50; |
||
| 382 | while ( isset( $menu[ $position ] ) ) { |
||
| 383 | $position++; |
||
| 384 | } |
||
| 385 | |||
| 386 | // TODO: Replace with proper SVG data url. |
||
| 387 | $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"; |
||
| 388 | $jetpack_slug = 'https://wordpress.com/activity-log/' . $domain; |
||
| 389 | |||
| 390 | $this->add_admin_menu_separator( $position++, 'manage_options' ); |
||
| 391 | add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', $jetpack_slug, null, $jetpack_icon, $position ); |
||
| 392 | |||
| 393 | // Maintain id for jQuery selector. |
||
| 394 | $menu[ $position ][5] = 'toplevel_page_jetpack'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
||
| 395 | |||
| 396 | remove_menu_page( 'jetpack' ); |
||
| 397 | remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) ); |
||
| 398 | |||
| 399 | $this->migrate_submenus( 'jetpack', $jetpack_slug ); |
||
| 400 | |||
| 401 | add_submenu_page( $jetpack_slug, esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $domain, null, 5 ); |
||
| 402 | add_submenu_page( $jetpack_slug, esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $domain, null, 10 ); |
||
| 403 | add_submenu_page( $jetpack_slug, esc_attr__( 'Scan', 'jetpack' ), __( 'Scan', 'jetpack' ), 'manage_options', 'https://wordpress.com/scan/' . $domain, null, 15 ); |
||
| 404 | |||
| 405 | add_filter( 'parent_file', array( $this, 'jetpack_parent_file' ) ); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Filters the parent file of an admin menu sub-menu item. |
||
| 410 | * |
||
| 411 | * @param string $parent_file The parent file. |
||
| 412 | * @return string Updated parent file. |
||
| 413 | */ |
||
| 414 | public function jetpack_parent_file( $parent_file ) { |
||
| 415 | if ( 'jetpack' === $parent_file ) { |
||
| 416 | $parent_file = 'https://wordpress.com/activity-log/' . ( new Status() )->get_site_suffix(); |
||
| 417 | } |
||
| 418 | |||
| 419 | return $parent_file; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Adds Appearance menu. |
||
| 424 | * |
||
| 425 | * @param string $domain Site domain. |
||
| 426 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 427 | */ |
||
| 428 | public function add_appearance_menu( $domain, $calypso = true ) { |
||
| 429 | $user_can_customize = current_user_can( 'customize' ); |
||
| 430 | $appearance_cap = $user_can_customize ? 'customize' : 'edit_theme_options'; |
||
| 431 | $customize_slug = $calypso ? 'https://wordpress.com/customize/' . $domain : 'customize.php'; |
||
| 432 | $themes_slug = $calypso ? 'https://wordpress.com/themes/' . $domain : 'themes.php'; |
||
| 433 | $appearance_slug = $user_can_customize ? $customize_slug : $themes_slug; |
||
| 434 | $customize_url = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ); // phpcs:ignore |
||
| 435 | |||
| 436 | remove_menu_page( 'themes.php' ); |
||
| 437 | remove_submenu_page( 'themes.php', 'themes.php' ); |
||
| 438 | remove_submenu_page( 'themes.php', 'theme-editor.php' ); |
||
| 439 | remove_submenu_page( 'themes.php', $customize_url ); |
||
| 440 | |||
| 441 | add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), $appearance_cap, $appearance_slug, null, 'dashicons-admin-appearance', 60 ); |
||
| 442 | add_submenu_page( $appearance_slug, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $customize_slug, null, 5 ); |
||
| 443 | add_submenu_page( $appearance_slug, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', $themes_slug, null, 10 ); |
||
| 444 | |||
| 445 | View Code Duplication | if ( current_theme_supports( 'custom-header' ) && $user_can_customize ) { |
|
| 446 | $customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ); |
||
| 447 | remove_submenu_page( 'themes.php', $customize_header_url ); |
||
| 448 | |||
| 449 | $customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $appearance_slug ); |
||
| 450 | add_submenu_page( $appearance_slug, __( 'Header', 'jetpack' ), __( 'Header', 'jetpack' ), $appearance_cap, esc_url( $customize_header_url ), null, 15 ); |
||
| 451 | } |
||
| 452 | |||
| 453 | View Code Duplication | if ( current_theme_supports( 'custom-background' ) && $user_can_customize ) { |
|
| 454 | $customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_url ); |
||
| 455 | remove_submenu_page( 'themes.php', $customize_background_url ); |
||
| 456 | |||
| 457 | $customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $appearance_slug ); |
||
| 458 | add_submenu_page( $appearance_slug, esc_attr__( 'Background', 'jetpack' ), __( 'Background', 'jetpack' ), $appearance_cap, esc_url( $customize_background_url ), null, 20 ); |
||
| 459 | } |
||
| 460 | |||
| 461 | View Code Duplication | if ( current_theme_supports( 'widgets' ) ) { |
|
| 462 | remove_submenu_page( 'themes.php', 'widgets.php' ); |
||
| 463 | |||
| 464 | $customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $appearance_slug ); |
||
| 465 | add_submenu_page( $appearance_slug, esc_attr__( 'Widgets', 'jetpack' ), __( 'Widgets', 'jetpack' ), $appearance_cap, esc_url( $customize_menu_url ), null, 20 ); |
||
| 466 | } |
||
| 467 | |||
| 468 | View Code Duplication | if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) { |
|
| 469 | remove_submenu_page( 'themes.php', 'nav-menus.php' ); |
||
| 470 | |||
| 471 | $customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $appearance_slug ); |
||
| 472 | add_submenu_page( $appearance_slug, esc_attr__( 'Menus', 'jetpack' ), __( 'Menus', 'jetpack' ), $appearance_cap, esc_url( $customize_menu_url ), null, 20 ); |
||
| 473 | } |
||
| 474 | |||
| 475 | $this->migrate_submenus( 'themes.php', $appearance_slug ); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Adds Plugins menu. |
||
| 480 | * |
||
| 481 | * @param string $domain Site domain. |
||
| 482 | */ |
||
| 483 | public function add_plugins_menu( $domain ) { |
||
| 484 | $calypso = $this->is_wpcom_site(); |
||
| 485 | |||
| 486 | remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); |
||
| 487 | |||
| 488 | if ( $calypso ) { |
||
| 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 = $calypso ? 'https://wordpress.com/me' : 'grofiles-editor'; |
||
| 521 | $account_slug = $calypso ? 'https://wordpress.com/me/account' : 'grofiles-user-settings'; |
||
| 522 | |||
| 523 | if ( current_user_can( 'list_users' ) ) { |
||
| 524 | remove_menu_page( 'users.php' ); |
||
| 525 | remove_submenu_page( 'users.php', 'users.php' ); |
||
| 526 | remove_submenu_page( 'users.php', 'user-new.php' ); |
||
| 527 | remove_submenu_page( 'users.php', 'profile.php' ); |
||
| 528 | remove_submenu_page( 'users.php', 'grofiles-editor' ); |
||
| 529 | remove_submenu_page( 'users.php', 'grofiles-user-settings' ); |
||
| 530 | |||
| 531 | add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 ); |
||
| 532 | add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 ); |
||
| 533 | add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 ); |
||
| 534 | add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 ); |
||
| 535 | add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 20 ); |
||
| 536 | $this->migrate_submenus( 'users.php', $users_slug ); |
||
| 537 | } else { |
||
| 538 | remove_menu_page( 'profile.php' ); |
||
| 539 | remove_submenu_page( 'profile.php', 'grofiles-editor' ); |
||
| 540 | remove_submenu_page( 'profile.php', 'grofiles-user-settings' ); |
||
| 541 | |||
| 542 | add_menu_page( esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 'dashicons-admin-users', 70 ); |
||
| 543 | add_submenu_page( $profile_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 5 ); |
||
| 544 | $this->migrate_submenus( 'profile.php', $profile_slug ); |
||
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Adds Tools menu. |
||
| 550 | * |
||
| 551 | * @param string $domain Site domain. |
||
| 552 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 553 | */ |
||
| 554 | public function add_tools_menu( $domain, $calypso = true ) { |
||
| 555 | $admin_slug = 'tools.php'; |
||
| 556 | $menu_slug = $calypso ? 'https://wordpress.com/marketing/tools/' . $domain : $admin_slug; |
||
| 557 | |||
| 558 | add_submenu_page( $menu_slug, esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'manage_options', 'https://wordpress.com/marketing/tools/' . $domain, null, 5 ); |
||
| 559 | add_submenu_page( $menu_slug, esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $domain, null, 10 ); |
||
| 560 | |||
| 561 | if ( $calypso ) { |
||
| 562 | remove_menu_page( $admin_slug ); |
||
| 563 | remove_submenu_page( $admin_slug, $admin_slug ); |
||
| 564 | remove_submenu_page( $admin_slug, 'import.php' ); |
||
| 565 | remove_submenu_page( $admin_slug, 'export.php' ); |
||
| 566 | remove_submenu_page( $admin_slug, 'delete-blog' ); |
||
| 567 | |||
| 568 | add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-admin-tools', 75 ); |
||
| 569 | add_submenu_page( $menu_slug, esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', 'https://wordpress.com/import/' . $domain, null, 15 ); |
||
| 570 | add_submenu_page( $menu_slug, esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', 'https://wordpress.com/export/' . $domain, null, 20 ); |
||
| 571 | |||
| 572 | $this->migrate_submenus( $admin_slug, $menu_slug ); |
||
| 573 | } |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Adds Settings menu. |
||
| 578 | * |
||
| 579 | * @param string $domain Site domain. |
||
| 580 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
| 581 | */ |
||
| 582 | public function add_options_menu( $domain, $calypso = true ) { |
||
| 583 | if ( $calypso ) { |
||
| 584 | remove_submenu_page( 'options-general.php', 'options-discussion.php' ); |
||
| 585 | remove_submenu_page( 'options-general.php', 'options-writing.php' ); |
||
| 586 | } |
||
| 587 | |||
| 588 | add_options_page( esc_attr__( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $domain, null, 1 ); |
||
| 589 | add_options_page( esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $domain, null, 6 ); |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs. |
||
| 594 | * |
||
| 595 | * @param string $old_slug WP-Admin menu slug. |
||
| 596 | * @param string $new_slug Calypso menu slug. (Calypso URL). |
||
| 597 | */ |
||
| 598 | public function migrate_submenus( $old_slug, $new_slug ) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Adds a menu separator. |
||
| 615 | * |
||
| 616 | * @param int $position The position in the menu order this item should appear. |
||
| 617 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. |
||
| 618 | * Default: 'read'. |
||
| 619 | */ |
||
| 620 | public function add_admin_menu_separator( $position, $cap = 'read' ) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Enqueues scripts and styles. |
||
| 637 | */ |
||
| 638 | public function enqueue_scripts() { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Dequeues unnecessary scripts. |
||
| 656 | */ |
||
| 657 | public function dequeue_scripts() { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Whether we're in a WordPress.com context. |
||
| 663 | * |
||
| 664 | * @return bool |
||
| 665 | */ |
||
| 666 | private function is_wpcom_site() { |
||
| 676 | } |
||
| 677 |
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: