diff --git a/plugins/woocommerce-admin/client/navigation/components/container/index.js b/plugins/woocommerce-admin/client/navigation/components/container/index.js index 35976515860..63f7b3ffb5b 100644 --- a/plugins/woocommerce-admin/client/navigation/components/container/index.js +++ b/plugins/woocommerce-admin/client/navigation/components/container/index.js @@ -47,8 +47,10 @@ const Container = ( { menuItems } ) => { useEffect( () => { const initialMatchedItem = getMatchingItem( menuItems ); - setActiveItem( initialMatchedItem ); - setActiveLevel( initialMatchedItem.parent ); + if ( initialMatchedItem ) { + setActiveItem( initialMatchedItem ); + setActiveLevel( initialMatchedItem.parent ); + } const removeListener = addHistoryListener( () => { setTimeout( () => { diff --git a/plugins/woocommerce-admin/client/navigation/utils.js b/plugins/woocommerce-admin/client/navigation/utils.js index 7bba2ffce39..cf5d4da6bc4 100644 --- a/plugins/woocommerce-admin/client/navigation/utils.js +++ b/plugins/woocommerce-admin/client/navigation/utils.js @@ -69,9 +69,19 @@ export const getMatchScore = ( location, url ) => { } const urlParams = getParams( urlLocation ); - const locationParams = getParams( location ); - let matchingParamCount = 0; + // Post type match. + if ( + window.wcNavigation.postType === urlParams.post_type && + urlPathname.indexOf( 'edit.php' ) >= 0 && + origin === urlOrigin + ) { + return Number.MAX_SAFE_INTEGER - 2; + } + + // Add points for each matching param. + let matchingParamCount = 0; + const locationParams = getParams( location ); Object.keys( urlParams ).forEach( ( key ) => { if ( urlParams[ key ] === locationParams[ key ] ) { matchingParamCount++; @@ -139,11 +149,11 @@ export const getMatchingItem = ( items ) => { window.location, getAdminLink( item.url ) ); - if ( score >= highestMatch ) { + if ( score >= highestMatch && score > 0 ) { matchedItem = item; highestMatch = score; } } ); - return matchedItem ? matchedItem : null; + return matchedItem || null; }; diff --git a/plugins/woocommerce-admin/src/Features/Navigation/Menu.php b/plugins/woocommerce-admin/src/Features/Navigation/Menu.php index 312d8724545..9e1069cf33e 100644 --- a/plugins/woocommerce-admin/src/Features/Navigation/Menu.php +++ b/plugins/woocommerce-admin/src/Features/Navigation/Menu.php @@ -538,6 +538,7 @@ class Menu { $data = array( 'menuItems' => self::get_prepared_menu_item_data(), + 'postType' => isset( $_GET['post'] ) ? get_post_type( $_GET['post'] ) : null, ); $paul = wp_add_inline_script( WC_ADMIN_APP, 'window.wcNavigation = ' . wp_json_encode( $data ), 'before' );