Completed
Push — add/check-update-button ( 32424b )
by
unknown
241:49 queued 232:20
created

WPcom_Admin_Menu   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 355
Duplicated Lines 16.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 57
loc 355
rs 8.64
c 0
b 0
f 0
wmc 47
lcom 1
cbo 2

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A rest_api_init() 0 7 1
A reregister_menu_items() 14 14 2
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 add_jetpack_menu() 31 31 3
A add_plugins_menu() 0 29 4
B add_users_menu() 0 44 8
A add_tools_menu() 0 11 2
A add_options_menu() 0 10 2
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
/**
13
 * Class WPcom_Admin_Menu.
14
 */
15
class WPcom_Admin_Menu extends Admin_Menu {
16
17
	/**
18
	 * WPcom_Admin_Menu constructor.
19
	 */
20
	protected function __construct() {
21
		parent::__construct();
22
23
		$this->customize_slug = 'https://wordpress.com/customize/' . $this->domain;
24
	}
25
26
	/**
27
	 * Sets up class properties for REST API requests.
28
	 */
29
	public function rest_api_init() {
30
		parent::rest_api_init();
31
32
		// Get domain for requested site.
33
		$this->domain         = ( new Status() )->get_site_suffix();
34
		$this->customize_slug = 'https://wordpress.com/customize/' . $this->domain;
35
	}
36
37
	/**
38
	 * Create the desired menu output.
39
	 */
40 View Code Duplication
	public function reregister_menu_items() {
41
		parent::reregister_menu_items();
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_jetpack_menu();
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 Jetpack menu.
205
	 */
206 View Code Duplication
	public function add_jetpack_menu() {
207
		global $menu;
208
209
		$position = 50;
210
		while ( isset( $menu[ $position ] ) ) {
211
			$position++;
212
		}
213
214
		// TODO: Replace with proper SVG data url.
215
		$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";
216
		$jetpack_slug = 'https://wordpress.com/activity-log/' . $this->domain;
217
218
		$this->add_admin_menu_separator( $position++, 'manage_options' );
219
		add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', $jetpack_slug, null, $jetpack_icon, $position );
220
221
		// Maintain id for jQuery selector.
222
		$menu[ $position ][5] = 'toplevel_page_jetpack'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
223
224
		remove_menu_page( 'jetpack' );
225
		$this->migrate_submenus( 'jetpack', $jetpack_slug );
226
227
		add_submenu_page( $jetpack_slug, esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', $jetpack_slug, null, 5 );
228
		add_submenu_page( $jetpack_slug, esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 10 );
229
230
		add_filter(
231
			'parent_file',
232
			function ( $parent_file ) use ( $jetpack_slug ) {
233
				return 'jetpack' === $parent_file ? $jetpack_slug : $parent_file;
234
			}
235
		);
236
	}
237
238
	/**
239
	 * Adds Plugins menu.
240
	 */
241
	public function add_plugins_menu() {
242
		parent::add_plugins_menu();
243
244
		$menu_slug = 'https://wordpress.com/plugins/' . $this->domain;
245
246
		remove_menu_page( 'plugins.php' );
247
		remove_submenu_page( 'plugins.php', 'plugins.php' );
248
249
		$count = '';
250
		if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) {
251
			$update_data = wp_get_update_data();
252
			$count       = sprintf(
253
				'<span class="update-plugins count-%s"><span class="plugin-count">%s</span></span>',
254
				$update_data['counts']['plugins'],
255
				number_format_i18n( $update_data['counts']['plugins'] )
256
			);
257
		}
258
259
		/* translators: %s: Number of pending plugin updates. */
260
		add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), sprintf( __( 'Plugins %s', 'jetpack' ), $count ), 'activate_plugins', $menu_slug, null, 'dashicons-admin-plugins', 65 );
261
262
		$this->migrate_submenus( 'plugins.php', $menu_slug );
263
		add_filter(
264
			'parent_file',
265
			function ( $parent_file ) use ( $menu_slug ) {
266
				return 'jetpack' === $parent_file ? $menu_slug : $parent_file;
267
			}
268
		);
269
	}
