From 59e7681207240f0ead29e56c0f5883f5587882a6 Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Thu, 15 Oct 2020 16:28:03 -0400 Subject: [PATCH] Fix up menu item migrations in WC navigation (https://github.com/woocommerce/woocommerce-admin/pull/5363) * Add URLs to categories to assist migrating items * Migrate items when URL matches callback * Check if nav is enabled before adding extra marketing menu * Unset URLs on categories after migration --- .../src/Features/Analytics.php | 24 +++---- .../src/Features/AnalyticsDashboard.php | 9 +-- .../src/Features/Marketing.php | 20 +++--- .../src/Features/Navigation/CoreMenu.php | 1 + .../src/Features/Navigation/Menu.php | 14 ++-- .../woocommerce-admin/src/PageController.php | 71 ++++++++++--------- 6 files changed, 77 insertions(+), 62 deletions(-) diff --git a/plugins/woocommerce-admin/src/Features/Analytics.php b/plugins/woocommerce-admin/src/Features/Analytics.php index b06615c4c5b..54fefb5b650 100644 --- a/plugins/woocommerce-admin/src/Features/Analytics.php +++ b/plugins/woocommerce-admin/src/Features/Analytics.php @@ -113,15 +113,14 @@ class Analytics { */ public function register_pages() { $overview_page = array( - 'id' => 'woocommerce-analytics', - 'title' => __( 'Analytics', 'woocommerce-admin' ), - 'path' => '/analytics/overview', - 'icon' => 'dashicons-chart-bar', - 'position' => 56, // After WooCommerce & Product menu items. + 'id' => 'woocommerce-analytics', + 'title' => __( 'Analytics', 'woocommerce-admin' ), + 'path' => '/analytics/overview', + 'icon' => 'dashicons-chart-bar', + 'position' => 56, // After WooCommerce & Product menu items. + 'is_category' => true, + 'is_top_level' => true, ); - if ( Loader::is_feature_enabled( 'navigation' ) ) { - unset( $overview_page['path'] ); - } $report_pages = array( $overview_page, @@ -186,10 +185,11 @@ class Analytics { 'path' => '/analytics/stock', ) : null, array( - 'id' => 'woocommerce-analytics-customers', - 'title' => __( 'Customers', 'woocommerce-admin' ), - 'parent' => 'woocommerce', - 'path' => '/customers', + 'id' => 'woocommerce-analytics-customers', + 'title' => __( 'Customers', 'woocommerce-admin' ), + 'parent' => 'woocommerce', + 'path' => '/customers', + 'is_top_level' => true, ), array( 'id' => 'woocommerce-analytics-settings', diff --git a/plugins/woocommerce-admin/src/Features/AnalyticsDashboard.php b/plugins/woocommerce-admin/src/Features/AnalyticsDashboard.php index 3203b1f2412..7b5f40d207f 100644 --- a/plugins/woocommerce-admin/src/Features/AnalyticsDashboard.php +++ b/plugins/woocommerce-admin/src/Features/AnalyticsDashboard.php @@ -83,10 +83,11 @@ class AnalyticsDashboard { public function register_page() { wc_admin_register_page( array( - 'id' => 'woocommerce-home', - 'title' => __( 'Home', 'woocommerce-admin' ), - 'parent' => 'woocommerce', - 'path' => self::MENU_SLUG, + 'id' => 'woocommerce-home', + 'title' => __( 'Home', 'woocommerce-admin' ), + 'parent' => 'woocommerce', + 'path' => self::MENU_SLUG, + 'is_top_level' => true, ) ); } diff --git a/plugins/woocommerce-admin/src/Features/Marketing.php b/plugins/woocommerce-admin/src/Features/Marketing.php index 2e95a1bd4ce..dd0bec30522 100644 --- a/plugins/woocommerce-admin/src/Features/Marketing.php +++ b/plugins/woocommerce-admin/src/Features/Marketing.php @@ -69,15 +69,17 @@ class Marketing { * Uses priority of 9 so other items can easily be added at the default priority (10). */ public function add_parent_menu_item() { - add_menu_page( - __( 'Marketing', 'woocommerce-admin' ), - __( 'Marketing', 'woocommerce-admin' ), - 'manage_woocommerce', - 'woocommerce-marketing', - null, - 'dashicons-megaphone', - 58 - ); + if ( ! Loader::is_feature_enabled( 'navigation' ) ) { + add_menu_page( + __( 'Marketing', 'woocommerce-admin' ), + __( 'Marketing', 'woocommerce-admin' ), + 'manage_woocommerce', + 'woocommerce-marketing', + null, + 'dashicons-megaphone', + 58 + ); + } PageController::get_instance()->connect_page( [ diff --git a/plugins/woocommerce-admin/src/Features/Navigation/CoreMenu.php b/plugins/woocommerce-admin/src/Features/Navigation/CoreMenu.php index 08e5d48611f..a0d25200932 100644 --- a/plugins/woocommerce-admin/src/Features/Navigation/CoreMenu.php +++ b/plugins/woocommerce-admin/src/Features/Navigation/CoreMenu.php @@ -129,6 +129,7 @@ class CoreMenu { 'id' => 'settings', 'menuId' => 'secondary', 'order' => 10, + 'url' => 'admin.php?page=wc-settings', 'is_top_level' => true, ) ); diff --git a/plugins/woocommerce-admin/src/Features/Navigation/Menu.php b/plugins/woocommerce-admin/src/Features/Navigation/Menu.php index ba6a0d9311e..f0f67d302aa 100644 --- a/plugins/woocommerce-admin/src/Features/Navigation/Menu.php +++ b/plugins/woocommerce-admin/src/Features/Navigation/Menu.php @@ -167,9 +167,7 @@ class Menu { ); $menu_item = wp_parse_args( $args, $defaults ); $menu_item['title'] = wp_strip_all_tags( wp_specialchars_decode( $menu_item['title'] ) ); - if ( isset( $menu_item['url'] ) ) { - $menu_item['url'] = self::get_callback_url( $menu_item['url'] ); - } + unset( $menu_item['url'] ); if ( true === $menu_item['is_top_level'] ) { $menu_item['parent'] = 'woocommerce'; @@ -297,8 +295,14 @@ class Menu { foreach ( $submenu as $parent_key => $parent ) { foreach ( $parent as $key => $menu_item ) { if ( - isset( self::$callbacks[ $menu_item[ self::CALLBACK ] ] ) && - self::$callbacks[ $menu_item[ self::CALLBACK ] ] + ( + isset( self::$callbacks[ $menu_item[ self::CALLBACK ] ] ) && + self::$callbacks[ $menu_item[ self::CALLBACK ] ] + ) || + ( + isset( self::$callbacks[ self::get_callback_url( $menu_item[ self::CALLBACK ] ) ] ) && + self::$callbacks[ self::get_callback_url( $menu_item[ self::CALLBACK ] ) ] + ) ) { // Disable phpcs since we need to override submenu classes. // Note that `phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited` does not work to disable this check. diff --git a/plugins/woocommerce-admin/src/PageController.php b/plugins/woocommerce-admin/src/PageController.php index 7f596e07c67..9e50233cf1f 100644 --- a/plugins/woocommerce-admin/src/PageController.php +++ b/plugins/woocommerce-admin/src/PageController.php @@ -429,8 +429,6 @@ class PageController { $options['path'] = self::PAGE_ROOT . '&path=' . $options['path']; } - $navigation_enabled = Loader::is_feature_enabled( 'navigation' ); - if ( is_null( $options['parent'] ) ) { add_menu_page( $options['title'], @@ -441,22 +439,6 @@ class PageController { $options['icon'], $options['position'] ); - - if ( $navigation_enabled ) { - $category_options = array( - 'id' => $options['id'], - 'title' => $options['title'], - 'capability' => $options['capability'], - 'url' => $options['path'], - 'is_top_level' => true, - ); - - // If there is no path option, remove url because its a parent category item. - if ( 'wc-admin&path=' === $options['path'] ) { - unset( $category_options['url'] ); - } - \Automattic\WooCommerce\Admin\Features\Navigation\Menu::add_category( $category_options ); - } } else { $parent_path = $this->get_path_from_id( $options['parent'] ); // @todo check for null path. @@ -468,25 +450,50 @@ class PageController { $options['path'], array( __CLASS__, 'page_wrapper' ) ); - - if ( $navigation_enabled ) { - $top_level_ids = array( 'woocommerce-home', 'woocommerce-analytics-customers' ); - \Automattic\WooCommerce\Admin\Features\Navigation\Menu::add_item( - array( - 'id' => $options['id'], - 'parent' => $options['parent'], - 'title' => $options['title'], - 'capability' => $options['capability'], - 'url' => $options['path'], - 'is_top_level' => in_array( $options['id'], $top_level_ids, true ), - ) - ); - } } + self::add_nav_item( $options ); $this->connect_page( $options ); } + /** + * Add the item to the WooCommerce Navigation menu. + * + * @param array $options { + * Array describing the page. + * + * @type string id Id to reference the page. + * @type string title Page title. Used in menus and breadcrumbs. + * @type string|null parent Parent ID. Null for new top level page. + * @type string path Path for this page, full path in app context; ex /analytics/report + * @type string capability Capability needed to access the page. + * @type string icon Icon. Dashicons helper class, base64-encoded SVG, or 'none'. + * @type int position Menu item position. + * } + */ + public static function add_nav_item( $options ) { + $navigation_enabled = Loader::is_feature_enabled( 'navigation' ); + + if ( ! $navigation_enabled ) { + return; + } + + $item_options = array( + 'id' => $options['id'], + 'parent' => $options['parent'], + 'title' => $options['title'], + 'capability' => $options['capability'], + 'url' => $options['path'], + 'is_top_level' => isset( $options['is_top_level'] ) && $options['is_top_level'], + ); + + if ( isset( $options['is_category'] ) && $options['is_category'] ) { + \Automattic\WooCommerce\Admin\Features\Navigation\Menu::add_category( $item_options ); + } else { + \Automattic\WooCommerce\Admin\Features\Navigation\Menu::add_item( $item_options ); + } + } + /** * Set up a div for the app to render into. */