CoreMenu.php
5 years ago
Favorites.php
5 years ago
Init.php
5 years ago
Menu.php
5 years ago
Screen.php
5 years ago
CoreMenu.php
396 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Navigation Core Menu |
| 4 | * |
| 5 | * @package Woocommerce Admin |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\Features\Navigation; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\Features\Features; |
| 11 | use Automattic\WooCommerce\Admin\Features\Navigation\Menu; |
| 12 | use Automattic\WooCommerce\Admin\Features\Navigation\Screen; |
| 13 | |
| 14 | /** |
| 15 | * CoreMenu class. Handles registering Core menu items. |
| 16 | */ |
| 17 | class CoreMenu { |
| 18 | /** |
| 19 | * Class instance. |
| 20 | * |
| 21 | * @var Menu instance |
| 22 | */ |
| 23 | protected static $instance = null; |
| 24 | |
| 25 | /** |
| 26 | * Get class instance. |
| 27 | */ |
| 28 | final public static function instance() { |
| 29 | if ( ! static::$instance ) { |
| 30 | static::$instance = new static(); |
| 31 | } |
| 32 | return static::$instance; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Init. |
| 37 | */ |
| 38 | public function init() { |
| 39 | add_action( 'admin_menu', array( $this, 'register_post_types' ) ); |
| 40 | // Add this after we've finished migrating menu items to avoid hiding these items. |
| 41 | add_action( 'admin_menu', array( $this, 'add_dashboard_menu_items' ), PHP_INT_MAX ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Add registered admin settings as menu items. |
| 46 | */ |
| 47 | public static function get_setting_items() { |
| 48 | // Let the Settings feature add pages to the navigation if enabled. |
| 49 | if ( Features::is_enabled( 'settings' ) ) { |
| 50 | return array(); |
| 51 | } |
| 52 | |
| 53 | // Calling this method adds pages to the below tabs filter on non-settings pages. |
| 54 | \WC_Admin_Settings::get_settings_pages(); |
| 55 | $tabs = apply_filters( 'woocommerce_settings_tabs_array', array() ); |
| 56 | |
| 57 | $menu_items = array(); |
| 58 | $order = 0; |
| 59 | foreach ( $tabs as $key => $setting ) { |
| 60 | $order += 10; |
| 61 | $menu_items[] = ( |
| 62 | array( |
| 63 | 'parent' => 'woocommerce-settings', |
| 64 | 'title' => $setting, |
| 65 | 'capability' => 'manage_woocommerce', |
| 66 | 'id' => 'settings-' . $key, |
| 67 | 'url' => 'admin.php?page=wc-settings&tab=' . $key, |
| 68 | 'order' => $order, |
| 69 | ) |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | return $menu_items; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get all menu categories. |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public static function get_categories() { |
| 82 | return array( |
| 83 | array( |
| 84 | 'title' => __( 'Orders', 'woocommerce' ), |
| 85 | 'id' => 'woocommerce-orders', |
| 86 | 'order' => 10, |
| 87 | ), |
| 88 | array( |
| 89 | 'title' => __( 'Products', 'woocommerce' ), |
| 90 | 'id' => 'woocommerce-products', |
| 91 | 'order' => 20, |
| 92 | ), |
| 93 | array( |
| 94 | 'title' => __( 'Analytics', 'woocommerce' ), |
| 95 | 'id' => 'woocommerce-analytics', |
| 96 | 'order' => 30, |
| 97 | ), |
| 98 | array( |
| 99 | 'title' => __( 'Reports', 'woocommerce' ), |
| 100 | 'id' => 'woocommerce-reports', |
| 101 | 'parent' => 'woocommerce-analytics', |
| 102 | 'order' => 200, |
| 103 | ), |
| 104 | array( |
| 105 | 'title' => __( 'Marketing', 'woocommerce' ), |
| 106 | 'id' => 'woocommerce-marketing', |
| 107 | 'order' => 40, |
| 108 | ), |
| 109 | array( |
| 110 | 'title' => __( 'Settings', 'woocommerce' ), |
| 111 | 'id' => 'woocommerce-settings', |
| 112 | 'menuId' => 'secondary', |
| 113 | 'order' => 20, |
| 114 | 'url' => 'admin.php?page=wc-settings', |
| 115 | ), |
| 116 | array( |
| 117 | 'title' => __( 'Tools', 'woocommerce' ), |
| 118 | 'id' => 'woocommerce-tools', |
| 119 | 'menuId' => 'secondary', |
| 120 | 'order' => 30, |
| 121 | ), |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get all menu items. |
| 127 | * |
| 128 | * @return array |
| 129 | */ |
| 130 | public static function get_items() { |
| 131 | $order_items = Menu::get_post_type_items( 'shop_order', array( 'parent' => 'woocommerce-orders' ) ); |
| 132 | $product_items = Menu::get_post_type_items( 'product', array( 'parent' => 'woocommerce-products' ) ); |
| 133 | $product_tag_items = Menu::get_taxonomy_items( |
| 134 | 'product_tag', |
| 135 | array( |
| 136 | 'parent' => 'woocommerce-products', |
| 137 | 'order' => 30, |
| 138 | ) |
| 139 | ); |
| 140 | $product_cat_items = Menu::get_taxonomy_items( |
| 141 | 'product_cat', |
| 142 | array( |
| 143 | 'parent' => 'woocommerce-products', |
| 144 | 'order' => 20, |
| 145 | ) |
| 146 | ); |
| 147 | |
| 148 | $coupon_items = Menu::get_post_type_items( 'shop_coupon', array( 'parent' => 'woocommerce-marketing' ) ); |
| 149 | $setting_items = self::get_setting_items(); |
| 150 | $wca_items = array(); |
| 151 | $wca_pages = \Automattic\WooCommerce\Admin\PageController::get_instance()->get_pages(); |
| 152 | |
| 153 | foreach ( $wca_pages as $page ) { |
| 154 | if ( ! isset( $page['nav_args'] ) ) { |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | $path = isset( $page['path'] ) ? $page['path'] : null; |
| 159 | $item = array_merge( |
| 160 | array( |
| 161 | 'id' => $page['id'], |
| 162 | 'url' => $path, |
| 163 | 'title' => $page['title'][0], |
| 164 | 'capability' => isset( $page['capability'] ) ? $page['capability'] : 'manage_woocommerce', |
| 165 | ), |
| 166 | $page['nav_args'] |
| 167 | ); |
| 168 | |
| 169 | // Don't allow top-level items to be added to the primary menu. |
| 170 | if ( ! isset( $item['parent'] ) || 'woocommerce' === $item['parent'] ) { |
| 171 | $item['menuId'] = 'plugins'; |
| 172 | } |
| 173 | |
| 174 | $wca_items[] = $item; |
| 175 | } |
| 176 | |
| 177 | $home_item = array(); |
| 178 | if ( defined( '\Automattic\WooCommerce\Admin\Features\AnalyticsDashboard::MENU_SLUG' ) ) { |
| 179 | $home_item = array( |
| 180 | 'id' => 'woocommerce-home', |
| 181 | 'title' => __( 'Home', 'woocommerce' ), |
| 182 | 'url' => \Automattic\WooCommerce\Admin\Features\AnalyticsDashboard::MENU_SLUG, |
| 183 | 'order' => 0, |
| 184 | 'matchExpression' => 'page=wc-admin((?!path=).)*$', |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | $customers_item = array(); |
| 189 | if ( class_exists( '\Automattic\WooCommerce\Admin\Features\Analytics' ) ) { |
| 190 | $customers_item = array( |
| 191 | 'id' => 'woocommerce-analytics-customers', |
| 192 | 'title' => __( 'Customers', 'woocommerce' ), |
| 193 | 'url' => 'wc-admin&path=/customers', |
| 194 | 'order' => 50, |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | return array_merge( |
| 199 | array( |
| 200 | $home_item, |
| 201 | $customers_item, |
| 202 | $order_items['all'], |
| 203 | $order_items['new'], |
| 204 | $product_items['all'], |
| 205 | $product_cat_items['default'], |
| 206 | $product_tag_items['default'], |
| 207 | array( |
| 208 | 'id' => 'woocommerce-product-attributes', |
| 209 | 'title' => __( 'Attributes', 'woocommerce' ), |
| 210 | 'url' => 'edit.php?post_type=product&page=product_attributes', |
| 211 | 'capability' => 'manage_product_terms', |
| 212 | 'order' => 40, |
| 213 | 'parent' => 'woocommerce-products', |
| 214 | 'matchExpression' => 'edit.php(?=.*[?|&]page=product_attributes(&|$|#))|edit-tags.php(?=.*[?|&]taxonomy=pa_)(?=.*[?|&]post_type=product(&|$|#))', |
| 215 | ), |
| 216 | array_merge( $product_items['new'], array( 'order' => 50 ) ), |
| 217 | $coupon_items['default'], |
| 218 | // Marketplace category. |
| 219 | array( |
| 220 | 'title' => __( 'Marketplace', 'woocommerce' ), |
| 221 | 'capability' => 'manage_woocommerce', |
| 222 | 'id' => 'woocommerce-marketplace', |
| 223 | 'url' => 'wc-addons', |
| 224 | 'menuId' => 'secondary', |
| 225 | 'order' => 10, |
| 226 | ), |
| 227 | ), |
| 228 | // Tools category. |
| 229 | self::get_tool_items(), |
| 230 | // WooCommerce Admin items. |
| 231 | $wca_items, |
| 232 | // Settings category. |
| 233 | $setting_items, |
| 234 | // Legacy report items. |
| 235 | self::get_legacy_report_items() |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get items for tools category. |
| 241 | * |
| 242 | * @returna array |
| 243 | */ |
| 244 | public static function get_tool_items() { |
| 245 | $tabs = array( |
| 246 | 'status' => __( 'System status', 'woocommerce' ), |
| 247 | 'tools' => __( 'Utilities', 'woocommerce' ), |
| 248 | 'logs' => __( 'Logs', 'woocommerce' ), |
| 249 | ); |
| 250 | $tabs = apply_filters( 'woocommerce_admin_status_tabs', $tabs ); |
| 251 | |
| 252 | $order = 1; |
| 253 | $items = array( |
| 254 | array( |
| 255 | 'parent' => 'woocommerce-tools', |
| 256 | 'title' => __( 'Import / Export', 'woocommerce' ), |
| 257 | 'capability' => 'import', |
| 258 | 'id' => 'tools-import-export', |
| 259 | 'url' => 'import.php', |
| 260 | 'migrate' => false, |
| 261 | 'order' => 0, |
| 262 | ), |
| 263 | ); |
| 264 | |
| 265 | foreach ( $tabs as $key => $tab ) { |
| 266 | $items[] = array( |
| 267 | 'parent' => 'woocommerce-tools', |
| 268 | 'title' => $tab, |
| 269 | 'capability' => 'manage_woocommerce', |
| 270 | 'id' => 'tools-' . $key, |
| 271 | 'url' => 'wc-status&tab=' . $key, |
| 272 | 'order' => $order, |
| 273 | ); |
| 274 | $order++; |
| 275 | } |
| 276 | |
| 277 | return $items; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Get legacy report items. |
| 282 | * |
| 283 | * @return array |
| 284 | */ |
| 285 | public static function get_legacy_report_items() { |
| 286 | $reports = \WC_Admin_Reports::get_reports(); |
| 287 | $menu_items = array(); |
| 288 | |
| 289 | $order = 0; |
| 290 | foreach ( $reports as $key => $report ) { |
| 291 | $menu_items[] = array( |
| 292 | 'parent' => 'woocommerce-reports', |
| 293 | 'title' => $report['title'], |
| 294 | 'capability' => 'view_woocommerce_reports', |
| 295 | 'id' => $key, |
| 296 | 'url' => 'wc-reports&tab=' . $key, |
| 297 | 'order' => $order, |
| 298 | ); |
| 299 | $order++; |
| 300 | } |
| 301 | |
| 302 | return $menu_items; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Register all core post types. |
| 307 | */ |
| 308 | public function register_post_types() { |
| 309 | Screen::register_post_type( 'shop_order' ); |
| 310 | Screen::register_post_type( 'product' ); |
| 311 | Screen::register_post_type( 'shop_coupon' ); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Add the dashboard items to the WP menu to create a quick-access flyout menu. |
| 316 | */ |
| 317 | public function add_dashboard_menu_items() { |
| 318 | global $submenu, $menu; |
| 319 | $mapped_items = Menu::get_mapped_menu_items(); |
| 320 | $top_level = $mapped_items['woocommerce']; |
| 321 | |
| 322 | // phpcs:disable |
| 323 | if ( ! isset( $submenu['woocommerce'] ) || empty( $top_level ) ) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | $menuIds = array( |
| 328 | 'primary', |
| 329 | 'secondary', |
| 330 | 'favorites', |
| 331 | ); |
| 332 | |
| 333 | foreach ( $menuIds as $menuId ) { |
| 334 | foreach( $top_level[ $menuId ] as $item ) { |
| 335 | // Skip specific categories. |
| 336 | if ( |
| 337 | in_array( |
| 338 | $item['id'], |
| 339 | array( |
| 340 | 'woocommerce-tools', |
| 341 | ), |
| 342 | true |
| 343 | ) |
| 344 | ) { |
| 345 | continue; |
| 346 | } |
| 347 | |
| 348 | // Use the link from the first item if it's a category. |
| 349 | if ( ! isset( $item['url'] ) ) { |
| 350 | $categoryMenuId = $menuId === 'favorites' ? 'plugins' : $menuId; |
| 351 | $category_items = $mapped_items[ $item['id'] ][ $categoryMenuId ]; |
| 352 | |
| 353 | if ( ! empty( $category_items ) ) { |
| 354 | $first_item = $category_items[0]; |
| 355 | |
| 356 | |
| 357 | $submenu['woocommerce'][] = array( |
| 358 | $item['title'], |
| 359 | $first_item['capability'], |
| 360 | isset( $first_item['url'] ) ? $first_item['url'] : null, |
| 361 | $item['title'], |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | // Show top-level items. |
| 369 | $submenu['woocommerce'][] = array( |
| 370 | $item['title'], |
| 371 | $item['capability'], |
| 372 | isset( $item['url'] ) ? $item['url'] : null, |
| 373 | $item['title'], |
| 374 | ); |
| 375 | } |
| 376 | } |
| 377 | // phpcs:enable |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Get items excluded from WooCommerce menu migration. |
| 382 | * |
| 383 | * @return array |
| 384 | */ |
| 385 | public static function get_excluded_items() { |
| 386 | $excluded_items = array( |
| 387 | 'woocommerce', |
| 388 | 'wc-reports', |
| 389 | 'wc-settings', |
| 390 | 'wc-status', |
| 391 | ); |
| 392 | |
| 393 | return apply_filters( 'woocommerce_navigation_core_excluded_items', $excluded_items ); |
| 394 | } |
| 395 | } |
| 396 |