Açıklama Yok

class-jetpack-admin-menu.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Jetpack Admin Menu file.
  4. *
  5. * @package Jetpack
  6. */
  7. namespace Automattic\Jetpack\Dashboard_Customizations;
  8. require_once __DIR__ . '/class-admin-menu.php';
  9. /**
  10. * Class Jetpack_Admin_Menu.
  11. */
  12. class Jetpack_Admin_Menu extends Admin_Menu {
  13. /**
  14. * Determines whether the current locale is right-to-left (RTL).
  15. *
  16. * Performs the check against the current locale set on the WordPress.com's account settings.
  17. * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`.
  18. */
  19. public function is_rtl() {
  20. return get_user_option( 'jetpack_wpcom_is_rtl' );
  21. }
  22. /**
  23. * Create the desired menu output.
  24. */
  25. public function reregister_menu_items() {
  26. global $menu, $submenu;
  27. // Reset menus so there are no third-party plugin items.
  28. $menu = array(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
  29. $submenu = array(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
  30. parent::reregister_menu_items();
  31. $this->add_feedback_menu();
  32. $this->add_wp_admin_menu();
  33. ksort( $GLOBALS['menu'] );
  34. }
  35. /**
  36. * Get the preferred view for the given screen.
  37. *
  38. * @param string $screen Screen identifier.
  39. * @param bool $fallback_global_preference (Optional) Whether the global preference for all screens should be used
  40. * as fallback if there is no specific preference for the given screen.
  41. * Default: true.
  42. * @return string
  43. */
  44. public function get_preferred_view( $screen, $fallback_global_preference = true ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  45. // Force default views (Calypso) on Jetpack sites since Nav Unification is disabled on WP Admin.
  46. return self::DEFAULT_VIEW;
  47. }
  48. /**
  49. * Adds Posts menu.
  50. */
  51. public function add_posts_menu() {
  52. $post = get_post_type_object( 'post' );
  53. add_menu_page( esc_attr( $post->labels->menu_name ), $post->labels->menu_name, $post->cap->edit_posts, 'https://wordpress.com/posts/' . $this->domain, null, 'dashicons-admin-post' );
  54. }
  55. /**
  56. * Adds Media menu.
  57. */
  58. public function add_media_menu() {
  59. add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', 'https://wordpress.com/media/' . $this->domain, null, 'dashicons-admin-media' );
  60. }
  61. /**
  62. * Adds Page menu.
  63. */
  64. public function add_page_menu() {
  65. $page = get_post_type_object( 'page' );
  66. add_menu_page( esc_attr( $page->labels->menu_name ), $page->labels->menu_name, $page->cap->edit_posts, 'https://wordpress.com/pages/' . $this->domain, null, 'dashicons-admin-page' );
  67. }
  68. /**
  69. * Adds a custom post type menu.
  70. *
  71. * @param string $post_type Custom post type.
  72. */
  73. public function add_custom_post_type_menu( $post_type ) {
  74. $ptype_obj = get_post_type_object( $post_type );
  75. if ( empty( $ptype_obj ) ) {
  76. return;
  77. }
  78. $menu_slug = 'https://wordpress.com/types/' . $post_type . '/' . $this->domain;
  79. // Menu icon.
  80. $menu_icon = 'dashicons-admin-post';
  81. if ( is_string( $ptype_obj->menu_icon ) ) {
  82. // Special handling for data:image/svg+xml and Dashicons.
  83. if ( 0 === strpos( $ptype_obj->menu_icon, 'data:image/svg+xml;base64,' ) || 0 === strpos( $ptype_obj->menu_icon, 'dashicons-' ) ) {
  84. $menu_icon = $ptype_obj->menu_icon;
  85. } else {
  86. $menu_icon = esc_url( $ptype_obj->menu_icon );
  87. }
  88. }
  89. add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, $menu_icon );
  90. }
  91. /**
  92. * Adds Comments menu.
  93. */
  94. public function add_comments_menu() {
  95. add_menu_page( esc_attr__( 'Comments', 'jetpack' ), __( 'Comments', 'jetpack' ), 'edit_posts', 'https://wordpress.com/comments/all/' . $this->domain, null, 'dashicons-admin-comments' );
  96. }
  97. /**
  98. * Adds Feedback menu.
  99. */
  100. public function add_feedback_menu() {
  101. $post_type = 'feedback';
  102. $ptype_obj = get_post_type_object( $post_type );
  103. if ( empty( $ptype_obj ) ) {
  104. return;
  105. }
  106. $slug = 'edit.php?post_type=' . $post_type;
  107. $name = __( 'Feedback', 'jetpack' );
  108. $capability = $ptype_obj->cap->edit_posts;
  109. $icon = $ptype_obj->menu_icon;
  110. $position = 45; // Before Jetpack.
  111. add_menu_page( esc_attr( $name ), $name, $capability, $slug, null, $icon, $position );
  112. }
  113. /**
  114. * Adds Jetpack menu.
  115. */
  116. public function add_jetpack_menu() {
  117. parent::add_jetpack_menu();
  118. // Place "Scan" submenu after Backup.
  119. $position = 0;
  120. global $submenu;
  121. foreach ( $submenu['jetpack'] as $submenu_item ) {
  122. $position ++;
  123. if ( __( 'Backup', 'jetpack' ) === $submenu_item[3] ) {
  124. break;
  125. }
  126. }
  127. add_submenu_page( 'jetpack', esc_attr__( 'Scan', 'jetpack' ), __( 'Scan', 'jetpack' ), 'manage_options', 'https://wordpress.com/scan/' . $this->domain, null, $position );
  128. }
  129. /**
  130. * Adds Appearance menu.
  131. *
  132. * @return string The Customizer URL.
  133. */
  134. public function add_appearance_menu() {
  135. $themes_url = 'https://wordpress.com/themes/' . $this->domain;
  136. // Customize on Jetpack sites is always done on WP Admin (unsupported by Calypso).
  137. $customize_url = 'customize.php';
  138. add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), 'switch_themes', $themes_url, null, 'dashicons-admin-appearance', 60 );
  139. add_submenu_page( $themes_url, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', 'https://wordpress.com/themes/' . $this->domain );
  140. add_submenu_page( $themes_url, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $customize_url );
  141. return $customize_url;
  142. }
  143. /**
  144. * Adds Plugins menu.
  145. */
  146. public function add_plugins_menu() {
  147. add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), __( 'Plugins', 'jetpack' ), 'activate_plugins', 'https://wordpress.com/plugins/' . $this->domain, null, 'dashicons-admin-plugins', 65 );
  148. }
  149. /**
  150. * Adds Users menu.
  151. */
  152. public function add_users_menu() {
  153. if ( current_user_can( 'list_users' ) ) {
  154. add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', 'https://wordpress.com/people/team/' . $this->domain, null, 'dashicons-admin-users', 70 );
  155. } else {
  156. add_menu_page( esc_attr__( 'My Profile', 'jetpack' ), __( 'Profile', 'jetpack' ), 'read', 'https://wordpress.com/me', null, 'dashicons-admin-users', 70 );
  157. }
  158. }
  159. /**
  160. * Adds Tools menu.
  161. */
  162. public function add_tools_menu() {
  163. add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'publish_posts', 'tools.php', null, 'dashicons-admin-tools', 75 );
  164. add_submenu_page( 'tools.php', esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/tools/' . $this->domain );
  165. add_submenu_page( 'tools.php', esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain );
  166. // Import/Export on Jetpack sites is always handled on WP Admin.
  167. add_submenu_page( 'tools.php', esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', 'import.php' );
  168. add_submenu_page( 'tools.php', esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', 'export.php' );
  169. // Remove the submenu auto-created by Core.
  170. $this->hide_submenu_page( 'tools.php', 'tools.php' );
  171. }
  172. /**
  173. * Adds Settings menu.
  174. */
  175. public function add_options_menu() {
  176. $slug = 'https://wordpress.com/settings/general/' . $this->domain;
  177. add_menu_page( esc_attr__( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'manage_options', $slug, null, 'dashicons-admin-settings', 80 );
  178. add_submenu_page( $slug, esc_attr__( 'General', 'jetpack' ), __( 'General', 'jetpack' ), 'manage_options', $slug );
  179. add_submenu_page( $slug, esc_attr__( 'Security', 'jetpack' ), __( 'Security', 'jetpack' ), 'manage_options', 'https://wordpress.com/settings/security/' . $this->domain );
  180. add_submenu_page( $slug, esc_attr__( 'Performance', 'jetpack' ), __( 'Performance', 'jetpack' ), 'manage_options', 'https://wordpress.com/settings/performance/' . $this->domain );
  181. add_submenu_page( $slug, esc_attr__( 'Writing', 'jetpack' ), __( 'Writing', 'jetpack' ), 'manage_options', 'https://wordpress.com/settings/writing/' . $this->domain );
  182. add_submenu_page( $slug, esc_attr__( 'Discussion', 'jetpack' ), __( 'Discussion', 'jetpack' ), 'manage_options', 'https://wordpress.com/settings/discussion/' . $this->domain );
  183. $has_scan = \Jetpack_Plan::supports( 'scan' );
  184. $rewind_state = get_transient( 'jetpack_rewind_state' );
  185. $has_backup = $rewind_state && in_array( $rewind_state->state, array( 'awaiting_credentials', 'provisioning', 'active' ), true );
  186. if ( $has_scan || $has_backup ) {
  187. add_submenu_page( $slug, esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', 'https://wordpress.com/settings/jetpack/' . $this->domain );
  188. }
  189. }
  190. /**
  191. * Adds WP Admin menu.
  192. */
  193. public function add_wp_admin_menu() {
  194. global $menu;
  195. // Attempt to get last position.
  196. ksort( $menu );
  197. end( $menu );
  198. $position = key( $menu );
  199. $this->add_admin_menu_separator( ++ $position );
  200. add_menu_page( __( 'WP Admin', 'jetpack' ), __( 'WP Admin', 'jetpack' ), 'read', 'index.php', null, 'dashicons-wordpress-alt', $position );
  201. }
  202. }