Completed
Push — master ( 543fc3...aa0078 )
by Mike
18:04
created

WC_Admin_Menus::attributes_page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Setup menus in WP admin.
4
 *
5
 * @author   WooThemes
6
 * @category Admin
7
 * @package  WooCommerce/Admin
8
 * @version  2.5.0
9
 */
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
if ( ! class_exists( 'WC_Admin_Menus' ) ) :
16
17
/**
18
 * WC_Admin_Menus Class.
19
 */
20
class WC_Admin_Menus {
21
22
	/**
23
	 * Hook in tabs.
24
	 */
25
	public function __construct() {
26
		// Add menus
27
		add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 );
28
		add_action( 'admin_menu', array( $this, 'reports_menu' ), 20 );
29
		add_action( 'admin_menu', array( $this, 'settings_menu' ), 50 );
30
		add_action( 'admin_menu', array( $this, 'status_menu' ), 60 );
31
32
		if ( apply_filters( 'woocommerce_show_addons_page', true ) ) {
33
			add_action( 'admin_menu', array( $this, 'addons_menu' ), 70 );
34
		}
35
36
		add_action( 'admin_head', array( $this, 'menu_highlight' ) );
37
		add_action( 'admin_head', array( $this, 'menu_order_count' ) );
38
		add_filter( 'menu_order', array( $this, 'menu_order' ) );
39
		add_filter( 'custom_menu_order', array( $this, 'custom_menu_order' ) );
40
41
		// Rename taxonomies at Appearance > Menus > Pages
42
		add_filter( 'nav_menu_meta_box_object', array( $this, 'rename_nav_menu_meta_boxes' ) );
43
44
		// Add endpoints custom URLs in Appearance > Menus > Pages
45
		add_action( 'admin_init', array( $this, 'add_nav_menu_meta_boxes' ) );
46
47
		// Admin bar menus
48
		if ( apply_filters( 'woocommerce_show_admin_bar_visit_store', true ) ) {
49
			add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 31 );
50
		}
51
	}
52
53
	/**
54
	 * Add menu items.
55
	 */
56
	public function admin_menu() {
57
		global $menu;
58
59
		if ( current_user_can( 'manage_woocommerce' ) ) {
60
			$menu[] = array( '', 'read', 'separator-woocommerce', '', 'wp-menu-separator woocommerce' );
61
		}
62
63
		add_menu_page( __( 'WooCommerce', 'woocommerce' ), __( 'WooCommerce', 'woocommerce' ), 'manage_woocommerce', 'woocommerce', null, null, '55.5' );
64
65
		add_submenu_page( 'edit.php?post_type=product', __( 'Attributes', 'woocommerce' ), __( 'Attributes', 'woocommerce' ), 'manage_product_terms', 'product_attributes', array( $this, 'attributes_page' ) );
66
	}
67
68
	/**
69
	 * Add menu item.
70
	 */
71
	public function reports_menu() {
72
		if ( current_user_can( 'manage_woocommerce' ) ) {
73
			add_submenu_page( 'woocommerce', __( 'Reports', 'woocommerce' ),  __( 'Reports', 'woocommerce' ) , 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ) );
74
		} else {
75
			add_menu_page( __( 'Sales Reports', 'woocommerce' ),  __( 'Sales Reports', 'woocommerce' ) , 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ), null, '55.6' );
76
		}
77
	}
78
79
	/**
80
	 * Add menu item.
81
	 */
82
	public function settings_menu() {
83
		$settings_page = add_submenu_page( 'woocommerce', __( 'WooCommerce Settings', 'woocommerce' ),  __( 'Settings', 'woocommerce' ) , 'manage_woocommerce', 'wc-settings', array( $this, 'settings_page' ) );
84
85
		add_action( 'load-' . $settings_page, array( $this, 'settings_page_init' ) );
86
	}
87
88
	/**
89
	 * Loads gateways and shipping methods into memory for use within settings.
90
	 */
91
	public function settings_page_init() {
92
		WC()->payment_gateways();
93
		WC()->shipping();
94
	}
95
96
	/**
97
	 * Add menu item.
98
	 */
99
	public function status_menu() {
100
		add_submenu_page( 'woocommerce', __( 'WooCommerce Status', 'woocommerce' ),  __( 'System Status', 'woocommerce' ) , 'manage_woocommerce', 'wc-status', array( $this, 'status_page' ) );
101
	}
102
103
	/**
104
	 * Addons menu item.
105
	 */
106
	public function addons_menu() {
107
		add_submenu_page( 'woocommerce', __( 'WooCommerce Add-ons/Extensions', 'woocommerce' ),  __( 'Add-ons', 'woocommerce' ) , 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) );
108
	}
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
116
		switch ( $post_type ) {
117
			case 'shop_order' :
118
			case 'shop_coupon' :
119
				$parent_file = 'woocommerce';
120
			break;
121
			case 'product' :
122
				$screen = get_current_screen();
123
				if ( $screen && taxonomy_is_product_attribute( $screen->taxonomy ) ) {
124
					$submenu_file = 'product_attributes';
125
					$parent_file  = 'edit.php?post_type=product';
126
				}
127
			break;
128
		}
129
	}
130
131
	/**
132
	 * Adds the order processing count to the menu.
133
	 */
134
	public function menu_order_count() {
135
		global $submenu;
136
137
		if ( isset( $submenu['woocommerce'] ) ) {
138
			// Remove 'WooCommerce' sub menu item
139
			unset( $submenu['woocommerce'][0] );
140
141
			// Add count if user has access
142
			if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'manage_woocommerce' ) && ( $order_count = wc_processing_order_count() ) ) {
143
				foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
144
					if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) {
145
						$submenu['woocommerce'][ $key ][0] .= ' <span class="awaiting-mod update-plugins count-' . $order_count . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>';
146
						break;
147
					}
148
				}
149
			}
150
		}
151
	}
152
153
	/**
154
	 * Reorder the WC menu items in admin.
155
	 *
156
	 * @param mixed $menu_order
157
	 * @return array
158
	 */
159
	public function menu_order( $menu_order ) {
160
		// Initialize our custom order array
161
		$woocommerce_menu_order = array();
162
163
		// Get the index of our custom separator
164
		$woocommerce_separator = array_search( 'separator-woocommerce', $menu_order );
165
166
		// Get index of product menu
167
		$woocommerce_product = array_search( 'edit.php?post_type=product', $menu_order );
168
169
		// Loop through menu order and do some rearranging
170
		foreach ( $menu_order as $index => $item ) {
171
172
			if ( ( ( 'woocommerce' ) == $item ) ) {
173
				$woocommerce_menu_order[] = 'separator-woocommerce';
174
				$woocommerce_menu_order[] = $item;
175
				$woocommerce_menu_order[] = 'edit.php?post_type=product';
176
				unset( $menu_order[ $woocommerce_separator ] );
177
				unset( $menu_order[ $woocommerce_product ] );
178
			} elseif ( ! in_array( $item, array( 'separator-woocommerce' ) ) ) {
179
				$woocommerce_menu_order[] = $item;
180
			}
181
182
		}
183
184
		// Return order
185
		return $woocommerce_menu_order;
186
	}
187
188
	/**
189
	 * Custom menu order.
190
	 *
191
	 * @return bool
192
	 */
193
	public function custom_menu_order() {
194
		return current_user_can( 'manage_woocommerce' );
195
	}
196
197
	/**
198
	 * Init the reports page.
199
	 */
200
	public function reports_page() {
201
		WC_Admin_Reports::output();
202
	}
203
204
	/**
205
	 * Init the settings page.
206
	 */
207
	public function settings_page() {
208
		WC_Admin_Settings::output();
209
	}
210
211
	/**
212
	 * Init the attributes page.
213
	 */
214
	public function attributes_page() {
215
		WC_Admin_Attributes::output();
216
	}
217
218
	/**
219
	 * Init the status page.
220
	 */
221
	public function status_page() {
222
		WC_Admin_Status::output();
223
	}
224
225
	/**
226
	 * Init the addons page.
227
	 */
228
	public function addons_page() {
229
		WC_Admin_Addons::output();
230
	}
231
232
	/**
233
	 * Rename taxonomies in admin menus meta boxes.
234
	 */
235
	public function rename_nav_menu_meta_boxes( $tax ) {
236
		if ( 'product_cat' === $tax->name ) {
237
			$tax->labels->name = __( 'Product Categories', 'woocommerce' );
238
		} elseif ( 'product_tag' === $tax->name ) {
239
			$tax->labels->name = __( 'Product Tags', 'woocommerce' );
240
		}
241
242
		return $tax;
243
	}
244
245
	/**
246
	 * Add custom nav meta box.
247
	 *
248
	 * Adapted from http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/.
249
	 */
250
	public function add_nav_menu_meta_boxes() {
251
		add_meta_box( 'woocommerce_endpoints_nav_link', __( 'WooCommerce Endpoints', 'woocommerce' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' );
252
	}
253
254
	/**
255
	 * Output menu links.
256
	 */
257
	public function nav_menu_links() {
258
		$exclude = array( 'view-order', 'add-payment-method', 'order-pay', 'order-received' );
259
		?>
260
		<div id="posttype-woocommerce-endpoints" class="posttypediv">
261
			<div id="tabs-panel-woocommerce-endpoints" class="tabs-panel tabs-panel-active">
262
				<ul id="woocommerce-endpoints-checklist" class="categorychecklist form-no-clear">
263
					<?php
264
					$i = -1;
265
					foreach ( WC()->query->query_vars as $key => $value ) {
266
						if ( in_array( $key, $exclude ) ) {
267
							continue;
268
						}
269
						?>
270
						<li>
271
							<label class="menu-item-title">
272
								<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 ); ?>
273
							</label>
274
							<input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-type]" value="custom" />
275
							<input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-title]" value="<?php echo esc_html( $key ); ?>" />
276
							<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' ) ) ); ?>" />
277
							<input type="hidden" class="menu-item-classes" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-classes]" />
278
						</li>
279
						<?php
280
						$i --;
281
					}
282
					?>
283
				</ul>
284
			</div>
285
			<p class="button-controls">
286
				<span class="list-controls">
287
					<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>
288
				</span>
289
				<span class="add-to-menu">
290
					<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">
291
					<span class="spinner"></span>
292
				</span>
293
			</p>
294
		</div>
295
		<?php
296
	}
297
298
	/**
299
	 * Add the "Visit Store" link in admin bar main menu.
300
	 *
301
	 * @since 2.4.0
302
	 * @param WP_Admin_Bar $wp_admin_bar
303
	 */
304
	public function admin_bar_menus( $wp_admin_bar ) {
305
		if ( ! is_admin() || ! is_user_logged_in() ) {
306
			return;
307
		}
308
309
		// Show only when the user is a member of this site, or they're a super admin.
310
		if ( ! is_user_member_of_blog() && ! is_super_admin() ) {
311
			return;
312
		}
313
314
		// Don't display when shop page is the same of the page on front.
315
		if ( get_option( 'page_on_front' ) == wc_get_page_id( 'shop' ) ) {
316
			return;
317
		}
318
319
		// Add an option to visit the store.
320
		$wp_admin_bar->add_node( array(
321
			'parent' => 'site-name',
322
			'id'     => 'view-store',
323
			'title'  => __( 'Visit Store', 'woocommerce' ),
324
			'href'   => wc_get_page_permalink( 'shop' )
325
		) );
326
	}
327
}
328
329
endif;
330
331
return new WC_Admin_Menus();
332