Нет описания

class-base-admin-menu.php 20KB

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