Completed
Pull Request — master (#9826)
by Mike
10:01
created

WC_Admin_Menus::shipping_menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

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