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_purchases_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() { |
||
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 ) { |
||
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 ) { |
||
241 | |||
242 | /** |
||
243 | * Adds Stats menu. |
||
244 | * |
||
245 | * @param string $domain Site domain. |
||
246 | */ |
||
247 | public function add_stats_menu( $domain ) { |
||
260 | |||
261 | /** |
||
262 | * Adds Purchases menu. |
||
263 | * |
||
264 | * @param string $domain Site domain. |
||
265 | */ |
||
266 | public function add_purchases_menu( $domain ) { |
||
271 | |||
272 | /** |
||
273 | * Adds Posts menu. |
||
274 | * |
||
275 | * @param string $domain Site domain. |
||
276 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
277 | */ |
||
278 | View Code Duplication | public function add_posts_menu( $domain, $calypso = true ) { |
|
296 | |||
297 | /** |
||
298 | * Adds Media menu. |
||
299 | * |
||
300 | * @param string $domain Site domain. |
||
301 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
302 | */ |
||
303 | public function add_media_menu( $domain, $calypso = true ) { |
||
315 | |||
316 | /** |
||
317 | * Adds Page menu. |
||
318 | * |
||
319 | * @param string $domain Site domain. |
||
320 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
321 | */ |
||
322 | View Code Duplication | public function add_page_menu( $domain, $calypso = true ) { |
|
339 | |||
340 | /** |
||
341 | * Adds Comments menu. |
||
342 | * |
||
343 | * @param string $domain Site domain. |
||
344 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
345 | */ |
||
346 | public function add_comments_menu( $domain, $calypso = true ) { |
||
367 | |||
368 | /** |
||
369 | * Adds Jetpack menu. |
||
370 | * |
||
371 | * @param string $domain Site domain. |
||
372 | */ |
||
373 | public function add_jetpack_menu( $domain ) { |
||
374 | if ( ! $this->is_wpcom_site() && ! jetpack_is_atomic_site() ) { |
||
375 | return; |
||
376 | } |
||
377 | |||
378 | global $menu; |
||
379 | |||
380 | $position = 50; |
||
381 | while ( isset( $menu[ $position ] ) ) { |
||
382 | $position++; |
||
383 | } |
||
384 | |||
385 | // TODO: Replace with proper SVG data url. |
||
386 | $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"; |
||
387 | $jetpack_slug = 'https://wordpress.com/activity-log/' . $domain; |
||
388 | |||
389 | $this->add_admin_menu_separator( $position++, 'manage_options' ); |
||
390 | add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', $jetpack_slug, null, $jetpack_icon, $position ); |
||
391 | |||
392 | // Maintain id for jQuery selector. |
||
393 | $menu[ $position ][5] = 'toplevel_page_jetpack'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
||
394 | |||
395 | remove_menu_page( 'jetpack' ); |
||
396 | $this->migrate_submenus( 'jetpack', $jetpack_slug ); |
||
397 | |||
398 | add_submenu_page( $jetpack_slug, esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $domain, null, 5 ); |
||
399 | |||
400 | add_filter( 'parent_file', array( $this, 'jetpack_parent_file' ) ); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Filters the parent file of an admin menu sub-menu item. |
||
405 | * |
||
406 | * @param string $parent_file The parent file. |
||
407 | * @return string Updated parent file. |
||
408 | */ |
||
409 | public function jetpack_parent_file( $parent_file ) { |
||
410 | if ( 'jetpack' === $parent_file ) { |
||
411 | $parent_file = 'https://wordpress.com/activity-log/' . ( new Status() )->get_site_suffix(); |
||
412 | } |
||
413 | |||
414 | return $parent_file; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Adds Appearance menu. |
||
419 | * |
||
420 | * @param string $domain Site domain. |
||
421 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
422 | */ |
||
423 | public function add_appearance_menu( $domain, $calypso = true ) { |
||
424 | $user_can_customize = current_user_can( 'customize' ); |
||
425 | $appearance_cap = $user_can_customize ? 'customize' : 'edit_theme_options'; |
||
426 | $customize_slug = $calypso ? 'https://wordpress.com/customize/' . $domain : 'customize.php'; |
||
427 | $themes_slug = $calypso ? 'https://wordpress.com/themes/' . $domain : 'themes.php'; |
||
428 | $appearance_slug = $user_can_customize ? $customize_slug : $themes_slug; |
||
429 | $customize_url = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ); // phpcs:ignore |
||
430 | |||
431 | remove_menu_page( 'themes.php' ); |
||
432 | remove_submenu_page( 'themes.php', 'themes.php' ); |
||
433 | remove_submenu_page( 'themes.php', 'theme-editor.php' ); |
||
434 | remove_submenu_page( 'themes.php', $customize_url ); |
||
435 | |||
436 | add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), $appearance_cap, $appearance_slug, null, 'dashicons-admin-appearance', 60 ); |
||
437 | add_submenu_page( $appearance_slug, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $customize_slug, null, 5 ); |
||
438 | add_submenu_page( $appearance_slug, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', $themes_slug, null, 10 ); |
||
439 | |||
440 | View Code Duplication | if ( current_theme_supports( 'custom-header' ) && $user_can_customize ) { |
|
441 | $customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ); |
||
442 | remove_submenu_page( 'themes.php', $customize_header_url ); |
||
443 | |||
444 | $customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $appearance_slug ); |
||
445 | add_submenu_page( $appearance_slug, __( 'Header', 'jetpack' ), __( 'Header', 'jetpack' ), $appearance_cap, esc_url( $customize_header_url ), null, 15 ); |
||
446 | } |
||
447 | |||
448 | View Code Duplication | if ( current_theme_supports( 'custom-background' ) && $user_can_customize ) { |
|
449 | $customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_url ); |
||
450 | remove_submenu_page( 'themes.php', $customize_background_url ); |
||
451 | |||
452 | $customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $appearance_slug ); |
||
453 | add_submenu_page( $appearance_slug, esc_attr__( 'Background', 'jetpack' ), __( 'Background', 'jetpack' ), $appearance_cap, esc_url( $customize_background_url ), null, 20 ); |
||
454 | } |
||
455 | |||
456 | View Code Duplication | if ( current_theme_supports( 'widgets' ) ) { |
|
457 | remove_submenu_page( 'themes.php', 'widgets.php' ); |
||
458 | |||
459 | $customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $appearance_slug ); |
||
460 | add_submenu_page( $appearance_slug, esc_attr__( 'Widgets', 'jetpack' ), __( 'Widgets', 'jetpack' ), $appearance_cap, esc_url( $customize_menu_url ), null, 20 ); |
||
461 | } |
||
462 | |||
463 | View Code Duplication | if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) { |
|
464 | remove_submenu_page( 'themes.php', 'nav-menus.php' ); |
||
465 | |||
466 | $customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $appearance_slug ); |
||
467 | add_submenu_page( $appearance_slug, esc_attr__( 'Menus', 'jetpack' ), __( 'Menus', 'jetpack' ), $appearance_cap, esc_url( $customize_menu_url ), null, 20 ); |
||
468 | } |
||
469 | |||
470 | $this->migrate_submenus( 'themes.php', $appearance_slug ); |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * Adds Plugins menu. |
||
475 | * |
||
476 | * @param string $domain Site domain. |
||
477 | */ |
||
478 | public function add_plugins_menu( $domain ) { |
||
479 | remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); |
||
480 | |||
481 | if ( $this->is_wpcom_site() ) { |
||
482 | remove_menu_page( 'plugins.php' ); |
||
483 | |||
484 | if ( $this->is_api_request ) { |
||
485 | remove_submenu_page( 'plugins.php', 'plugins.php' ); |
||
486 | } |
||
487 | |||
488 | $count = ''; |
||
489 | if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) { |
||
490 | $update_data = wp_get_update_data(); |
||
491 | $count = sprintf( |
||
492 | '<span class="update-plugins count-%s"><span class="plugin-count">%s</span></span>', |
||
493 | $update_data['counts']['plugins'], |
||
494 | number_format_i18n( $update_data['counts']['plugins'] ) |
||
495 | ); |
||
496 | } |
||
497 | |||
498 | /* translators: %s: Number of pending plugin updates. */ |
||
499 | add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), sprintf( __( 'Plugins %s', 'jetpack' ), $count ), 'activate_plugins', 'https://wordpress.com/plugins/' . $domain, null, 'dashicons-admin-plugins', 65 ); |
||
500 | $this->migrate_submenus( 'plugins.php', 'https://wordpress.com/plugins/' . $domain ); |
||
501 | } |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Adds Users menu. |
||
506 | * |
||
507 | * @param string $domain Site domain. |
||
508 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
509 | */ |
||
510 | public function add_users_menu( $domain, $calypso = true ) { |
||
511 | $users_slug = $calypso ? 'https://wordpress.com/people/team/' . $domain : 'users.php'; |
||
512 | $add_new_slug = 'https://wordpress.com/people/new/' . $domain; |
||
513 | $profile_slug = $this->is_wpcom_site() ? 'grofiles-editor' : 'profile.php'; |
||
514 | $profile_slug = $calypso ? 'https://wordpress.com/me' : $profile_slug; |
||
515 | $account_slug = $this->is_wpcom_site() && ! $calypso ? 'grofiles-user-settings' : 'https://wordpress.com/me/account'; |
||
516 | |||
517 | if ( current_user_can( 'list_users' ) ) { |
||
518 | remove_menu_page( 'users.php' ); |
||
519 | remove_submenu_page( 'users.php', 'users.php' ); |
||
520 | remove_submenu_page( 'users.php', 'user-new.php' ); |
||
521 | remove_submenu_page( 'users.php', 'profile.php' ); |
||
522 | remove_submenu_page( 'users.php', 'grofiles-editor' ); |
||
523 | remove_submenu_page( 'users.php', 'grofiles-user-settings' ); |
||
524 | |||
525 | add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 ); |
||
526 | add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 ); |
||
527 | add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 ); |
||
528 | add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 ); |
||
529 | add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 20 ); |
||
530 | $this->migrate_submenus( 'users.php', $users_slug ); |
||
531 | } elseif ( $calypso && $this->is_wpcom_site() ) { |
||
532 | remove_menu_page( 'profile.php' ); |
||
533 | remove_submenu_page( 'profile.php', 'grofiles-editor' ); |
||
534 | remove_submenu_page( 'profile.php', 'grofiles-user-settings' ); |
||
535 | |||
536 | add_menu_page( esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 'dashicons-admin-users', 70 ); |
||
537 | add_submenu_page( $profile_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 5 ); |
||
538 | $this->migrate_submenus( 'profile.php', $profile_slug ); |
||
539 | } |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * Adds Tools menu. |
||
544 | * |
||
545 | * @param string $domain Site domain. |
||
546 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
547 | */ |
||
548 | public function add_tools_menu( $domain, $calypso = true ) { |
||
549 | $admin_slug = 'tools.php'; |
||
550 | $menu_slug = $calypso ? 'https://wordpress.com/marketing/tools/' . $domain : $admin_slug; |
||
551 | |||
552 | View Code Duplication | if ( $this->is_wpcom_site() || jetpack_is_atomic_site() ) { |
|
553 | add_submenu_page( $menu_slug, esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'manage_options', 'https://wordpress.com/marketing/tools/' . $domain, null, 5 ); |
||
554 | add_submenu_page( $menu_slug, esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $domain, null, 10 ); |
||
555 | } |
||
556 | |||
557 | if ( $calypso ) { |
||
558 | remove_menu_page( $admin_slug ); |
||
559 | remove_submenu_page( $admin_slug, $admin_slug ); |
||
560 | remove_submenu_page( $admin_slug, 'import.php' ); |
||
561 | remove_submenu_page( $admin_slug, 'export.php' ); |
||
562 | remove_submenu_page( $admin_slug, 'delete-blog' ); |
||
563 | |||
564 | add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-admin-tools', 75 ); |
||
565 | add_submenu_page( $menu_slug, esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', 'https://wordpress.com/import/' . $domain, null, 15 ); |
||
566 | add_submenu_page( $menu_slug, esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', 'https://wordpress.com/export/' . $domain, null, 20 ); |
||
567 | |||
568 | $this->migrate_submenus( $admin_slug, $menu_slug ); |
||
569 | } |
||
570 | } |
||
571 | |||
572 | /** |
||
573 | * Adds Settings menu. |
||
574 | * |
||
575 | * @param string $domain Site domain. |
||
576 | * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
||
577 | */ |
||
578 | public function add_options_menu( $domain, $calypso = true ) { |
||
579 | if ( $calypso ) { |
||
580 | remove_submenu_page( 'options-general.php', 'options-discussion.php' ); |
||
581 | remove_submenu_page( 'options-general.php', 'options-writing.php' ); |
||
582 | } |
||
583 | |||
584 | View Code Duplication | if ( $this->is_wpcom_site() || jetpack_is_atomic_site() ) { |
|
585 | add_options_page( esc_attr__( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $domain, null, 1 ); |
||
586 | add_options_page( esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $domain, null, 6 ); |
||
587 | } |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs. |
||
592 | * |
||
593 | * @param string $old_slug WP-Admin menu slug. |
||
594 | * @param string $new_slug Calypso menu slug. (Calypso URL). |
||
595 | */ |
||
596 | public function migrate_submenus( $old_slug, $new_slug ) { |
||
610 | |||
611 | /** |
||
612 | * Adds a menu separator. |
||
613 | * |
||
614 | * @param int $position The position in the menu order this item should appear. |
||
615 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. |
||
616 | * Default: 'read'. |
||
617 | */ |
||
618 | public function add_admin_menu_separator( $position, $cap = 'read' ) { |
||
632 | |||
633 | /** |
||
634 | * Enqueues scripts and styles. |
||
635 | */ |
||
636 | public function enqueue_scripts() { |
||
651 | |||
652 | /** |
||
653 | * Dequeues unnecessary scripts. |
||
654 | */ |
||
655 | public function dequeue_scripts() { |
||
656 | wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php. |
||
657 | } |
||
658 | |||
659 | /** |
||
660 | * Whether we're in a WordPress.com context. |
||
661 | * |
||
662 | * @return bool |
||
663 | */ |
||
664 | private function is_wpcom_site() { |
||
678 | } |
||
679 |
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
SomeClass
to useself
instead: