Skip to content
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

Avoid declaring a function inside another function #49049

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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