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_init', 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. register_setting( 'woocommerce_status_settings_fields', 'woocommerce_status_options' ); 
  71.  
  72. /** 
  73. * Addons menu item. 
  74. */ 
  75. public function addons_menu() { 
  76. add_submenu_page( 'woocommerce', __( 'WooCommerce Add-ons/Extensions', 'woocommerce' ), __( 'Add-ons', 'woocommerce' ) , 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) ); 
  77.  
  78. /** 
  79. * Highlights the correct top level admin menu item for post type add screens. 
  80. */ 
  81. public function menu_highlight() { 
  82. global $parent_file, $submenu_file, $post_type; 
  83.  
  84. switch ( $post_type ) { 
  85. case 'shop_order' : 
  86. case 'shop_coupon' : 
  87. $parent_file = 'woocommerce'; 
  88. break; 
  89. case 'product' : 
  90. $screen = get_current_screen(); 
  91. if ( $screen && taxonomy_is_product_attribute( $screen->taxonomy ) ) { 
  92. $submenu_file = 'product_attributes'; 
  93. $parent_file = 'edit.php?post_type=product'; 
  94. break; 
  95.  
  96. /** 
  97. * Adds the order processing count to the menu. 
  98. */ 
  99. public function menu_order_count() { 
  100. global $submenu; 
  101.  
  102. if ( isset( $submenu['woocommerce'] ) ) { 
  103. // Remove 'WooCommerce' sub menu item 
  104. unset( $submenu['woocommerce'][0] ); 
  105.  
  106. // Add count if user has access 
  107. if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'manage_woocommerce' ) && ( $order_count = wc_processing_order_count() ) ) { 
  108. foreach ( $submenu['woocommerce'] as $key => $menu_item ) { 
  109. if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) { 
  110. $submenu['woocommerce'][ $key ][0] .= ' <span class="awaiting-mod update-plugins count-' . $order_count . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; 
  111. break; 
  112.  
  113. /** 
  114. * Reorder the WC menu items in admin. 
  115. * @param mixed $menu_order 
  116. * @return array 
  117. */ 
  118. public function menu_order( $menu_order ) { 
  119. // Initialize our custom order array 
  120. $woocommerce_menu_order = array(); 
  121.  
  122. // Get the index of our custom separator 
  123. $woocommerce_separator = array_search( 'separator-woocommerce', $menu_order ); 
  124.  
  125. // Get index of product menu 
  126. $woocommerce_product = array_search( 'edit.php?post_type=product', $menu_order ); 
  127.  
  128. // Loop through menu order and do some rearranging 
  129. foreach ( $menu_order as $index => $item ) { 
  130.  
  131. if ( ( ( 'woocommerce' ) == $item ) ) { 
  132. $woocommerce_menu_order[] = 'separator-woocommerce'; 
  133. $woocommerce_menu_order[] = $item; 
  134. $woocommerce_menu_order[] = 'edit.php?post_type=product'; 
  135. unset( $menu_order[ $woocommerce_separator ] ); 
  136. unset( $menu_order[ $woocommerce_product ] ); 
  137. } elseif ( ! in_array( $item, array( 'separator-woocommerce' ) ) ) { 
  138. $woocommerce_menu_order[] = $item; 
  139.  
  140.  
  141. // Return order 
  142. return $woocommerce_menu_order; 
  143.  
  144. /** 
  145. * Custom menu order. 
  146. * @return bool 
  147. */ 
  148. public function custom_menu_order() { 
  149. return current_user_can( 'manage_woocommerce' ); 
  150.  
  151. /** 
  152. * Init the reports page. 
  153. */ 
  154. public function reports_page() { 
  155. WC_Admin_Reports::output(); 
  156.  
  157. /** 
  158. * Init the settings page. 
  159. */ 
  160. public function settings_page() { 
  161. WC_Admin_Settings::output(); 
  162.  
  163. /** 
  164. * Init the attributes page. 
  165. */ 
  166. public function attributes_page() { 
  167. WC_Admin_Attributes::output(); 
  168.  
  169. /** 
  170. * Init the status page. 
  171. */ 
  172. public function status_page() { 
  173. WC_Admin_Status::output(); 
  174.  
  175. /** 
  176. * Init the addons page. 
  177. */ 
  178. public function addons_page() { 
  179. WC_Admin_Addons::output(); 
  180.  
  181. /** 
  182. * Add custom nav meta box. 
  183. * Adapted from http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/. 
  184. */ 
  185. public function add_nav_menu_meta_boxes() { 
  186. add_meta_box( 'woocommerce_endpoints_nav_link', __( 'WooCommerce Endpoints', 'woocommerce' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' ); 
  187.  
  188. /** 
  189. * Output menu links. 
  190. */ 
  191. public function nav_menu_links() { 
  192. $exclude = array( 'view-order', 'add-payment-method', 'order-pay', 'order-received' ); 
  193. ?> 
  194. <div id="posttype-woocommerce-endpoints" class="posttypediv"> 
  195. <div id="tabs-panel-woocommerce-endpoints" class="tabs-panel tabs-panel-active"> 
  196. <ul id="woocommerce-endpoints-checklist" class="categorychecklist form-no-clear"> 
  197. <?php 
  198. $i = -1; 
  199. foreach ( WC()->query->query_vars as $key => $value ) { 
  200. if ( in_array( $key, $exclude ) ) { 
  201. continue; 
  202. ?> 
  203. <li> 
  204. <label class="menu-item-title"> 
  205. <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( $key ); ?> 
  206. </label> 
  207. <input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-type]" value="custom" /> 
  208. <input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-title]" value="<?php echo esc_html( $key ); ?>" /> 
  209. <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_endpoint_url( $key, '', wc_get_page_permalink( 'myaccount' ) ) ); ?>" /> 
  210. <input type="hidden" class="menu-item-classes" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-classes]" /> 
  211. </li> 
  212. <?php 
  213. $i --; 
  214. ?> 
  215. </ul> 
  216. </div> 
  217. <p class="button-controls"> 
  218. <span class="list-controls"> 
  219. <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> 
  220. </span> 
  221. <span class="add-to-menu"> 
  222. <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"> 
  223. <span class="spinner"></span> 
  224. </span> 
  225. </p> 
  226. </div> 
  227. <?php 
  228.  
  229. /** 
  230. * Add the "Visit Store" link in admin bar main menu. 
  231. * @since 2.4.0 
  232. * @param WP_Admin_Bar $wp_admin_bar 
  233. */ 
  234. public function admin_bar_menus( $wp_admin_bar ) { 
  235. if ( ! is_admin() || ! is_user_logged_in() ) { 
  236. return; 
  237.  
  238. // Show only when the user is a member of this site, or they're a super admin. 
  239. if ( ! is_user_member_of_blog() && ! is_super_admin() ) { 
  240. return; 
  241.  
  242. // Don't display when shop page is the same of the page on front. 
  243. if ( get_option( 'page_on_front' ) == wc_get_page_id( 'shop' ) ) { 
  244. return; 
  245.  
  246. // Add an option to visit the store. 
  247. $wp_admin_bar->add_node( array( 
  248. 'parent' => 'site-name',  
  249. 'id' => 'view-store',  
  250. 'title' => __( 'Visit Store', 'woocommerce' ),  
  251. 'href' => wc_get_page_permalink( 'shop' ) 
  252. ) );