Skip to content

Commit

Permalink
Implement recursion checking in core/block (WordPress#26923)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Nov 16, 2020
1 parent 13ab479 commit 3f67687
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
}

/**
Expand All @@ -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',
)
);
}
Expand Down

0 comments on commit 3f67687

Please sign in to comment.