Completed
Push — update/wpcom-block-editor-shor... ( 9897fe...f43ed6 )
by Jeremy
148:26 queued 138:20
created

Admin_Menu   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 590
Duplicated Lines 4.58 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 27
loc 590
rs 2.64
c 0
b 0
f 0
wmc 72
lcom 2
cbo 2

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A get_instance() 0 9 2
A rest_api_init() 0 5 1
A reregister_menu_items() 0 48 3
A add_my_home_menu() 0 3 1
A add_stats_menu() 0 3 1
A add_upgrades_menu() 0 23 5
A add_posts_menu() 11 11 2
A add_media_menu() 0 9 2
A add_page_menu() 11 11 2
A add_testimonials_menu() 0 3 1
A add_portfolio_menu() 0 3 1
A add_custom_post_type_menu() 0 11 2
A add_comments_menu() 0 7 2
A add_tools_menu() 0 16 3
A add_options_menu() 0 10 2
A add_jetpack_menu() 0 31 4
D update_menu() 0 55 12
A update_submenus() 0 15 4
A remove_submenus() 0 8 2
A add_admin_menu_separator() 0 13 1
A enqueue_scripts() 5 22 4
A dequeue_scripts() 0 3 1
A should_link_to_wp_admin() 0 3 1
B add_appearance_menu() 0 44 6
A add_plugins_menu() 0 10 2
A add_users_menu() 0 24 4

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

1
<?php
2
/**
3
 * Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Redirect;
11
use Automattic\Jetpack\Status;
12
13
/**
14
 * Class Admin_Menu.
15
 */
16
class Admin_Menu {
17
	/**
18
	 * Holds class instances.
19
	 *
20
	 * @var array
21
	 */
22
	protected static $instances;
23
24
	/**
25
	 * Whether the current request is a REST API request.
26
	 *
27
	 * @var bool
28
	 */
29
	protected $is_api_request = false;
30
31
	/**
32
	 * Domain of the current site.
33
	 *
34
	 * @var string
35
	 */
36
	protected $domain;
37
38
	/**
39
	 * Admin_Menu constructor.
40
	 */
41
	protected function __construct() {
42
		add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 );
43
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
44
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
45
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
46
		add_filter( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 );
47
48
		$this->domain = ( new Status() )->get_site_suffix();
49
	}
50
51
	/**
52
	 * Returns class instance.
53
	 *
54
	 * @return Admin_Menu
55
	 */
56
	public static function get_instance() {
57
		$class = get_called_class();
58
59
		if ( empty( static::$instances[ $class ] ) ) {
60
			static::$instances[ $class ] = new $class();
61
		}
62
63
		return static::$instances[ $class ];
64
	}
65
66
	/**
67
	 * Sets up class properties for REST API requests.
68
	 *
69
	 * @param WP_REST_Response $response Response from the endpoint.
70
	 */
71
	public function rest_api_init( $response ) {
72
		$this->is_api_request = true;
73
74
		return $response;
75
	}
76
77
	/**
78
	 * Create the desired menu output.
79
	 */
80
	public function reregister_menu_items() {
81
		// Constant is not defined until parse_request.
82
		if ( ! $this->is_api_request ) {
83
			$this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
84
		}
85
86
		/*
87
		 * Whether links should point to Calypso or wp-admin.
88
		 *
89
		 * Options:
90
		 * false - Calypso (Default).
91
		 * true  - wp-admin.
92
		 */
93
		$wp_admin = $this->should_link_to_wp_admin();
94
95
		// Remove separators.
96
		remove_menu_page( 'separator1' );
97
98
		$this->add_stats_menu();
99
		$this->add_upgrades_menu();
100
		$this->add_posts_menu( $wp_admin );
101
		$this->add_media_menu( $wp_admin );
102
		$this->add_page_menu( $wp_admin );
103
		$this->add_testimonials_menu( $wp_admin );
104
		$this->add_portfolio_menu( $wp_admin );
105
		$this->add_comments_menu( $wp_admin );
106
107
		// Whether Themes/Customize links should point to Calypso (false) or wp-admin (true).
108
		$wp_admin_themes    = $wp_admin;
109
		$wp_admin_customize = $wp_admin;
110
		$this->add_appearance_menu( $wp_admin_themes, $wp_admin_customize );
111
		$this->add_plugins_menu( $wp_admin );
112
		$this->add_users_menu( $wp_admin );
113
114
		// Whether Import/Export links should point to Calypso (false) or wp-admin (true).
115
		$wp_admin_import = $wp_admin;
116
		$wp_admin_export = $wp_admin;
117
		$this->add_tools_menu( $wp_admin_import, $wp_admin_export );
118
119
		$this->add_options_menu( $wp_admin );
120
		$this->add_jetpack_menu();
121
122
		// Remove Links Manager menu since its usage is discouraged.
123
		// @see https://core.trac.wordpress.org/ticket/21307#comment:73.
124
		remove_menu_page( 'link-manager.php' );
125
126
		ksort( $GLOBALS['menu'] );
127
	}
