Completed
Push — update/admin-menu-sso-disabled ( 39bf4b...eefb5e )
by
unknown
10:52
created

Base_Admin_Menu::is_item_visible()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Base 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 Base_Admin_Menu
14
 */
15
abstract class Base_Admin_Menu {
16
	/**
17
	 * Holds class instances.
18
	 *
19
	 * @var array
20
	 */
21
	protected static $instances;
22
23
	/**
24
	 * Whether the current request is a REST API request.
25
	 *
26
	 * @var bool
27
	 */
28
	protected $is_api_request = false;
29
30
	/**
31
	 * Domain of the current site.
32
	 *
33
	 * @var string
34
	 */
35
	protected $domain;
36
37
	/**
38
	 * The CSS classes used to hide the submenu items in navigation.
39
	 *
40
	 * @var string
41
	 */
42
	const HIDE_CSS_CLASS = 'hide-if-js';
43
44
	/**
45
	 * Base_Admin_Menu constructor.
46
	 */
47
	protected function __construct() {
48
		$this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST || 0 === strpos( $_SERVER['REQUEST_URI'], '/?rest_route=%2Fwpcom%2Fv2%2Fadmin-menu' );
49
		$this->domain         = ( new Status() )->get_site_suffix();
50
51
		if ( ! $this->should_customize_nav() ) {
52
			return;
53
		}
54
55
		add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99998 );
56
		add_action( 'admin_menu', array( $this, 'hide_parent_of_hidden_submenus' ), 99999 );
57
58
		if ( ! $this->is_api_request ) {
59
			add_filter( 'admin_menu', array( $this, 'override_svg_icons' ), 99999 );
60
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 11 );
61
			add_action( 'admin_head', array( $this, 'set_site_icon_inline_styles' ) );
62
		}
63
	}
64
65
	/**
66
	 * Returns class instance.
67
	 *
68
	 * @return Admin_Menu
69
	 */
70
	public static function get_instance() {
71
		$class = get_called_class();
72
73
		if ( empty( static::$instances[ $class ] ) ) {
74
			static::$instances[ $class ] = new $class();
75
		}
76
77
		return static::$instances[ $class ];
78
	}
79
80
	/**
81
	 * Updates the menu data of the given menu slug.
82
	 *
83
	 * @param string $slug Slug of the menu to update.
84
	 * @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...
85
	 * @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...
86
	 * @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...
87
	 * @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...
88
	 * @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...
89
	 * @return bool Whether the menu has been updated.
90
	 */
91
	public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) {
92
		global $menu, $submenu;
93
94
		$menu_item     = null;
95
		$menu_position = null;
96
97
		foreach ( $menu as $i => $item ) {
98
			if ( $slug === $item[2] ) {
99
				$menu_item     = $item;
100
				$menu_position = $i;
101
				break;
102
			}
103
		}
104
105
		if ( ! $menu_item ) {
106
			return false;
107
		}
108
109
		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...
110
			$menu_item[0] = $title;
111
			$menu_item[3] = esc_attr( $title );
112
		}
113
114
		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...
115
			$menu_item[1] = $cap;
116
		}
117
118
		// Change parent slug only if there are no submenus (the slug of the 1st submenu will be used if there are submenus).
119
		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...
120
			$this->hide_submenu_page( $slug, $slug );
121
122
			if ( ! isset( $submenu[ $slug ] ) || ! $this->has_visible_items( $submenu[ $slug ] ) ) {
123
				$menu_item[2] = $url;
124
			}
125
		}
126
127
		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...
128
			$menu_item[4] = 'menu-top';
129
			$menu_item[6] = $icon;
130
		}
131
132
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
133
		unset( $menu[ $menu_position ] );
134
		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...
135
			$menu_position = $position;
136
		}
137
		$this->set_menu_item( $menu_item, $menu_position );
138
139
		// Only add submenu when there are other submenu items.
