class-wc-admin-menus.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * Setup menus in WP admin.
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 2.5.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. if ( class_exists( 'WC_Admin_Menus', false ) ) {
  10. return new WC_Admin_Menus();
  11. }
  12. /**
  13. * WC_Admin_Menus Class.
  14. */
  15. class WC_Admin_Menus {
  16. /**
  17. * Hook in tabs.
  18. */
  19. public function __construct() {
  20. // Add menus.
  21. add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 );
  22. add_action( 'admin_menu', array( $this, 'reports_menu' ), 20 );
  23. add_action( 'admin_menu', array( $this, 'settings_menu' ), 50 );
  24. add_action( 'admin_menu', array( $this, 'status_menu' ), 60 );
  25. if ( apply_filters( 'woocommerce_show_addons_page', true ) ) {
  26. add_action( 'admin_menu', array( $this, 'addons_menu' ), 70 );
  27. }
  28. add_action( 'admin_head', array( $this, 'menu_highlight' ) );
  29. add_action( 'admin_head', array( $this, 'menu_order_count' ) );
  30. add_filter( 'menu_order', array( $this, 'menu_order' ) );
  31. add_filter( 'custom_menu_order', array( $this, 'custom_menu_order' ) );
  32. add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 );
  33. // Add endpoints custom URLs in Appearance > Menus > Pages.
  34. add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) );
  35. // Admin bar menus.
  36. if ( apply_filters( 'woocommerce_show_admin_bar_visit_store', true ) ) {
  37. add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 31 );
  38. }
  39. }
  40. /**
  41. * Add menu items.
  42. */
  43. public function admin_menu() {
  44. global $menu;
  45. if ( current_user_can( 'manage_woocommerce' ) ) {
  46. $menu[] = array( '', 'read', 'separator-woocommerce', '', 'wp-menu-separator woocommerce' ); // WPCS: override ok.
  47. }
  48. add_menu_page( __( 'WooCommerce', 'woocommerce' ), __( 'WooCommerce', 'woocommerce' ), 'manage_woocommerce', 'woocommerce', null, null, '55.5' );
  49. add_submenu_page( 'edit.php?post_type=product', __( 'Attributes', 'woocommerce' ), __( 'Attributes', 'woocommerce' ), 'manage_product_terms', 'product_attributes', array( $this, 'attributes_page' ) );
  50. }
  51. /**
  52. * Add menu item.
  53. */
  54. public function reports_menu() {
  55. if ( current_user_can( 'manage_woocommerce' ) ) {
  56. add_submenu_page( 'woocommerce', __( 'Reports', 'woocommerce' ), __( 'Reports', 'woocommerce' ), 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ) );
  57. } else {
  58. add_menu_page( __( 'Sales reports', 'woocommerce' ), __( 'Sales reports', 'woocommerce' ), 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ), null, '55.6' );
  59. }
  60. }
  61. /**
  62. * Add menu item.
  63. */
  64. public function settings_menu() {
  65. $settings_page = add_submenu_page( 'woocommerce', __( 'WooCommerce settings', 'woocommerce' ), __( 'Settings', 'woocommerce' ), 'manage_woocommerce', 'wc-settings', array( $this, 'settings_page' ) );
  66. add_action( 'load-' . $settings_page, array( $this, 'settings_page_init' ) );
  67. }
  68. /**
  69. * Loads gateways and shipping methods into memory for use within settings.
  70. */
  71. public function settings_page_init() {
  72. global $current_tab, $current_section;
  73. WC()->payment_gateways();
  74. WC()->shipping();
  75. // Include settings pages.
  76. WC_Admin_Settings::get_settings_pages();
  77. // Get current tab/section.
  78. $current_tab = empty( $_GET['tab'] ) ? 'general' : sanitize_title( wp_unslash( $_GET['tab'] ) ); // WPCS: input var okay, CSRF ok.
  79. $current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( wp_unslash( $_REQUEST['section'] ) ); // WPCS: input var okay, CSRF ok.
  80. // Save settings if data has been posted.
  81. if ( '' !== $current_section && apply_filters( "woocommerce_save_settings_{$current_tab}_{$current_section}", ! empty( $_POST ) ) ) { // WPCS: input var okay, CSRF ok.
  82. WC_Admin_Settings::save();
  83. } elseif ( '' === $current_section && apply_filters( "woocommerce_save_settings_{$current_tab}", ! empty( $_POST ) ) ) { // WPCS: input var okay, CSRF ok.
  84. WC_Admin_Settings::save();
  85. }
  86. // Add any posted messages.
  87. if ( ! empty( $_GET['wc_error'] ) ) { // WPCS: input var okay, CSRF ok.
  88. WC_Admin_Settings::add_error( wp_kses_post( wp_unslash( $_GET['wc_error'] ) ) ); // WPCS: input var okay, CSRF ok.
  89. }
  90. if ( ! empty( $_GET['wc_message'] ) ) { // WPCS: input var okay, CSRF ok.
  91. WC_Admin_Settings::add_message( wp_kses_post( wp_unslash( $_GET['wc_message'] ) ) ); // WPCS: input var okay, CSRF ok.
  92. }
  93. do_action( 'woocommerce_settings_page_init' );
  94. }
  95. /**
  96. * Add menu item.
  97. */
  98. public function status_menu() {
  99. add_submenu_page( 'woocommerce', __( 'WooCommerce status', 'woocommerce' ), __( 'Status', 'woocommerce' ), 'manage_woocommerce', 'wc-status', array( $this, 'status_page' ) );
  100. }
  101. /**
  102. * Addons menu item.
  103. */
  104. public function addons_menu() {
  105. $count_html = WC_Helper_Updater::get_updates_count_html();
  106. /* translators: %s: extensions count */
  107. $menu_title = sprintf( __( 'Extensions %s', 'woocommerce' ), $count_html );
  108. add_submenu_page( 'woocommerce', __( 'WooCommerce extensions', 'woocommerce' ), $menu_title, 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) );
  109. }
  110. /**
  111. * Highlights the correct top level admin menu item for post type add screens.
  112. */
  113. public function menu_highlight() {
  114. global $parent_file, $submenu_file, $post_type;
  115. switch ( $post_type ) {
  116. case 'shop_order':
  117. case 'shop_coupon':
  118. $parent_file = 'woocommerce'; // WPCS: override ok.
  119. break;
  120. case 'product':
  121. $screen = get_current_screen();
  122. if ( $screen && taxonomy_is_product_attribute( $screen->taxonomy ) ) {
  123. $submenu_file = 'product_attributes'; // WPCS: override ok.
  124. $parent_file = 'edit.php?post_type=product'; // WPCS: override ok.
  125. }
  126. break;
  127. }
  128. }
  129. /**
  130. * Adds the order processing count to the menu.
  131. */
  132. public function menu_order_count() {
  133. global $submenu;
  134. if ( isset( $submenu['woocommerce'] ) ) {
  135. // Remove 'WooCommerce' sub menu item.
  136. unset( $submenu['woocommerce'][0] );
  137. $order_count = wc_processing_order_count();
  138. // Add count if user has access.
  139. if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'manage_woocommerce' ) && $order_count ) {
  140. foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
  141. if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) {
  142. $submenu['woocommerce'][ $key ][0] .= ' <span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; // WPCS: override ok.
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. /**
  150. * Reorder the WC menu items in admin.
  151. *
  152. * @param int $menu_order Menu order.
  153. * @return array
  154. */
  155. public function menu_order( $menu_order ) {
  156. // Initialize our custom order array.
  157. $woocommerce_menu_order = array();
  158. // Get the index of our custom separator.
  159. $woocommerce_separator = array_search( 'separator-woocommerce', $menu_order, true );
  160. // Get index of product menu.
  161. $woocommerce_product = array_search( 'edit.php?post_type=product', $menu_order, true );
  162. // Loop through menu order and do some rearranging.
  163. foreach ( $menu_order as $index => $item ) {
  164. if ( 'woocommerce' === $item ) {
  165. $woocommerce_menu_order[] = 'separator-woocommerce';
  166. $woocommerce_menu_order[] = $item;
  167. $woocommerce_menu_order[] = 'edit.php?post_type=product';
  168. unset( $menu_order[ $woocommerce_separator ] );
  169. unset( $menu_order[ $woocommerce_product ] );
  170. } elseif ( ! in_array( $item, array( 'separator-woocommerce' ), true ) ) {
  171. $woocommerce_menu_order[] = $item;
  172. }
  173. }
  174. // Return order.
  175. return $woocommerce_menu_order;
  176. }
  177. /**
  178. * Custom menu order.
  179. *
  180. * @return bool
  181. */
  182. public function custom_menu_order() {
  183. return current_user_can( 'manage_woocommerce' );
  184. }
  185. /**
  186. * Validate screen options on update.
  187. *
  188. * @param bool|int $status Screen option value. Default false to skip.
  189. * @param string $option The option name.
  190. * @param int $value The number of rows to use.
  191. */
  192. public function set_screen_option( $status, $option, $value ) {
  193. if ( in_array( $option, array( 'woocommerce_keys_per_page', 'woocommerce_webhooks_per_page' ), true ) ) {
  194. return $value;
  195. }
  196. return $status;
  197. }
  198. /**
  199. * Init the reports page.
  200. */
  201. public function reports_page() {
  202. WC_Admin_Reports::output();
  203. }
  204. /**
  205. * Init the settings page.
  206. */
  207. public function settings_page() {
  208. WC_Admin_Settings::output();
  209. }
  210. /**
  211. * Init the attributes page.
  212. */
  213. public function attributes_page() {
  214. WC_Admin_Attributes::output();
  215. }
  216. /**
  217. * Init the status page.
  218. */
  219. public function status_page() {
  220. WC_Admin_Status::output();
  221. }
  222. /**
  223. * Init the addons page.
  224. */
  225. public function addons_page() {
  226. WC_Admin_Addons::output();
  227. }
  228. /**
  229. * Add custom nav meta box.
  230. *
  231. * Adapted from http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/.
  232. */
  233. public function add_nav_menu_meta_boxes() {
  234. add_meta_box( 'woocommerce_endpoints_nav_link', __( 'WooCommerce endpoints', 'woocommerce' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' );
  235. }
  236. /**
  237. * Output menu links.
  238. */
  239. public function nav_menu_links() {
  240. // Get items from account menu.
  241. $endpoints = wc_get_account_menu_items();
  242. // Remove dashboard item.
  243. if ( isset( $endpoints['dashboard'] ) ) {
  244. unset( $endpoints['dashboard'] );
  245. }
  246. // Include missing lost password.
  247. $endpoints['lost-password'] = __( 'Lost password', 'woocommerce' );
  248. $endpoints = apply_filters( 'woocommerce_custom_nav_menu_items', $endpoints );
  249. ?>
  250. <div id="posttype-woocommerce-endpoints" class="posttypediv">
  251. <div id="tabs-panel-woocommerce-endpoints" class="tabs-panel tabs-panel-active">
  252. <ul id="woocommerce-endpoints-checklist" class="categorychecklist form-no-clear">
  253. <?php
  254. $i = -1;
  255. foreach ( $endpoints as $key => $value ) :
  256. ?>
  257. <li>
  258. <label class="menu-item-title">
  259. <input type="checkbox" class="menu-item-checkbox" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-object-id]" value="<?php echo esc_attr( $i ); ?>" /> <?php echo esc_html( $value ); ?>
  260. </label>
  261. <input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-type]" value="custom" />
  262. <input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-title]" value="<?php echo esc_html( $value ); ?>" />
  263. <input type="hidden" class="menu-item-url" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-url]" value="<?php echo esc_url( wc_get_account_endpoint_url( $key ) ); ?>" />
  264. <input type="hidden" class="menu-item-classes" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-classes]" />
  265. </li>
  266. <?php
  267. $i--;
  268. endforeach;
  269. ?>
  270. </ul>
  271. </div>
  272. <p class="button-controls">
  273. <span class="list-controls">
  274. <a href="<?php echo esc_url( admin_url( 'nav-menus.php?page-tab=all&selectall=1#posttype-woocommerce-endpoints' ) ); ?>" class="select-all"><?php esc_html_e( 'Select all', 'woocommerce' ); ?></a>
  275. </span>
  276. <span class="add-to-menu">
  277. <button type="submit" class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to menu', 'woocommerce' ); ?>" name="add-post-type-menu-item" id="submit-posttype-woocommerce-endpoints"><?php esc_html_e( 'Add to menu', 'woocommerce' ); ?></button>
  278. <span class="spinner"></span>
  279. </span>
  280. </p>
  281. </div>
  282. <?php
  283. }
  284. /**
  285. * Add the "Visit Store" link in admin bar main menu.
  286. *
  287. * @since 2.4.0
  288. * @param WP_Admin_Bar $wp_admin_bar Admin bar instance.
  289. */
  290. public function admin_bar_menus( $wp_admin_bar ) {
  291. if ( ! is_admin() || ! is_user_logged_in() ) {
  292. return;
  293. }
  294. // Show only when the user is a member of this site, or they're a super admin.
  295. if ( ! is_user_member_of_blog() && ! is_super_admin() ) {
  296. return;
  297. }
  298. // Don't display when shop page is the same of the page on front.
  299. if ( intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) {
  300. return;
  301. }
  302. // Add an option to visit the store.
  303. $wp_admin_bar->add_node(
  304. array(
  305. 'parent' => 'site-name',
  306. 'id' => 'view-store',
  307. 'title' => __( 'Visit Store', 'woocommerce' ),
  308. 'href' => wc_get_page_permalink( 'shop' ),
  309. )
  310. );
  311. }
  312. }
  313. return new WC_Admin_Menus();