From 17f68445a4abb2e397a67acb56d27842dd21aab8 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 3 Aug 2022 15:48:23 +0100 Subject: [PATCH 1/4] Check post is available before asserting on permission status --- packages/block-library/src/navigation/edit/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index eda2a2b7967dfd..53520c2443078f 100644 --- a/packages/block-library/src/navigation/edit/index.js +++ b/packages/block-library/src/navigation/edit/index.js @@ -207,6 +207,9 @@ function Navigation( { hasResolvedCanUserCreateNavigationMenu, } = useNavigationMenu( ref ); + const navMenuResolvedButMissing = + hasResolvedNavigationMenus && isNavigationMenuMissing; + // Attempt to retrieve and prioritize any existing navigation menu unless // a specific ref is allocated or the user is explicitly creating a new menu. The aim is // for the block to "just work" from a user perspective using existing data. @@ -386,6 +389,7 @@ function Navigation( { if ( isSelected || isInnerBlockSelected ) { if ( ref && + ! navMenuResolvedButMissing && hasResolvedCanUserUpdateNavigationMenu && ! canUserUpdateNavigationMenu ) { From 29681823a2790d2ecf2a1a67d8465c6b715de377 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 4 Aug 2022 14:48:22 +0100 Subject: [PATCH 2/4] Ignore Navigation posts that are trashed --- .../src/navigation/use-navigation-menu.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/navigation/use-navigation-menu.js b/packages/block-library/src/navigation/use-navigation-menu.js index 11ac415d18bd92..97a64a0733febc 100644 --- a/packages/block-library/src/navigation/use-navigation-menu.js +++ b/packages/block-library/src/navigation/use-navigation-menu.js @@ -99,15 +99,17 @@ function selectExistingMenu( select, ref ) { args ); + // Trashed Navigation posts are considered invalid. + const isNavigationMenuPublished = editedNavigationMenu.status === 'publish'; + return { isNavigationMenuResolved: hasResolvedNavigationMenu, - isNavigationMenuMissing: hasResolvedNavigationMenu && ! navigationMenu, + isNavigationMenuMissing: + hasResolvedNavigationMenu && + ( ! navigationMenu || ! isNavigationMenuPublished ), // getEditedEntityRecord will return the post regardless of status. // Therefore if the found post is not published then we should ignore it. - navigationMenu: - editedNavigationMenu.status === 'publish' - ? editedNavigationMenu - : null, + navigationMenu: isNavigationMenuPublished ? editedNavigationMenu : null, }; } From ee375776210332a4033cd0bbc7c70ebb32172c3c Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 5 Aug 2022 16:17:37 +0100 Subject: [PATCH 3/4] Ignore trashed posts on the front of the site --- packages/block-library/src/navigation/index.php | 6 ++++++ .../block-library/src/navigation/use-navigation-menu.js | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 39f6def58aca60..b47aa7d44fc922 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -472,6 +472,12 @@ function render_block_core_navigation( $attributes, $content, $block ) { return ''; } + // Only published posts are valid. If this is changed then a corresponding change + // must also be implemented in `use-navigation-menu.js` + if( 'publish' !== $navigation_post->post_status ) { + return ''; + } + $nav_menu_name = $navigation_post->post_title; if ( isset( $seen_menu_names[ $nav_menu_name ] ) ) { diff --git a/packages/block-library/src/navigation/use-navigation-menu.js b/packages/block-library/src/navigation/use-navigation-menu.js index 97a64a0733febc..e6ccad1d017c89 100644 --- a/packages/block-library/src/navigation/use-navigation-menu.js +++ b/packages/block-library/src/navigation/use-navigation-menu.js @@ -99,7 +99,9 @@ function selectExistingMenu( select, ref ) { args ); - // Trashed Navigation posts are considered invalid. + // Only published Navigation posts are considered valid. + // If this is changed then a corresponding change must also be made + // in the index.php file. const isNavigationMenuPublished = editedNavigationMenu.status === 'publish'; return { From 5a762794f07167a6c681796049ec944720a34799 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Mon, 8 Aug 2022 12:55:16 +0100 Subject: [PATCH 4/4] linting fixes --- packages/block-library/src/navigation/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index b47aa7d44fc922..8dbda39b5bdde2 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -473,8 +473,8 @@ function render_block_core_navigation( $attributes, $content, $block ) { } // Only published posts are valid. If this is changed then a corresponding change - // must also be implemented in `use-navigation-menu.js` - if( 'publish' !== $navigation_post->post_status ) { + // must also be implemented in `use-navigation-menu.js`. + if ( 'publish' !== $navigation_post->post_status ) { return ''; }