270
271
	/**
272
	 * Adds Users menu.
273
	 *
274
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
275
	 */
276
	public function add_users_menu( $wp_admin = false ) {
277
		$users_slug   = $wp_admin ? 'users.php' : 'https://wordpress.com/people/team/' . $this->domain;
278
		$add_new_slug = 'https://wordpress.com/people/new/' . $this->domain;
279
		$profile_slug = $wp_admin ? 'grofiles-editor' : 'https://wordpress.com/me';
280
		$account_slug = $wp_admin ? 'grofiles-user-settings' : 'https://wordpress.com/me/account';
281
282
		if ( current_user_can( 'list_users' ) ) {
283
			remove_menu_page( 'users.php' );
284
			remove_submenu_page( 'users.php', 'users.php' );
285
			remove_submenu_page( 'users.php', 'user-new.php' );
286
			remove_submenu_page( 'users.php', 'profile.php' );
287
			remove_submenu_page( 'users.php', 'grofiles-editor' );
288
			remove_submenu_page( 'users.php', 'grofiles-user-settings' );
289
290
			add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 );
291
			add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 );
292
			add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 );
293
			add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 );
294
			add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 20 );
295
296
			$this->migrate_submenus( 'users.php', $users_slug );
297
			add_filter(
298
				'parent_file',
299
				function ( $parent_file ) use ( $users_slug ) {
300
					return 'users.php' === $parent_file ? $users_slug : $parent_file;
301
				}
302
			);
303
		} elseif ( ! $wp_admin ) {
304
			remove_menu_page( 'profile.php' );
305
			remove_submenu_page( 'profile.php', 'grofiles-editor' );
306
			remove_submenu_page( 'profile.php', 'grofiles-user-settings' );
307
308
			add_menu_page( esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 'dashicons-admin-users', 70 );
309
			add_submenu_page( $profile_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 5 );
310
311
			$this->migrate_submenus( 'profile.php', $profile_slug );
312
			add_filter(
313
				'parent_file',
314
				function ( $parent_file ) use ( $profile_slug ) {
315
					return 'profile.php' === $parent_file ? $profile_slug : $parent_file;
316
				}
317
			);
318
		}
319
	}
320
321
	/**
322
	 * Adds Tools menu.
323
	 *
324
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
325
	 */
326
	public function add_tools_menu( $wp_admin = false ) {
327
		$menu_slug = $wp_admin ? 'tools.php' : 'https://wordpress.com/marketing/tools/' . $this->domain;
328
329
		remove_submenu_page( 'tools.php', 'export.php' );
330
331
		add_submenu_page( $menu_slug, esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'manage_options', 'https://wordpress.com/marketing/tools/' . $this->domain, null, 5 );
332
		add_submenu_page( $menu_slug, esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain, null, 10 );
333
		add_submenu_page( $menu_slug, esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', 'https://wordpress.com/export/' . $this->domain, null, 20 );
334
335
		parent::add_tools_menu( $wp_admin );
336
	}
337
338
	/**
339
	 * Adds Settings menu.
340
	 *
341
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
342
	 */
343
	public function add_options_menu( $wp_admin = false ) {
344
		add_options_page( esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 6 );
345
346
		// Replace sharing menu if it exists. See Publicize_UI::sharing_menu.
347
		if ( remove_submenu_page( 'options-general.php', 'sharing' ) ) {
348
			add_options_page( esc_attr__( 'Sharing Settings', 'jetpack' ), __( 'Sharing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/sharing-buttons/' . $this->domain, null, 30 );
349
		}
350
351
		parent::add_options_menu( $wp_admin );
352
	}
353
354
	/**
355
	 * Whether to use wp-admin pages rather than Calypso.
356
	 *
357
	 * @return bool
358
	 */
359
	public function should_link_to_wp_admin() {
360
		$result = false; // Calypso.
361
362
		$user_attribute = get_user_attribute( get_current_user_id(), 'calypso_preferences' );
363
		if ( ! empty( $user_attribute['linkDestination'] ) ) {
364
			$result = $user_attribute['linkDestination'];
365
		}
366
367
		return $result;
368
	}
369
}
370