WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/25/2020 03:03:57 PM (9 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/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
Note: See TracChangeset for help on using the changeset viewer.