Geen omschrijving

class-wc-admin-menus.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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, 'menu_highlight' ) );
  22. add_action( 'admin_menu', array( $this, 'menu_order_count' ) );
  23. add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 );
  24. add_action( 'admin_menu', array( $this, 'reports_menu' ), 20 );
  25. add_action( 'admin_menu', array( $this, 'settings_menu' ), 50 );
  26. add_action( 'admin_menu', array( $this, 'status_menu' ), 60 );
  27. if ( apply_filters( 'woocommerce_show_addons_page', true ) ) {
  28. add_action( 'admin_menu', array( $this, 'addons_menu' ), 70 );
  29. }
  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_filter( 'submenu_file', array( $this, 'update_menu_highlight' ), 10, 2 );
  34. add_filter( 'admin_title', array( $this, 'update_my_subscriptions_title' ) );
  35. // Add endpoints custom URLs in Appearance > Menus > Pages.
  36. add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) );
  37. // Admin bar menus.
  38. if ( apply_filters( 'woocommerce_show_admin_bar_visit_store', true ) ) {
  39. add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 31 );
  40. }
  41. // Handle saving settings earlier than load-{page} hook to avoid race conditions in conditional menus.
  42. add_action( 'wp_loaded', array( $this, 'save_settings' ) );
  43. }
  44. /**
  45. * Add menu items.
  46. */
  47. public function admin_menu() {
  48. global $menu;
  49. $woocommerce_icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDI0IDEwMjQiPjxwYXRoIGZpbGw9IiNhMmFhYjIiIGQ9Ik02MTIuMTkyIDQyNi4zMzZjMC02Ljg5Ni0zLjEzNi01MS42LTI4LTUxLjYtMzcuMzYgMC00Ni43MDQgNzIuMjU2LTQ2LjcwNCA4Mi42MjQgMCAzLjQwOCAzLjE1MiA1OC40OTYgMjguMDMyIDU4LjQ5NiAzNC4xOTItLjAzMiA0Ni42NzItNzIuMjg4IDQ2LjY3Mi04OS41MnptMjAyLjE5MiAwYzAtNi44OTYtMy4xNTItNTEuNi0yOC4wMzItNTEuNi0zNy4yOCAwLTQ2LjYwOCA3Mi4yNTYtNDYuNjA4IDgyLjYyNCAwIDMuNDA4IDMuMDcyIDU4LjQ5NiAyNy45NTIgNTguNDk2IDM0LjE5Mi0uMDMyIDQ2LjY4OC03Mi4yODggNDYuNjg4LTg5LjUyek0xNDEuMjk2Ljc2OGMtNjguMjI0IDAtMTIzLjUwNCA1NS40ODgtMTIzLjUwNCAxMjMuOTJ2NjUwLjcyYzAgNjguND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k2OCAxLjg1NiAyLjkyOCAxLjUwNCA3LjAwOCA0LjU2IDEwLjQzMiAzLjE1Mi0xMC4yODggNjYuOTI4LTE2OC43ODQgOTQuOTYtMTY4Ljc4NCAyMi41NDQgMCAzMC40IDQ0LjU5MiAzMy41MzYgNjEuODI0IDYuMjA4IDIwLjY1NiAxMy4wODggNTUuMjE2IDIyLjQxNiA4Mi43NTIgMC0xMy43NzYgMTIuNDgtMjAzLjEyIDY1LjM5Mi0yMDMuMTIgMTguNTkyLjAzMiAyNi43MDQgNi45MjggMjYuNzA0IDI3LjU2OHpNODcwLjMyIDQyMi45MjhjMCA1NS4wODgtMzEuMDg4IDE1NC44OC0xMDIuNjQgMTU0Ljg4LTYuMTkyIDAtMTguNDQ4LTMuNjE2LTI1LjQyNC02LjAxNi0zMi40MzItMTEuMTY4LTUwLjE3Ni00OS42OTYtNTIuMjg4LTY2LjI1NiAwIDAtMy44ODgtMTcuOTItMy44ODgtNDAuODk2czMuODg4LTQ1LjE4NCAzLjg4OC00NS4xODRjMTUuNTUyLTc1LjcyOCA0My40ODgtMTA2LjczNiA5Ni4zODQtMTA2LjczNiA1OS4xMDQtLjAzMiA4My45NjggNTguNTI4IDgzLjk2OCAxMTAuMjA4eiIvPjwvc3ZnPg==';
  50. if ( current_user_can( 'edit_others_shop_orders' ) ) {
  51. $menu[] = array( '', 'read', 'separator-woocommerce', '', 'wp-menu-separator woocommerce' ); // WPCS: override ok.
  52. }
  53. add_menu_page( __( 'WooCommerce', 'woocommerce' ), __( 'WooCommerce', 'woocommerce' ), 'edit_others_shop_orders', 'woocommerce', null, $woocommerce_icon, '55.5' );
  54. add_submenu_page( 'edit.php?post_type=product', __( 'Attributes', 'woocommerce' ), __( 'Attributes', 'woocommerce' ), 'manage_product_terms', 'product_attributes', array( $this, 'attributes_page' ) );
  55. }
  56. /**
  57. * Add menu item.
  58. */
  59. public function reports_menu() {
  60. if ( current_user_can( 'edit_others_shop_orders' ) ) {
  61. add_submenu_page( 'woocommerce', __( 'Reports', 'woocommerce' ), __( 'Reports', 'woocommerce' ), 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ) );
  62. } else {
  63. add_menu_page( __( 'Sales reports', 'woocommerce' ), __( 'Sales reports', 'woocommerce' ), 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ), 'dashicons-chart-bar', '55.6' );
  64. }
  65. }
  66. /**
  67. * Add menu item.
  68. */
  69. public function settings_menu() {
  70. $settings_page = add_submenu_page( 'woocommerce', __( 'WooCommerce settings', 'woocommerce' ), __( 'Settings', 'woocommerce' ), 'manage_woocommerce', 'wc-settings', array( $this, 'settings_page' ) );
  71. add_action( 'load-' . $settings_page, array( $this, 'settings_page_init' ) );
  72. }
  73. /**
  74. * Loads gateways and shipping methods into memory for use within settings.
  75. */
  76. public function settings_page_init() {
  77. WC()->payment_gateways();
  78. WC()->shipping();
  79. // Include settings pages.
  80. WC_Admin_Settings::get_settings_pages();
  81. // Add any posted messages.
  82. if ( ! empty( $_GET['wc_error'] ) ) { // WPCS: input var okay, CSRF ok.
  83. WC_Admin_Settings::add_error( wp_kses_post( wp_unslash( $_GET['wc_error'] ) ) ); // WPCS: input var okay, CSRF ok.
  84. }
  85. if ( ! empty( $_GET['wc_message'] ) ) { // WPCS: input var okay, CSRF ok.
  86. WC_Admin_Settings::add_message( wp_kses_post( wp_unslash( $_GET['wc_message'] ) ) ); // WPCS: input var okay, CSRF ok.
  87. }
  88. do_action( 'woocommerce_settings_page_init' );
  89. }
  90. /**
  91. * Handle saving of settings.
  92. *
  93. * @return void
  94. */
  95. public function save_settings() {
  96. global $current_tab, $current_section;
  97. // We should only save on the settings page.
  98. if ( ! is_admin() || ! isset( $_GET['page'] ) || 'wc-settings' !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  99. return;
  100. }
  101. // Include settings pages.
  102. WC_Admin_Settings::get_settings_pages();
  103. // Get current tab/section.
  104. $current_tab = empty( $_GET['tab'] ) ? 'general' : sanitize_title( wp_unslash( $_GET['tab'] ) ); // WPCS: input var okay, CSRF ok.
  105. $current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( wp_unslash( $_REQUEST['section'] ) ); // WPCS: input var okay, CSRF ok.
  106. // Save settings if data has been posted.
  107. if ( '' !== $current_section && apply_filters( "woocommerce_save_settings_{$current_tab}_{$current_section}", ! empty( $_POST['save'] ) ) ) { // WPCS: input var okay, CSRF ok.
  108. WC_Admin_Settings::save();
  109. } elseif ( '' === $current_section && apply_filters( "woocommerce_save_settings_{$current_tab}", ! empty( $_POST['save'] ) ) ) { // WPCS: input var okay, CSRF ok.
  110. WC_Admin_Settings::save();
  111. }
  112. }
  113. /**
  114. * Add menu item.
  115. */
  116. public function status_menu() {
  117. add_submenu_page( 'woocommerce', __( 'WooCommerce status', 'woocommerce' ), __( 'Status', 'woocommerce' ), 'manage_woocommerce', 'wc-status', array( $this, 'status_page' ) );
  118. }
  119. /**
  120. * Addons menu item.
  121. */
  122. public function addons_menu() {
  123. $count_html = WC_Helper_Updater::get_updates_count_html();
  124. /* translators: %s: extensions count */
  125. $menu_title = sprintf( __( 'My Subscriptions %s', 'woocommerce' ), $count_html );
  126. add_submenu_page( 'woocommerce', __( 'WooCommerce Marketplace', 'woocommerce' ), __( 'Marketplace', 'woocommerce' ), 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) );
  127. add_submenu_page( 'woocommerce', __( 'My WooCommerce.com Subscriptions', 'woocommerce' ), $menu_title, 'manage_woocommerce', 'wc-addons&section=helper', array( $this, 'addons_page' ) );
  128. }
  129. /**
  130. * Highlights the correct top level admin menu item for post type add screens.
  131. */
  132. public function menu_highlight() {
  133. global $parent_file, $submenu_file, $post_type;
  134. switch ( $post_type ) {
  135. case 'shop_order':
  136. case 'shop_coupon':
  137. $parent_file = 'woocommerce'; // WPCS: override ok.
  138. break;
  139. case 'product':
  140. $screen = get_current_screen();
  141. if ( $screen && taxonomy_is_product_attribute( $screen->taxonomy ) ) {
  142. $submenu_file = 'product_attributes'; // WPCS: override ok.
  143. $parent_file = 'edit.php?post_type=product'; // WPCS: override ok.
  144. }
  145. break;
  146. }
  147. }
  148. /**
  149. * Adds the order processing count to the menu.
  150. */
  151. public function menu_order_count() {
  152. global $submenu;
  153. if ( isset( $submenu['woocommerce'] ) ) {
  154. // Remove 'WooCommerce' sub menu item.
  155. unset( $submenu['woocommerce'][0] );
  156. // Add count if user has access.
  157. if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'edit_others_shop_orders' ) ) {
  158. $order_count = apply_filters( 'woocommerce_menu_order_count', wc_processing_order_count() );
  159. if ( $order_count ) {
  160. foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
  161. if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) {
  162. $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>'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. * Reorder the WC menu items in admin.
  172. *
  173. * @param int $menu_order Menu order.
  174. * @return array
  175. */
  176. public function menu_order( $menu_order ) {
  177. // Initialize our custom order array.
  178. $woocommerce_menu_order = array();
  179. // Get the index of our custom separator.
  180. $woocommerce_separator = array_search( 'separator-woocommerce', $menu_order, true );
  181. // Get index of product menu.
  182. $woocommerce_product = array_search( 'edit.php?post_type=product', $menu_order, true );
  183. // Loop through menu order and do some rearranging.
  184. foreach ( $menu_order as $index => $item ) {
  185. if ( 'woocommerce' === $item ) {
  186. $woocommerce_menu_order[] = 'separator-woocommerce';
  187. $woocommerce_menu_order[] = $item;
  188. $woocommerce_menu_order[] = 'edit.php?post_type=product';
  189. unset( $menu_order[ $woocommerce_separator ] );
  190. unset( $menu_order[ $woocommerce_product ] );
  191. } elseif ( ! in_array( $item, array( 'separator-woocommerce' ), true ) ) {
  192. $woocommerce_menu_order[] = $item;
  193. }
  194. }
  195. // Return order.
  196. return $woocommerce_menu_order;
  197. }
  198. /**
  199. * Custom menu order.
  200. *
  201. * @param bool $enabled Whether custom menu ordering is already enabled.
  202. * @return bool
  203. */
  204. public function custom_menu_order( $enabled ) {
  205. return $enabled || current_user_can( 'edit_others_shop_orders' );
  206. }
  207. /**
  208. * Validate screen options on update.
  209. *
  210. * @param bool|int $status Screen option value. Default false to skip.
  211. * @param string $option The option name.
  212. * @param int $value The number of rows to use.
  213. */
  214. public function set_screen_option( $status, $option, $value ) {
  215. if ( in_array( $option, array( 'woocommerce_keys_per_page', 'woocommerce_webhooks_per_page' ), true ) ) {
  216. return $value;
  217. }
  218. return $status;
  219. }
  220. /**
  221. * Init the reports page.
  222. */
  223. public function reports_page() {
  224. WC_Admin_Reports::output();
  225. }
  226. /**
  227. * Init the settings page.
  228. */
  229. public function settings_page() {
  230. WC_Admin_Settings::output();
  231. }
  232. /**
  233. * Init the attributes page.
  234. */
  235. public function attributes_page() {
  236. WC_Admin_Attributes::output();
  237. }
  238. /**
  239. * Init the status page.
  240. */
  241. public function status_page() {
  242. WC_Admin_Status::output();
  243. }
  244. /**
  245. * Init the addons page.
  246. */
  247. public function addons_page() {
  248. WC_Admin_Addons::output();
  249. }
  250. /**
  251. * Add custom nav meta box.
  252. *
  253. * Adapted from http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/.
  254. */
  255. public function add_nav_menu_meta_boxes() {
  256. add_meta_box( 'woocommerce_endpoints_nav_link', __( 'WooCommerce endpoints', 'woocommerce' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' );
  257. }
  258. /**
  259. * Output menu links.
  260. */
  261. public function nav_menu_links() {
  262. // Get items from account menu.
  263. $endpoints = wc_get_account_menu_items();
  264. // Remove dashboard item.
  265. if ( isset( $endpoints['dashboard'] ) ) {
  266. unset( $endpoints['dashboard'] );
  267. }
  268. // Include missing lost password.
  269. $endpoints['lost-password'] = __( 'Lost password', 'woocommerce' );
  270. $endpoints = apply_filters( 'woocommerce_custom_nav_menu_items', $endpoints );
  271. ?>
  272. <div id="posttype-woocommerce-endpoints" class="posttypediv">
  273. <div id="tabs-panel-woocommerce-endpoints" class="tabs-panel tabs-panel-active">
  274. <ul id="woocommerce-endpoints-checklist" class="categorychecklist form-no-clear">
  275. <?php
  276. $i = -1;
  277. foreach ( $endpoints as $key => $value ) :
  278. ?>
  279. <li>
  280. <label class="menu-item-title">
  281. <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 ); ?>
  282. </label>
  283. <input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-type]" value="custom" />
  284. <input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-title]" value="<?php echo esc_attr( $value ); ?>" />
  285. <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 ) ); ?>" />
  286. <input type="hidden" class="menu-item-classes" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-classes]" />
  287. </li>
  288. <?php
  289. $i--;
  290. endforeach;
  291. ?>
  292. </ul>
  293. </div>
  294. <p class="button-controls">
  295. <span class="list-controls">
  296. <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>
  297. </span>
  298. <span class="add-to-menu">
  299. <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>
  300. <span class="spinner"></span>
  301. </span>
  302. </p>
  303. </div>
  304. <?php
  305. }
  306. /**
  307. * Add the "Visit Store" link in admin bar main menu.
  308. *
  309. * @since 2.4.0
  310. * @param WP_Admin_Bar $wp_admin_bar Admin bar instance.
  311. */
  312. public function admin_bar_menus( $wp_admin_bar ) {
  313. if ( ! is_admin() || ! is_admin_bar_showing() ) {
  314. return;
  315. }
  316. // Show only when the user is a member of this site, or they're a super admin.
  317. if ( ! is_user_member_of_blog() && ! is_super_admin() ) {
  318. return;
  319. }
  320. // Don't display when shop page is the same of the page on front.
  321. if ( intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) {
  322. return;
  323. }
  324. // Add an option to visit the store.
  325. $wp_admin_bar->add_node(
  326. array(
  327. 'parent' => 'site-name',
  328. 'id' => 'view-store',
  329. 'title' => __( 'Visit Store', 'woocommerce' ),
  330. 'href' => wc_get_page_permalink( 'shop' ),
  331. )
  332. );
  333. }
  334. /**
  335. * Highlight the My Subscriptions menu item when on that page
  336. *
  337. * @param string $submenu_file The submenu file.
  338. * @param string $parent_file currently opened page.
  339. *
  340. * @return string
  341. */
  342. public function update_menu_highlight( $submenu_file, $parent_file ) {
  343. if ( 'woocommerce' === $parent_file && isset( $_GET['section'] ) && 'helper' === $_GET['section'] ) {
  344. $submenu_file = 'wc-addons&section=helper';
  345. }
  346. return $submenu_file;
  347. }
  348. /**
  349. * Update the My Subscriptions document title when on that page.
  350. * We want to maintain existing page URL but add it as a separate page,
  351. * which requires updating it manually.
  352. *
  353. * @param string $admin_title existing page title.
  354. * @return string
  355. */
  356. public function update_my_subscriptions_title( $admin_title ) {
  357. if (
  358. isset( $_GET['page'] ) && 'wc-addons' === $_GET['page'] &&
  359. isset( $_GET['section'] ) && 'helper' === $_GET['section']
  360. ) {
  361. $admin_title = 'My WooCommerce.com Subscriptions';
  362. }
  363. return $admin_title;
  364. }
  365. }
  366. return new WC_Admin_Menus();