From 3f67687060579bf47d6652deac738388c7dbea56 Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Mon, 16 Nov 2020 15:50:39 +0000 Subject: [PATCH] Implement recursion checking in core/block (#26923) --- packages/block-library/src/block/index.php | 32 +++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index 5fed48bd9a7c74..eba083666780f8 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -6,27 +6,45 @@ */ /** - * Renders the `core/block` block on server. + * Renders the `core/block` block on server if safe to do so. * * @param array $attributes The block attributes. + * @param string $content The block content. + * @param object $block The block being rendered. * * @return string Rendered HTML of the referenced block. */ -function render_block_core_block( $attributes ) { +function render_block_core_block_safely( $attributes, $content, $block ) { if ( empty( $attributes['ref'] ) ) { return ''; } + if ( gutenberg_process_this_content( $attributes['ref'], $block->name ) ) { + $html = render_block_core_block( $attributes ); + gutenberg_clear_processed_content(); + } else { + $html = gutenberg_report_recursion_error(); + } + return $html; +} - $reusable_block = get_post( $attributes['ref'] ); - if ( ! $reusable_block || 'wp_block' !== $reusable_block->post_type ) { +/** + * Renders the `core/block` block on server. + * + * @param array $attributes The block attributes. + * + * @return string Rendered HTML of the referenced block. + */ +function render_block_core_block( $attributes ) { + $reusable_block = get_post($attributes['ref']); + if (!$reusable_block || 'wp_block' !== $reusable_block->post_type) { return ''; } - if ( 'publish' !== $reusable_block->post_status || ! empty( $reusable_block->post_password ) ) { + if ('publish' !== $reusable_block->post_status || !empty($reusable_block->post_password)) { return ''; } - return do_blocks( $reusable_block->post_content ); + return( do_blocks($reusable_block->post_content) ); } /** @@ -36,7 +54,7 @@ function register_block_core_block() { register_block_type_from_metadata( __DIR__ . '/block', array( - 'render_callback' => 'render_block_core_block', + 'render_callback' => 'render_block_core_block_safely', ) ); }