From 761553b5918e467d1cb206787651e731235b66df Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 2 Dec 2022 14:25:46 +0000 Subject: [PATCH 1/6] =?UTF-8?q?Recursively=20remove=20Navigation=20block?= =?UTF-8?q?=E2=80=99s=20from=20appearing=20inside=20Navigation=20block=20c?= =?UTF-8?q?ontent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../block-library/src/navigation/index.php | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 34c4769c3ab9f6..fd5e906d1886a0 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -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. - $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. From 818d173a266ac1d9e20e2a66902a114b85fb07ad Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 2 Dec 2022 15:06:40 +0000 Subject: [PATCH 2/6] Fix lint --- packages/block-library/src/navigation/index.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index fd5e906d1886a0..90f27b7aa44dab 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -609,7 +609,6 @@ function render_block_core_navigation( $attributes, $content, $block ) { // it encounters whitespace. This code strips it. $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. $inner_blocks = new WP_Block_List( $compacted_blocks, $attributes ); From 3340b65fac27b667d6b0c6c14ae18bc1137c95cb Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 2 Dec 2022 15:13:59 +0000 Subject: [PATCH 3/6] Use whitelist approach --- .../block-library/src/navigation/index.php | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 90f27b7aa44dab..da8cda50558656 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -384,10 +384,11 @@ function block_core_navigation_get_most_recently_published_navigation() { } /** - * Recursively filter out blocks from the block list that are considered invalid inside Navigation. - * This list includes: + * 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 @@ -397,10 +398,23 @@ function block_core_navigation_get_most_recently_published_navigation() { * @return array the filtered parsed blocks. */ function block_core_navigation_filter_out_invalid_blocks( $parsed_blocks ) { + + $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( $carry, $block ) { - if ( isset( $block['blockName'] ) && 'core/navigation' !== $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'] ); } From 0bbffc99de8bf14a6f3c5ba69979012be18b1d01 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 2 Dec 2022 15:39:20 +0000 Subject: [PATCH 4/6] add a dupliation warning comment --- packages/block-library/src/navigation/edit/inner-blocks.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/navigation/edit/inner-blocks.js b/packages/block-library/src/navigation/edit/inner-blocks.js index cb7f9a171f72ad..c06d1196e37ccf 100644 --- a/packages/block-library/src/navigation/edit/inner-blocks.js +++ b/packages/block-library/src/navigation/edit/inner-blocks.js @@ -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', From 4ecc4a37ca50050493ef5b87a0a89bc03db509d4 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 2 Dec 2022 15:39:53 +0000 Subject: [PATCH 5/6] Update packages/block-library/src/navigation/index.php --- packages/block-library/src/navigation/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index da8cda50558656..9d77e88a6f4fad 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -398,7 +398,7 @@ function block_core_navigation_get_most_recently_published_navigation() { * @return array the filtered parsed blocks. */ 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', From 662c0acf5a3709ad161d93bae79b8bf0d6770f85 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 2 Dec 2022 16:17:56 +0000 Subject: [PATCH 6/6] pacify linter --- packages/block-library/src/navigation/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 9d77e88a6f4fad..8cb9728fe37f28 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -398,7 +398,7 @@ function block_core_navigation_get_most_recently_published_navigation() { * @return array the filtered parsed blocks. */ function block_core_navigation_filter_out_invalid_blocks( $parsed_blocks ) { - // This list is duplicated in /packages/block-library/src/navigation/edit/inner-blocks.js + // This list is duplicated in /packages/block-library/src/navigation/edit/inner-blocks.js. $allowed_blocks = array( 'core/navigation-link', 'core/search',