Skip to content

Commit

Permalink
Avoid declaring a function inside another function (#49049)
Browse files Browse the repository at this point in the history
* Avoid declaring a function inside another function.

* Update variable name.
  • Loading branch information
felixarntz authored Mar 15, 2023
1 parent 0bb7a9c commit 2ad87aa
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions lib/experimental/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
$found_block = gutenberg_find_first_block( $block_name, $block['innerBlocks'] );

if ( ! empty( $found_block ) ) {
return $found_block;
}
}
}

return array();
}

/**
* Adds styles and __experimentalFeatures to the block editor settings.
*
Expand Down Expand Up @@ -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'];
Expand Down

1 comment on commit 2ad87aa

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 2ad87aa.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4427667460
📝 Reported issues:

Please sign in to comment.