From 5b800e4d089aeebdba0b1c450ae87a05a6d57dac Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 13 Mar 2023 15:42:20 -0700 Subject: [PATCH 1/2] Avoid declaring a function inside another function. --- lib/experimental/block-editor-settings.php | 48 +++++++++++----------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/experimental/block-editor-settings.php b/lib/experimental/block-editor-settings.php index 7888595eba0f40..cf2b7bd23e5be7 100644 --- a/lib/experimental/block-editor-settings.php +++ b/lib/experimental/block-editor-settings.php @@ -5,6 +5,30 @@ * @package gutenberg */ +/** + * Finds the first occurrence of a specific block in an array of blocks. + * + * @param string $block_name Name of the block to find. + * @param array $blocks Array of blocks. + * @return array Found block, or empty array if none found. + */ +function gutenberg_find_first_block( $block_name, $blocks ) { + foreach ( $blocks as $block ) { + if ( $block_name === $block['blockName'] ) { + return $block; + } + if ( ! empty( $block['innerBlocks'] ) ) { + $post_content = gutenberg_find_first_block( $block_name, $block['innerBlocks'] ); + + if ( ! empty( $post_content ) ) { + return $post_content; + } + } + } + + return array(); +} + /** * Adds styles and __experimentalFeatures to the block editor settings. * @@ -50,31 +74,9 @@ function gutenberg_get_block_editor_settings_experimental( $settings ) { $current_template = gutenberg_get_block_templates( array( 'slug__in' => array( $template_slug ) ) ); - /** - * Finds Post Content in an array of blocks - * - * @param array $blocks Array of blocks. - * - * @return array Post Content block. - */ - function get_post_content_block( $blocks ) { - foreach ( $blocks as $block ) { - if ( 'core/post-content' === $block['blockName'] ) { - return $block; - } - if ( ! empty( $block['innerBlocks'] ) ) { - $post_content = get_post_content_block( $block['innerBlocks'] ); - - if ( ! empty( $post_content ) ) { - return $post_content; - } - } - } - } - if ( ! empty( $current_template ) ) { $template_blocks = parse_blocks( $current_template[0]->content ); - $post_content_block = get_post_content_block( $template_blocks ); + $post_content_block = gutenberg_find_first_block( 'core/post-content', $template_blocks ); if ( ! empty( $post_content_block['attrs'] ) ) { $settings['postContentAttributes'] = $post_content_block['attrs']; From 876b58f83ba25ecbc144051e2a03e3c0684d93a9 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 14 Mar 2023 07:53:29 -0700 Subject: [PATCH 2/2] Update variable name. --- lib/experimental/block-editor-settings.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/experimental/block-editor-settings.php b/lib/experimental/block-editor-settings.php index cf2b7bd23e5be7..db7070dbd0b17b 100644 --- a/lib/experimental/block-editor-settings.php +++ b/lib/experimental/block-editor-settings.php @@ -18,10 +18,10 @@ function gutenberg_find_first_block( $block_name, $blocks ) { return $block; } if ( ! empty( $block['innerBlocks'] ) ) { - $post_content = gutenberg_find_first_block( $block_name, $block['innerBlocks'] ); + $found_block = gutenberg_find_first_block( $block_name, $block['innerBlocks'] ); - if ( ! empty( $post_content ) ) { - return $post_content; + if ( ! empty( $found_block ) ) { + return $found_block; } } }