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

List draft navigations in Browse mode Navigation section #51422

Merged
merged 9 commits into from
Jun 21, 2023
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',
];

scruffian marked this conversation as resolved.
Show resolved Hide resolved
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
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',
Comment on lines -47 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I much prefer the inlining here 👍

} );
}

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 = {
scruffian marked this conversation as resolved.
Show resolved Hide resolved
per_page: 100,
status: 'publish',
status: [ 'publish', 'draft' ],
Comment on lines -6 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to amend the preloading in https://github.com/WordPress/gutenberg/blob/trunk/lib/compat/wordpress-6.3/navigation-block-preloading.php

Also can we add a link to that file as a code comment so folks are aware of this in future?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change might mean we can actually only have a single preloaded request which would be excellent. Needs to be carefully tested though.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I'm happy with this duplication. However let's track it in #51455

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