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 | * WPcom_Admin_Menu constructor. |
||
20 | */ |
||
21 | protected function __construct() { |
||
22 | parent::__construct(); |
||
23 | |||
24 | add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) ); |
||
25 | add_action( 'admin_init', array( $this, 'sync_sidebar_collapsed_state' ) ); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Sets up class properties for REST API requests. |
||
30 | * |
||
31 | * @param WP_REST_Response $response Response from the endpoint. |
||
32 | */ |
||
33 | public function rest_api_init( $response ) { |
||
34 | parent::rest_api_init( $response ); |
||
35 | |||
36 | // Get domain for requested site. |
||
37 | $this->domain = ( new Status() )->get_site_suffix(); |
||
38 | |||
39 | return $response; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Create the desired menu output. |
||
44 | */ |
||
45 | View Code Duplication | public function reregister_menu_items() { |
|
46 | parent::reregister_menu_items(); |
||
47 | |||
48 | $this->add_my_home_menu(); |
||
49 | |||
50 | // Not needed outside of wp-admin. |
||
51 | if ( ! $this->is_api_request ) { |
||
52 | $this->add_browse_sites_link(); |
||
53 | $this->add_site_card_menu(); |
||
54 | $this->add_new_site_link(); |
||
55 | } |
||
56 | |||
57 | ksort( $GLOBALS['menu'] ); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Adds the site switcher link if user has more than one site. |
||
62 | */ |
||
63 | public function add_browse_sites_link() { |
||
64 | if ( count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) { |
||
65 | return; |
||
66 | } |
||
67 | |||
68 | // Add the menu item. |
||
69 | add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 ); |
||
70 | add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Adds a custom element class for Site Switcher menu item. |
||
75 | * |
||
76 | * @param array $menu Associative array of administration menu items. |
||
77 | * @return array |
||
78 | */ |
||
79 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
80 | foreach ( $menu as $key => $menu_item ) { |
||
81 | if ( 'site-switcher' !== $menu_item[3] ) { |
||
82 | continue; |
||
83 | } |
||
84 | |||
85 | $menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] ); |
||
86 | break; |
||
87 | } |
||
88 | |||
89 | return $menu; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Adds a link to the menu to create a new site. |
||
94 | */ |
||
95 | public function add_new_site_link() { |
||
96 | if ( count( get_blogs_of_user( get_current_user_id() ) ) > 1 ) { |
||
97 | return; |
||
98 | } |
||
99 | |||
100 | $this->add_admin_menu_separator(); |
||
101 | add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' ); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Adds site card component. |
||
106 | */ |
||
107 | public function add_site_card_menu() { |
||
108 | $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>' ); |
||
109 | $icon = get_site_icon_url( 32, $default ); |
||
110 | $blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain; |
||
111 | |||
112 | if ( $default === $icon && blavatar_exists( $this->domain ) ) { |
||
113 | $icon = blavatar_url( $this->domain, 'img', 32 ); |
||
114 | } |
||
115 | |||
116 | $badge = ''; |
||
117 | if ( is_private_blog() ) { |
||
118 | $badge .= sprintf( |
||
119 | '<span class="site__badge site__badge-private">%s</span>', |
||
120 | wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | if ( is_redirected_domain( $this->domain ) ) { |
||
125 | $badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>'; |
||
126 | } |
||
127 | |||
128 | if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) { |
||
129 | $badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>'; |
||
130 | } |
||
131 | |||
132 | $site_card = ' |
||
133 | <div class="site__info"> |
||
134 | <div class="site__title">%1$s</div> |
||
135 | <div class="site__domain">%2$s</div> |
||
136 | %3$s |
||
137 | </div>'; |
||
138 | |||
139 | $site_card = sprintf( |
||
140 | $site_card, |
||
141 | $blog_name, |
||
142 | $this->domain, |
||
143 | $badge |
||
144 | ); |
||
145 | |||
146 | add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
||
147 | add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Adds a custom element class and id for Site Card's menu item. |
||
152 | * |
||
153 | * @param array $menu Associative array of administration menu items. |
||
154 | * @return array |
||
155 | */ |
||
156 | public function set_site_card_menu_class( array $menu ) { |
||
174 | |||
175 | /** |
||
176 | * Adds Stats menu. |
||
177 | */ |
||
178 | public function add_stats_menu() { |
||
191 | |||
192 | /** |
||
193 | * Adds Upgrades menu. |
||
194 | */ |
||
195 | public function add_upgrades_menu() { |
||
200 | |||
201 | /** |
||
202 | * Adds Appearance menu. |
||
203 | * |
||
204 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
205 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
206 | */ |
||
207 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
221 | |||
222 | /** |
||
223 | * Adds Users menu. |
||
224 | * |
||
225 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
226 | */ |
||
227 | public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
244 | |||
245 | /** |
||
246 | * Adds Tools menu. |
||
247 | * |
||
248 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
249 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
250 | */ |
||
251 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
255 | |||
256 | /** |
||
257 | * Adds Settings menu. |
||
258 | * |
||
259 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
260 | */ |
||
261 | public function add_options_menu( $wp_admin = false ) { |
||
262 | parent::add_options_menu( $wp_admin ); |
||
268 | |||
269 | /** |
||
270 | * Also remove the Gutenberg plugin menu. |
||
271 | * |
||
272 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
273 | */ |
||
274 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
279 | |||
280 | /** |
||
281 | * Whether to use wp-admin pages rather than Calypso. |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function should_link_to_wp_admin() { |
||
295 | |||
296 | /** |
||
297 | * Adds Plugins menu. |
||
298 | * |
||
299 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
300 | */ |
||
301 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
318 | |||
319 | /** |
||
320 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
321 | */ |
||
322 | public function ajax_sidebar_state() { |
||
342 | |||
343 | /** |
||
344 | * Syncs the sidebar collapsed state from Calypso Preferences. |
||
345 | */ |
||
346 | public function sync_sidebar_collapsed_state() { |
||
350 | } |
||
351 |