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
This commit is contained in:
parent
51e6228635
commit
a0ac0ff13d
@ -135,9 +135,8 @@ class WP_REST_Block_Renderer_Controller extends WP_REST_Controller {
|
|||||||
setup_postdata( $post );
|
setup_postdata( $post );
|
||||||
}
|
}
|
||||||
$registry = WP_Block_Type_Registry::get_instance();
|
$registry = WP_Block_Type_Registry::get_instance();
|
||||||
$block = $registry->get_registered( $request['name'] );
|
|
||||||
|
|
||||||
if ( null === $block ) {
|
if ( null === $registry->get_registered( $request['name'] ) ) {
|
||||||
return new WP_Error(
|
return new WP_Error(
|
||||||
'block_invalid',
|
'block_invalid',
|
||||||
__( 'Invalid block.' ),
|
__( 'Invalid block.' ),
|
||||||
@ -147,8 +146,19 @@ class WP_REST_Block_Renderer_Controller extends WP_REST_Controller {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$attributes = $request->get_param( 'attributes' );
|
||||||
|
|
||||||
|
// Create an array representation simulating the output of parse_blocks.
|
||||||
|
$block = array(
|
||||||
|
'blockName' => $request['name'],
|
||||||
|
'attrs' => $attributes,
|
||||||
|
'innerHTML' => '',
|
||||||
|
'innerContent' => array(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Render using render_block to ensure all relevant filters are used.
|
||||||
$data = array(
|
$data = array(
|
||||||
'rendered' => $block->render( $request->get_param( 'attributes' ) ),
|
'rendered' => render_block( $block ),
|
||||||
);
|
);
|
||||||
|
|
||||||
return rest_ensure_response( $data );
|
return rest_ensure_response( $data );
|
||||||
|
@ -373,7 +373,38 @@ class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check filtering block output using the pre_render_block filter.
|
||||||
|
*
|
||||||
|
* @ticket 49387
|
||||||
|
*/
|
||||||
|
public function test_get_item_with_pre_render_block_filter() {
|
||||||
|
wp_set_current_user( self::$user_id );
|
||||||
|
|
||||||
|
$pre_render_filter = function( $output, $block ) {
|
||||||
|
if ( $block['blockName'] === self::$block_name ) {
|
||||||
|
return '<p>Alternate content.</p>';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
add_filter( 'pre_render_block', $pre_render_filter, 10, 2 );
|
||||||
|
|
||||||
|
$attributes = array(
|
||||||
|
'some_int' => '123',
|
||||||
|
'some_string' => 'foo',
|
||||||
|
'some_array' => array( 1, '2', 3 ),
|
||||||
|
);
|
||||||
|
|
||||||
|
$request = new WP_REST_Request( 'GET', self::$rest_api_route . self::$block_name );
|
||||||
|
$request->set_param( 'context', 'edit' );
|
||||||
|
$request->set_param( 'attributes', $attributes );
|
||||||
|
$response = rest_get_server()->dispatch( $request );
|
||||||
|
$this->assertEquals( 200, $response->get_status() );
|
||||||
|
|
||||||
|
$data = $response->get_data();
|
||||||
|
$this->assertEquals( '<p>Alternate content.</p>', $data['rendered'] );
|
||||||
|
|
||||||
|
remove_filter( 'pre_render_block', $pre_render_filter );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check success response for getting item with layout attribute provided.
|
* Check success response for getting item with layout attribute provided.
|
||||||
|
Loading…
Reference in New Issue
Block a user