From 4acfb3a6c5f79bd0b0c393190717eff4a340c91d Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Thu, 1 Dec 2022 16:41:01 -0500 Subject: [PATCH] Navigation block: add location->primary to fallback nav creation for classic menus. (#45976) * Add location primary to classic menu fallback creation. * Fix accidental line removal. * Request and export entities for menuLocations. * Check for primary menu during classic menu conversion. * Make linter happy. * Check for primary menu location OR slug. * Remove ununused hooks. * Fix php lint errors. * Look for menu with slug named primary. --- .../src/navigation/edit/index.js | 38 ++++++++++++++++--- .../block-library/src/navigation/index.php | 24 ++++++++++-- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index 0a24063716fe0a..ddc42d9d99d95e 100644 --- a/packages/block-library/src/navigation/edit/index.js +++ b/packages/block-library/src/navigation/edit/index.js @@ -284,18 +284,44 @@ function Navigation( { ! hasResolvedNavigationMenus || isConvertingClassicMenu || fallbackNavigationMenus?.length > 0 || - classicMenus?.length !== 1 + ! classicMenus?.length ) { return; } // If there's non fallback navigation menus and - // only one classic menu then create a new navigation menu based on it. - convertClassicMenu( - classicMenus[ 0 ].id, - classicMenus[ 0 ].name, - 'publish' + // a classic menu with a `primary` location or slug, + // then create a new navigation menu based on it. + // Otherwise, use the first classic menu. + const primaryMenus = classicMenus.filter( + ( classicMenu ) => + classicMenu.locations.includes( 'primary' ) || + classicMenu.slug === 'primary' ); + + if ( primaryMenus.length ) { + // Sort by location to allow the menu assigned to primary location to take precedence. + primaryMenus.sort( ( a, b ) => { + if ( a.locations < b.locations ) { + return 1; + } + if ( a.locations > b.locations ) { + return -1; + } + return 0; + } ); + convertClassicMenu( + primaryMenus[ 0 ].id, + primaryMenus[ 0 ].name, + 'publish' + ); + } else { + convertClassicMenu( + classicMenus[ 0 ].id, + classicMenus[ 0 ].name, + 'publish' + ); + } }, [ hasResolvedNavigationMenus ] ); const navRef = useRef(); diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 87ea817270a391..34c4769c3ab9f6 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -257,10 +257,26 @@ function block_core_navigation_get_classic_menu_fallback() { $classic_nav_menus = wp_get_nav_menus(); // If menus exist. - if ( $classic_nav_menus && ! is_wp_error( $classic_nav_menus ) && count( $classic_nav_menus ) === 1 ) { - // Use the first classic menu only. Handles simple use case where user has a single - // classic menu and switches to a block theme. In future this maybe expanded to - // determine the most appropriate classic menu to be used based on location. + if ( $classic_nav_menus && ! is_wp_error( $classic_nav_menus ) ) { + // Handles simple use case where user has a classic menu and switches to a block theme. + + // Returns the menu assigned to location `primary`. + $locations = get_nav_menu_locations(); + if ( isset( $locations['primary'] ) ) { + $primary_menu = wp_get_nav_menu_object( $locations['primary'] ); + if ( $primary_menu ) { + return $primary_menu; + } + } + + // Returns a menu if `primary` is its slug. + foreach ( $classic_nav_menus as $classic_nav_menu ) { + if ( 'primary' === $classic_nav_menu->slug ) { + return $classic_nav_menu; + } + } + + // Otherwise return the most recently created classic menu. return $classic_nav_menus[0]; } }