-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fetch Nav fallbacks and side load resulting entity into state #50126
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f1b6f49
Update Core Data with new selector
getdave fb0b26a
Update Nav block editor code to utilise Core Data
getdave 1a27981
Include required fields in embedded Nav post response
getdave e244e83
Remove redundant commented-out code
getdave fa1a898
Document selector
getdave 8cda2e6
Rename everything to be ID based
getdave 6331052
Renaming and types
getdave 9815f61
Update docs
getdave df602ce
Refactor further towards storing Id only in state
getdave 8f17d92
Fix PHPCS
getdave 3da6b43
Move fallback fetch to hook in block library
getdave d2d3233
Remove Core Data work
getdave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Navigation Fallback | ||
* | ||
* Functions required for managing Navigation fallbacks behavior. | ||
* | ||
* @package Gutenberg | ||
* @since 6.3.0 | ||
*/ | ||
|
||
/** | ||
* Expose additional fields in the embeddable links of the | ||
* Navigation Fallback REST endpoint. | ||
* | ||
* The endpoint may embed the full Navigation Menu object into the | ||
* response as the `self` link. By default the Posts Controller | ||
* will only exposes a limited subset of fields but the editor requires | ||
* additional fields to be available in order to utilise the menu. | ||
* | ||
* @param array $schema the schema for the `wp_navigation` post. | ||
* @return array the modified schema. | ||
*/ | ||
function gutenberg_add_fields_to_navigation_fallback_embeded_links( $schema ) { | ||
// Expose top level fields. | ||
$schema['properties']['status']['context'] = array_merge( $schema['properties']['status']['context'], array( 'embed' ) ); | ||
$schema['properties']['content']['context'] = array_merge( $schema['properties']['content']['context'], array( 'embed' ) ); | ||
|
||
// Expose sub properties of content field. | ||
$schema['properties']['content']['properties']['raw']['context'] = array_merge( $schema['properties']['content']['properties']['raw']['context'], array( 'embed' ) ); | ||
$schema['properties']['content']['properties']['rendered']['context'] = array_merge( $schema['properties']['content']['properties']['rendered']['context'], array( 'embed' ) ); | ||
$schema['properties']['content']['properties']['block_version']['context'] = array_merge( $schema['properties']['content']['properties']['block_version']['context'], array( 'embed' ) ); | ||
|
||
return $schema; | ||
} | ||
|
||
add_filter( | ||
'rest_wp_navigation_item_schema', | ||
'gutenberg_add_fields_to_navigation_fallback_embeded_links' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/block-library/src/navigation/edit/use-fallback.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import { useRef } from '@wordpress/element'; | ||
|
||
export default function useFallback() { | ||
const isFetching = useRef( false ); | ||
const { receiveEntityRecords } = useDispatch( coreStore ); | ||
|
||
return async function () { | ||
if ( isFetching?.current ) { | ||
return; | ||
} | ||
|
||
isFetching.current = true; | ||
|
||
const fallback = await apiFetch( { | ||
path: addQueryArgs( '/wp-block-editor/v1/navigation-fallback', { | ||
_embed: true, | ||
} ), | ||
} ); | ||
|
||
const record = fallback?._embedded?.self; | ||
|
||
if ( record ) { | ||
receiveEntityRecords( 'postType', 'wp_navigation', record ); | ||
} | ||
|
||
isFetching.current = false; | ||
|
||
return fallback?.id; | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a
debugger
here after thereceiveEntityRecords
call and then switched toconsole
and ran:...and the record is in state.
So why - with this non-Core data approach - do we get a 2nd network request coming from:
gutenberg/packages/block-library/src/navigation/use-navigation-menu.js
Line 98 in fedc841
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider calling