Skip to content

Commit

Permalink
Recursively remove Navigation block’s from appearing inside Navigatio…
Browse files Browse the repository at this point in the history
…n block on front of site (#46279)

* Recursively remove Navigation block’s from appearing inside Navigation block content

* Fix lint

* Use whitelist approach

* add a dupliation warning comment

* Update packages/block-library/src/navigation/index.php

* pacify linter

Co-authored-by: Ben Dwyer <ben@scruffian.com>
  • Loading branch information
getdave and scruffian authored Dec 2, 2022
1 parent f4dfa32 commit afc8b42
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/navigation/edit/inner-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useMemo } from '@wordpress/element';
*/
import PlaceholderPreview from './placeholder/placeholder-preview';

// This list is duplicated in packages/block-library/src/navigation/index.php
const ALLOWED_BLOCKS = [
'core/navigation-link',
'core/search',
Expand Down
48 changes: 37 additions & 11 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,20 +384,45 @@ 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 not whitelisted.
* This list of exclusions includes:
* - The Navigation block itself (results in recursion).
* - empty "null" blocks from the block list.
* - other blocks that are not yet handled.
*
* 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 ) {
// This list is duplicated in /packages/block-library/src/navigation/edit/inner-blocks.js.
$allowed_blocks = array(
'core/navigation-link',
'core/search',
'core/social-links',
'core/page-list',
'core/spacer',
'core/home-link',
'core/site-title',
'core/site-logo',
'core/navigation-submenu',
);

$filtered = array_reduce(
$parsed_blocks,
function( $block ) {
return isset( $block['blockName'] );
}
function( $carry, $block ) use ( $allowed_blocks ) {
if ( isset( $block['blockName'] ) && in_array( $block['blockName'], $allowed_blocks, true ) ) {
if ( $block['innerBlocks'] ) {
$block['innerBlocks'] = block_core_navigation_filter_out_invalid_blocks( $block['innerBlocks'] );
}
$carry[] = $block;
}
return $carry;
},
array()
);

// Reset keys.
Expand Down Expand Up @@ -437,7 +462,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.
Expand Down Expand Up @@ -595,7 +621,7 @@ 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.
$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.
Expand Down

0 comments on commit afc8b42

Please sign in to comment.