WordPress.org

Make WordPress Core

Changeset 47360


Ignore:
Timestamp:
02/25/2020 03:03:57 PM (8 months ago)
Author:
kadamwhite
Message:

REST API: Apply all relevant block rendering filters when rendering block previews.

Several filters were introduced to the render_block method since the initial implementation of the block-renderer/ endpoints, causing the output of those endpoints to diverge from the rendered content of blocks on the frontend.

Props kadamwhite, TimothyBlynJacobs, miinasikk.
Fixes #49387.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php

    r47122 r47360  
    136136        }
    137137        $registry = WP_Block_Type_Registry::get_instance();
    138         $block    = $registry->get_registered( $request['name'] );
    139138
    140         if ( null === $block ) {
     139        if ( null === $registry->get_registered( $request['name'] ) ) {
    141140            return new WP_Error(
    142141                'block_invalid',
     
    148147        }
    149148
     149        $attributes = $request->get_param( 'attributes' );
     150
     151        // Create an array representation simulating the output of parse_blocks.
     152        $block = array(
     153            'blockName'    => $request['name'],
     154            'attrs'        => $attributes,
     155            'innerHTML'    => '',
     156            'innerContent' => array(),
     157        );
     158
     159        // Render using render_block to ensure all relevant filters are used.
    150160        $data = array(
    151             'rendered' => $block->render( $request->get_param( 'attributes' ) ),
     161            'rendered' => render_block( $block ),
    152162        );
    153163
  • trunk/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php

    r46586 r47360  
    374374    }
    375375
    376 
     376    /**
     377     * Check filtering block output using the pre_render_block filter.
     378     *
     379     * @ticket 49387
     380     */
     381    public function test_get_item_with_pre_render_block_filter() {
     382        wp_set_current_user( self::$user_id );
     383
     384        $pre_render_filter = function( $output, $block ) {
     385            if ( $block['blockName'] === self::$block_name ) {
     386                return '<p>Alternate content.</p>';
     387            }
     388        };
     389        add_filter( 'pre_render_block', $pre_render_filter, 10, 2 );
     390
     391        $attributes = array(
     392            'some_int'    => '123',
     393            'some_string' => 'foo',
     394            'some_array'  => array( 1, '2', 3 ),
     395        );
     396
     397        $request = new WP_REST_Request( 'GET', self::$rest_api_route . self::$block_name );
     398        $request->set_param( 'context', 'edit' );
     399        $request->set_param( 'attributes', $attributes );
     400        $response = rest_get_server()->dispatch( $request );
     401        $this->assertEquals( 200, $response->get_status() );
     402
     403        $data = $response->get_data();
     404        $this->assertEquals( '<p>Alternate content.</p>', $data['rendered'] );
     405
     406        remove_filter( 'pre_render_block', $pre_render_filter );
     407    }
    377408
    378409    /**
Note: See TracChangeset for help on using the changeset viewer.