-
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: Recursively remove Navigation block’s from appearing inside Navigation block on front of site #46279
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
761553b
Recursively remove Navigation block’s from appearing inside Navigatio…
getdave 818d173
Fix lint
getdave 3340b65
Use whitelist approach
getdave 0bbffc9
add a dupliation warning comment
scruffian 4ecc4a3
Update packages/block-library/src/navigation/index.php
scruffian 662c0ac
pacify linter
scruffian 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 |
---|---|---|
|
@@ -384,20 +384,31 @@ function block_core_navigation_get_most_recently_published_navigation() { | |
} | ||
|
||
/** | ||
* Filter out empty "null" blocks from the block list. | ||
* 'parse_blocks' includes a null block with '\n\n' as the content when | ||
* Recursively filter out blocks from the block list that are considered invalid inside Navigation. | ||
* This list includes: | ||
* - The Navigation block itself (results in recursion). | ||
* - empty "null" blocks from the block list. | ||
* | ||
* Note: 'parse_blocks' includes a null block with '\n\n' as the content when | ||
* it encounters whitespace. This is not a bug but rather how the parser | ||
* is designed. | ||
* | ||
* @param array $parsed_blocks the parsed blocks to be normalized. | ||
* @return array the normalized parsed blocks. | ||
* @param array $parsed_blocks the parsed blocks to be filtered. | ||
* @return array the filtered parsed blocks. | ||
*/ | ||
function block_core_navigation_filter_out_empty_blocks( $parsed_blocks ) { | ||
$filtered = array_filter( | ||
function block_core_navigation_filter_out_invalid_blocks( $parsed_blocks ) { | ||
$filtered = array_reduce( | ||
$parsed_blocks, | ||
function( $block ) { | ||
return isset( $block['blockName'] ); | ||
} | ||
function( $carry, $block ) { | ||
if ( isset( $block['blockName'] ) && 'core/navigation' !== $block['blockName'] ) { | ||
if ( $block['innerBlocks'] ) { | ||
$block['innerBlocks'] = block_core_navigation_filter_out_invalid_blocks( $block['innerBlocks'] ); | ||
} | ||
$carry[] = $block; | ||
} | ||
return $carry; | ||
}, | ||
array() | ||
); | ||
|
||
// Reset keys. | ||
|
@@ -437,7 +448,8 @@ function block_core_navigation_get_fallback_blocks() { | |
|
||
// Use the first non-empty Navigation as fallback if available. | ||
if ( $navigation_post ) { | ||
$maybe_fallback = block_core_navigation_filter_out_empty_blocks( parse_blocks( $navigation_post->post_content ) ); | ||
|
||
$maybe_fallback = block_core_navigation_filter_out_invalid_blocks( parse_blocks( $navigation_post->post_content ) ); | ||
|
||
// Normalizing blocks may result in an empty array of blocks if they were all `null` blocks. | ||
// In this case default to the (Page List) fallback. | ||
|
@@ -595,7 +607,8 @@ function render_block_core_navigation( $attributes, $content, $block ) { | |
|
||
// 'parse_blocks' includes a null block with '\n\n' as the content when | ||
// it encounters whitespace. This code strips it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should update this comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually this comment is fine, now I read it more carefully. |
||
$compacted_blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); | ||
$compacted_blocks = block_core_navigation_filter_out_invalid_blocks( $parsed_blocks ); | ||
|
||
|
||
// TODO - this uses the full navigation block attributes for the | ||
// context which could be refined. | ||
|
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.
Should we use an array of allowedBlocks here? There's this array.
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.
Updated...but is there any danger of breaking sites that might utilise 3rd party blocks here?