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