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