WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/25/2020 03:03:57 PM (7 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.