PageRenderTime 28ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-menus.php

https://bitbucket.org/babinkochana/triptrills
PHP | 370 lines | 209 code | 56 blank | 105 comment | 29 complexity | 0406e6e3ffba318fbe6beae3bfe1e32c MD5 | raw file
Possible License(s): MIT, Apache-2.0, GPL-3.0, 0BSD, GPL-2.0
  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['save'] ) ) ) { // WPCS: input var okay, CSRF ok.
  82. WC_Admin_Settings::save();
  83. } elseif ( '' === $current_section && apply_filters( "woocommerce_save_settings_{$current_tab}", ! empty( $_POST['save'] ) ) ) { // 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. * @param bool $enabled Whether custom menu ordering is already enabled.
  181. * @return bool
  182. */
  183. public function custom_menu_order( $enabled ) {
  184. return $enabled || current_user_can( 'manage_woocommerce' );
  185. }
  186. /**
  187. * Validate screen options on update.
  188. *
  189. * @param bool|int $status Screen option value. Default false to skip.
  190. * @param string $option The option name.
  191. * @param int $value The number of rows to use.
  192. */
  193. public function set_screen_option( $status, $option, $value ) {
  194. if ( in_array( $option, array( 'woocommerce_keys_per_page', 'woocommerce_webhooks_per_page' ), true ) ) {
  195. return $value;
  196. }
  197. return $status;
  198. }
  199. /**
  200. * Init the reports page.
  201. */
  202. public function reports_page() {
  203. WC_Admin_Reports::output();
  204. }
  205. /**
  206. * Init the settings page.
  207. */
  208. public function settings_page() {
  209. WC_Admin_Settings::output();
  210. }
  211. /**
  212. * Init the attributes page.
  213. */
  214. public function attributes_page() {
  215. WC_Admin_Attributes::output();
  216. }
  217. /**
  218. * Init the status page.
  219. */
  220. public function status_page() {
  221. WC_Admin_Status::output();
  222. }
  223. /**
  224. * Init the addons page.
  225. */
  226. public function addons_page() {
  227. WC_Admin_Addons::output();
  228. }
  229. /**
  230. * Add custom nav meta box.
  231. *
  232. * Adapted from http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/.
  233. */
  234. public function add_nav_menu_meta_boxes() {
  235. add_meta_box( 'woocommerce_endpoints_nav_link', __( 'WooCommerce endpoints', 'woocommerce' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' );
  236. }
  237. /**
  238. * Output menu links.
  239. */
  240. public function nav_menu_links() {
  241. // Get items from account menu.
  242. $endpoints = wc_get_account_menu_items();
  243. // Remove dashboard item.
  244. if ( isset( $endpoints['dashboard'] ) ) {
  245. unset( $endpoints['dashboard'] );
  246. }
  247. // Include missing lost password.
  248. $endpoints['lost-password'] = __( 'Lost password', 'woocommerce' );
  249. $endpoints = apply_filters( 'woocommerce_custom_nav_menu_items', $endpoints );
  250. ?>
  251. <div id="posttype-woocommerce-endpoints" class="posttypediv">
  252. <div id="tabs-panel-woocommerce-endpoints" class="tabs-panel tabs-panel-active">
  253. <ul id="woocommerce-endpoints-checklist" class="categorychecklist form-no-clear">
  254. <?php
  255. $i = -1;
  256. foreach ( $endpoints as $key => $value ) :
  257. ?>
  258. <li>
  259. <label class="menu-item-title">
  260. <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 ); ?>
  261. </label>
  262. <input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-type]" value="custom" />
  263. <input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-title]" value="<?php echo esc_html( $value ); ?>" />
  264. <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 ) ); ?>" />
  265. <input type="hidden" class="menu-item-classes" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-classes]" />
  266. </li>
  267. <?php
  268. $i--;
  269. endforeach;
  270. ?>
  271. </ul>
  272. </div>
  273. <p class="button-controls">
  274. <span class="list-controls">
  275. <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>
  276. </span>
  277. <span class="add-to-menu">
  278. <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>
  279. <span class="spinner"></span>
  280. </span>
  281. </p>
  282. </div>
  283. <?php
  284. }
  285. /**
  286. * Add the "Visit Store" link in admin bar main menu.
  287. *
  288. * @since 2.4.0
  289. * @param WP_Admin_Bar $wp_admin_bar Admin bar instance.
  290. */
  291. public function admin_bar_menus( $wp_admin_bar ) {
  292. if ( ! is_admin() || ! is_admin_bar_showing() ) {
  293. return;
  294. }
  295. // Show only when the user is a member of this site, or they're a super admin.
  296. if ( ! is_user_member_of_blog() && ! is_super_admin() ) {
  297. return;
  298. }
  299. // Don't display when shop page is the same of the page on front.
  300. if ( intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) {
  301. return;
  302. }
  303. // Add an option to visit the store.
  304. $wp_admin_bar->add_node(
  305. array(
  306. 'parent' => 'site-name',
  307. 'id' => 'view-store',
  308. 'title' => __( 'Visit Store', 'woocommerce' ),
  309. 'href' => wc_get_page_permalink( 'shop' ),
  310. )
  311. );
  312. }
  313. }
  314. return new WC_Admin_Menus();