Skip to content

Commit

Permalink
List draft navigations in Browse mode Navigation section (WordPress#5…
Browse files Browse the repository at this point in the history
…1422)

* Navigation: List draft navigations

* Add a draft status to the navigation name

* update the endpoints to make them more similar

* Update packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js

Co-authored-by: Dave Smith <[email protected]>

* pacify PHPCS

* update the entity to check

* move the query config to a const

* update tests

* remove the abstraction for tests

---------

Co-authored-by: Dave Smith <[email protected]>
  • Loading branch information
2 people authored and sethrubenstein committed Jul 13, 2023
1 parent b39831f commit efcf08d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 30 deletions.
13 changes: 8 additions & 5 deletions lib/compat/wordpress-6.3/navigation-block-preloading.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
),
Expand Down
11 changes: 11 additions & 0 deletions packages/block-library/src/navigation/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down Expand Up @@ -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
),
};
},
Expand Down
18 changes: 15 additions & 3 deletions packages/block-library/src/navigation/test/use-navigation-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
} );
}

Expand Down
19 changes: 11 additions & 8 deletions packages/block-library/src/navigation/use-navigation-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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
),
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
};
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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(
Expand Down Expand Up @@ -67,16 +86,14 @@ export default function SidebarNavigationScreenNavigationMenus() {
return (
<SidebarNavigationScreenWrapper>
<ItemGroup>
{ navigationMenus?.map( ( navMenu ) => (
{ navigationMenus?.map( ( { id, title, status }, index ) => (
<NavMenuItem
postId={ navMenu.id }
key={ navMenu.id }
postId={ id }
key={ id }
withChevron
icon={ navigation }
>
{ decodeEntities(
navMenu.title?.rendered || navMenu.slug
) }
{ buildMenuLabel( title, index + 1, status ) }
</NavMenuItem>
) ) }
</ItemGroup>
Expand Down

0 comments on commit efcf08d

Please sign in to comment.