128
129
	/**
130
	 * Adds My Home menu.
131
	 */
132
	public function add_my_home_menu() {
133
		$this->update_menu( 'index.php', 'https://wordpress.com/home/' . $this->domain, __( 'My Home', 'jetpack' ), 'manage_options', 'dashicons-admin-home' );
134
	}
135
136
	/**
137
	 * Adds Stats menu.
138
	 */
139
	public function add_stats_menu() {
140
		add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
141
	}
142
143
	/**
144
	 * Adds Upgrades menu.
145
	 */
146
	public function add_upgrades_menu() {
147
		global $menu;
148
149
		$menu_exists = false;
150
		foreach ( $menu as $item ) {
151
			if ( 'paid-upgrades.php' === $item[2] ) {
152
				$menu_exists = true;
153
				break;
154
			}
155
		}
156
157
		if ( ! $menu_exists ) {
158
			add_menu_page( __( 'Upgrades', 'jetpack' ), __( 'Upgrades', 'jetpack' ), 'manage_options', 'paid-upgrades.php', null, 'dashicons-cart', 4 );
159
		}
160
161
		add_submenu_page( 'paid-upgrades.php', __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', 'https://wordpress.com/plans/' . $this->domain, null, 5 );
162
		add_submenu_page( 'paid-upgrades.php', __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 15 );
163
164
		if ( ! $menu_exists ) {
165
			// Remove the submenu auto-created by Core.
166
			remove_submenu_page( 'paid-upgrades.php', 'paid-upgrades.php' );
167
		}
168
	}
169
170
	/**
171
	 * Adds Posts menu.
172
	 *
173
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
174
	 */
175 View Code Duplication
	public function add_posts_menu( $wp_admin = false ) {
176
		if ( $wp_admin ) {
177
			return;
178
		}
179
180
		$submenus_to_update = array(
181
			'edit.php'     => 'https://wordpress.com/posts/' . $this->domain,
182
			'post-new.php' => 'https://wordpress.com/post/' . $this->domain,
183
		);
184
		$this->update_submenus( 'edit.php', $submenus_to_update );
185
	}
186
187
	/**
188
	 * Adds Media menu.
189
	 *
190
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
191
	 */
192
	public function add_media_menu( $wp_admin = false ) {
193
		if ( $wp_admin ) {
194
			return;
195
		}
196
197
		remove_submenu_page( 'upload.php', 'media-new.php' );
198
199
		$this->update_menu( 'upload.php', 'https://wordpress.com/media/' . $this->domain );
200
	}
201
202
	/**
203
	 * Adds Page menu.
204
	 *
205
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
206
	 */
207 View Code Duplication
	public function add_page_menu( $wp_admin = false ) {
208
		if ( $wp_admin ) {
209
			return;
210
		}
211
212
		$submenus_to_update = array(
213
			'edit.php?post_type=page'     => 'https://wordpress.com/pages/' . $this->domain,
214
			'post-new.php?post_type=page' => 'https://wordpress.com/page/' . $this->domain,
215
		);
216
		$this->update_submenus( 'edit.php?post_type=page', $submenus_to_update );
217
	}
218
219
	/**
220
	 * Adds Testimonials menu.
221
	 *
222
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
223
	 */
224
	public function add_testimonials_menu( $wp_admin = false ) {
225
		$this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin );
226
	}
227
228
	/**
229
	 * Adds Portfolio menu.
230
	 *
231
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
232
	 */
233
	public function add_portfolio_menu( $wp_admin = false ) {
234
		$this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin );
