Completed
Push — add/userless-connection-flow ( 692d67...403970 )
by
unknown
555:43 queued 545:14
created

WPcom_Admin_Menu   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 312
Duplicated Lines 9.62 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 30
loc 312
rs 8.96
c 0
b 0
f 0
wmc 43
lcom 1
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A add_browse_sites_link() 0 9 2
A set_browse_sites_link_class() 12 12 3
A add_new_site_link() 0 16 3
B add_site_card_menu() 0 41 7
A set_site_card_menu_class() 0 18 4
A add_stats_menu() 0 13 2
A add_upgrades_menu() 0 5 1
A rest_api_init() 0 8 1
A reregister_menu_items() 18 18 2
B add_users_menu() 0 44 8
A add_tools_menu() 0 4 1
A add_options_menu() 0 10 2
A add_gutenberg_menus() 0 26 5
A should_link_to_wp_admin() 0 10 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
/**
3
 * WP.com Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Status;
11
12
require_once __DIR__ . '/class-admin-menu.php';
13
14
/**
15
 * Class WPcom_Admin_Menu.
16
 */
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() {
59
		if ( count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) {
60
			return;
61
		}
62
63
		// Add the menu item.
64
		add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 );
65
		add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) );
66
	}
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 ) {
75
		foreach ( $menu as $key => $menu_item ) {
76
			if ( 'site-switcher' !== $menu_item[3] ) {
77
				continue;
78
			}
79
80
			$menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] );
81
			break;
82
		}
83
84
		return $menu;
85
	}
86
87
	/**
88
	 * Adds a link to the menu to create a new site.
89
	 */
90
	public function add_new_site_link() {
91
		global $menu;
92
93
		if ( count( get_blogs_of_user( get_current_user_id() ) ) > 1 ) {
94
			return;
95
		}
96
97
		// Attempt to get last position.
98
		$position = 1000;
99
		while ( isset( $menu[ $position ] ) ) {
100
			$position++;
101
		}
102
103
		$this->add_admin_menu_separator( ++$position );
104
		add_menu_page( __( 'Add new site', 'jetpack' ), __( 'Add new site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt', ++$position );
105
	}
106
107
	/**
108
	 * Adds site card component.
109
	 */
110
	public function add_site_card_menu() {
111
		$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>' );
112
		$icon    = get_site_icon_url( 32, $default );
113
114
		if ( $default === $icon && blavatar_exists( $this->domain ) ) {
115
			$icon = blavatar_url( $this->domain, 'img', 32 );
116
		}
117
118
		$badge = '';
119
		if ( is_private_blog() ) {
120
			$badge .= sprintf(
121
				'<span class="site__badge site__badge-private">%s</span>',
122
				wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
123
			);
124
		}
125
126
		if ( is_redirected_domain( $this->domain ) ) {
127
			$badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>';
128
		}
129
130
		if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) {
131
			$badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>';
132
		}
133
134
		$site_card = '
135
<div class="site__info">
136
	<div class="site__title">%1$s</div>
137
	<div class="site__domain">%2$s</div>
138
	%3$s
139
</div>';
140
141
		$site_card = sprintf(
142
			$site_card,
143
			get_option( 'blogname' ),
144
			$this->domain,
145
			$badge
146
		);
147
148
		add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
149
		add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
150
	}
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 ) {
159
		foreach ( $menu as $key => $menu_item ) {
160
			if ( 'site-card' !== $menu_item[3] ) {
161
				continue;
162
			}
163
164
			$classes = ' toplevel_page_site-card';
165
			if ( blavatar_exists( $this->domain ) ) {
166
				$classes .= ' has-site-icon';
167
			}
168
169
			$menu[ $key ][4] = $menu_item[4] . $classes;
170
			$menu[ $key ][5] = 'toplevel_page_site_card';
171
			break;
172
		}
173
174
		return $menu;
175
	}
176
177
	/**
178
	 * Adds Stats menu.
179
	 */