140
		if ( $url && isset( $submenu[ $slug ] ) && $this->has_visible_items( $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...
141
			add_submenu_page( $slug, $menu_item[3], $menu_item[0], $menu_item[1], $url, null, 0 );
142
		}
143
144
		return true;
145
	}
146
147
	/**
148
	 * Updates the submenus of the given menu slug.
149
	 *
150
	 * It hides the menu by adding the `hide-if-js` css class and duplicates the submenu with the new slug.
151
	 *
152
	 * @param string $slug Menu slug.
153
	 * @param array  $submenus_to_update Array of new submenu slugs.
154
	 */
155
	public function update_submenus( $slug, $submenus_to_update ) {
156
		global $submenu;
157
158
		if ( ! isset( $submenu[ $slug ] ) ) {
159
			return;
160
		}
161
162
		// This is needed for cases when the submenus to update have the same new slug.
163
		$submenus_to_update = array_filter(
164
			$submenus_to_update,
165
			static function ( $item, $old_slug ) {
166
				return $item !== $old_slug;
167
			},
168
			ARRAY_FILTER_USE_BOTH
169
		);
170
171
		/**
172
		 * Iterate over all submenu items and add the hide the submenus with CSS classes.
173
		 * This is done separately of the second foreach because the position of the submenu might change.
174
		 */
175
		foreach ( $submenu[ $slug ] as $index => $item ) {
176
			if ( ! array_key_exists( $item[2], $submenus_to_update ) ) {
177
				continue;
178
			}
179
180
			$this->hide_submenu_element( $index, $slug, $item );
181
		}
182
183
		$submenu_items = array_values( $submenu[ $slug ] );
184
185
		/**
186
		 * Iterate again over the submenu array. We need a copy of the array because add_submenu_page will add new elements
187
		 * to submenu array that might cause an infinite loop.
188
		 */
189
		foreach ( $submenu_items as $i => $submenu_item ) {
190
			if ( ! array_key_exists( $submenu_item[2], $submenus_to_update ) ) {
191
				continue;
192
			}
193
194
			add_submenu_page(
195
				$slug,
196
				isset( $submenu_item[3] ) ? $submenu_item[3] : '',
197
				isset( $submenu_item[0] ) ? $submenu_item[0] : '',
198
				isset( $submenu_item[1] ) ? $submenu_item[1] : 'read',
199
				$submenus_to_update[ $submenu_item[2] ],
200
				'',
201
				$i
202
			);
203
		}
204
	}
205
206
	/**
207
	 * Adds a menu separator.
208
	 *
209
	 * @param int    $position The position in the menu order this item should appear.
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...
210
	 * @param string $cap Optional. The capability required for this menu to be displayed to the user.
211
	 *                         Default: 'read'.
212
	 */
213
	public function add_admin_menu_separator( $position = null, $cap = 'read' ) {
214
		$menu_item = array(
215
			'',                                  // Menu title (ignored).
216
			$cap,                                // Required capability.
217
			wp_unique_id( 'separator-custom-' ), // URL or file (ignored, but must be unique).
218
			'',                                  // Page title (ignored).
219
			'wp-menu-separator',                 // CSS class. Identifies this item as a separator.
220
		);
221
222
		$this->set_menu_item( $menu_item, $position );
223
	}
224
225
	/**
226
	 * Enqueues scripts and styles.
227
	 */
228
	public function enqueue_scripts() {
229
		$is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
230
231
		if ( $this->is_rtl() ) {
232
			if ( $is_wpcom ) {
233
				$css_path = 'rtl/admin-menu-rtl.css';
234
			} else {
235
				$css_path = 'admin-menu-rtl.css';
236
			}
237
		} else {
238
			$css_path = 'admin-menu.css';
239
		}
240
241
		wp_enqueue_style(
242
			'jetpack-admin-menu',
243
			plugins_url( $css_path, __FILE__ ),
244
			array(),
245
			JETPACK__VERSION
246
		);
247
248
		wp_style_add_data( 'jetpack-admin-menu', 'rtl', $this->is_rtl() );
249
		$this->configure_colors_for_rtl_stylesheets();
250
251
		wp_enqueue_script(
252
			'jetpack-admin-menu',
253
			plugins_url( 'admin-menu.js', __FILE__ ),
254
			array(),
255
			JETPACK__VERSION,
256
			true
257
		);
258
	}