235
	}
236
237
	/**
238
	 * Adds a custom post type menu.
239
	 *
240
	 * @param string $post_type Custom post type.
241
	 * @param bool   $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
242
	 */
243
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) {
244
		if ( $wp_admin ) {
245
			return;
246
		}
247
248
		$submenus_to_update = array(
249
			'edit.php?post_type=' . $post_type     => 'https://wordpress.com/types/' . $post_type . '/' . $this->domain,
250
			'post-new.php?post_type=' . $post_type => 'https://wordpress.com/edit/' . $post_type . '/' . $this->domain,
251
		);
252
		$this->update_submenus( 'edit.php?post_type=' . $post_type, $submenus_to_update );
253
	}
254
255
	/**
256
	 * Adds Comments menu.
257
	 *
258
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
259
	 */
260
	public function add_comments_menu( $wp_admin = false ) {
261
		if ( $wp_admin ) {
262
			return;
263
		}
264
265
		$this->update_menu( 'edit-comments.php', 'https://wordpress.com/comments/all/' . $this->domain );
266
	}
267
268
	/**
269
	 * Adds Appearance menu.
270
	 *
271
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
272
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
273
	 * @return string The Customizer URL.
274
	 */
275
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) {
276
		$request_uri                     = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
277
		$default_customize_slug          = add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), $request_uri ) ), 'customize.php' );
278
		$default_customize_header_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $default_customize_slug );
279
		// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
280
		$default_customize_header_slug_2     = admin_url( 'themes.php?page=custom-header' );
281
		$default_customize_background_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $default_customize_slug );
282
		// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
283
		$default_customize_background_slug_2 = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
284
285
		if ( ! $wp_admin_customize ) {
286
			$customize_url = 'https://wordpress.com/customize/' . $this->domain;
287
		} elseif ( $this->is_api_request ) {
288
			// In case this is an api request we will have to add the 'return' querystring via JS.
289
			$customize_url = 'customize.php';
290
		} else {
291
			$customize_url = $default_customize_slug;
292
		}
293
294
		$submenus_to_update = array(
295
			$default_customize_slug              => $customize_url,
296
			$default_customize_header_slug_1     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
297
			$default_customize_header_slug_2     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
298
			$default_customize_background_slug_1 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
299
			$default_customize_background_slug_2 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
300
		);
301
302
		if ( ! $wp_admin_themes ) {
303
			$submenus_to_update['themes.php'] = 'https://wordpress.com/themes/' . $this->domain;
304
		}
305
306
		if ( ! $wp_admin_customize ) {
307
			$submenus_to_update['widgets.php']       = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
308
			$submenus_to_update['gutenberg-widgets'] = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
309
			$submenus_to_update['nav-menus.php']     = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_url );
310
		}
311
312
		$this->update_submenus( 'themes.php', $submenus_to_update );
313
314
		remove_submenu_page( 'themes.php', 'custom-header' );
315
		remove_submenu_page( 'themes.php', 'custom-background' );
316
317
		return $customize_url;
318
	}
319
320
	/**
321
	 * Adds Plugins menu.
322
	 *
323
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
324
	 */
325
	public function add_plugins_menu( $wp_admin = false ) {
326
		if ( $wp_admin ) {
327
			return;
328
		}
329
330
		remove_submenu_page( 'plugins.php', 'plugin-install.php' );
331
		remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
332
333
		$this->update_menu( 'plugins.php', 'https://wordpress.com/plugins/' . $this->domain );
334
	}
335
336
	/**
337
	 * Adds Users menu.
338
	 *
339
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
340
	 */
