Make WordPress Core


Ignore:
Timestamp:
11/16/2020 12:45:55 AM (3 years ago)
Author:
noisysocks
Message:

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.

Fixes #51612.
Props gaambo, gziolo, TimothyBlynJacobs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/block.php

    r48937 r49608  
    4646    }
    4747
     48    function filter_pre_render_block( $pre_render, $parsed_block ) {
     49        if ( 'core/skip' === $parsed_block['blockName'] ) {
     50            return 'Hello world!';
     51        }
     52        return null;
     53    }
     54
     55    function filter_render_block_data( $parsed_block, $source_block ) {
     56        $parsed_block['attrs']['tag'] = $parsed_block['attrs']['tag'] . '-filtered';
     57        return $parsed_block;
     58    }
     59
     60    function filter_render_block_context( $available_context, $parsed_block ) {
     61        $available_context['core/recordId'] += 1;
     62        return $available_context;
     63    }
     64
    4865    /**
    4966     * @ticket 49927
     
    395412    }
    396413
     414    /**
     415     * @ticket 51612
     416     */
     417    function test_applies_pre_render_block_filter() {
     418        $this->registry->register( 'core/skip', array() );
     419
     420        add_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ), 10, 2 );
     421
     422        $parsed_blocks = parse_blocks( '<!-- wp:skip /-->' );
     423        $parsed_block  = $parsed_blocks[0];
     424        $context       = array();
     425        $block         = new WP_Block( $parsed_block, $context, $this->registry );
     426
     427        $rendered_content = $block->render();
     428
     429        remove_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ) );
     430
     431        $this->assertSame( 'Hello world!', $rendered_content );
     432    }
     433
     434    /**
     435     * @ticket 51612
     436     */
     437    function test_applies_pre_render_block_filter_to_inner_blocks() {
     438        $this->registry->register( 'core/outer', array() );
     439        $this->registry->register( 'core/skip', array() );
     440
     441        add_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ), 10, 2 );
     442
     443        $parsed_blocks = parse_blocks( '<!-- wp:outer --><!-- wp:skip /--> How are you?<!-- /wp:outer -->' );
     444        $parsed_block  = $parsed_blocks[0];
     445        $context       = array();
     446        $block         = new WP_Block( $parsed_block, $context, $this->registry );
     447
     448        $rendered_content = $block->render();
     449
     450        remove_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ) );
     451
     452        $this->assertSame( 'Hello world! How are you?', $rendered_content );
     453    }
     454
     455    /**
     456     * @ticket 51612
     457     */
     458    function test_applies_render_block_data_filter() {
     459        $this->registry->register(
     460            'core/wrapper',
     461            array(
     462                'attributes'      => array(
     463                    'tag' => array(
     464                        'type' => 'string',
     465                    ),
     466                ),
     467                'render_callback' => function( $block_attributes, $content ) {
     468                    return sprintf(
     469                        '<%1$s>%2$s</%1$s>',
     470                        $block_attributes['tag'],
     471                        $content
     472                    );
     473                },
     474            )
     475        );
     476
     477        add_filter( 'render_block_data', array( $this, 'filter_render_block_data' ), 10, 2 );
     478
     479        $parsed_blocks = parse_blocks( '<!-- wp:wrapper {"tag":"outer"} --><!-- wp:wrapper {"tag":"inner"} -->Hello!<!-- /wp:wrapper --><!-- /wp:wrapper -->' );
     480        $parsed_block  = $parsed_blocks[0];
     481        $context       = array();
     482        $block         = new WP_Block( $parsed_block, $context, $this->registry );
     483
     484        $rendered_content = $block->render();
     485
     486        remove_filter( 'render_block_data', array( $this, 'filter_render_block_data' ) );
     487
     488        $this->assertSame( '<outer-filtered><inner-filtered>Hello!</inner-filtered></outer-filtered>', $rendered_content );
     489    }
     490
     491    /**
     492     * @ticket 51612
     493     */
     494    function test_applies_render_block_context_filter() {
     495        $this->registry->register(
     496            'core/provider',
     497            array(
     498                'attributes'       => array(
     499                    'recordId' => array(
     500                        'type' => 'number',
     501                    ),
     502                ),
     503                'uses_context'     => array( 'core/recordId' ),
     504                'provides_context' => array(
     505                    'core/recordId' => 'recordId',
     506                ),
     507            )
     508        );
     509        $this->registry->register(
     510            'core/consumer',
     511            array(
     512                'uses_context'    => array( 'core/recordId' ),
     513                'render_callback' => function( $block_attributes, $content, $block ) {
     514                    return sprintf( 'Record ID: %d ', $block->context['core/recordId'] );
     515                },
     516            )
     517        );
     518
     519        add_filter( 'render_block_context', array( $this, 'filter_render_block_context' ), 10, 2 );
     520
     521        $parsed_blocks = parse_blocks( '<!-- wp:consumer /--><!-- wp:provider {"recordId":20} --><!-- wp:consumer /--><!-- /wp:provider -->' );
     522        $context       = array( 'core/recordId' => 10 );
     523
     524        $rendered_content = '';
     525
     526        foreach ( $parsed_blocks as $parsed_block ) {
     527            $block = new WP_Block( $parsed_block, $context, $this->registry );
     528
     529            $rendered_content .= $block->render();
     530        }
     531
     532        remove_filter( 'render_block_context', array( $this, 'filter_render_block_context' ) );
     533
     534        $this->assertSame( 'Record ID: 11 Record ID: 21 ', $rendered_content );
     535    }
     536
    397537}
Note: See TracChangeset for help on using the changeset viewer.