259
260
	/**
261
	 * Mark the core colors stylesheets as RTL depending on the value from the environment.
262
	 * This fixes a core issue where the extra RTL data is not added to the colors stylesheet.
263
	 * https://core.trac.wordpress.org/ticket/53090
264
	 */
265
	public function configure_colors_for_rtl_stylesheets() {
266
		wp_style_add_data( 'colors', 'rtl', $this->is_rtl() );
267
	}
268
269
	/**
270
	 * Injects inline-styles for site icon for when third-party plugins remove enqueued stylesheets.
271
	 * Unable to use wp_add_inline_style as plugins remove styles from all non-standard handles
272
	 */
273
	public function set_site_icon_inline_styles() {
274
		echo '<style>
275
			#adminmenu .toplevel_page_site-card .wp-menu-image,
276
			#adminmenu .toplevel_page_site-card .wp-menu-image img {
277
				height: 32px;
278
				width: 32px;
279
			}
280
		</style>';
281
	}
282
283
	/**
284
	 * Hide the submenu page based on slug and return the item that was hidden.
285
	 *
286
	 * Instead of actually removing the submenu item, a safer approach is to hide it and filter it in the API response.
287
	 * In this manner we'll avoid breaking third-party plugins depending on items that no longer exist.
288
	 *
289
	 * A false|array value is returned to be consistent with remove_submenu_page() function
290
	 *
291
	 * @param string $menu_slug The parent menu slug.
292
	 * @param string $submenu_slug The submenu slug that should be hidden.
293
	 * @return false|array
294
	 */
295
	public function hide_submenu_page( $menu_slug, $submenu_slug ) {
296
		global $submenu;
297
298
		if ( ! isset( $submenu[ $menu_slug ] ) ) {
299
			return false;
300
		}
301
302
		foreach ( $submenu[ $menu_slug ] as $i => $item ) {
303
			if ( $submenu_slug !== $item[2] ) {
304
				continue;
305
			}
306
307
			$this->hide_submenu_element( $i, $menu_slug, $item );
308
309
			return $item;
310
		}
311
312
		return false;
313
	}
314
315
	/**
316
	 * Apply the hide-if-js CSS class to a submenu item.
317
	 *
318
	 * @param int    $index The position of a submenu item in the submenu array.
319
	 * @param string $parent_slug The parent slug.
320
	 * @param array  $item The submenu item.
321
	 */
322
	public function hide_submenu_element( $index, $parent_slug, $item ) {
323
		global $submenu;
324
325
		$css_classes = empty( $item[4] ) ? self::HIDE_CSS_CLASS : $item[4] . ' ' . self::HIDE_CSS_CLASS;
326
327
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
328
		$submenu [ $parent_slug ][ $index ][4] = $css_classes;
329
	}
330
331
	/**
332
	 * Check if the menu has submenu items visible
333
	 *
334
	 * @param array $submenu_items The submenu items.
335
	 * @return bool
336
	 */
337
	public function has_visible_items( $submenu_items ) {
338
		$visible_items = array_filter(
339
			$submenu_items,
340
			array( $this, 'is_item_visible' )
341
		);
342
343
		return array() !== $visible_items;
344
	}
345
346
	/**
347
	 * Return the number of existing submenu items under the supplied parent slug.
348
	 *
349
	 * @param string $parent_slug The slug of the parent menu.
350
	 * @return int The number of submenu items under $parent_slug.
351
	 */
352
	public function get_submenu_item_count( $parent_slug ) {
353
		global $submenu;
354
355
		if ( empty( $parent_slug ) || empty( $submenu[ $parent_slug ] ) || ! is_array( $submenu[ $parent_slug ] ) ) {
356
			return 0;
357
		}
358
359
		return count( $submenu[ $parent_slug ] );
360
	}