341
	public function add_users_menu( $wp_admin = false ) {
342
		if ( current_user_can( 'list_users' ) ) {
343
			if ( ! $wp_admin ) {
344
				$submenus_to_update = array(
345
					'users.php'    => 'https://wordpress.com/people/team/' . $this->domain,
346
					'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
347
					'profile.php'  => 'https://wordpress.com/me',
348
				);
349
				$this->update_submenus( 'users.php', $submenus_to_update );
350
			}
351
352
			add_submenu_page( 'users.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
353
		} else {
354
			if ( ! $wp_admin ) {
355
				$submenus_to_update = array(
356
					'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
357
					'profile.php'  => 'https://wordpress.com/me',
358
				);
359
				$this->update_submenus( 'profile.php', $submenus_to_update );
360
			}
361
362
			add_submenu_page( 'profile.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
363
		}
364
	}
365
366
	/**
367
	 * Adds Tools menu.
368
	 *
369
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
370
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
371
	 */
372
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {
373
		$submenus_to_update = array();
374
		if ( ! $wp_admin_import ) {
375
			$submenus_to_update['import.php'] = 'https://wordpress.com/import/' . $this->domain;
376
		}
377
		if ( ! $wp_admin_export ) {
378
			$submenus_to_update['export.php'] = 'https://wordpress.com/export/' . $this->domain;
379
		}
380
		$this->update_submenus( 'tools.php', $submenus_to_update );
381
382
		remove_submenu_page( 'tools.php', 'tools.php' );
383
		remove_submenu_page( 'tools.php', 'delete-blog' );
384
385
		add_submenu_page( 'tools.php', esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/tools/' . $this->domain, null, 0 );
386
		add_submenu_page( 'tools.php', esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain, null, 1 );
387
	}
388
389
	/**
390
	 * Adds Settings menu.
391
	 *
392
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
393
	 */
394
	public function add_options_menu( $wp_admin = false ) {
395
		if ( $wp_admin ) {
396
			return;
397
		}
398
399
		$this->update_submenus( 'options-general.php', array( 'options-general.php' => 'https://wordpress.com/settings/general/' . $this->domain ) );
400
401
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
402
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
403
	}
404
405
	/**
406
	 * Adds Jetpack menu.
407
	 */
408
	public function add_jetpack_menu() {
409
		global $menu;
410
411
		$position = 50;
412
		while ( isset( $menu[ $position ] ) ) {
413
			$position++;
414
		}
415
		$this->add_admin_menu_separator( $position++, 'manage_options' );
416
417
		// TODO: Replace with proper SVG data url.
418
		$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";
419
420
		$is_menu_updated = $this->update_menu( 'jetpack', null, null, null, $icon, $position );
421
		if ( ! $is_menu_updated ) {
422
			add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', 'jetpack', null, $icon, $position );
423
		}
424
425
		add_submenu_page( 'jetpack', esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $this->domain, null, 2 );
426
		add_submenu_page( 'jetpack', esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 3 );
427
		/* translators: Jetpack sidebar menu item. */
428
		add_submenu_page( 'jetpack', esc_attr__( 'Search', 'jetpack' ), __( 'Search', 'jetpack' ), 'read', 'https://wordpress.com/jetpack-search/' . $this->domain, null, 4 );
429
430
		remove_submenu_page( 'jetpack', 'stats' );
431
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) );
432
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-scanner' ) ) );
433
434
		if ( ! $is_menu_updated ) {
435
			// Remove the submenu auto-created by Core.
436
			remove_submenu_page( 'jetpack', 'jetpack' );
437
		}
438
	}
439
440
	/**
441
	 * Updates the menu data of the given menu slug.
442
	 *
443
	 * @param string $slug Slug of the menu to update.
444
	 * @param string $url New menu URL.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $url 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...
445
	 * @param string $title New menu title.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $title 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...
446
	 * @param string $cap New menu capability.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cap 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...
447
	 * @param string $icon New menu icon.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $icon 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...
448
	 * @param int    $position New menu position.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $position not be integer|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...
449
	 * @return bool Whether the menu has been updated.
450
	 */
451
	public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) {
452
		global $menu, $submenu;
453
454
		$menu_item     = null;
455
		$menu_position = null;
456
457
		foreach ( $menu as $i => $item ) {
458
			if ( $slug === $item[2] ) {
459
				$menu_item     = $item;
460
				$menu_position = $i;
461
				break;
462
			}
463
		}
464
465
		if ( ! $menu_item ) {
466
			return false;
467
		}
468
469
		if ( $title ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $title of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
470
			$menu_item[0] = $title;
471
			$menu_item[3] = esc_attr( $title );
472
		}
473
474
		if ( $cap ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cap of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
475
			$menu_item[1] = $cap;
476
		}
477
478
		// Change parent slug only if there are no submenus (the slug of the 1st submenu will be used if there are submenus).
479
		if ( $url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
480
			remove_submenu_page( $slug, $slug );
481
			if ( empty( $submenu[ $slug ] ) ) {
482
				$menu_item[2] = $url;
483
			}
484
		}
485
486
		if ( $icon ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $icon of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
487
			$menu_item[4] = 'menu-top';
488
			$menu_item[6] = $icon;
489
		}
490
491
		if ( $position ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $position of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
492
			// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
493
			unset( $menu[ $menu_position ] );
494
			$menu_position = $position;
495
		}
496
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
497
		$menu[ $menu_position ] = $menu_item;
498
499
		// Only add submenu when there are other submenu items.
500
		if ( $url && ! empty( $submenu[ $slug ] ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
501
			add_submenu_page( $slug, $menu_item[3], $menu_item[0], $menu_item[1], $url, null, 0 );
502
		}
503
504
		return true;
505
	}
506
507
	/**
508
	 * Updates the submenus of the given menu slug.
509
	 *
510
	 * @param string $slug Menu slug.
511
	 * @param array  $submenus_to_update Array of new submenu slugs.
512
	 */
513
	public function update_submenus( $slug, $submenus_to_update ) {
514
		global $submenu;
515
516
		if ( ! isset( $submenu[ $slug ] ) ) {
517
			return;
518
		}
519
520
		foreach ( $submenu[ $slug ] as $i => $submenu_item ) {
521
			if ( array_key_exists( $submenu_item[2], $submenus_to_update ) ) {
522
				$submenu_item[2] = $submenus_to_update[ $submenu_item[2] ];
523
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
524
				$submenu[ $slug ][ $i ] = $submenu_item;
525
			}
526
		}
527
	}
528
529
	/**
530
	 * Remove submenu items from given menu slug.
531
	 *
532
	 * @param string $slug Menu slug.
533
	 */
534
	public function remove_submenus( $slug ) {
535
		global $submenu;
536
537
		if ( isset( $submenu[ $slug ] ) ) {
538
			// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
539
			$submenu[ $slug ] = array();
540
		}
541
	}
542
543
	/**
544
	 * Adds a menu separator.
545
	 *
546
	 * @param int    $position The position in the menu order this item should appear.
547
	 * @param string $cap Optional. The capability required for this menu to be displayed to the user.
548
	 *                         Default: 'read'.
549
	 */
550
	public function add_admin_menu_separator( $position, $cap = 'read' ) {
551
		global $menu;
552
553
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
554
		$menu[ $position ] = array(
555
			'',                                  // Menu title (ignored).
556
			$cap,                                // Required capability.
557
			wp_unique_id( 'separator-custom-' ), // URL or file (ignored, but must be unique).
558
			'',                                  // Page title (ignored).
559
			'wp-menu-separator',                 // CSS class. Identifies this item as a separator.
560
		);
561
		ksort( $menu );
562
	}
563
564
	/**
565
	 * Enqueues scripts and styles.
566
	 */
567
	public function enqueue_scripts() {
568
		$style_dependencies = array();
0 ignored issues
show
Unused Code introduced by
$style_dependencies is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
569
		$rtl                = is_rtl() ? '-rtl' : '';
570 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
571
			$style_dependencies = array( 'wpcom-admin-bar', 'wpcom-masterbar-css' );
572
		} else {
573
			$style_dependencies = array( 'a8c-wpcom-masterbar' . $rtl, 'a8c-wpcom-masterbar-overrides' . $rtl );
574
		}
575
		wp_enqueue_style(
576
			'jetpack-admin-menu',
577
			plugins_url( 'admin-menu.css', __FILE__ ),
578
			$style_dependencies,
579
			JETPACK__VERSION
580
		);
581
		wp_enqueue_script(
582
			'jetpack-admin-menu',
583
			plugins_url( 'admin-menu.js', __FILE__ ),
584
			array(),
585
			JETPACK__VERSION,
586
			true
587
		);
588
	}
589
590
	/**
591
	 * Dequeues unnecessary scripts.
592
	 */
593
	public function dequeue_scripts() {
594
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
595
	}
596
597
	/**
598
	 * Whether to use wp-admin pages rather than Calypso.
599
	 *
600
	 * @return bool
601
	 */
602
	public function should_link_to_wp_admin() {
603
		return get_user_option( 'jetpack_admin_menu_link_destination' );
604
	}
605
}
606