Skip to content

Commit b04425a

Browse files
committed
Editor: Move pre_render_block, render_block_data, render_block_context
Move the pre_render_block, render_block_data, and render_block_context filters from render_block() to WP_Block. This ensures that they are called for all blocks, including nested blocks, not just top-level blocks.
1 parent 9a14f55 commit b04425a

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

src/wp-includes/blocks.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -661,31 +661,6 @@ function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) {
661661
function render_block( $parsed_block ) {
662662
global $post, $wp_query;
663663

664-
/**
665-
* Allows render_block() to be short-circuited, by returning a non-null value.
666-
*
667-
* @since 5.1.0
668-
*
669-
* @param string|null $pre_render The pre-rendered content. Default null.
670-
* @param array $parsed_block The block being rendered.
671-
*/
672-
$pre_render = apply_filters( 'pre_render_block', null, $parsed_block );
673-
if ( ! is_null( $pre_render ) ) {
674-
return $pre_render;
675-
}
676-
677-
$source_block = $parsed_block;
678-
679-
/**
680-
* Filters the block being rendered in render_block(), before it's processed.
681-
*
682-
* @since 5.1.0
683-
*
684-
* @param array $parsed_block The block being rendered.
685-
* @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
686-
*/
687-
$parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block );
688-
689664
$context = array();
690665

691666
if ( $post instanceof WP_Post ) {
@@ -707,16 +682,6 @@ function render_block( $parsed_block ) {
707682
}
708683
}
709684

710-
/**
711-
* Filters the default context provided to a rendered block.
712-
*
713-
* @since 5.5.0
714-
*
715-
* @param array $context Default context.
716-
* @param array $parsed_block Block being rendered, filtered by `render_block_data`.
717-
*/
718-
$context = apply_filters( 'render_block_context', $context, $parsed_block );
719-
720685
$block = new WP_Block( $parsed_block, $context );
721686

722687
return $block->render();

src/wp-includes/class-wp-block.php

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,37 @@ class WP_Block {
103103
*
104104
* @since 5.5.0
105105
*
106-
* @param array $block Array of parsed block properties.
106+
* @param array $parsed_block Array of parsed block properties.
107107
* @param array $available_context Optional array of ancestry context values.
108108
* @param WP_Block_Type_Registry $registry Optional block type registry.
109109
*/
110-
public function __construct( $block, $available_context = array(), $registry = null ) {
111-
$this->parsed_block = $block;
112-
$this->name = $block['blockName'];
110+
public function __construct( $parsed_block, $available_context = array(), $registry = null ) {
111+
$source_block = $parsed_block;
112+
113+
/**
114+
* Filters a block which is to be rendered by render_block() or
115+
* WP_Block::render().
116+
*
117+
* @since 5.1.0
118+
*
119+
* @param array $parsed_block The block being rendered.
120+
* @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
121+
*/
122+
$parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block );
123+
124+
/**
125+
* Filters the default context of a block which is to be rendered by
126+
* render_block() or WP_Block::render().
127+
*
128+
* @since 5.5.0
129+
*
130+
* @param array $available_context Default context.
131+
* @param array $parsed_block Block being rendered, filtered by `render_block_data`.
132+
*/
133+
$available_context = apply_filters( 'render_block_context', $available_context, $parsed_block );
134+
135+
$this->parsed_block = $parsed_block;
136+
$this->name = $parsed_block['blockName'];
113137

114138
if ( is_null( $registry ) ) {
115139
$registry = WP_Block_Type_Registry::get_instance();
@@ -127,7 +151,7 @@ public function __construct( $block, $available_context = array(), $registry = n
127151
}
128152
}
129153

130-
if ( ! empty( $block['innerBlocks'] ) ) {
154+
if ( ! empty( $parsed_block['innerBlocks'] ) ) {
131155
$child_context = $this->available_context;
132156

133157
if ( ! empty( $this->block_type->provides_context ) ) {
@@ -138,15 +162,15 @@ public function __construct( $block, $available_context = array(), $registry = n
138162
}
139163
}
140164

141-
$this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
165+
$this->inner_blocks = new WP_Block_List( $parsed_block['innerBlocks'], $child_context, $registry );
142166
}
143167

144-
if ( ! empty( $block['innerHTML'] ) ) {
145-
$this->inner_html = $block['innerHTML'];
168+
if ( ! empty( $parsed_block['innerHTML'] ) ) {
169+
$this->inner_html = $parsed_block['innerHTML'];
146170
}
147171

148-
if ( ! empty( $block['innerContent'] ) ) {
149-
$this->inner_content = $block['innerContent'];
172+
if ( ! empty( $parsed_block['innerContent'] ) ) {
173+
$this->inner_content = $parsed_block['innerContent'];
150174
}
151175
}
152176

@@ -193,6 +217,21 @@ public function __get( $name ) {
193217
*/
194218
public function render( $options = array() ) {
195219
global $post;
220+
221+
/**
222+
* Allows render_block() or WP_Block::render() to be short-circuited, by
223+
* returning a non-null value.
224+
*
225+
* @since 5.1.0
226+
*
227+
* @param string|null $pre_render The pre-rendered content. Default null.
228+
* @param array $parsed_block The block being rendered.
229+
*/
230+
$pre_render = apply_filters( 'pre_render_block', null, $this->parsed_block );
231+
if ( ! is_null( $pre_render ) ) {
232+
return $pre_render;
233+
}
234+
196235
$options = wp_parse_args(
197236
$options,
198237
array(

0 commit comments

Comments
 (0)