361
362
	/**
363
	 * Adds the given menu item in the specified position.
364
	 *
365
	 * @param array $item The menu item to add.
366
	 * @param int   $position The position in the menu order this item should appear.
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...
367
	 */
368
	public function set_menu_item( $item, $position = null ) {
369
		global $menu;
370
371
		// Handle position (avoids overwriting menu items already populated in the given position).
372
		// Inspired by https://core.trac.wordpress.org/browser/trunk/src/wp-admin/menu.php?rev=49837#L160.
373
		if ( null === $position ) {
374
			$menu[] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
375
		} elseif ( isset( $menu[ "$position" ] ) ) {
376
			$position            = $position + substr( base_convert( md5( $item[2] . $item[0] ), 16, 10 ), -5 ) * 0.00001;
377
			$menu[ "$position" ] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
378
		} else {
379
			$menu[ $position ] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
380
		}
381
	}
382
383
	/**
384
	 * Determines whether the current locale is right-to-left (RTL).
385
	 */
386
	public function is_rtl() {
387
		return is_rtl();
388
	}
389
390
	/**
391
	 * Checks for any SVG icons in the menu, and overrides things so that
392
	 * we can display the icon in the correct colour for the theme.
393
	 */
394
	public function override_svg_icons() {
395
		global $menu;
396
397
		// Only do this if we're not in an API request, as we override the $menu global.
398
		if ( $this->is_api_request ) {
399
			return;
400
		}
401
402
		$svg_items = array();
403
		foreach ( $menu as $idx => $menu_item ) {
404
			// Menu items that don't have icons, for example separators, have less than 7
405
			// elements, partly because the 7th is the icon. So, if we have less than 7,
406
			// let's skip it.
407
			if ( count( $menu_item ) < 7 ) {
408
				continue;
409
			}
410
411
			// If the hookname contain a URL than sanitize it by replacing invalid characters.
412
			if ( false !== strpos( $menu_item[5], '://' ) ) {
413
				$menu_item[5] = preg_replace( '![:/.]+!', '_', $menu_item[5] );
414
			}
415
416
			if ( 0 === strpos( $menu_item[6], 'data:image/svg+xml' ) && 'site-card' !== $menu_item[3] ) {
417
				$svg_items[]   = array(
418
					'icon' => $menu_item[6],
419
					'id'   => $menu_item[5],
420
				);
421
				$menu_item[4] .= ' menu-svg-icon';
422
				$menu_item[6]  = 'none';
423
			}
424
			// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
425
			$menu[ $idx ] = $menu_item;
426
		}
427
		if ( count( $svg_items ) > 0 ) {
428
			$styles = '.menu-svg-icon .wp-menu-image { background-repeat: no-repeat; background-position: center center } ';
429
			foreach ( $svg_items as $svg_item ) {
430
				$styles .= sprintf( '#%s .wp-menu-image { background-image: url( "%s" ) }', $svg_item['id'], $svg_item['icon'] );
431
			}
432
			$styles .= '@supports ( mask-image: none ) or ( -webkit-mask-image: none ) { ';
433
			$styles .= '.menu-svg-icon .wp-menu-image { background-image: none; } ';
434
			$styles .= '.menu-svg-icon .wp-menu-image::before { background-color: currentColor; ';
435
			$styles .= 'mask-size: contain; mask-position: center center; mask-repeat: no-repeat; ';
436
			$styles .= '-webkit-mask-size: contain; -webkit-mask-position: center center; -webkit-mask-repeat: no-repeat; content:"" } ';
437
			foreach ( $svg_items as $svg_item ) {
438
				$styles .= sprintf(
439
					'#%s .wp-menu-image { background-image: none; } #%s .wp-menu-image::before{ mask-image: url( "%s" ); -webkit-mask-image: url( "%s" ) }',
440
					$svg_item['id'],
441
					$svg_item['id'],
442
					$svg_item['icon'],
443
					$svg_item['icon']
444
				);
445
			}
446
			$styles .= '}';
447
448
			wp_register_style( 'svg-menu-overrides', false, array(), '20210331' );
449
			wp_enqueue_style( 'svg-menu-overrides' );
450
			wp_add_inline_style( 'svg-menu-overrides', $styles );
451
		}
452
	}
