Make WordPress Core


Ignore:
Timestamp:
10/06/2021 06:47:09 PM (4 years ago)
Author:
azaozz
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r51888 r51894  
    814814function render_block( $parsed_block ) {
    815815    global $post;
     816    $parent_block = null;
    816817
    817818    /**
     
    820821     * @since 5.1.0
    821822     *
    822      * @param string|null $pre_render   The pre-rendered content. Default null.
    823      * @param array       $parsed_block The block being rendered.
     823     * @param string|null   $pre_render   The pre-rendered content. Default null.
     824     * @param array         $parsed_block The block being rendered.
     825     * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
    824826     */
    825     $pre_render = apply_filters( 'pre_render_block', null, $parsed_block );
     827    $pre_render = apply_filters( 'pre_render_block', null, $parsed_block, $parent_block );
    826828    if ( ! is_null( $pre_render ) ) {
    827829        return $pre_render;
     
    835837     * @since 5.1.0
    836838     *
    837      * @param array $parsed_block The block being rendered.
    838      * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
     839     * @param array         $parsed_block The block being rendered.
     840     * @param array         $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
     841     * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
    839842     */
    840     $parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block );
     843    $parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block, $parent_block );
    841844
    842845    $context = array();
     
    859862     * @since 5.5.0
    860863     *
    861      * @param array $context      Default context.
    862      * @param array $parsed_block Block being rendered, filtered by `render_block_data`.
     864     * @param array         $context      Default context.
     865     * @param array         $parsed_block Block being rendered, filtered by `render_block_data`.
     866     * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
    863867     */
    864     $context = apply_filters( 'render_block_context', $context, $parsed_block );
     868    $context = apply_filters( 'render_block_context', $context, $parsed_block, $parent_block );
    865869
    866870    $block = new WP_Block( $parsed_block, $context );
Note: See TracChangeset for help on using the changeset viewer.