Skip to content

Commit

Permalink
Navigation block: add location->primary to fallback nav creation for …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
jffng committed Dec 1, 2022
1 parent 4b4a3f0 commit 4acfb3a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
38 changes: 32 additions & 6 deletions packages/block-library/src/navigation/edit/index.js
Expand Up @@ -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();
Expand Down
24 changes: 20 additions & 4 deletions packages/block-library/src/navigation/index.php
Expand Up @@ -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];
}
}
Expand Down

0 comments on commit 4acfb3a

Please sign in to comment.