453
454
	/**
455
	 * Hide menus that are unauthorized and don't have visible submenus and cases when the menu has the same slug
456
	 * as the first submenu item.
457
	 *
458
	 * This must be done at the end of menu and submenu manipulation in order to avoid performing this check each time
459
	 * the submenus are altered.
460
	 */
461
	public function hide_parent_of_hidden_submenus() {
462
		global $menu, $submenu;
463
464
		$this->sort_hidden_submenus();
465
466
		foreach ( $menu as $menu_index => $menu_item ) {
467
			$has_submenus = isset( $submenu[ $menu_item[2] ] );
468
469
			// Skip if the menu doesn't have submenus.
470
			if ( ! $has_submenus ) {
471
				continue;
472
			}
473
474
			// If the first submenu item is hidden then we should also hide the parent.
475
			// Since the submenus are ordered by self::HIDE_CSS_CLASS (hidden submenus should be at the end of the array),
476
			// we can say that if the first submenu is hidden then we should also hide the menu.
477
			$first_submenu_item       = array_values( $submenu[ $menu_item[2] ] )[0];
478
			$is_first_submenu_visible = $this->is_item_visible( $first_submenu_item );
479
480
			// if the user does not have access to the menu and the first submenu is hidden, then hide the menu.
481
			if ( ! current_user_can( $menu_item[1] ) && ! $is_first_submenu_visible ) {
482
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
483
				$menu[ $menu_index ][4] = self::HIDE_CSS_CLASS;
484
			}
485
486
			// if the menu has the same slug as the first submenu then hide the submenu.
487
			if ( $menu_item[2] === $first_submenu_item[2] && ! $is_first_submenu_visible ) {
488
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
489
				$menu[ $menu_index ][4] = self::HIDE_CSS_CLASS;
490
			}
491
		}
492
	}
493
494
	/**
495
	 * Sort the hidden submenus by moving them at the end of the array in order to avoid WP using them as default URLs.
496
	 *
497
	 * This operation has to be done at the end of submenu manipulation in order to guarantee that the hidden submenus
498
	 * are at the end of the array.
499
	 */
500
	public function sort_hidden_submenus() {
501
		global $submenu;
502
503
		foreach ( $submenu as $menu_slug => $submenu_items ) {
504
			foreach ( $submenu_items as $submenu_index => $submenu_item ) {
505
				if ( $this->is_item_visible( $submenu_item ) ) {
506
					continue;
507
				}
508
509
				unset( $submenu[ $menu_slug ][ $submenu_index ] );
510
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
511
				$submenu[ $menu_slug ][] = $submenu_item;
512
			}
513
		}
514
	}
515
516
	/**
517
	 * Check if the given item is visible or not in the admin menu.
518
	 *
519
	 * @param array $item A menu or submenu array.
520
	 */
521
	public function is_item_visible( $item ) {
522
		return ! isset( $item[4] ) || false === strpos( $item[4], self::HIDE_CSS_CLASS );
523
	}
524
525
	/**
526
	 * Whether to use wp-admin pages rather than Calypso.
527
	 *
528
	 * Options:
529
	 * false - Calypso (Default).
530
	 * true  - wp-admin.
531
	 *
532
	 * @return bool
533
	 */
534
	abstract public function should_link_to_wp_admin();
535
536
	/**
537
	 * Create the desired menu output.
538
	 */
539
	abstract public function reregister_menu_items();
540
541
	/**
542
	 * Checks whether the navigation customizations should be performed.
543
	 *
544
	 * @return bool
545
	 */
546
	public function should_customize_nav() {
547
		return true;
548
	}
549
}
550