-
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
Navigation Block: Fix warning error when no ref #47479
Conversation
@@ -643,11 +647,6 @@ function render_block_core_navigation( $attributes, $content, $block ) { | |||
$inner_blocks = new WP_Block_List( $fallback_blocks, $attributes ); | |||
} | |||
|
|||
$parsed_blocks = parse_blocks( $navigation_post->post_content ); | |||
if ( block_core_navigation_block_contains_core_navigation( $parsed_blocks ) ) { |
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.
Could the check be kept here, but changed to something like this?
if ( block_core_navigation_block_contains_core_navigation( $inner_blocks ) ) {
I think that would cover more cases (i.e. where the inner blocks are serialized instead of pulled from the wp_navigation post).
$inner_blocks
is a WP_Block_List
, but it implements Iterator
, so should still support loops. The values will be WP_Block
objects, so it'd have to internally check for $block->name
instead of $block['blockName']
.
I think it'd be good to also add extra safety to block_core_navigation_block_contains_core_navigation
, by having it return false
when the parsed_blocks
isn't set.
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.
$inner_blocks is a WP_Block_List, but it implements Iterator, so should still support loops. The values will be WP_Block objects, so it'd have to internally check for $block->name instead of $block['blockName'].
Makes Sense! I have responded with dec0f03, but I would like to fix the unit test now.
I think it'd be good to also add extra safety to block_core_navigation_block_contains_core_navigation, by having it return false when the parsed_blocks isn't set.
Do you mean to change it as follows?
if ( ! isset( $parsed_blocks ) || block_core_navigation_block_contains_core_navigation( $inner_blocks ) ) {
return '';
}
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.
Could this be solved simpler by moving this call up to be inside the ref exists condition ?
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.
Could this be solved simpler by moving this call up to be inside the ref exists condition ?
I tried that approach with my first commit. But, as a series of comments, it would be better not to limit it to only blocks with ref.
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.
Do you mean to change it as follows?
@t-hamano Something like this is what I had in mind:
function block_core_navigation_block_contains_core_navigation( $inner_blocks ) {
if ( ! isset( $inner_blocks ) ) {
return false;
}
foreach ( $inner_blocks as $block ) {
if ( 'core/navigation' === $block->name ) {
return true;
}
if ( block_core_navigation_block_contains_core_navigation( $block->inner_blocks ) ) {
return true;
}
}
return false;
}
Otherwise the foreach
causes an error if $inner_blocks
is null
.
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 have added an extra safety in 33780cf. This implementation is based on the post template.
I changed it to check when this function is called recursively in a foreach instead of at the beginning of the function. My understanding is that this problem can occur when the block_core_navigation_block_contains_core_navigation()
is called recursively.
Flaky tests detected in 33780cf. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4044553771
|
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.
Thanks for taking care of this for me. LGTM
Thanks for the review!
I have not yet been able to address this issue due to a lack of my understanding. If you have anything to add to this PR, I would be glad to hear your advice. |
I've left a comment in the thread above that should clear that up 👍 |
Related: #47453
What?
This PR fixes a problem that caused a warning error when rendering navigation blocks.
Why?
My understanding is that if any changes are made to the navigation block, the content is stored in the database and
ref
attribute is given. It then retrieves the post from the database based on this ref, stores it in the$navigation_post
variable, and checks whether the navigation block is nested based on its content.However, if the user has not made any changes to this block, the ref does not exist. As a result,
$navigation_post
is undefined in the current implementation and an error occurs.How?
I have moved the process of checking for nesting into an if statement that is only executed if the block has a ref.Used
$inner_blocks
instead of $navigation_post in theblock_core_navigation_block_contains_core_navigation
argument to prevent undefined errors. Theblock_core_navigation_get_menu_items_at_location
function has also been adjusted to reflect the change in argument types.Testing Instructions
http://localhost:8888/wp-admin/edit.php?post_type=wp_navigation
and remove all navigation.Regression Test
Please confirm that block_core_navigation_get_menu_items_at_location is working properly as before by following the steps below.
wp_navigation
posts and Classic Menus from your sitepost_content
of yourwp_navigation
post with the following content, being sure to replace%%REPLACE_ME%%
with the ID of thewp_navigation
post YOU just created: