Completed
Push — try/wpcom-nav-package ( c38648 )
by
unknown
12:50 queued 02:43
created

Atomic_Admin_Menu   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 335
Duplicated Lines 8.66 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 29
loc 335
rs 9.0399
c 0
b 0
f 0
wmc 42
lcom 1
cbo 2

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 2
A dequeue_scripts() 0 3 1
A is_rtl() 0 3 1
A reregister_menu_items() 14 14 2
A add_posts_menu() 0 3 1
A add_page_menu() 0 3 1
A add_plugins_menu() 0 4 1
A add_browse_sites_link() 0 10 3
A set_browse_sites_link_class() 12 12 3
A add_new_site_link() 0 9 3
A add_site_card_menu() 0 30 5
A set_site_card_menu_class() 0 20 5
A add_upgrades_menu() 3 25 3
A add_tools_menu() 0 4 1
A add_options_menu() 0 11 2
A add_appearance_menu() 0 6 1
A override_the_theme_installer() 0 8 3
A add_users_menu() 0 5 1
A add_gutenberg_menus() 0 5 1
A add_comments_menu() 0 3 1
A ajax_sidebar_state() 0 14 1

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 Atomic_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 Atomic_Admin_Menu, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Atomic Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Jetpack_Plan;
11
12
/**
13
 * Class Atomic_Admin_Menu.
14
 */
15
class Atomic_Admin_Menu extends Admin_Menu {
16
17
	/**
18
	 * Atomic_Admin_Menu constructor.
19
	 */
20
	protected function __construct() {
21
		parent::__construct();
22
23
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
24
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
25
		add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) );
26
27
		if ( ! $this->is_api_request ) {
28
			add_filter( 'submenu_file', array( $this, 'override_the_theme_installer' ), 10, 2 );
29
		}
30
31
		add_action(
32
			'admin_menu',
33
			function () {
34
				remove_action( 'admin_menu', 'gutenberg_menu', 9 );
35
			},
36
			0
37
		);
38
	}
39
40
	/**
41
	 * Dequeues unnecessary scripts.
42
	 */
43
	public function dequeue_scripts() {
44
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
45
	}
46
47
	/**
48
	 * Determines whether the current locale is right-to-left (RTL).
49
	 *
50
	 * Performs the check against the current locale set on the WordPress.com's account settings.
51
	 * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`.
52
	 */
53
	public function is_rtl() {
54
		return get_user_option( 'jetpack_wpcom_is_rtl' );
55
	}
56
57
	/**
58
	 * Create the desired menu output.
59
	 */
60 View Code Duplication
	public function reregister_menu_items() {
61
		parent::reregister_menu_items();
62
63
		$this->add_my_home_menu();
64
65
		// Not needed outside of wp-admin.
66
		if ( ! $this->is_api_request ) {
67
			$this->add_browse_sites_link();
68
			$this->add_site_card_menu();
69
			$this->add_new_site_link();
70
		}
71
72
		ksort( $GLOBALS['menu'] );
73
	}
74
75
	/**
76
	 * Forces Posts menu to WPAdmin for Atomic sites only.
77
	 * Overloads `add_posts_menu` in parent class.
78
	 *
79
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
80
	 */
81
	public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
82
		return false; // return explicit `false` to force WPAdmin links.
83
	}
84
85
	/**
86
	 * Forces Pages menu to WPAdmin for Atomic sites only.
87
	 * Overloads `add_page_menu` in parent class.
88
	 *
89
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
90
	 */
91
	public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
92
		return false; // return explicit `false` to force WPAdmin links.
93
	}
94
95
	/**
96
	 * Adds Plugins menu.
97
	 *
98
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
99
	 */
100
	public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
101
		// Plugins on Atomic sites are always managed on WP Admin.
102
		parent::add_plugins_menu( true );
103
	}
104
105
	/**
106
	 * Adds the site switcher link if user has more than one site.
107
	 */
