From 2dc8f381ca7798d74f431cf276927a8c3fa77674 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Wed, 6 Oct 2021 18:49:01 +0000 Subject: [PATCH] Apply the `pre_render_block`, `render_block_data`, and `render_block_context` filters when rendering inner/nested blocks. Introdices another param to these filters: `$parent_block` that is the "parent" WP_Block instance for nested blocks and null for top level blocks. Adds unit tests for the filters. Props noisysocks, gaambo, azaozz. Fixes #51612. Built from https://develop.svn.wordpress.org/trunk@51894 git-svn-id: http://core.svn.wordpress.org/trunk@51487 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 22 +++++++++++-------- wp-includes/class-wp-block.php | 40 +++++++++++++++++++++++++++++++--- wp-includes/version.php | 2 +- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index a7919090cf..0ab0e7ca6a 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -813,16 +813,18 @@ function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) { */ function render_block( $parsed_block ) { global $post; + $parent_block = null; /** * Allows render_block() to be short-circuited, by returning a non-null value. * * @since 5.1.0 * - * @param string|null $pre_render The pre-rendered content. Default null. - * @param array $parsed_block The block being rendered. + * @param string|null $pre_render The pre-rendered content. Default null. + * @param array $parsed_block The block being rendered. + * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. */ - $pre_render = apply_filters( 'pre_render_block', null, $parsed_block ); + $pre_render = apply_filters( 'pre_render_block', null, $parsed_block, $parent_block ); if ( ! is_null( $pre_render ) ) { return $pre_render; } @@ -834,10 +836,11 @@ function render_block( $parsed_block ) { * * @since 5.1.0 * - * @param array $parsed_block The block being rendered. - * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content. + * @param array $parsed_block The block being rendered. + * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content. + * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. */ - $parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block ); + $parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block, $parent_block ); $context = array(); @@ -858,10 +861,11 @@ function render_block( $parsed_block ) { * * @since 5.5.0 * - * @param array $context Default context. - * @param array $parsed_block Block being rendered, filtered by `render_block_data`. + * @param array $context Default context. + * @param array $parsed_block Block being rendered, filtered by `render_block_data`. + * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. */ - $context = apply_filters( 'render_block_context', $context, $parsed_block ); + $context = apply_filters( 'render_block_context', $context, $parsed_block, $parent_block ); $block = new WP_Block( $parsed_block, $context ); diff --git a/wp-includes/class-wp-block.php b/wp-includes/class-wp-block.php index 189ce4e615..ed5b0f0767 100644 --- a/wp-includes/class-wp-block.php +++ b/wp-includes/class-wp-block.php @@ -57,6 +57,15 @@ class WP_Block { */ protected $available_context; + /** + * Block type registry. + * + * @since 5.9.0 + * @var WP_Block_Type_Registry + * @access protected + */ + protected $registry; + /** * List of inner blocks (of this same class) * @@ -115,6 +124,8 @@ class WP_Block { $registry = WP_Block_Type_Registry::get_instance(); } + $this->registry = $registry; + $this->block_type = $registry->get_registered( $this->name ); $this->available_context = $available_context; @@ -205,10 +216,33 @@ class WP_Block { if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { $index = 0; + foreach ( $this->inner_content as $chunk ) { - $block_content .= is_string( $chunk ) ? - $chunk : - $this->inner_blocks[ $index++ ]->render(); + if ( is_string( $chunk ) ) { + $block_content .= $chunk; + } else { + $inner_block = $this->inner_blocks[ $index ]; + $parent_block = $this; + + /** This filter is documented in wp-includes/blocks.php */ + $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); + + if ( ! is_null( $pre_render ) ) { + $block_content .= $pre_render; + } else { + $source_block = $inner_block->parsed_block; + + /** This filter is documented in wp-includes/blocks.php */ + $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); + + /** This filter is documented in wp-includes/blocks.php */ + $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); + + $block_content .= $inner_block->render(); + } + + $index++; + } } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 35fabe3a2b..154e4b0d71 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-alpha-51893'; +$wp_version = '5.9-alpha-51894'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.