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/class-wp-block.php

    r51841 r51894  
    5959
    6060    /**
     61     * Block type registry.
     62     *
     63     * @since 5.9.0
     64     * @var WP_Block_Type_Registry
     65     * @access protected
     66     */
     67    protected $registry;
     68
     69    /**
    6170     * List of inner blocks (of this same class)
    6271     *
     
    115124            $registry = WP_Block_Type_Registry::get_instance();
    116125        }
     126
     127        $this->registry = $registry;
    117128
    118129        $this->block_type = $registry->get_registered( $this->name );
     
    206217        if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) {
    207218            $index = 0;
     219
    208220            foreach ( $this->inner_content as $chunk ) {
    209                 $block_content .= is_string( $chunk ) ?
    210                     $chunk :
    211                     $this->inner_blocks[ $index++ ]->render();
     221                if ( is_string( $chunk ) ) {
     222                    $block_content .= $chunk;
     223                } else {
     224                    $inner_block  = $this->inner_blocks[ $index ];
     225                    $parent_block = $this;
     226
     227                    /** This filter is documented in wp-includes/blocks.php */
     228                    $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block );
     229
     230                    if ( ! is_null( $pre_render ) ) {
     231                        $block_content .= $pre_render;
     232                    } else {
     233                        $source_block = $inner_block->parsed_block;
     234
     235                        /** This filter is documented in wp-includes/blocks.php */
     236                        $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block );
     237
     238                        /** This filter is documented in wp-includes/blocks.php */
     239                        $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block );
     240
     241                        $block_content .= $inner_block->render();
     242                    }
     243
     244                    $index++;
     245                }
    212246            }
    213247        }
Note: See TracChangeset for help on using the changeset viewer.