108
	public function add_browse_sites_link() {
109
		$site_count = get_user_option( 'wpcom_site_count' );
110
		if ( ! $site_count || $site_count < 2 ) {
111
			return;
112
		}
113
114
		// Add the menu item.
115
		add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 );
116
		add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) );
117
	}
118
119
	/**
120
	 * Adds a custom element class for Site Switcher menu item.
121
	 *
122
	 * @param array $menu Associative array of administration menu items.
123
	 *
124
	 * @return array
125
	 */
126 View Code Duplication
	public function set_browse_sites_link_class( array $menu ) {
127
		foreach ( $menu as $key => $menu_item ) {
128
			if ( 'site-switcher' !== $menu_item[3] ) {
129
				continue;
130
			}
131
132
			$menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] );
133
			break;
134
		}
135
136
		return $menu;
137
	}
138
139
	/**
140
	 * Adds a link to the menu to create a new site.
141
	 */
142
	public function add_new_site_link() {
143
		$site_count = get_user_option( 'wpcom_site_count' );
144
		if ( $site_count && $site_count > 1 ) {
145
			return;
146
		}
147
148
		$this->add_admin_menu_separator();
149
		add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' );
150
	}
151
152
	/**
153
	 * Adds site card component.
154
	 */
155
	public function add_site_card_menu() {
156
		$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>' );
157
		$icon      = get_site_icon_url( 32, $default );
158
		$blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain;
159
160
		$badge = '';
161
		if ( function_exists( 'site_is_private' ) && site_is_private() ) {
162
			$badge .= sprintf(
163
				'<span class="site__badge site__badge-private">%s</span>',
164
				site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
165
			);
166
		}
167
168
		$site_card = '
169
<div class="site__info">
170
	<div class="site__title">%1$s</div>
171
	<div class="site__domain">%2$s</div>
172
	%3$s
173
</div>';
174
175
		$site_card = sprintf(
176
			$site_card,
177
			$blog_name,
178
			$this->domain,
179
			$badge
180
		);
181
182
		add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
183
		add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
184
	}
185
186
	/**
187
	 * Adds a custom element class and id for Site Card's menu item.
188
	 *
189
	 * @param array $menu Associative array of administration menu items.
190
	 *
191
	 * @return array
192
	 */
193
	public function set_site_card_menu_class( array $menu ) {
194
		foreach ( $menu as $key => $menu_item ) {
195
			if ( 'site-card' !== $menu_item[3] ) {
196
				continue;
197
			}
198
199
			$classes = ' toplevel_page_site-card';
200
201
			// webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon.
202
			if ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) {
203
				$classes .= ' has-site-icon';
204
			}
205
206
			$menu[ $key ][4] = $menu_item[4] . $classes;
207
			$menu[ $key ][5] = 'toplevel_page_site_card';
208
			break;
209
		}
210
211
		return $menu;
212
	}
213
214
	/**
215
	 * Adds Upgrades menu.
216
	 *
217
	 * @param string $plan The current WPCOM plan of the blog.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $plan not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
218
	 */
219
	public function add_upgrades_menu( $plan = null ) {
220
		$products = Jetpack_Plan::get();
221
		if ( array_key_exists( 'product_name_short', $products ) ) {
222
			$plan = $products['product_name_short'];
223
		}
224
		parent::add_upgrades_menu( $plan );
225
226
		$last_upgrade_submenu_position = $this->get_submenu_item_count( 'paid-upgrades.php' );
227
228
		add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, $last_upgrade_submenu_position - 1 );
229
230
		/**
231
		 * Whether to show the WordPress.com Emails submenu under the main Upgrades menu.
232
		 *
233
		 * @use add_filter( 'jetpack_show_wpcom_upgrades_email_menu', '__return_true' );
234
		 * @module masterbar
235
		 *
236
		 * @since 9.7.0
237
		 *
238
		 * @param bool $show_wpcom_upgrades_email_menu Load the WordPress.com Emails submenu item. Default to false.
239
		 */
