Make WordPress Core

Changeset 49310


Ignore:
Timestamp:
10/26/2020 08:29:04 AM (4 years ago)
Author:
youknowriad
Message:

Block Editor: Fix WP_Block_Supports class compatibility with Gutenberg-provided class.

When using WordPress trunk with Gutenberg master, there's an incompatibility causing
the dynamic block generated classes to be omitted.
This commit refactors the block supports to fix that problem.

Props nosolosw.
Fixes #51606.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r49226 r49310  
    649649
    650650/**
    651  * Block currently being parsed.
    652  *
    653  * @type array
    654 */
    655 global $current_parsed_block;
    656 
    657 $current_parsed_block = array(
    658     'blockName'  => null,
    659     'attributes' => null,
    660 );
    661 
    662 /**
    663651 * Renders a single block into a HTML string.
    664652 *
    665653 * @since 5.0.0
    666654 *
    667  * @global array    $current_parsed_block Block currently being parsed.
    668655 * @global WP_Post  $post                 The post to edit.
    669656 * @global WP_Query $wp_query             WordPress Query object.
     
    674661 */
    675662function render_block( $parsed_block ) {
    676     global $post, $wp_query, $current_parsed_block;
     663    global $post, $wp_query;
    677664
    678665    /**
     
    688675        return $pre_render;
    689676    }
    690 
    691     $current_parsed_block = $parsed_block;
    692677
    693678    $source_block = $parsed_block;
  • trunk/src/wp-includes/class-wp-block-supports.php

    r49226 r49310  
    2323     */
    2424    private $block_supports = array();
     25
     26    /**
     27     * Tracks the current block to be rendered.
     28     *
     29     * @var array
     30     */
     31    public static $block_to_render = null;
    2532
    2633    /**
     
    7380    }
    7481
    75 
    7682    /**
    7783     * Generates an array of HTML attributes, such as classes, by applying to
     
    8086     * @since 5.6.0
    8187     *
    82      * @param  array $parsed_block Block as parsed from content.
    8388     * @return array               Array of HTML attributes.
    8489     */
    85     public function apply_block_supports( $parsed_block ) {
    86         $block_attributes = $parsed_block['attrs'];
     90    public function apply_block_supports() {
     91        $block_attributes = self::$block_to_render['attrs'];
    8792        $block_type       = WP_Block_Type_Registry::get_instance()->get_registered(
    88             $parsed_block['blockName']
     93            self::$block_to_render['blockName']
    8994        );
    9095
     
    156161 * @since 5.6.0
    157162 *
    158  * @global array    $current_parsed_block Block currently being parsed.
    159  *
    160163 * @param array $extra_attributes Optional. Extra attributes to render on the block wrapper.
    161164 *
     
    163166 */
    164167function get_block_wrapper_attributes( $extra_attributes = array() ) {
    165     global $current_parsed_block;
    166     $new_attributes = WP_Block_Supports::get_instance()->apply_block_supports( $current_parsed_block );
     168    $new_attributes = WP_Block_Supports::get_instance()->apply_block_supports();
    167169
    168170    if ( empty( $new_attributes ) && empty( $extra_attributes ) ) {
     
    209211    return implode( ' ', $normalized_attributes );
    210212}
    211 
  • trunk/src/wp-includes/class-wp-block.php

    r49226 r49310  
    193193     */
    194194    public function render( $options = array() ) {
    195         global $post, $current_parsed_block;
     195        global $post;
    196196        $options = wp_parse_args(
    197197            $options,
     
    207207            $index = 0;
    208208            foreach ( $this->inner_content as $chunk ) {
    209                 if ( is_string( $chunk ) ) {
    210                     $block_content .= $chunk;
    211                 } else {
    212                     $parent_parsed_block  = $current_parsed_block;
    213                     $current_parsed_block = $this->inner_blocks[ $index ]->parsed_block;
    214                     $block_content       .= $this->inner_blocks[ $index++ ]->render();
    215                     $current_parsed_block = $parent_parsed_block;
    216                 }
     209                $block_content .= is_string( $chunk ) ?
     210                    $chunk :
     211                    $this->inner_blocks[ $index++ ]->render();
    217212            }
    218213        }
     
    220215        if ( $is_dynamic ) {
    221216            $global_post   = $post;
     217            $parent = WP_Block_Supports::$block_to_render;
     218            WP_Block_Supports::$block_to_render = $this->parsed_block;
    222219            $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this );
     220            WP_Block_Supports::$block_to_render = $parent;
    223221            $post          = $global_post;
    224222        }
  • trunk/tests/phpunit/includes/testcase-block-supports.php

    r49226 r49310  
    9797     *
    9898     * @param array $block Block to render.
     99     *
     100     * @return string Rendered output for the current block.
    99101     */
    100102    private function render_example_block( $block ) {
    101         global $current_parsed_block;
    102         $current_parsed_block = $block;
    103         $wrapper_attributes   = get_block_wrapper_attributes(
     103        WP_Block_Supports::init();
     104        WP_Block_Supports::$block_to_render = $block;
     105        $wrapper_attributes                 = get_block_wrapper_attributes(
    104106            array(
    105107                'class' => 'foo-bar-class',
Note: See TracChangeset for help on using the changeset viewer.