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