PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 12.1.3
Jetpack – WP Security, Backup, Speed, & Growth v12.1.3
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / masterbar / admin-menu / class-base-admin-menu.php
jetpack / modules / masterbar / admin-menu Last commit date
admin-menu-rtl.css 3 years ago admin-menu-rtl.min.css 3 years ago admin-menu.css 3 years ago admin-menu.js 3 years ago admin-menu.min.css 3 years ago class-admin-menu.php 3 years ago class-atomic-admin-menu.php 3 years ago class-base-admin-menu.php 3 years ago class-dashboard-switcher-tracking.php 3 years ago class-domain-only-admin-menu.php 3 years ago class-jetpack-admin-menu.php 3 years ago class-p2-admin-menu.php 4 years ago class-wpcom-admin-menu.php 3 years ago class-wpcom-email-subscription-checker.php 3 years ago dashicon-set.php 4 years ago globe-icon.svg 3 years ago load.php 3 years ago menu-mappings.php 3 years ago
class-base-admin-menu.php
765 lines
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 * Identifier denoting that the default WordPress.com view should be used for a certain screen.
46 *
47 * @var string
48 */
49 const DEFAULT_VIEW = 'default';
50
51 /**
52 * Identifier denoting that the classic WP Admin view should be used for a certain screen.
53 *
54 * @var string
55 */
56 const CLASSIC_VIEW = 'classic';
57
58 /**
59 * Identifier denoting no preferred view has been set for a certain screen.
60 *
61 * @var string
62 */
63 const UNKNOWN_VIEW = 'unknown';
64
65 /**
66 * Base_Admin_Menu constructor.
67 */
68 protected function __construct() {
69 $this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST || isset( $_SERVER['REQUEST_URI'] ) && 0 === strpos( filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/?rest_route=%2Fwpcom%2Fv2%2Fadmin-menu' );
70 $this->domain = ( new Status() )->get_site_suffix();
71
72 add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99998 );
73 add_action( 'admin_menu', array( $this, 'hide_parent_of_hidden_submenus' ), 99999 );
74
75 if ( ! $this->is_api_request ) {
76 add_filter( 'admin_menu', array( $this, 'override_svg_icons' ), 99999 );
77 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 11 );
78 add_action( 'admin_head', array( $this, 'set_site_icon_inline_styles' ) );
79 add_action( 'in_admin_header', array( $this, 'add_dashboard_switcher' ) );
80 add_action( 'admin_footer', array( $this, 'dashboard_switcher_scripts' ) );
81 add_action( 'admin_menu', array( $this, 'handle_preferred_view' ), 99997 );
82 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
83 add_action( 'adminmenu', array( $this, 'inject_core_mobile_toggle' ) );
84 }
85 }
86
87 /**
88 * Returns class instance.
89 *
90 * @return Admin_Menu
91 */
92 public static function get_instance() {
93 $class = static::class;
94
95 if ( empty( static::$instances[ $class ] ) ) {
96 static::$instances[ $class ] = new $class();
97 }
98
99 return static::$instances[ $class ];
100 }
101
102 /**
103 * Updates the menu data of the given menu slug.
104 *
105 * @param string $slug Slug of the menu to update.
106 * @param string $url New menu URL.
107 * @param string $title New menu title.
108 * @param string $cap New menu capability.
109 * @param string $icon New menu icon.
110 * @param int $position New menu position.
111 * @return bool Whether the menu has been updated.
112 */
113 public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) {
114 global $menu, $submenu;
115
116 $menu_item = null;
117 $menu_position = null;
118
119 foreach ( $menu as $i => $item ) {
120 if ( $slug === $item[2] ) {
121 $menu_item = $item;
122 $menu_position = $i;
123 break;
124 }
125 }
126
127 if ( ! $menu_item ) {
128 return false;
129 }
130
131 if ( $title ) {
132 $menu_item[0] = $title;
133 $menu_item[3] = esc_attr( $title );
134 }
135
136 if ( $cap ) {
137 $menu_item[1] = $cap;
138 }
139
140 // Change parent slug only if there are no submenus (the slug of the 1st submenu will be used if there are submenus).
141 if ( $url ) {
142 $this->hide_submenu_page( $slug, $slug );
143
144 if ( ! isset( $submenu[ $slug ] ) || ! $this->has_visible_items( $submenu[ $slug ] ) ) {
145 $menu_item[2] = $url;
146 }
147 }
148
149 if ( $icon ) {
150 $menu_item[4] = 'menu-top';
151 $menu_item[6] = $icon;
152 }
153
154 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
155 unset( $menu[ $menu_position ] );
156 if ( $position ) {
157 $menu_position = $position;
158 }
159 $this->set_menu_item( $menu_item, $menu_position );
160
161 // Only add submenu when there are other submenu items.
162 if ( $url && isset( $submenu[ $slug ] ) && $this->has_visible_items( $submenu[ $slug ] ) ) {
163 add_submenu_page( $slug, $menu_item[3], $menu_item[0], $menu_item[1], $url, null, 0 );
164 }
165
166 return true;
167 }
168
169 /**
170 * Updates the submenus of the given menu slug.
171 *
172 * It hides the menu by adding the `hide-if-js` css class and duplicates the submenu with the new slug.
173 *
174 * @param string $slug Menu slug.
175 * @param array $submenus_to_update Array of new submenu slugs.
176 */
177 public function update_submenus( $slug, $submenus_to_update ) {
178 global $submenu;
179
180 if ( ! isset( $submenu[ $slug ] ) ) {
181 return;
182 }
183
184 // This is needed for cases when the submenus to update have the same new slug.
185 $submenus_to_update = array_filter(
186 $submenus_to_update,
187 static function ( $item, $old_slug ) {
188 return $item !== $old_slug;
189 },
190 ARRAY_FILTER_USE_BOTH
191 );
192
193 /**
194 * Iterate over all submenu items and add the hide the submenus with CSS classes.
195 * This is done separately of the second foreach because the position of the submenu might change.
196 */
197 foreach ( $submenu[ $slug ] as $index => $item ) {
198 if ( ! array_key_exists( $item[2], $submenus_to_update ) ) {
199 continue;
200 }
201
202 $this->hide_submenu_element( $index, $slug, $item );
203 }
204
205 $submenu_items = array_values( $submenu[ $slug ] );
206
207 /**
208 * Iterate again over the submenu array. We need a copy of the array because add_submenu_page will add new elements
209 * to submenu array that might cause an infinite loop.
210 */
211 foreach ( $submenu_items as $i => $submenu_item ) {
212 if ( ! array_key_exists( $submenu_item[2], $submenus_to_update ) ) {
213 continue;
214 }
215
216 add_submenu_page(
217 $slug,
218 isset( $submenu_item[3] ) ? $submenu_item[3] : '',
219 isset( $submenu_item[0] ) ? $submenu_item[0] : '',
220 isset( $submenu_item[1] ) ? $submenu_item[1] : 'read',
221 $submenus_to_update[ $submenu_item[2] ],
222 '',
223 0 === $i ? 0 : $i + 1
224 );
225 }
226 }
227
228 /**
229 * Adds a menu separator.
230 *
231 * @param int $position The position in the menu order this item should appear.
232 * @param string $cap Optional. The capability required for this menu to be displayed to the user.
233 * Default: 'read'.
234 */
235 public function add_admin_menu_separator( $position = null, $cap = 'read' ) {
236 $menu_item = array(
237 '', // Menu title (ignored).
238 $cap, // Required capability.
239 wp_unique_id( 'separator-custom-' ), // URL or file (ignored, but must be unique).
240 '', // Page title (ignored).
241 'wp-menu-separator', // CSS class. Identifies this item as a separator.
242 );
243
244 $this->set_menu_item( $menu_item, $position );
245 }
246
247 /**
248 * Enqueues scripts and styles.
249 */
250 public function enqueue_scripts() {
251 if ( $this->is_rtl() ) {
252 $css_path = 'admin-menu-rtl.css';
253 } else {
254 $css_path = 'admin-menu.css';
255 }
256
257 wp_enqueue_style(
258 'jetpack-admin-menu',
259 plugins_url( $css_path, __FILE__ ),
260 array(),
261 JETPACK__VERSION
262 );
263
264 wp_style_add_data( 'jetpack-admin-menu', 'rtl', $this->is_rtl() );
265 $this->configure_colors_for_rtl_stylesheets();
266
267 wp_enqueue_script(
268 'jetpack-admin-menu',
269 plugins_url( 'admin-menu.js', __FILE__ ),
270 array(),
271 JETPACK__VERSION,
272 true
273 );
274
275 wp_localize_script(
276 'jetpack-admin-menu',
277 'jetpackAdminMenu',
278 array(
279 'upsellNudgeJitm' => wp_create_nonce( 'upsell_nudge_jitm' ),
280 'jitmDismissNonce' => wp_create_nonce( 'jitm_dismiss' ),
281 )
282 );
283 }
284
285 /**
286 * Mark the core colors stylesheets as RTL depending on the value from the environment.
287 * This fixes a core issue where the extra RTL data is not added to the colors stylesheet.
288 * https://core.trac.wordpress.org/ticket/53090
289 */
290 public function configure_colors_for_rtl_stylesheets() {
291 wp_style_add_data( 'colors', 'rtl', $this->is_rtl() );
292 }
293
294 /**
295 * Injects inline-styles for site icon for when third-party plugins remove enqueued stylesheets.
296 * Unable to use wp_add_inline_style as plugins remove styles from all non-standard handles
297 */
298 public function set_site_icon_inline_styles() {
299 echo '<style>
300 #adminmenu .toplevel_page_site-card .wp-menu-image,
301 #adminmenu .toplevel_page_site-card .wp-menu-image img {
302 height: 32px;
303 width: 32px;
304 }
305 </style>';
306 }
307
308 /**
309 * Hide the submenu page based on slug and return the item that was hidden.
310 *
311 * Instead of actually removing the submenu item, a safer approach is to hide it and filter it in the API response.
312 * In this manner we'll avoid breaking third-party plugins depending on items that no longer exist.
313 *
314 * A false|array value is returned to be consistent with remove_submenu_page() function
315 *
316 * @param string $menu_slug The parent menu slug.
317 * @param string $submenu_slug The submenu slug that should be hidden.
318 * @return false|array
319 */
320 public function hide_submenu_page( $menu_slug, $submenu_slug ) {
321 global $submenu;
322
323 if ( ! isset( $submenu[ $menu_slug ] ) ) {
324 return false;
325 }
326
327 foreach ( $submenu[ $menu_slug ] as $i => $item ) {
328 if ( $submenu_slug !== $item[2] ) {
329 continue;
330 }
331
332 $this->hide_submenu_element( $i, $menu_slug, $item );
333
334 return $item;
335 }
336
337 return false;
338 }
339
340 /**
341 * Apply the hide-if-js CSS class to a submenu item.
342 *
343 * @param int $index The position of a submenu item in the submenu array.
344 * @param string $parent_slug The parent slug.
345 * @param array $item The submenu item.
346 */
347 public function hide_submenu_element( $index, $parent_slug, $item ) {
348 global $submenu;
349
350 $css_classes = empty( $item[4] ) ? self::HIDE_CSS_CLASS : $item[4] . ' ' . self::HIDE_CSS_CLASS;
351
352 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
353 $submenu [ $parent_slug ][ $index ][4] = $css_classes;
354 }
355
356 /**
357 * Check if the menu has submenu items visible
358 *
359 * @param array $submenu_items The submenu items.
360 * @return bool
361 */
362 public function has_visible_items( $submenu_items ) {
363 $visible_items = array_filter(
364 $submenu_items,
365 array( $this, 'is_item_visible' )
366 );
367
368 return array() !== $visible_items;
369 }
370
371 /**
372 * Return the number of existing submenu items under the supplied parent slug.
373 *
374 * @param string $parent_slug The slug of the parent menu.
375 * @return int The number of submenu items under $parent_slug.
376 */
377 public function get_submenu_item_count( $parent_slug ) {
378 global $submenu;
379
380 if ( empty( $parent_slug ) || empty( $submenu[ $parent_slug ] ) || ! is_array( $submenu[ $parent_slug ] ) ) {
381 return 0;
382 }
383
384 return count( $submenu[ $parent_slug ] );
385 }
386
387 /**
388 * Adds the given menu item in the specified position.
389 *
390 * @param array $item The menu item to add.
391 * @param int $position The position in the menu order this item should appear.
392 */
393 public function set_menu_item( $item, $position = null ) {
394 global $menu;
395
396 // Handle position (avoids overwriting menu items already populated in the given position).
397 // Inspired by https://core.trac.wordpress.org/browser/trunk/src/wp-admin/menu.php?rev=49837#L160.
398 if ( null === $position ) {
399 $menu[] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
400 } elseif ( isset( $menu[ "$position" ] ) ) {
401 $position = $position + substr( base_convert( md5( $item[2] . $item[0] ), 16, 10 ), -5 ) * 0.00001;
402 $menu[ "$position" ] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
403 } else {
404 $menu[ $position ] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
405 }
406 }
407
408 /**
409 * Determines whether the current locale is right-to-left (RTL).
410 */
411 public function is_rtl() {
412 return is_rtl();
413 }
414
415 /**
416 * Checks for any SVG icons in the menu, and overrides things so that
417 * we can display the icon in the correct colour for the theme.
418 */
419 public function override_svg_icons() {
420 global $menu;
421
422 $svg_items = array();
423 foreach ( $menu as $idx => $menu_item ) {
424 // Menu items that don't have icons, for example separators, have less than 7
425 // elements, partly because the 7th is the icon. So, if we have less than 7,
426 // let's skip it.
427 if ( count( $menu_item ) < 7 ) {
428 continue;
429 }
430
431 // If the hookname contain a URL than sanitize it by replacing invalid characters.
432 if ( false !== strpos( $menu_item[5], '://' ) ) {
433 $menu_item[5] = preg_replace( '![:/.]+!', '_', $menu_item[5] );
434 }
435
436 if ( 0 === strpos( $menu_item[6], 'data:image/svg+xml' ) && 'site-card' !== $menu_item[3] ) {
437 $svg_items[] = array(
438 'icon' => $menu_item[6],
439 'id' => $menu_item[5],
440 );
441 $menu_item[4] .= ' menu-svg-icon';
442 $menu_item[6] = 'none';
443 }
444 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
445 $menu[ $idx ] = $menu_item;
446 }
447 if ( count( $svg_items ) > 0 ) {
448 $styles = '.menu-svg-icon .wp-menu-image { background-repeat: no-repeat; background-position: center center } ';
449 foreach ( $svg_items as $svg_item ) {
450 $styles .= sprintf( '#%s .wp-menu-image { background-image: url( "%s" ) }', $svg_item['id'], $svg_item['icon'] );
451 }
452 $styles .= '@supports ( mask-image: none ) or ( -webkit-mask-image: none ) { ';
453 $styles .= '.menu-svg-icon .wp-menu-image { background-image: none; } ';
454 $styles .= '.menu-svg-icon .wp-menu-image::before { background-color: currentColor; ';
455 $styles .= 'mask-size: contain; mask-position: center center; mask-repeat: no-repeat; ';
456 $styles .= '-webkit-mask-size: contain; -webkit-mask-position: center center; -webkit-mask-repeat: no-repeat; content:"" } ';
457 foreach ( $svg_items as $svg_item ) {
458 $styles .= sprintf(
459 '#%s .wp-menu-image { background-image: none; } #%s .wp-menu-image::before{ mask-image: url( "%s" ); -webkit-mask-image: url( "%s" ) }',
460 $svg_item['id'],
461 $svg_item['id'],
462 $svg_item['icon'],
463 $svg_item['icon']
464 );
465 }
466 $styles .= '}';
467
468 wp_register_style( 'svg-menu-overrides', false, array(), '20210331' );
469 wp_enqueue_style( 'svg-menu-overrides' );
470 wp_add_inline_style( 'svg-menu-overrides', $styles );
471 }
472 }
473
474 /**
475 * Hide menus that are unauthorized and don't have visible submenus and cases when the menu has the same slug
476 * as the first submenu item.
477 *
478 * This must be done at the end of menu and submenu manipulation in order to avoid performing this check each time
479 * the submenus are altered.
480 */
481 public function hide_parent_of_hidden_submenus() {
482 global $menu, $submenu;
483
484 $this->sort_hidden_submenus();
485
486 foreach ( $menu as $menu_index => $menu_item ) {
487 $has_submenus = isset( $submenu[ $menu_item[2] ] );
488
489 // Skip if the menu doesn't have submenus.
490 if ( ! $has_submenus ) {
491 continue;
492 }
493
494 // If the first submenu item is hidden then we should also hide the parent.
495 // Since the submenus are ordered by self::HIDE_CSS_CLASS (hidden submenus should be at the end of the array),
496 // we can say that if the first submenu is hidden then we should also hide the menu.
497 $first_submenu_item = array_values( $submenu[ $menu_item[2] ] )[0];
498 $is_first_submenu_visible = $this->is_item_visible( $first_submenu_item );
499
500 // if the user does not have access to the menu and the first submenu is hidden, then hide the menu.
501 if ( ! current_user_can( $menu_item[1] ) && ! $is_first_submenu_visible ) {
502 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
503 $menu[ $menu_index ][4] = self::HIDE_CSS_CLASS;
504 }
505
506 // if the menu has the same slug as the first submenu then hide the submenu.
507 if ( $menu_item[2] === $first_submenu_item[2] && ! $is_first_submenu_visible ) {
508 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
509 $menu[ $menu_index ][4] = self::HIDE_CSS_CLASS;
510 }
511 }
512 }
513
514 /**
515 * Sort the hidden submenus by moving them at the end of the array in order to avoid WP using them as default URLs.
516 *
517 * This operation has to be done at the end of submenu manipulation in order to guarantee that the hidden submenus
518 * are at the end of the array.
519 */
520 public function sort_hidden_submenus() {
521 global $submenu;
522
523 foreach ( $submenu as $menu_slug => $submenu_items ) {
524 foreach ( $submenu_items as $submenu_index => $submenu_item ) {
525 if ( $this->is_item_visible( $submenu_item ) ) {
526 continue;
527 }
528
529 unset( $submenu[ $menu_slug ][ $submenu_index ] );
530 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
531 $submenu[ $menu_slug ][] = $submenu_item;
532 }
533 }
534 }
535
536 /**
537 * Check if the given item is visible or not in the admin menu.
538 *
539 * @param array $item A menu or submenu array.
540 */
541 public function is_item_visible( $item ) {
542 return ! isset( $item[4] ) || false === strpos( $item[4], self::HIDE_CSS_CLASS );
543 }
544
545 /**
546 * Adds a dashboard switcher to the list of screen meta links of the current page.
547 */
548 public function add_dashboard_switcher() {
549 $menu_mappings = require __DIR__ . '/menu-mappings.php';
550 $screen = $this->get_current_screen();
551
552 // Let's show the switcher only in screens that we have a Calypso mapping to switch to.
553 if ( empty( $menu_mappings[ $screen ] ) ) {
554 return;
555 }
556 ?>
557 <div id="view-link-wrap" class="hide-if-no-js screen-meta-toggle">
558 <button type="button" id="view-link" class="button show-settings" aria-expanded="false"><?php echo esc_html_x( 'View', 'View options to switch between', 'jetpack' ); ?></button>
559 </div>
560 <div id="view-wrap" class="screen-options-tab__wrapper hide-if-no-js hidden" tabindex="-1">
561 <div class="screen-options-tab__dropdown" data-testid="screen-options-dropdown">
562 <div class="screen-switcher">
563 <a class="screen-switcher__button" href="<?php echo esc_url( add_query_arg( 'preferred-view', 'default' ) ); ?>" data-view="default">
564 <strong><?php esc_html_e( 'Default view', 'jetpack' ); ?></strong>
565 <?php esc_html_e( 'Our WordPress.com redesign for a better experience.', 'jetpack' ); ?>
566 </a>
567 <button class="screen-switcher__button" data-view="classic">
568 <strong><?php esc_html_e( 'Classic view', 'jetpack' ); ?></strong>
569 <?php esc_html_e( 'The classic WP-Admin WordPress interface.', 'jetpack' ); ?>
570 </button>
571 </div>
572 </div>
573 </div>
574 <?php
575 }
576
577 /**
578 * Adds a script to append the dashboard switcher to screen meta
579 */
580 public function dashboard_switcher_scripts() {
581 wp_add_inline_script(
582 'common',
583 "(function( $ ) {
584 $( '#view-link-wrap' ).appendTo( '#screen-meta-links' );
585
586 var viewLink = $( '#view-link' );
587 var viewWrap = $( '#view-wrap' );
588
589 viewLink.on( 'click', function() {
590 viewWrap.toggle();
591 viewLink.toggleClass( 'screen-meta-active' );
592 } );
593
594 $( document ).on( 'mouseup', function( event ) {
595 if ( ! viewLink.is( event.target ) && ! viewWrap.is( event.target ) && viewWrap.has( event.target ).length === 0 ) {
596 viewWrap.hide();
597 viewLink.removeClass( 'screen-meta-active' );
598 }
599 });
600 })( jQuery );"
601 );
602 }
603
604 /**
605 * Sets the given view as preferred for the givens screen.
606 *
607 * @param string $screen Screen identifier.
608 * @param string $view Preferred view.
609 */
610 public function set_preferred_view( $screen, $view ) {
611 $preferred_views = $this->get_preferred_views();
612 $screen = str_replace( '?post_type=post', '', $screen );
613 $preferred_views[ $screen ] = $view;
614 update_user_option( get_current_user_id(), 'jetpack_admin_menu_preferred_views', $preferred_views );
615 }
616
617 /**
618 * Get the preferred views for all screens.
619 *
620 * @return array
621 */
622 public function get_preferred_views() {
623 $preferred_views = get_user_option( 'jetpack_admin_menu_preferred_views' );
624
625 if ( ! $preferred_views ) {
626 return array();
627 }
628
629 return $preferred_views;
630 }
631
632 /**
633 * Get the preferred view for the given screen.
634 *
635 * @param string $screen Screen identifier.
636 * @param bool $fallback_global_preference (Optional) Whether the global preference for all screens should be used
637 * as fallback if there is no specific preference for the given screen.
638 * Default: true.
639 * @return string
640 */
641 public function get_preferred_view( $screen, $fallback_global_preference = true ) {
642 $preferred_views = $this->get_preferred_views();
643
644 if ( ! isset( $preferred_views[ $screen ] ) ) {
645 if ( ! $fallback_global_preference ) {
646 return self::UNKNOWN_VIEW;
647 }
648 return $this->should_link_to_wp_admin() ? self::CLASSIC_VIEW : self::DEFAULT_VIEW;
649 }
650
651 return $preferred_views[ $screen ];
652 }
653
654 /**
655 * Gets the identifier of the current screen.
656 *
657 * @return string
658 */
659 public function get_current_screen() {
660 // phpcs:disable WordPress.Security.NonceVerification
661 global $pagenow;
662 $screen = isset( $_REQUEST['screen'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['screen'] ) ) : $pagenow;
663 if ( isset( $_GET['post_type'] ) ) {
664 $screen = add_query_arg( 'post_type', sanitize_text_field( wp_unslash( $_GET['post_type'] ) ), $screen );
665 }
666 if ( isset( $_GET['taxonomy'] ) ) {
667 $screen = add_query_arg( 'taxonomy', sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ), $screen );
668 }
669 if ( isset( $_GET['page'] ) ) {
670 $screen = add_query_arg( 'page', sanitize_text_field( wp_unslash( $_GET['page'] ) ), $screen );
671 }
672 return $screen;
673 // phpcs:enable WordPress.Security.NonceVerification
674 }
675
676 /**
677 * Stores the preferred view for the current screen.
678 */
679 public function handle_preferred_view() {
680 // phpcs:disable WordPress.Security.NonceVerification
681 if ( ! isset( $_GET['preferred-view'] ) ) {
682 return;
683 }
684
685 // phpcs:disable WordPress.Security.NonceVerification
686 $preferred_view = sanitize_key( $_GET['preferred-view'] );
687
688 if ( ! in_array( $preferred_view, array( self::DEFAULT_VIEW, self::CLASSIC_VIEW ), true ) ) {
689 return;
690 }
691
692 $current_screen = $this->get_current_screen();
693
694 $this->set_preferred_view( $current_screen, $preferred_view );
695
696 /**
697 * Dashboard Quick switcher action triggered when a user switches to a different view.
698 *
699 * @module masterbar
700 *
701 * @since 9.9.1
702 *
703 * @param string The current screen of the user.
704 * @param string The preferred view the user selected.
705 */
706 \do_action( 'jetpack_dashboard_switcher_changed_view', $current_screen, $preferred_view );
707
708 if ( self::DEFAULT_VIEW === $preferred_view ) {
709 // Redirect to default view if that's the newly preferred view.
710 $menu_mappings = require __DIR__ . '/menu-mappings.php';
711 if ( isset( $menu_mappings[ $current_screen ] ) ) {
712 // Using `wp_redirect` intentionally because we're redirecting to Calypso.
713 wp_redirect( $menu_mappings[ $current_screen ] . $this->domain ); // phpcs:ignore WordPress.Security.SafeRedirect
714 exit;
715 }
716 } elseif ( self::CLASSIC_VIEW === $preferred_view ) {
717 // Removes the `preferred-view` param from the URL to avoid issues with
718 // screens that don't expect this param to be present in the URL.
719 wp_safe_redirect( remove_query_arg( 'preferred-view' ) );
720 exit;
721 }
722 // phpcs:enable WordPress.Security.NonceVerification
723 }
724
725 /**
726 * Adds the necessary CSS class to the admin body class.
727 *
728 * @param string $admin_body_classes Contains all the admin body classes.
729 *
730 * @return string
731 */
732 public function admin_body_class( $admin_body_classes ) {
733 return " is-nav-unification $admin_body_classes ";
734 }
735
736 /**
737 * Whether to use wp-admin pages rather than Calypso.
738 *
739 * Options:
740 * false - Calypso (Default).
741 * true - wp-admin.
742 *
743 * @return bool
744 */
745 public function should_link_to_wp_admin() {
746 return get_user_option( 'jetpack_admin_menu_link_destination' );
747 }
748
749 /**
750 * Injects the core's mobile toggle for proper positioning of the submenus.
751 *
752 * @see https://core.trac.wordpress.org/ticket/32747
753 *
754 * @return void
755 */
756 public function inject_core_mobile_toggle() {
757 echo '<span id="wp-admin-bar-menu-toggle" style="display: none!important">';
758 }
759
760 /**
761 * Create the desired menu output.
762 */
763 abstract public function reregister_menu_items();
764 }
765