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:
1 | <?php |
||
17 | class WPcom_Admin_Menu extends Admin_Menu { |
||
18 | |||
19 | /** |
||
20 | * Sets up class properties for REST API requests. |
||
21 | * |
||
22 | * @param WP_REST_Response $response Response from the endpoint. |
||
23 | */ |
||
24 | public function rest_api_init( $response ) { |
||
25 | parent::rest_api_init( $response ); |
||
26 | |||
27 | // Get domain for requested site. |
||
28 | $this->domain = ( new Status() )->get_site_suffix(); |
||
29 | |||
30 | return $response; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Create the desired menu output. |
||
35 | */ |
||
36 | View Code Duplication | public function reregister_menu_items() { |
|
37 | parent::reregister_menu_items(); |
||
38 | |||
39 | $wp_admin = $this->should_link_to_wp_admin(); |
||
40 | |||
41 | $this->add_my_home_menu( $wp_admin ); |
||
42 | |||
43 | // Not needed outside of wp-admin. |
||
44 | if ( ! $this->is_api_request ) { |
||
45 | $this->add_browse_sites_link(); |
||
46 | $this->add_site_card_menu(); |
||
47 | $this->add_new_site_link(); |
||
48 | } |
||
49 | |||
50 | $this->add_gutenberg_menus( $wp_admin ); |
||
51 | |||
52 | ksort( $GLOBALS['menu'] ); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Adds the site switcher link if user has more than one site. |
||
57 | */ |
||
58 | public function add_browse_sites_link() { |
||
67 | |||
68 | /** |
||
69 | * Adds a custom element class for Site Switcher menu item. |
||
70 | * |
||
71 | * @param array $menu Associative array of administration menu items. |
||
72 | * @return array |
||
73 | */ |
||
74 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
86 | |||
87 | /** |
||
88 | * Adds a link to the menu to create a new site. |
||
89 | */ |
||
90 | public function add_new_site_link() { |
||
106 | |||
107 | /** |
||
108 | * Adds site card component. |
||
109 | */ |
||
110 | public function add_site_card_menu() { |
||
151 | |||
152 | /** |
||
153 | * Adds a custom element class and id for Site Card's menu item. |
||
154 | * |
||
155 | * @param array $menu Associative array of administration menu items. |
||
156 | * @return array |
||
157 | */ |
||
158 | public function set_site_card_menu_class( array $menu ) { |
||
176 | |||
177 | /** |
||
178 | * Adds Stats menu. |
||
179 | */ |
||
180 | public function add_stats_menu() { |
||
193 | |||
194 | /** |
||
195 | * Adds Upgrades menu. |
||
196 | */ |
||
197 | public function add_upgrades_menu() { |
||
202 | |||
203 | /** |
||
204 | * Adds Users menu. |
||
205 | * |
||
206 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
207 | */ |
||
208 | public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
212 | |||
213 | /** |
||
214 | * Adds Tools menu. |
||
215 | * |
||
216 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
217 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
218 | */ |
||
219 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
223 | |||
224 | /** |
||
225 | * Adds Settings menu. |
||
226 | * |
||
227 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
228 | */ |
||
229 | public function add_options_menu( $wp_admin = false ) { |
||
239 | |||
240 | /** |
||
241 | * 1. Remove the Gutenberg plugin menu |
||
242 | * 2. Re-add the Site Editor menu |
||
243 | * |
||
244 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
245 | */ |
||
246 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
272 | |||
273 | /** |
||
274 | * Whether to use wp-admin pages rather than Calypso. |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function should_link_to_wp_admin() { |
||
288 | |||
289 | /** |
||
290 | * Adds Plugins menu. |
||
291 | * |
||
292 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
293 | */ |
||
294 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
298 | } |
||
299 |