240 View Code Duplication
		if ( apply_filters( 'jetpack_show_wpcom_upgrades_email_menu', false ) ) {
241
			add_submenu_page( 'paid-upgrades.php', __( 'Emails', 'jetpack' ), __( 'Emails', 'jetpack' ), 'manage_options', 'https://wordpress.com/email/' . $this->domain, null, $last_upgrade_submenu_position );
242
		}
243
	}
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
252
		// Export on Atomic sites is always handled on WP Admin.
253
		parent::add_tools_menu( $wp_admin_import, true );
254
	}
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 );
263
264
		add_submenu_page( 'options-general.php', esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 6 );
265
266
		// No need to add a menu linking to WP Admin if there is already one.
267
		if ( ! $wp_admin ) {
268
			add_submenu_page( 'options-general.php', esc_attr__( 'Advanced General', 'jetpack' ), __( 'Advanced General', 'jetpack' ), 'manage_options', 'options-general.php' );
269
			add_submenu_page( 'options-general.php', esc_attr__( 'Advanced Writing', 'jetpack' ), __( 'Advanced Writing', 'jetpack' ), 'manage_options', 'options-writing.php' );
270
		}
271
	}
272
273
	/**
274
	 * Adds Appearance menu.
275
	 *
276
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
277
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
278
	 */
279
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
280
		// Customize on Atomic sites is always done on WP Admin.
281
		parent::add_appearance_menu( $wp_admin_themes, true );
282
283
		add_submenu_page( 'themes.php', esc_attr__( 'Add New Theme', 'jetpack' ), __( 'Add New Theme', 'jetpack' ), 'install_themes', 'theme-install.php', null, 1 );
284
	}
285
286
	/**
287
	 * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly.
288
	 *
289
	 * @param string $submenu_file The current pages $submenu_file global variable value.
290
	 * @return string | null
291
	 */
292
	public function override_the_theme_installer( $submenu_file ) {
293
		global $pagenow;
294
295
		if ( 'themes.php' === $submenu_file && 'theme-install.php' === $pagenow ) {
296
			return null;
297
		}
298
		return $submenu_file;
299
	}
300
301
	/**
302
	 * Adds Users menu.
303
	 *
304
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
305
	 */
306
	public function add_users_menu( $wp_admin = false ) {
307
		parent::add_users_menu( $wp_admin );
308
309
		add_submenu_page( 'users.php', esc_attr__( 'Advanced Users Management', 'jetpack' ), __( 'Advanced Users Management', 'jetpack' ), 'list_users', 'users.php', null, 2 );
310
	}
311
312
	/**
313
	 * Also remove the Gutenberg plugin menu.
314
	 *
315
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
316
	 */
317
	public function add_gutenberg_menus( $wp_admin = false ) {
318
		// Always remove the Gutenberg menu.
319
		remove_menu_page( 'gutenberg' );
320
		parent::add_gutenberg_menus( $wp_admin );
321
	}
322
323
	/**
324
	 * Always use WP Admin for comments.
325
	 *
326
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
327
	 */
328
	public function add_comments_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
329
		parent::add_comments_menu( true );
330
	}
331
332
	/**
333
	 * Saves the sidebar state ( expanded / collapsed ) via an ajax request.
334
	 */
335
	public function ajax_sidebar_state() {
336
		$expanded = filter_var( $_REQUEST['expanded'], FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
337
		Client::wpcom_json_api_request_as_user(
338
			'/me/preferences',
339
			'2',
340
			array(
341
				'method' => 'POST',
342
			),
343
			(object) array( 'calypso_preferences' => (object) array( 'sidebarCollapsed' => ! $expanded ) ),
344
			'wpcom'
345
		);
346
347
		wp_die();
348
	}
349
}
350
351