Skip to content

Commit a0ac0ff

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@47360 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 51e6228 commit a0ac0ff

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ public function get_item( $request ) {
135135
setup_postdata( $post );
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',
143142
__( 'Invalid block.' ),
@@ -147,8 +146,19 @@ public function get_item( $request ) {
147146
);
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

154164
return rest_ensure_response( $data );

tests/phpunit/tests/rest-api/rest-block-renderer-controller.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,38 @@ public function test_get_item() {
373373
);
374374
}
375375

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 );
376390

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
/**
379410
* Check success response for getting item with layout attribute provided.

0 commit comments

Comments
 (0)