Нет описания

class-atomic-admin-menu.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * Atomic Admin Menu file.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. namespace Automattic\Jetpack\Dashboard_Customizations;
  8. use Automattic\Jetpack\Connection\Client;
  9. use Jetpack_Plan;
  10. require_once __DIR__ . '/class-admin-menu.php';
  11. /**
  12. * Class Atomic_Admin_Menu.
  13. */
  14. class Atomic_Admin_Menu extends Admin_Menu {
  15. /**
  16. * Atomic_Admin_Menu constructor.
  17. */
  18. protected function __construct() {
  19. parent::__construct();
  20. add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
  21. add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
  22. add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) );
  23. if ( ! $this->is_api_request ) {
  24. add_filter( 'submenu_file', array( $this, 'override_the_theme_installer' ), 10, 2 );
  25. }
  26. add_action(
  27. 'admin_menu',
  28. function () {
  29. remove_action( 'admin_menu', 'gutenberg_menu', 9 );
  30. },
  31. 0
  32. );
  33. }
  34. /**
  35. * Dequeues unnecessary scripts.
  36. */
  37. public function dequeue_scripts() {
  38. wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
  39. }
  40. /**
  41. * Determines whether the current locale is right-to-left (RTL).
  42. *
  43. * Performs the check against the current locale set on the WordPress.com's account settings.
  44. * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`.
  45. */
  46. public function is_rtl() {
  47. return get_user_option( 'jetpack_wpcom_is_rtl' );
  48. }
  49. /**
  50. * Create the desired menu output.
  51. */
  52. public function reregister_menu_items() {
  53. parent::reregister_menu_items();
  54. $this->add_my_home_menu();
  55. $this->add_inbox_menu();
  56. // Not needed outside of wp-admin.
  57. if ( ! $this->is_api_request ) {
  58. $this->add_browse_sites_link();
  59. $this->add_site_card_menu();
  60. $nudge = $this->get_upsell_nudge();
  61. if ( $nudge ) {
  62. parent::add_upsell_nudge( $nudge );
  63. }
  64. $this->add_new_site_link();
  65. }
  66. $this->add_beta_testing_menu();
  67. ksort( $GLOBALS['menu'] );
  68. }
  69. /**
  70. * Get the preferred view for the given screen.
  71. *
  72. * @param string $screen Screen identifier.
  73. * @param bool $fallback_global_preference (Optional) Whether the global preference for all screens should be used
  74. * as fallback if there is no specific preference for the given screen.
  75. * Default: true.
  76. * @return string
  77. */
  78. public function get_preferred_view( $screen, $fallback_global_preference = true ) {
  79. // Plugins and Export on Atomic sites are always managed on WP Admin.
  80. if ( in_array( $screen, array( 'plugins.php', 'export.php' ), true ) ) {
  81. return self::CLASSIC_VIEW;
  82. }
  83. /**
  84. * When Jetpack SSO is disabled, we need to force Calypso because it might create confusion to be redirected to WP-Admin.
  85. * Furthermore, because we don't display the quick switcher, users having an WP-Admin interface by default won't be able to go back to the Calyso version.
  86. */
  87. if ( ! \Jetpack::is_module_active( 'sso' ) ) {
  88. return self::DEFAULT_VIEW;
  89. }
  90. return parent::get_preferred_view( $screen, $fallback_global_preference );
  91. }
  92. /**
  93. * Adds the site switcher link if user has more than one site.
  94. */
  95. public function add_browse_sites_link() {
  96. $site_count = get_user_option( 'wpcom_site_count' );
  97. if ( ! $site_count || $site_count < 2 ) {
  98. return;
  99. }
  100. // Add the menu item.
  101. add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 );
  102. add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) );
  103. }
  104. /**
  105. * Adds a custom element class for Site Switcher menu item.
  106. *
  107. * @param array $menu Associative array of administration menu items.
  108. *
  109. * @return array
  110. */
  111. public function set_browse_sites_link_class( array $menu ) {
  112. foreach ( $menu as $key => $menu_item ) {
  113. if ( 'site-switcher' !== $menu_item[3] ) {
  114. continue;
  115. }
  116. $menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] );
  117. break;
  118. }
  119. return $menu;
  120. }
  121. /**
  122. * Adds a link to the menu to create a new site.
  123. */
  124. public function add_new_site_link() {
  125. $site_count = get_user_option( 'wpcom_site_count' );
  126. if ( $site_count && $site_count > 1 ) {
  127. return;
  128. }
  129. $this->add_admin_menu_separator();
  130. add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' );
  131. }
  132. /**
  133. * Adds site card component.
  134. */
  135. public function add_site_card_menu() {
  136. $default = 'data:image/svg+xml,' . rawurlencode( '<svg class="gridicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Globe</title><rect fill-opacity="0" x="0" width="24" height="24"/><g><path fill="#fff" d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm0 18l2-2 1-1v-2h-2v-1l-1-1H9v3l2 2v1.93c-3.94-.494-7-3.858-7-7.93l1 1h2v-2h2l3-3V6h-2L9 5v-.41C9.927 4.21 10.94 4 12 4s2.073.212 3 .59V6l-1 1v2l1 1 3.13-3.13c.752.897 1.304 1.964 1.606 3.13H18l-2 2v2l1 1h2l.286.286C18.03 18.06 15.24 20 12 20z"/></g></svg>' );
  137. $icon = get_site_icon_url( 32, $default );
  138. $blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain;
  139. $is_coming_soon = ( function_exists( 'site_is_coming_soon' ) && site_is_coming_soon() ) || (bool) get_option( 'wpcom_public_coming_soon' );
  140. $badge = '';
  141. if ( ( function_exists( 'site_is_private' ) && site_is_private() ) || $is_coming_soon ) {
  142. $badge .= sprintf(
  143. '<span class="site__badge site__badge-private">%s</span>',
  144. $is_coming_soon ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
  145. );
  146. }
  147. $site_card = '
  148. <div class="site__info">
  149. <div class="site__title">%1$s</div>
  150. <div class="site__domain">%2$s</div>
  151. %3$s
  152. </div>';
  153. $site_card = sprintf(
  154. $site_card,
  155. $blog_name,
  156. $this->domain,
  157. $badge
  158. );
  159. add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
  160. add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
  161. }
  162. /**
  163. * Adds a custom element class and id for Site Card's menu item.
  164. *
  165. * @param array $menu Associative array of administration menu items.
  166. *
  167. * @return array
  168. */
  169. public function set_site_card_menu_class( array $menu ) {
  170. foreach ( $menu as $key => $menu_item ) {
  171. if ( 'site-card' !== $menu_item[3] ) {
  172. continue;
  173. }
  174. $classes = ' toplevel_page_site-card';
  175. // webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon.
  176. if ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) {
  177. $classes .= ' has-site-icon';
  178. }
  179. $menu[ $key ][4] = $menu_item[4] . $classes;
  180. $menu[ $key ][5] = 'toplevel_page_site_card';
  181. break;
  182. }
  183. return $menu;
  184. }
  185. /**
  186. * Returns the first available upsell nudge.
  187. *
  188. * @return array
  189. */
  190. public function get_upsell_nudge() {
  191. $jitm = \Automattic\Jetpack\JITMS\JITM::get_instance();
  192. $message_path = 'calypso:sites:sidebar_notice';
  193. $message = $jitm->get_messages( $message_path, wp_json_encode( array( 'message_path' => $message_path ) ), false );
  194. if ( isset( $message[0] ) ) {
  195. $message = $message[0];
  196. return array(
  197. 'content' => $message->content->message,
  198. 'cta' => $message->CTA->message, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  199. 'link' => $message->CTA->link, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  200. 'tracks_impression_event_name' => $message->tracks->display->name,
  201. 'tracks_impression_cta_name' => $message->tracks->display->props->cta_name,
  202. 'tracks_click_event_name' => $message->tracks->click->name,
  203. 'tracks_click_cta_name' => $message->tracks->click->props->cta_name,
  204. );
  205. }
  206. }
  207. /**
  208. * Adds Upgrades menu.
  209. *
  210. * @param string $plan The current WPCOM plan of the blog.
  211. */
  212. public function add_upgrades_menu( $plan = null ) {
  213. $products = Jetpack_Plan::get();
  214. if ( array_key_exists( 'product_name_short', $products ) ) {
  215. $plan = $products['product_name_short'];
  216. }
  217. parent::add_upgrades_menu( $plan );
  218. $last_upgrade_submenu_position = $this->get_submenu_item_count( 'paid-upgrades.php' );
  219. add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, $last_upgrade_submenu_position - 1 );
  220. /**
  221. * Whether to show the WordPress.com Emails submenu under the main Upgrades menu.
  222. *
  223. * @use add_filter( 'jetpack_show_wpcom_upgrades_email_menu', '__return_true' );
  224. * @module masterbar
  225. *
  226. * @since 9.7.0
  227. *
  228. * @param bool $show_wpcom_upgrades_email_menu Load the WordPress.com Emails submenu item. Default to false.
  229. */
  230. if ( apply_filters( 'jetpack_show_wpcom_upgrades_email_menu', false ) ) {
  231. add_submenu_page( 'paid-upgrades.php', __( 'Emails', 'jetpack' ), __( 'Emails', 'jetpack' ), 'manage_options', 'https://wordpress.com/email/' . $this->domain, null, $last_upgrade_submenu_position );
  232. }
  233. }
  234. /**
  235. * Adds Settings menu.
  236. */
  237. public function add_options_menu() {
  238. parent::add_options_menu();
  239. if ( Jetpack_Plan::supports( 'security-settings' ) ) {
  240. add_submenu_page(
  241. 'options-general.php',
  242. esc_attr__( 'Security', 'jetpack' ),
  243. __( 'Security', 'jetpack' ),
  244. 'manage_options',
  245. 'https://wordpress.com/settings/security/' . $this->domain,
  246. null,
  247. 2
  248. );
  249. }
  250. add_submenu_page( 'options-general.php', esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 11 );
  251. add_submenu_page( 'options-general.php', esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', 'https://wordpress.com/settings/jetpack/' . $this->domain, null, 12 );
  252. // Page Optimize is active by default on all Atomic sites and registers a Settings > Performance submenu which
  253. // would conflict with our own Settings > Performance that links to Calypso, so we hide it it since the Calypso
  254. // performance settings already have a link to Page Optimize settings page.
  255. $this->hide_submenu_page( 'options-general.php', 'page-optimize' );
  256. }
  257. /**
  258. * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly.
  259. *
  260. * @param string $submenu_file The current pages $submenu_file global variable value.
  261. * @return string | null
  262. */
  263. public function override_the_theme_installer( $submenu_file ) {
  264. global $pagenow;
  265. if ( 'themes.php' === $submenu_file && 'theme-install.php' === $pagenow ) {
  266. return null;
  267. }
  268. return $submenu_file;
  269. }
  270. /**
  271. * Also remove the Gutenberg plugin menu.
  272. */
  273. public function add_gutenberg_menus() {
  274. // Always remove the Gutenberg menu.
  275. remove_menu_page( 'gutenberg' );
  276. parent::add_gutenberg_menus();
  277. }
  278. /**
  279. * Saves the sidebar state ( expanded / collapsed ) via an ajax request.
  280. */
  281. public function ajax_sidebar_state() {
  282. $expanded = filter_var( $_REQUEST['expanded'], FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  283. Client::wpcom_json_api_request_as_user(
  284. '/me/preferences',
  285. '2',
  286. array(
  287. 'method' => 'POST',
  288. ),
  289. (object) array( 'calypso_preferences' => (object) array( 'sidebarCollapsed' => ! $expanded ) ),
  290. 'wpcom'
  291. );
  292. wp_die();
  293. }
  294. }