diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index 3d910e789a299..0def6f2c7c791 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', @@ -47,11 +49,12 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { $preload_paths[] = array( add_query_arg( array( - 'context' => 'edit', - 'per_page' => 100, - 'order' => 'desc', - 'orderby' => 'date', - 'status' => 'publish', + 'context' => 'edit', + 'per_page' => 100, + 'order' => 'desc', + 'orderby' => 'date', + 'status[0]' => 'publish', + 'status[1]' => 'draft', ), $navigation_rest_route ), diff --git a/packages/block-library/src/navigation/constants.js b/packages/block-library/src/navigation/constants.js index 9bdf8736d7c2b..5e6b26ae40b0f 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: 100, + 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 51a8d2aed7fe2..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,11 +86,7 @@ export default function UnsavedInnerBlocks( { isSaving: isSavingEntityRecord( 'postType', 'wp_navigation' ), hasResolvedAllNavigationMenus: hasFinishedResolution( 'getEntityRecords', - [ - 'postType', - 'wp_navigation', - { per_page: -1, status: [ 'publish', 'draft' ] }, - ] + SELECT_NAVIGATION_MENUS_ARGS ), }; }, 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..f82acca835884 100644 --- a/packages/block-library/src/navigation/test/use-navigation-menu.js +++ b/packages/block-library/src/navigation/test/use-navigation-menu.js @@ -36,16 +36,28 @@ function resolveRecords( registry, menus ) { dispatch.startResolution( 'getEntityRecords', [ 'postType', 'wp_navigation', - { per_page: -1, status: [ 'publish', 'draft' ] }, + { + per_page: 100, + status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', + }, ] ); dispatch.finishResolution( 'getEntityRecords', [ 'postType', 'wp_navigation', - { per_page: -1, status: [ 'publish', 'draft' ] }, + { + per_page: 100, + status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', + }, ] ); dispatch.receiveEntityRecords( 'postType', 'wp_navigation', menus, { - per_page: -1, + per_page: 100, status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', } ); } diff --git a/packages/block-library/src/navigation/use-navigation-menu.js b/packages/block-library/src/navigation/use-navigation-menu.js index 607a92cb82f95..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,17 +73,15 @@ function selectNavigationMenus( select ) { const { getEntityRecords, hasFinishedResolution, isResolving } = select( coreStore ); - const args = [ - 'postType', - 'wp_navigation', - { per_page: -1, status: [ 'publish', 'draft' ] }, - ]; 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 ), }; } 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..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,9 +1,11 @@ // 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', + status: [ 'publish', 'draft' ], order: 'desc', orderby: 'date', }; 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 ) } ) ) }