Completed
Push — add/testing-info ( be1095...03b7e9 )
by
unknown
09:20
created

Base_Admin_Menu::is_rtl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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