Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation block: add location->primary to fallback nav creation for classic menus. #45976

Merged
merged 9 commits into from
Dec 1, 2022
38 changes: 32 additions & 6 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
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
jffng marked this conversation as resolved.
Show resolved Hide resolved
// 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'
);
getdave marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
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'] );
getdave marked this conversation as resolved.
Show resolved Hide resolved
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 ) {
getdave marked this conversation as resolved.
Show resolved Hide resolved
return $classic_nav_menu;
}
}

// Otherwise return the most recently created classic menu.
return $classic_nav_menus[0];
}
}
Expand Down