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

Base_Admin_Menu::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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