From 9115e9dcf67c462d40ccc7f208bd6741f0b793e9 Mon Sep 17 00:00:00 2001 From: scruffian Date: Mon, 12 Jun 2023 15:53:28 +0100 Subject: [PATCH 1/9] Navigation: List draft navigations --- .../sidebar-navigation-screen-navigation-menus/constants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js index 740474597b3b2..e8a70137cc482 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js @@ -3,7 +3,7 @@ // on apiFetch this query is limited to 100. export const PRELOADED_NAVIGATION_MENUS_QUERY = { per_page: 100, - status: 'publish', + status: [ 'publish', 'draft' ], order: 'desc', orderby: 'date', }; From 3999707fa21a2c705b2b1a40d09cb633dbb540e3 Mon Sep 17 00:00:00 2001 From: scruffian Date: Tue, 13 Jun 2023 10:10:13 +0100 Subject: [PATCH 2/9] Add a draft status to the navigation name --- .../index.js | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js index b3fae528aaed2..2234f1e1bbdee 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { __ } from '@wordpress/i18n'; +import { __, sprintf } from '@wordpress/i18n'; import { useEntityRecords } from '@wordpress/core-data'; import { decodeEntities } from '@wordpress/html-entities'; @@ -21,6 +21,25 @@ import { useLink } from '../routes/link'; import SingleNavigationMenu from '../sidebar-navigation-screen-navigation-menu/single-navigation-menu'; import useNavigationMenuHandlers from '../sidebar-navigation-screen-navigation-menu/use-navigation-menu-handlers'; +// Copied from packages/block-library/src/navigation/edit/navigation-menu-selector.js. +function buildMenuLabel( title, id, status ) { + if ( ! title?.rendered ) { + /* translators: %s is the index of the menu in the list of menus. */ + return sprintf( __( '(no title %s)' ), id ); + } + + if ( status === 'publish' ) { + return decodeEntities( title?.rendered ); + } + + return sprintf( + // translators: %1s: title of the menu; %2s: status of the menu (draft, pending, etc.). + __( '%1$s (%2$s)' ), + decodeEntities( title?.rendered ), + status + ); +} + export default function SidebarNavigationScreenNavigationMenus() { const { records: navigationMenus, isResolving: isLoading } = useEntityRecords( @@ -67,16 +86,14 @@ export default function SidebarNavigationScreenNavigationMenus() { return ( - { navigationMenus?.map( ( navMenu ) => ( + { navigationMenus?.map( ( { id, title, status }, index ) => ( - { decodeEntities( - navMenu.title?.rendered || navMenu.slug - ) } + { buildMenuLabel( title, index + 1, status ) } ) ) } From edf7cf36c95cadcf843f9ffb24ff70e719fe2949 Mon Sep 17 00:00:00 2001 From: scruffian Date: Tue, 13 Jun 2023 16:56:05 +0100 Subject: [PATCH 3/9] update the endpoints to make them more similar --- lib/compat/wordpress-6.3/navigation-block-preloading.php | 5 ++++- .../block-library/src/navigation/use-navigation-menu.js | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index 3d910e789a299..8e92849ef015d 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -33,6 +33,8 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { array( 'context' => 'edit', 'per_page' => 100, + 'order' => 'desc', + 'orderby' => 'date', '_locale' => 'user', // array indices are required to avoid query being encoded and not matching in cache. 'status[0]' => 'publish', @@ -51,7 +53,8 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { 'per_page' => 100, 'order' => 'desc', 'orderby' => 'date', - 'status' => 'publish', + 'status[0]' => 'publish', + 'status[1]' => 'draft', ), $navigation_rest_route ), diff --git a/packages/block-library/src/navigation/use-navigation-menu.js b/packages/block-library/src/navigation/use-navigation-menu.js index 607a92cb82f95..2c88d98b2968a 100644 --- a/packages/block-library/src/navigation/use-navigation-menu.js +++ b/packages/block-library/src/navigation/use-navigation-menu.js @@ -71,7 +71,12 @@ function selectNavigationMenus( select ) { const args = [ 'postType', 'wp_navigation', - { per_page: -1, status: [ 'publish', 'draft' ] }, + { + per_page: -1, + status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', + }, ]; return { navigationMenus: getEntityRecords( ...args ), From 0cd9180606c89d12ee11d204bee7022a95b599d2 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 13 Jun 2023 17:41:40 +0100 Subject: [PATCH 4/9] Update packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js Co-authored-by: Dave Smith --- .../sidebar-navigation-screen-navigation-menus/constants.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js index e8a70137cc482..ab2e6e9cefa33 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js @@ -1,6 +1,8 @@ // This requested is preloaded in `gutenberg_preload_navigation_posts`. // As unbounded queries are limited to 100 by `fetchAllMiddleware` // on apiFetch this query is limited to 100. +// These parameters must be kept aligned with those in +// lib/compat/wordpress-6.3/navigation-block-preloading.php export const PRELOADED_NAVIGATION_MENUS_QUERY = { per_page: 100, status: [ 'publish', 'draft' ], From a872e9cbb625e4d81e5b8b184d77ce94cfad2cb2 Mon Sep 17 00:00:00 2001 From: scruffian Date: Fri, 16 Jun 2023 16:40:56 +0100 Subject: [PATCH 5/9] pacify PHPCS --- .../wordpress-6.3/navigation-block-preloading.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index 8e92849ef015d..0def6f2c7c791 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -33,8 +33,8 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { array( 'context' => 'edit', 'per_page' => 100, - 'order' => 'desc', - 'orderby' => 'date', + 'order' => 'desc', + 'orderby' => 'date', '_locale' => 'user', // array indices are required to avoid query being encoded and not matching in cache. 'status[0]' => 'publish', @@ -49,10 +49,10 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { $preload_paths[] = array( add_query_arg( array( - 'context' => 'edit', - 'per_page' => 100, - 'order' => 'desc', - 'orderby' => 'date', + 'context' => 'edit', + 'per_page' => 100, + 'order' => 'desc', + 'orderby' => 'date', 'status[0]' => 'publish', 'status[1]' => 'draft', ), From df77360efad5b05284711e2220eaf08849224833 Mon Sep 17 00:00:00 2001 From: scruffian Date: Tue, 20 Jun 2023 11:13:46 +0100 Subject: [PATCH 6/9] update the entity to check --- .../src/navigation/edit/unsaved-inner-blocks.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/edit/unsaved-inner-blocks.js b/packages/block-library/src/navigation/edit/unsaved-inner-blocks.js index 51a8d2aed7fe2..bc095ba477924 100644 --- a/packages/block-library/src/navigation/edit/unsaved-inner-blocks.js +++ b/packages/block-library/src/navigation/edit/unsaved-inner-blocks.js @@ -85,7 +85,12 @@ export default function UnsavedInnerBlocks( { [ 'postType', 'wp_navigation', - { per_page: -1, status: [ 'publish', 'draft' ] }, + { + per_page: -1, + status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', + }, ] ), }; From a73cbf524af2e0d9e007702cfd175ac64b970bce Mon Sep 17 00:00:00 2001 From: scruffian Date: Tue, 20 Jun 2023 11:24:29 +0100 Subject: [PATCH 7/9] move the query config to a const --- .../block-library/src/navigation/constants.js | 11 +++++++++ .../navigation/edit/unsaved-inner-blocks.js | 17 +++++-------- .../src/navigation/use-navigation-menu.js | 24 +++++++++---------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/packages/block-library/src/navigation/constants.js b/packages/block-library/src/navigation/constants.js index 9bdf8736d7c2b..3248f01ef7427 100644 --- a/packages/block-library/src/navigation/constants.js +++ b/packages/block-library/src/navigation/constants.js @@ -19,3 +19,14 @@ export const PRIORITIZED_INSERTER_BLOCKS = [ 'core/navigation-link/page', 'core/navigation-link', ]; + +export const SELECT_NAVIGATION_MENUS_ARGS = [ + 'postType', + 'wp_navigation', + { + per_page: -1, + status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', + }, +]; diff --git a/packages/block-library/src/navigation/edit/unsaved-inner-blocks.js b/packages/block-library/src/navigation/edit/unsaved-inner-blocks.js index bc095ba477924..52f6d8134042d 100644 --- a/packages/block-library/src/navigation/edit/unsaved-inner-blocks.js +++ b/packages/block-library/src/navigation/edit/unsaved-inner-blocks.js @@ -11,7 +11,11 @@ import { useContext, useEffect, useRef, useMemo } from '@wordpress/element'; * Internal dependencies */ import { areBlocksDirty } from './are-blocks-dirty'; -import { DEFAULT_BLOCK, ALLOWED_BLOCKS } from '../constants'; +import { + DEFAULT_BLOCK, + ALLOWED_BLOCKS, + SELECT_NAVIGATION_MENUS_ARGS, +} from '../constants'; const EMPTY_OBJECT = {}; @@ -82,16 +86,7 @@ export default function UnsavedInnerBlocks( { isSaving: isSavingEntityRecord( 'postType', 'wp_navigation' ), hasResolvedAllNavigationMenus: hasFinishedResolution( 'getEntityRecords', - [ - 'postType', - 'wp_navigation', - { - per_page: -1, - status: [ 'publish', 'draft' ], - order: 'desc', - orderby: 'date', - }, - ] + SELECT_NAVIGATION_MENUS_ARGS ), }; }, diff --git a/packages/block-library/src/navigation/use-navigation-menu.js b/packages/block-library/src/navigation/use-navigation-menu.js index 2c88d98b2968a..30e5cdd99c092 100644 --- a/packages/block-library/src/navigation/use-navigation-menu.js +++ b/packages/block-library/src/navigation/use-navigation-menu.js @@ -7,6 +7,11 @@ import { } from '@wordpress/core-data'; import { useSelect } from '@wordpress/data'; +/** + * Internal dependencies + */ +import { SELECT_NAVIGATION_MENUS_ARGS } from './constants'; + export default function useNavigationMenu( ref ) { const permissions = useResourcePermissions( 'navigation', ref ); @@ -68,22 +73,15 @@ function selectNavigationMenus( select ) { const { getEntityRecords, hasFinishedResolution, isResolving } = select( coreStore ); - const args = [ - 'postType', - 'wp_navigation', - { - per_page: -1, - status: [ 'publish', 'draft' ], - order: 'desc', - orderby: 'date', - }, - ]; return { - navigationMenus: getEntityRecords( ...args ), - isResolvingNavigationMenus: isResolving( 'getEntityRecords', args ), + navigationMenus: getEntityRecords( ...SELECT_NAVIGATION_MENUS_ARGS ), + isResolvingNavigationMenus: isResolving( + 'getEntityRecords', + SELECT_NAVIGATION_MENUS_ARGS + ), hasResolvedNavigationMenus: hasFinishedResolution( 'getEntityRecords', - args + SELECT_NAVIGATION_MENUS_ARGS ), }; } From 0265bc0db0de768cf51ae9664901d02369403ba0 Mon Sep 17 00:00:00 2001 From: scruffian Date: Tue, 20 Jun 2023 15:08:52 +0100 Subject: [PATCH 8/9] update tests --- .../block-library/src/navigation/constants.js | 2 +- .../navigation/test/use-navigation-menu.js | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/block-library/src/navigation/constants.js b/packages/block-library/src/navigation/constants.js index 3248f01ef7427..5e6b26ae40b0f 100644 --- a/packages/block-library/src/navigation/constants.js +++ b/packages/block-library/src/navigation/constants.js @@ -24,7 +24,7 @@ export const SELECT_NAVIGATION_MENUS_ARGS = [ 'postType', 'wp_navigation', { - per_page: -1, + per_page: 100, status: [ 'publish', 'draft' ], order: 'desc', orderby: 'date', diff --git a/packages/block-library/src/navigation/test/use-navigation-menu.js b/packages/block-library/src/navigation/test/use-navigation-menu.js index b03776b663eaf..3c29479efbc3f 100644 --- a/packages/block-library/src/navigation/test/use-navigation-menu.js +++ b/packages/block-library/src/navigation/test/use-navigation-menu.js @@ -8,6 +8,7 @@ import { store as coreStore } from '@wordpress/core-data'; * Internal dependencies */ import useNavigationMenu from '../use-navigation-menu'; +import { SELECT_NAVIGATION_MENUS_ARGS } from '../constants'; function createRegistryWithStores() { // Create a registry and register used stores. @@ -33,19 +34,19 @@ jest.mock( '@wordpress/data/src/components/use-select', () => { function resolveRecords( registry, menus ) { const dispatch = registry.dispatch( coreStore ); - dispatch.startResolution( 'getEntityRecords', [ - 'postType', - 'wp_navigation', - { per_page: -1, status: [ 'publish', 'draft' ] }, - ] ); - dispatch.finishResolution( 'getEntityRecords', [ - 'postType', - 'wp_navigation', - { per_page: -1, status: [ 'publish', 'draft' ] }, - ] ); + dispatch.startResolution( + 'getEntityRecords', + SELECT_NAVIGATION_MENUS_ARGS + ); + dispatch.finishResolution( + 'getEntityRecords', + SELECT_NAVIGATION_MENUS_ARGS + ); dispatch.receiveEntityRecords( 'postType', 'wp_navigation', menus, { - per_page: -1, + per_page: 100, status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', } ); } From 0f358b320024e3f802040d5bf31118b65d932c5b Mon Sep 17 00:00:00 2001 From: scruffian Date: Wed, 21 Jun 2023 11:03:44 +0100 Subject: [PATCH 9/9] remove the abstraction for tests --- .../navigation/test/use-navigation-menu.js | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/navigation/test/use-navigation-menu.js b/packages/block-library/src/navigation/test/use-navigation-menu.js index 3c29479efbc3f..f82acca835884 100644 --- a/packages/block-library/src/navigation/test/use-navigation-menu.js +++ b/packages/block-library/src/navigation/test/use-navigation-menu.js @@ -8,7 +8,6 @@ import { store as coreStore } from '@wordpress/core-data'; * Internal dependencies */ import useNavigationMenu from '../use-navigation-menu'; -import { SELECT_NAVIGATION_MENUS_ARGS } from '../constants'; function createRegistryWithStores() { // Create a registry and register used stores. @@ -34,14 +33,26 @@ jest.mock( '@wordpress/data/src/components/use-select', () => { function resolveRecords( registry, menus ) { const dispatch = registry.dispatch( coreStore ); - dispatch.startResolution( - 'getEntityRecords', - SELECT_NAVIGATION_MENUS_ARGS - ); - dispatch.finishResolution( - 'getEntityRecords', - SELECT_NAVIGATION_MENUS_ARGS - ); + dispatch.startResolution( 'getEntityRecords', [ + 'postType', + 'wp_navigation', + { + per_page: 100, + status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', + }, + ] ); + dispatch.finishResolution( 'getEntityRecords', [ + 'postType', + 'wp_navigation', + { + per_page: 100, + status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', + }, + ] ); dispatch.receiveEntityRecords( 'postType', 'wp_navigation', menus, { per_page: 100, status: [ 'publish', 'draft' ],