WC_Admin_Menus

WC_Admin_Menus Class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wc-admin-menus.php  
  1. class WC_Admin_Menus { 
  2.  
  3. /** 
  4. * Hook in tabs. 
  5. */ 
  6. public function __construct() { 
  7. // Add menus 
  8. add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 ); 
  9. add_action( 'admin_menu', array( $this, 'reports_menu' ), 20 ); 
  10. add_action( 'admin_menu', array( $this, 'settings_menu' ), 50 ); 
  11. add_action( 'admin_menu', array( $this, 'status_menu' ), 60 ); 
  12.  
  13. if ( apply_filters( 'woocommerce_show_addons_page', true ) ) { 
  14. add_action( 'admin_menu', array( $this, 'addons_menu' ), 70 ); 
  15.  
  16. add_action( 'admin_head', array( $this, 'menu_highlight' ) ); 
  17. add_action( 'admin_head', array( $this, 'menu_order_count' ) ); 
  18. add_filter( 'menu_order', array( $this, 'menu_order' ) ); 
  19. add_filter( 'custom_menu_order', array( $this, 'custom_menu_order' ) ); 
  20.  
  21. // Add endpoints custom URLs in Appearance > Menus > Pages. 
  22. add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) ); 
  23.  
  24. // Admin bar menus 
  25. if ( apply_filters( 'woocommerce_show_admin_bar_visit_store', true ) ) { 
  26. add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 31 ); 
  27.  
  28. /** 
  29. * Add menu items. 
  30. */ 
  31. public function admin_menu() { 
  32. global $menu; 
  33.  
  34. if ( current_user_can( 'manage_woocommerce' ) ) { 
  35. $menu[] = array( '', 'read', 'separator-woocommerce', '', 'wp-menu-separator woocommerce' ); 
  36.  
  37. add_menu_page( __( 'WooCommerce', 'woocommerce' ), __( 'WooCommerce', 'woocommerce' ), 'manage_woocommerce', 'woocommerce', null, null, '55.5' ); 
  38.  
  39. add_submenu_page( 'edit.php?post_type=product', __( 'Attributes', 'woocommerce' ), __( 'Attributes', 'woocommerce' ), 'manage_product_terms', 'product_attributes', array( $this, 'attributes_page' ) ); 
  40.  
  41. /** 
  42. * Add menu item. 
  43. */ 
  44. public function reports_menu() { 
  45. if ( current_user_can( 'manage_woocommerce' ) ) { 
  46. add_submenu_page( 'woocommerce', __( 'Reports', 'woocommerce' ), __( 'Reports', 'woocommerce' ) , 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ) ); 
  47. } else { 
  48. add_menu_page( __( 'Sales reports', 'woocommerce' ), __( 'Sales reports', 'woocommerce' ) , 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ), null, '55.6' ); 
  49.  
  50. /** 
  51. * Add menu item. 
  52. */ 
  53. public function settings_menu() { 
  54. $settings_page = add_submenu_page( 'woocommerce', __( 'WooCommerce settings', 'woocommerce' ), __( 'Settings', 'woocommerce' ) , 'manage_woocommerce', 'wc-settings', array( $this, 'settings_page' ) ); 
  55.  
  56. add_action( 'load-' . $settings_page, array( $this, 'settings_page_init' ) ); 
  57.  
  58. /** 
  59. * Loads gateways and shipping methods into memory for use within settings. 
  60. */ 
  61. public function settings_page_init() { 
  62. WC()->payment_gateways(); 
  63. WC()->shipping(); 
  64.  
  65. /** 
  66. * Add menu item. 
  67. */ 
  68. public function status_menu() { 
  69. add_submenu_page( 'woocommerce', __( 'WooCommerce status', 'woocommerce' ), __( 'System status', 'woocommerce' ) , 'manage_woocommerce', 'wc-status', array( $this, 'status_page' ) ); 
  70.  
  71. /** 
  72. * Addons menu item. 
  73. */ 
  74. public function addons_menu() { 
  75. add_submenu_page( 'woocommerce', __( 'WooCommerce extensions', 'woocommerce' ), __( 'Extensions', 'woocommerce' ) , 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) ); 
  76.  
  77. /** 
  78. * Highlights the correct top level admin menu item for post type add screens. 
  79. */ 
  80. public function menu_highlight() { 
  81. global $parent_file, $submenu_file, $post_type; 
  82.  
  83. switch ( $post_type ) { 
  84. case 'shop_order' : 
  85. case 'shop_coupon' : 
  86. $parent_file = 'woocommerce'; 
  87. break; 
  88. case 'product' : 
  89. $screen = get_current_screen(); 
  90. if ( $screen && taxonomy_is_product_attribute( $screen->taxonomy ) ) { 
  91. $submenu_file = 'product_attributes'; 
  92. $parent_file = 'edit.php?post_type=product'; 
  93. break; 
  94.  
  95. /** 
  96. * Adds the order processing count to the menu. 
  97. */ 
  98. public function menu_order_count() { 
  99. global $submenu; 
  100.  
  101. if ( isset( $submenu['woocommerce'] ) ) { 
  102. // Remove 'WooCommerce' sub menu item 
  103. unset( $submenu['woocommerce'][0] ); 
  104.  
  105. // Add count if user has access 
  106. if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'manage_woocommerce' ) && ( $order_count = wc_processing_order_count() ) ) { 
  107. foreach ( $submenu['woocommerce'] as $key => $menu_item ) { 
  108. if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) { 
  109. $submenu['woocommerce'][ $key ][0] .= ' <span class="awaiting-mod update-plugins count-' . $order_count . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; 
  110. break; 
  111.  
  112. /** 
  113. * Reorder the WC menu items in admin. 
  114. * @param mixed $menu_order 
  115. * @return array 
  116. */ 
  117. public function menu_order( $menu_order ) { 
  118. // Initialize our custom order array 
  119. $woocommerce_menu_order = array(); 
  120.  
  121. // Get the index of our custom separator 
  122. $woocommerce_separator = array_search( 'separator-woocommerce', $menu_order ); 
  123.  
  124. // Get index of product menu 
  125. $woocommerce_product = array_search( 'edit.php?post_type=product', $menu_order ); 
  126.  
  127. // Loop through menu order and do some rearranging 
  128. foreach ( $menu_order as $index => $item ) { 
  129.  
  130. if ( ( ( 'woocommerce' ) == $item ) ) { 
  131. $woocommerce_menu_order[] = 'separator-woocommerce'; 
  132. $woocommerce_menu_order[] = $item; 
  133. $woocommerce_menu_order[] = 'edit.php?post_type=product'; 
  134. unset( $menu_order[ $woocommerce_separator ] ); 
  135. unset( $menu_order[ $woocommerce_product ] ); 
  136. } elseif ( ! in_array( $item, array( 'separator-woocommerce' ) ) ) { 
  137. $woocommerce_menu_order[] = $item; 
  138.  
  139. // Return order 
  140. return $woocommerce_menu_order; 
  141.  
  142. /** 
  143. * Custom menu order. 
  144. * @return bool 
  145. */ 
  146. public function custom_menu_order() { 
  147. return current_user_can( 'manage_woocommerce' ); 
  148.  
  149. /** 
  150. * Init the reports page. 
  151. */ 
  152. public function reports_page() { 
  153. WC_Admin_Reports::output(); 
  154.  
  155. /** 
  156. * Init the settings page. 
  157. */ 
  158. public function settings_page() { 
  159. WC_Admin_Settings::output(); 
  160.  
  161. /** 
  162. * Init the attributes page. 
  163. */ 
  164. public function attributes_page() { 
  165. WC_Admin_Attributes::output(); 
  166.  
  167. /** 
  168. * Init the status page. 
  169. */ 
  170. public function status_page() { 
  171. WC_Admin_Status::output(); 
  172.  
  173. /** 
  174. * Init the addons page. 
  175. */ 
  176. public function addons_page() { 
  177. WC_Admin_Addons::output(); 
  178.  
  179. /** 
  180. * Add custom nav meta box. 
  181. * Adapted from http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/. 
  182. */ 
  183. public function add_nav_menu_meta_boxes() { 
  184. add_meta_box( 'woocommerce_endpoints_nav_link', __( 'WooCommerce endpoints', 'woocommerce' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' ); 
  185.  
  186. /** 
  187. * Output menu links. 
  188. */ 
  189. public function nav_menu_links() { 
  190. // Get items from account menu. 
  191. $endpoints = wc_get_account_menu_items(); 
  192.  
  193. // Remove dashboard item. 
  194. if ( isset( $endpoints['dashboard'] ) ) { 
  195. unset( $endpoints['dashboard'] ); 
  196.  
  197. // Include missing lost password. 
  198. $endpoints['lost-password'] = __( 'Lost password', 'woocommerce' ); 
  199.  
  200. $endpoints = apply_filters( 'woocommerce_custom_nav_menu_items', $endpoints ); 
  201.  
  202. ?> 
  203. <div id="posttype-woocommerce-endpoints" class="posttypediv"> 
  204. <div id="tabs-panel-woocommerce-endpoints" class="tabs-panel tabs-panel-active"> 
  205. <ul id="woocommerce-endpoints-checklist" class="categorychecklist form-no-clear"> 
  206. <?php 
  207. $i = -1; 
  208. foreach ( $endpoints as $key => $value ) : 
  209. ?> 
  210. <li> 
  211. <label class="menu-item-title"> 
  212. <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 ); ?> 
  213. </label> 
  214. <input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-type]" value="custom" /> 
  215. <input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-title]" value="<?php echo esc_html( $value ); ?>" /> 
  216. <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 ) ); ?>" /> 
  217. <input type="hidden" class="menu-item-classes" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-classes]" /> 
  218. </li> 
  219. <?php 
  220. $i--; 
  221. endforeach; 
  222. ?> 
  223. </ul> 
  224. </div> 
  225. <p class="button-controls"> 
  226. <span class="list-controls"> 
  227. <a href="<?php echo admin_url( 'nav-menus.php?page-tab=all&selectall=1#posttype-woocommerce-endpoints' ); ?>" class="select-all"><?php _e( 'Select all', 'woocommerce' ); ?></a> 
  228. </span> 
  229. <span class="add-to-menu"> 
  230. <input 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"> 
  231. <span class="spinner"></span> 
  232. </span> 
  233. </p> 
  234. </div> 
  235. <?php 
  236.  
  237. /** 
  238. * Add the "Visit Store" link in admin bar main menu. 
  239. * @since 2.4.0 
  240. * @param WP_Admin_Bar $wp_admin_bar 
  241. */ 
  242. public function admin_bar_menus( $wp_admin_bar ) { 
  243. if ( ! is_admin() || ! is_user_logged_in() ) { 
  244. return; 
  245.  
  246. // Show only when the user is a member of this site, or they're a super admin. 
  247. if ( ! is_user_member_of_blog() && ! is_super_admin() ) { 
  248. return; 
  249.  
  250. // Don't display when shop page is the same of the page on front. 
  251. if ( get_option( 'page_on_front' ) == wc_get_page_id( 'shop' ) ) { 
  252. return; 
  253.  
  254. // Add an option to visit the store. 
  255. $wp_admin_bar->add_node( array( 
  256. 'parent' => 'site-name',  
  257. 'id' => 'view-store',  
  258. 'title' => __( 'Visit Store', 'woocommerce' ),  
  259. 'href' => wc_get_page_permalink( 'shop' ),  
  260. ) );