1 <?php
  2 /**
  3  * Setup menus in WP admin.
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 12 
 13 if ( ! class_exists( 'WC_Admin_Menus' ) ) :
 14 
 15 /**
 16  * WC_Admin_Menus Class
 17  */
 18 class WC_Admin_Menus {
 19 
 20     /**
 21      * Hook in tabs.
 22      */
 23     public function __construct() {
 24         // Add menus
 25         add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 );
 26         add_action( 'admin_menu', array( $this, 'reports_menu' ), 20 );
 27         add_action( 'admin_menu', array( $this, 'settings_menu' ), 50 );
 28         add_action( 'admin_menu', array( $this, 'status_menu' ), 60 );
 29 
 30         if ( apply_filters( 'woocommerce_show_addons_page', true ) ) {
 31             add_action( 'admin_menu', array( $this, 'addons_menu' ), 70 );
 32         }
 33 
 34         add_action( 'admin_head', array( $this, 'menu_highlight' ) );
 35         add_filter( 'menu_order', array( $this, 'menu_order' ) );
 36         add_filter( 'custom_menu_order', array( $this, 'custom_menu_order' ) );
 37     }
 38 
 39     /**
 40      * Add menu items
 41      */
 42     public function admin_menu() {
 43         global $menu, $woocommerce;
 44 
 45         if ( current_user_can( 'manage_woocommerce' ) )
 46             $menu[] = array( '', 'read', 'separator-woocommerce', '', 'wp-menu-separator woocommerce' );
 47 
 48         $main_page = add_menu_page( __( 'WooCommerce', 'woocommerce' ), __( 'WooCommerce', 'woocommerce' ), 'manage_woocommerce', 'woocommerce' , array( $this, 'settings_page' ), null, '55.5' );
 49 
 50         add_submenu_page( 'edit.php?post_type=product', __( 'Attributes', 'woocommerce' ), __( 'Attributes', 'woocommerce' ), 'manage_product_terms', 'product_attributes', array( $this, 'attributes_page' ) );
 51     }
 52 
 53     /**
 54      * Add menu item
 55      */
 56     public function reports_menu() {
 57         add_submenu_page( 'woocommerce', __( 'Reports', 'woocommerce' ),  __( 'Reports', 'woocommerce' ) , 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ) );
 58     }
 59 
 60     /**
 61      * Add menu item
 62      */
 63     public function settings_menu() {
 64         $settings_page = add_submenu_page( 'woocommerce', __( 'WooCommerce Settings', 'woocommerce' ),  __( 'Settings', 'woocommerce' ) , 'manage_woocommerce', 'wc-settings', array( $this, 'settings_page' ) );
 65 
 66         add_action( 'load-' . $settings_page, array( $this, 'settings_page_init' ) );
 67     }
 68 
 69     /**
 70      * Loads gateways and shipping methods into memory for use within settings.
 71      */
 72     public function settings_page_init() {
 73         WC()->payment_gateways();
 74         WC()->shipping();
 75     }
 76 
 77     /**
 78      * Add menu item
 79      */
 80     public function status_menu() {
 81         add_submenu_page( 'woocommerce', __( 'WooCommerce Status', 'woocommerce' ),  __( 'System Status', 'woocommerce' ) , 'manage_woocommerce', 'wc-status', array( $this, 'status_page' ) );
 82         register_setting( 'woocommerce_status_settings_fields', 'woocommerce_status_options' );
 83     }
 84 
 85     /**
 86      * Addons menu item
 87      */
 88     public function addons_menu() {
 89         add_submenu_page( 'woocommerce', __( 'WooCommerce Add-ons/Extensions', 'woocommerce' ),  __( 'Add-ons', 'woocommerce' ) , 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) );
 90     }
 91 
 92     /**
 93      * Highlights the correct top level admin menu item for post type add screens.
 94      *
 95      * @access public
 96      * @return void
 97      */
 98     public function menu_highlight() {
 99         global $menu, $submenu, $parent_file, $submenu_file, $self, $post_type, $taxonomy;
100 
101         $to_highlight_types = array( 'shop_order', 'shop_coupon' );
102 
103         if ( isset( $post_type ) ) {
104             if ( in_array( $post_type, $to_highlight_types ) ) {
105                 $submenu_file = 'edit.php?post_type=' . esc_attr( $post_type );
106                 $parent_file  = 'woocommerce';
107             }
108 
109             if ( 'product' == $post_type ) {
110                 $screen = get_current_screen();
111 
112                 if ( $screen->base == 'edit-tags' && taxonomy_is_product_attribute( $taxonomy ) ) {
113                     $submenu_file = 'product_attributes';
114                     $parent_file  = 'edit.php?post_type=' . esc_attr( $post_type );
115                 }
116             }
117         }
118 
119         if ( isset( $submenu['woocommerce'] ) && isset( $submenu['woocommerce'][1] ) ) {
120             $submenu['woocommerce'][0] = $submenu['woocommerce'][1];
121             unset( $submenu['woocommerce'][1] );
122         }
123 
124         // Sort out Orders menu when on the top level
125         if ( ! current_user_can( 'manage_woocommerce' ) ) {
126             foreach ( $menu as $key => $menu_item ) {
127                 if ( strpos( $menu_item[0], _x('Orders', 'Admin menu name', 'woocommerce') ) === 0 ) {
128 
129                     $menu_name = _x('Orders', 'Admin menu name', 'woocommerce');
130                     $menu_name_count = '';
131                     if ( $order_count = wc_processing_order_count() ) {
132                         $menu_name_count = " <span class='awaiting-mod update-plugins count-$order_count'><span class='processing-count'>" . number_format_i18n( $order_count ) . "</span></span>" ;
133                     }
134 
135                     $menu[$key][0] = $menu_name . $menu_name_count;
136                     $submenu['edit.php?post_type=shop_order'][5][0] = $menu_name;
137                     break;
138                 }
139             }
140         }
141     }
142 
143     /**
144      * Reorder the WC menu items in admin.
145      *
146      * @param mixed $menu_order
147      * @return array
148      */
149     public function menu_order( $menu_order ) {
150         // Initialize our custom order array
151         $woocommerce_menu_order = array();
152 
153         // Get the index of our custom separator
154         $woocommerce_separator = array_search( 'separator-woocommerce', $menu_order );
155 
156         // Get index of product menu
157         $woocommerce_product = array_search( 'edit.php?post_type=product', $menu_order );
158 
159         // Loop through menu order and do some rearranging
160         foreach ( $menu_order as $index => $item ) :
161 
162             if ( ( ( 'woocommerce' ) == $item ) ) :
163                 $woocommerce_menu_order[] = 'separator-woocommerce';
164                 $woocommerce_menu_order[] = $item;
165                 $woocommerce_menu_order[] = 'edit.php?post_type=product';
166                 unset( $menu_order[$woocommerce_separator] );
167                 unset( $menu_order[$woocommerce_product] );
168             elseif ( !in_array( $item, array( 'separator-woocommerce' ) ) ) :
169                 $woocommerce_menu_order[] = $item;
170             endif;
171 
172         endforeach;
173 
174         // Return order
175         return $woocommerce_menu_order;
176     }
177 
178     /**
179      * custom_menu_order
180      * @return bool
181      */
182     public function custom_menu_order() {
183         if ( ! current_user_can( 'manage_woocommerce' ) )
184             return false;
185         return true;
186     }
187 
188     /**
189      * Init the reports page
190      */
191     public function reports_page() {
192         include_once( 'class-wc-admin-reports.php' );
193         WC_Admin_Reports::output();
194     }
195 
196     /**
197      * Init the settings page
198      */
199     public function settings_page() {
200         include_once( 'class-wc-admin-settings.php' );
201         WC_Admin_Settings::output();
202     }
203 
204     /**
205      * Init the attributes page
206      */
207     public function attributes_page() {
208         $page = include( 'class-wc-admin-attributes.php' );
209         $page->output();
210     }
211 
212     /**
213      * Init the status page
214      */
215     public function status_page() {
216         $page = include( 'class-wc-admin-status.php' );
217         $page->output();
218     }
219 
220     /**
221      * Init the addons page
222      */
223     public function addons_page() {
224         $page = include( 'class-wc-admin-addons.php' );
225         $page->output();
226     }
227 }
228 
229 endif;
230 
231 return new WC_Admin_Menus();
WooCommerce API documentation generated by ApiGen 2.8.0