From 8faf366ab9a328a0d2fa1a3bdfc8a3d1eebaeb4c Mon Sep 17 00:00:00 2001 From: "K. Adam White" Date: Tue, 25 Feb 2020 15:05:06 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@47360 git-svn-id: http://core.svn.wordpress.org/trunk@47147 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-rest-block-renderer-controller.php | 16 +++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php index 2f901f9230..96b4dfc068 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php @@ -135,9 +135,8 @@ class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { setup_postdata( $post ); } $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( 'block_invalid', __( '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( - 'rendered' => $block->render( $request->get_param( 'attributes' ) ), + 'rendered' => render_block( $block ), ); return rest_ensure_response( $data ); diff --git a/wp-includes/version.php b/wp-includes/version.php index dc1fee1c87..6f08fa7754 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.4-beta2-47359'; +$wp_version = '5.4-beta2-47360'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.