180
	public function add_stats_menu() {
181
		$menu_title = __( 'Stats', 'jetpack' );
182
183
		if ( ! $this->is_api_request ) {
184
			$menu_title .= sprintf(
185
				'<img class="sidebar-unified__sparkline" width="80" height="20" src="%1$s" alt="%2$s">',
186
				esc_url( site_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ),
187
				esc_attr__( 'Hourly views', 'jetpack' )
188
			);
189
		}
190
191
		add_menu_page( __( 'Stats', 'jetpack' ), $menu_title, 'edit_posts', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
192
	}
193
194
	/**
195
	 * Adds Upgrades menu.
196
	 */
197
	public function add_upgrades_menu() {
198
		parent::add_upgrades_menu();
199
200
		add_submenu_page( 'https://wordpress.com/plans/' . $this->domain, __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, 10 );
201
	}
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 ) {
209
		$users_slug   = $wp_admin ? 'users.php' : 'https://wordpress.com/people/team/' . $this->domain;
210
		$add_new_slug = 'https://wordpress.com/people/new/' . $this->domain;
211
		$profile_slug = $wp_admin ? 'grofiles-editor' : 'https://wordpress.com/me';
212
		$account_slug = $wp_admin ? 'grofiles-user-settings' : 'https://wordpress.com/me/account';
213
214
		if ( current_user_can( 'list_users' ) ) {
215
			remove_menu_page( 'users.php' );
216
			remove_submenu_page( 'users.php', 'users.php' );
217
			remove_submenu_page( 'users.php', 'user-new.php' );
218
			remove_submenu_page( 'users.php', 'profile.php' );
219
			remove_submenu_page( 'users.php', 'grofiles-editor' );
220
			remove_submenu_page( 'users.php', 'grofiles-user-settings' );
221
222
			add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 );
223
			add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 );
224
			add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 );
225
			add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 );
226
			add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 20 );
227
228
			$this->migrate_submenus( 'users.php', $users_slug );
229
			add_filter(
230
				'parent_file',
231
				function ( $parent_file ) use ( $users_slug ) {
232
					return 'users.php' === $parent_file ? $users_slug : $parent_file;
233
				}
234
			);
235
		} elseif ( ! $wp_admin ) {
236
			remove_menu_page( 'profile.php' );
237
			remove_submenu_page( 'profile.php', 'grofiles-editor' );
238
			remove_submenu_page( 'profile.php', 'grofiles-user-settings' );
239
240
			add_menu_page( esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 'dashicons-admin-users', 70 );
241
			add_submenu_page( $profile_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 5 );
242
243
			$this->migrate_submenus( 'profile.php', $profile_slug );
244
			add_filter(
245
				'parent_file',
246
				function ( $parent_file ) use ( $profile_slug ) {
247
					return 'profile.php' === $parent_file ? $profile_slug : $parent_file;
248
				}
249
			);
250
		}
251
	}
252
253
	/**
254
	 * Adds Tools menu.
255
	 *
256
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
257
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
258
	 */
259
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {  // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
260
		// Export on Simple sites is always handled on Calypso.
261
		parent::add_tools_menu( $wp_admin_import, false );
262
	}
263
264
	/**
265
	 * Adds Settings menu.
266
	 *
267
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
268
	 */
269
	public function add_options_menu( $wp_admin = false ) {
270
		add_options_page( esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 6 );
271
272
		// Replace sharing menu if it exists. See Publicize_UI::sharing_menu.
273
		if ( remove_submenu_page( 'options-general.php', 'sharing' ) ) {
274
			add_options_page( esc_attr__( 'Sharing Settings', 'jetpack' ), __( 'Sharing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/sharing-buttons/' . $this->domain, null, 30 );
275
		}
276
277
		parent::add_options_menu( $wp_admin );
278
	}
279
280
	/**
281
	 * 1. Remove the Gutenberg plugin menu
282
	 * 2. Re-add the Site Editor menu
283
	 *
284
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
285
	 */
286
	public function add_gutenberg_menus( $wp_admin = false ) {
287
		// Always remove the Gutenberg menu.
288
		remove_menu_page( 'gutenberg' );
289
290
		// We can bail if we don't meet the conditions of the Site Editor.
291
		if ( ! ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) ) {
292
			return;
293
		}
294
295
		// Core Gutenberg registers without an explicit position, and we don't want the (beta) tag.
296
		remove_menu_page( 'gutenberg-edit-site' );
297
		// Core Gutenberg tries to manage its position, foiling our best laid plans. Unfoil.
298
		remove_filter( 'menu_order', 'gutenberg_menu_order' );
299
300
		$link = $wp_admin ? 'gutenberg-edit-site' : 'https://wordpress.com/site-editor/' . $this->domain;
301
302
		add_menu_page(
303
			__( 'Site Editor', 'jetpack' ),
304
			__( 'Site Editor', 'jetpack' ),
305
			'edit_theme_options',
306
			$link,
307
			$wp_admin ? 'gutenberg_edit_site_page' : null,
308
			'dashicons-layout',
309
			61 // Just under Appearance.
310
		);
311
	}
312
313
	/**
314
	 * Whether to use wp-admin pages rather than Calypso.
315
	 *
316
	 * @return bool
317
	 */
318
	public function should_link_to_wp_admin() {
319
		$result = false; // Calypso.
320
321
		$user_attribute = get_user_attribute( get_current_user_id(), 'calypso_preferences' );
322
		if ( ! empty( $user_attribute['linkDestination'] ) ) {
323
			$result = $user_attribute['linkDestination'];
324
		}
325
326
		return $result;
327
	}
328
}
329