diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 9e3e470304fb8..e7e71fac44444 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2090,29 +2090,7 @@ function render_block( $parsed_block ) { $context['postType'] = $post->post_type; } - /** - * Filters the default context provided to a rendered block. - * - * @since 5.5.0 - * @since 5.9.0 The `$parent_block` parameter was added. - * - * @param array $context Default context. - * @param array $parsed_block { - * An associative array of the block being rendered. See WP_Block_Parser_Block. - * - * @type string $blockName Name of block. - * @type array $attrs Attributes from block comment delimiters. - * @type array[] $innerBlocks List of inner blocks. An array of arrays that - * have the same structure as this one. - * @type string $innerHTML HTML from inside block comment delimiters. - * @type array $innerContent List of string fragments and null markers where - * inner blocks were found. - * } - * @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, $parent_block ); - - $block = new WP_Block( $parsed_block, $context ); + $block = new WP_Block( $parsed_block, $context, null, $parent_block ); return $block->render(); } diff --git a/src/wp-includes/class-wp-block-list.php b/src/wp-includes/class-wp-block-list.php index e1151e6745dfc..0e341e0449f38 100644 --- a/src/wp-includes/class-wp-block-list.php +++ b/src/wp-includes/class-wp-block-list.php @@ -41,18 +41,29 @@ class WP_Block_List implements Iterator, ArrayAccess, Countable { */ protected $registry; + /** + * Reference to the parent block. + * + * @since 6.7.0 + * @var WP_Block|null + * @access protected + */ + protected $parent_block; + /** * Constructor. * * Populates object properties from the provided block instance argument. * * @since 5.5.0 + * @since 6.7.0 Added the optional `$parent_block` argument. * * @param array[]|WP_Block[] $blocks Array of parsed block data, or block instances. * @param array $available_context Optional array of ancestry context values. * @param WP_Block_Type_Registry $registry Optional block type registry. + * @param WP_Block|null $parent_block Optional. If this is a nested block, a reference to the parent block. */ - public function __construct( $blocks, $available_context = array(), $registry = null ) { + public function __construct( $blocks, $available_context = array(), $registry = null, $parent_block = null ) { if ( ! $registry instanceof WP_Block_Type_Registry ) { $registry = WP_Block_Type_Registry::get_instance(); } @@ -60,6 +71,7 @@ public function __construct( $blocks, $available_context = array(), $registry = $this->blocks = $blocks; $this->available_context = $available_context; $this->registry = $registry; + $this->parent_block = $parent_block; } /** @@ -93,7 +105,7 @@ public function offsetGet( $offset ) { $block = $this->blocks[ $offset ]; if ( isset( $block ) && is_array( $block ) ) { - $block = new WP_Block( $block, $this->available_context, $this->registry ); + $block = new WP_Block( $block, $this->available_context, $this->registry, $this->parent_block ); $this->blocks[ $offset ] = $block; } diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index f7fd53dfc9710..e70981cf2ada5 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -112,6 +112,7 @@ class WP_Block { * its registered type will be assigned to the block's `context` property. * * @since 5.5.0 + * @since 6.7.0 Added the optional `$parent_block` argument. * * @param array $block { * An associative array of a single parsed block object. See WP_Block_Parser_Block. @@ -126,7 +127,7 @@ class WP_Block { * @param array $available_context Optional array of ancestry context values. * @param WP_Block_Type_Registry $registry Optional block type registry. */ - public function __construct( $block, $available_context = array(), $registry = null ) { + public function __construct( $block, $available_context = array(), $registry = null, $parent_block = null ) { $this->parsed_block = $block; $this->name = $block['blockName']; @@ -138,7 +139,7 @@ public function __construct( $block, $available_context = array(), $registry = n $this->block_type = $registry->get_registered( $this->name ); - $this->available_context = $available_context; + $this->available_context = apply_filters( 'render_block_context', $available_context, $block, $parent_block ); if ( ! empty( $this->block_type->uses_context ) ) { foreach ( $this->block_type->uses_context as $context_name ) { @@ -159,7 +160,7 @@ public function __construct( $block, $available_context = array(), $registry = n } } - $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry ); + $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry, $parent_block ); } if ( ! empty( $block['innerHTML'] ) ) {