From 84222b568b0fca847cd501952cea567cb3859243 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Wed, 27 Jan 2021 13:05:55 +1300 Subject: [PATCH] Navigation: Change default location and add Product category items (https://github.com/woocommerce/woocommerce-admin/pull/6179) * add product submenu items * better check * extesnions section * use add_plugin_item * htmlspecialchars_decode better * Manually add product_brand taxonomy * Determine if migrated items are a taxonomy or post_type. If they are, register them. * add in_array check to register post types and taxonomies * register a taxonomy or post_type, but not both --- plugins/woocommerce-admin/package-lock.json | 6 ++++ .../src/Features/Navigation/CoreMenu.php | 9 ++--- .../src/Features/Navigation/Menu.php | 34 ++++++++++++++++--- .../src/Features/Navigation/Screen.php | 8 +++-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/plugins/woocommerce-admin/package-lock.json b/plugins/woocommerce-admin/package-lock.json index a1a7cb30e53..08c563581f4 100644 --- a/plugins/woocommerce-admin/package-lock.json +++ b/plugins/woocommerce-admin/package-lock.json @@ -48,6 +48,7 @@ "config": "^3.2.4", "eslint": "6.7.2", "jest": "^24.9.0", + "prettier": "npm:wp-prettier@1.19.1", "puppeteer": "^2.0.0" }, "dependencies": { @@ -103,6 +104,11 @@ } } } + }, + "prettier": { + "version": "npm:wp-prettier@1.19.1", + "resolved": "https://registry.npmjs.org/wp-prettier/-/wp-prettier-1.19.1.tgz", + "integrity": "sha512-mqAC2r1NDmRjG+z3KCJ/i61tycKlmADIjxnDhQab+KBxSAGbF/W7/zwB2guy/ypIeKrrftNsIYkNZZQKf3vJcg==" } } }, diff --git a/plugins/woocommerce-admin/src/Features/Navigation/CoreMenu.php b/plugins/woocommerce-admin/src/Features/Navigation/CoreMenu.php index e1cd5f45cf5..83e451cf96b 100644 --- a/plugins/woocommerce-admin/src/Features/Navigation/CoreMenu.php +++ b/plugins/woocommerce-admin/src/Features/Navigation/CoreMenu.php @@ -134,10 +134,11 @@ class CoreMenu { 'order' => 20, ) ); - $coupon_items = Menu::get_post_type_items( 'shop_coupon', array( 'parent' => 'woocommerce-marketing' ) ); - $setting_items = self::get_setting_items(); - $wca_items = array(); - $wca_pages = \Automattic\WooCommerce\Admin\PageController::get_instance()->get_pages(); + + $coupon_items = Menu::get_post_type_items( 'shop_coupon', array( 'parent' => 'woocommerce-marketing' ) ); + $setting_items = self::get_setting_items(); + $wca_items = array(); + $wca_pages = \Automattic\WooCommerce\Admin\PageController::get_instance()->get_pages(); foreach ( $wca_pages as $page ) { if ( ! isset( $page['nav_args'] ) ) { diff --git a/plugins/woocommerce-admin/src/Features/Navigation/Menu.php b/plugins/woocommerce-admin/src/Features/Navigation/Menu.php index 4a072bb743f..d4bc9b93723 100644 --- a/plugins/woocommerce-admin/src/Features/Navigation/Menu.php +++ b/plugins/woocommerce-admin/src/Features/Navigation/Menu.php @@ -490,11 +490,16 @@ class Menu { public function migrate_core_child_items( $menu ) { global $submenu; - if ( ! isset( $submenu['woocommerce'] ) ) { + if ( ! isset( $submenu['woocommerce'] ) && ! isset( $submenu['edit.php?post_type=product'] ) ) { return; } - foreach ( $submenu['woocommerce'] as $key => $menu_item ) { + $submenu_items = array_merge( + isset( $submenu['woocommerce'] ) ? $submenu['woocommerce'] : array(), + isset( $submenu['edit.php?post_type=product'] ) ? $submenu['edit.php?post_type=product'] : array() + ); + + foreach ( $submenu_items as $key => $menu_item ) { if ( in_array( $menu_item[2], CoreMenu::get_excluded_items(), true ) ) { // phpcs:disable if ( ! isset( $menu_item[ self::CSS_CLASSES ] ) ) { @@ -506,21 +511,42 @@ class Menu { continue; } + $menu_item[2] = htmlspecialchars_decode( $menu_item[2] ); + // Don't add already added items. $callbacks = self::get_callbacks(); if ( array_key_exists( $menu_item[2], $callbacks ) ) { continue; } - self::add_item( + // Don't add these Product submenus because they are added elsewhere. + if ( in_array( $menu_item[2], array( 'product_importer', 'product_exporter', 'product_attributes' ), true ) ) { + continue; + } + + self::add_plugin_item( array( - 'parent' => 'woocommerce-settings', 'title' => $menu_item[0], 'capability' => $menu_item[1], 'id' => sanitize_title( $menu_item[0] ), 'url' => $menu_item[2], ) ); + + // Determine if migrated items are a taxonomy or post_type. If they are, register them. + $parsed_url = wp_parse_url( $menu_item[2] ); + $query_string = isset( $parsed_url['query'] ) ? $parsed_url['query'] : false; + + if ( $query_string ) { + $query = array(); + parse_str( $query_string, $query ); + + if ( isset( $query['taxonomy'] ) ) { + Screen::register_taxonomy( $query['taxonomy'] ); + } elseif ( isset( $query['post_type'] ) ) { + Screen::register_post_type( $query['post_type'] ); + } + } } return $menu; diff --git a/plugins/woocommerce-admin/src/Features/Navigation/Screen.php b/plugins/woocommerce-admin/src/Features/Navigation/Screen.php index 442244836b2..81bad50603b 100644 --- a/plugins/woocommerce-admin/src/Features/Navigation/Screen.php +++ b/plugins/woocommerce-admin/src/Features/Navigation/Screen.php @@ -213,7 +213,9 @@ class Screen { * @param string $post_type Post type to add. */ public static function register_post_type( $post_type ) { - self::$post_types[] = $post_type; + if ( ! in_array( $post_type, self::$post_types, true ) ) { + self::$post_types[] = $post_type; + } } /** @@ -222,6 +224,8 @@ class Screen { * @param string $taxonomy Taxonomy to add. */ public static function register_taxonomy( $taxonomy ) { - self::$taxonomies[] = $taxonomy; + if ( ! in_array( $taxonomy, self::$taxonomies, true ) ) { + self::$taxonomies[] = $taxonomy; + } } }