Make WordPress Core


Ignore:
Timestamp:
05/02/2024 06:59:30 AM (2 years ago)
Author:
isabel_brison
Message:

Editor: Merge element style and classname generation to single filter.

Fixes element classnames not being output when block attributes are filtered with render_block_data.

Props aaronrobertshaw, isabel_brison, jorbin.
Fixes #60681.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/elements.php

    r57664 r58074  
    2222/**
    2323 * Updates the block content with elements class names.
     24 *
     25 * @deprecated 6.6.0 Generation of element class name is handled via `render_block_data` filter.
    2426 *
    2527 * @since 5.8.0
    … …  
    3234 */
    3335function wp_render_elements_support( $block_content, $block ) {
    34         if ( ! $block_content || ! isset( $block['attrs']['style']['elements'] ) ) {
    35                 return $block_content;
    36         }
    37 
    38         $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    39         if ( ! $block_type ) {
    40                 return $block_content;
     36        _deprecated_function( __FUNCTION__, '6.6.0', 'wp_render_elements_class_name' );
     37        return $block_content;
     38}
     39
     40/**
     41 * Determines whether an elements class name should be added to the block.
     42 *
     43 * @since 6.6.0
     44 * @access private
     45 *
     46 * @param  array $block   Block object.
     47 * @param  array $options Per element type options e.g. whether to skip serialization.
     48 * @return boolean Whether the block needs an elements class name.
     49 */
     50function wp_should_add_elements_class_name( $block, $options ) {
     51        if ( ! isset( $block['attrs']['style']['elements'] ) ) {
     52                return false;
    4153        }
    4254
    4355        $element_color_properties = array(
    4456                'button'  => array(
    45                         'skip'  => wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' ),
     57                        'skip'  => isset( $options['button']['skip'] ) ? $options['button']['skip'] : false,
    4658                        'paths' => array(
    4759                                array( 'button', 'color', 'text' ),
    … …  
    5163                ),
    5264                'link'    => array(
    53                         'skip'  => wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ),
     65                        'skip'  => isset( $options['link']['skip'] ) ? $options['link']['skip'] : false,
    5466                        'paths' => array(
    5567                                array( 'link', 'color', 'text' ),
    … …  
    5870                ),
    5971                'heading' => array(
    60                         'skip'  => wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' ),
     72                        'skip'  => isset( $options['heading']['skip'] ) ? $options['heading']['skip'] : false,
    6173                        'paths' => array(
    6274                                array( 'heading', 'color', 'text' ),
    … …  
    8597        );
    8698
    87         $skip_all_element_color_serialization = $element_color_properties['button']['skip'] &&
    88                 $element_color_properties['link']['skip'] &&
    89                 $element_color_properties['heading']['skip'];
    90 
    91         if ( $skip_all_element_color_serialization ) {
    92                 return $block_content;
    93         }
    94 
    9599        $elements_style_attributes = $block['attrs']['style']['elements'];
    96100
    … …  
    102106                foreach ( $element_config['paths'] as $path ) {
    103107                        if ( null !== _wp_array_get( $elements_style_attributes, $path, null ) ) {
    104                                 /*
    105                                  * It only takes a single custom attribute to require that the custom
    106                                  * class name be added to the block, so once one is found there's no
    107                                  * need to continue looking for others.
    108                                  *
    109                                  * As is done with the layout hook, this code assumes that the block
    110                                  * contains a single wrapper and that it's the first element in the
    111                                  * rendered output. That first element, if it exists, gets the class.
    112                                  */
    113                                 $tags = new WP_HTML_Tag_Processor( $block_content );
    114                                 if ( $tags->next_tag() ) {
    115                                         $tags->add_class( wp_get_elements_class_name( $block ) );
    116                                 }
    117 
    118                                 return $tags->get_updated_html();
     108                                return true;
    119109                        }
    120110                }
    121111        }
    122112
    123         // If no custom attributes were found then there's nothing to modify.
    124         return $block_content;
    125 }
    126 
    127 /**
    128  * Renders the elements stylesheet.
     113        return false;
     114}
     115
     116/**
     117 * Render the elements stylesheet and adds elements class name to block as required.
    129118 *
    130119 * In the case of nested blocks we want the parent element styles to be rendered before their descendants.
    … …  
    134123 * @since 6.0.0
    135124 * @since 6.1.0 Implemented the style engine to generate CSS and classnames.
    136  * @access private
    137  *
    138  * @param string|null $pre_render The pre-rendered content. Default null.
    139  * @param array       $block      The block being rendered.
    140  * @return null
    141  */
    142 function wp_render_elements_support_styles( $pre_render, $block ) {
    143         $block_type           = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    144         $element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null;
     125 * @since 6.6.0 Element block support class and styles are generated via the `render_block_data` filter instead of `pre_render_block`.
     126 * @access private
     127 *
     128 * @param array $parsed_block The parsed block.
     129 * @return array The same parsed block with elements classname added if appropriate.
     130 */
     131function wp_render_elements_support_styles( $parsed_block ) {
     132        /*
     133         * The generation of element styles and classname were moved to the
     134         * `render_block_data` filter in 6.6.0 to avoid filtered attributes
     135         * breaking the application of the elements CSS class.
     136         *
     137         * @see https://github.com/WordPress/gutenberg/pull/59535.
     138         *
     139         * The change in filter means, the argument types for this function
     140         * have changed and require deprecating.
     141         */
     142        if ( is_string( $parsed_block ) ) {
     143                _deprecated_argument(
     144                        __FUNCTION__,
     145                        '6.6.0',
     146                        __( 'Use as a `pre_render_block` filter is deprecated. Use with `render_block_data` instead.' )
     147                );
     148        }
     149
     150        $block_type           = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );
     151        $element_block_styles = isset( $parsed_block['attrs']['style']['elements'] ) ? $parsed_block['attrs']['style']['elements'] : null;
    145152
    146153        if ( ! $element_block_styles ) {
    147                 return null;
     154                return $parsed_block;
    148155        }
    149156
    … …  
    156163
    157164        if ( $skips_all_element_color_serialization ) {
    158                 return null;
    159         }
    160 
    161         $class_name = wp_get_elements_class_name( $block );
    162 
     165                return $parsed_block;
     166        }
     167
     168        $options = array(
     169                'button'  => array( 'skip' => $skip_button_color_serialization ),
     170                'link'    => array( 'skip' => $skip_link_color_serialization ),
     171                'heading' => array( 'skip' => $skip_heading_color_serialization ),
     172        );
     173
     174        if ( ! wp_should_add_elements_class_name( $parsed_block, $options ) ) {
     175                return $parsed_block;
     176        }
     177
     178        $class_name         = wp_get_elements_class_name( $parsed_block );
     179        $updated_class_name = isset( $parsed_block['attrs']['className'] ) ? $parsed_block['attrs']['className'] . " $class_name" : $class_name;
     180
     181        _wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name );
     182
     183        // Generate element styles based on selector and store in style engine for enqueuing.
    163184        $element_types = array(
    164185                'button'  => array(
    … …  
    226247        }
    227248
    228         return null;
    229 }
    230 
    231 add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
    232 add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 );
     249        return $parsed_block;
     250}
     251
     252/**
     253 * Ensure the elements block support class name generated, and added to
     254 * block attributes, in the `render_block_data` filter gets applied to the
     255 * block's markup.
     256 *
     257 * @see wp_render_elements_support_styles
     258 * @since 6.6.0
     259 *
     260 * @param  string $block_content Rendered block content.
     261 * @param  array  $block         Block object.
     262 *
     263 * @return string                Filtered block content.
     264 */
     265function wp_render_elements_class_name( $block_content, $block ) {
     266        $class_string = $block['attrs']['className'] ?? '';
     267        preg_match( '/\bwp-elements-\S+\b/', $class_string, $matches );
     268
     269        if ( empty( $matches ) ) {
     270                return $block_content;
     271        }
     272
     273        $tags = new WP_HTML_Tag_Processor( $block_content );
     274
     275        if ( $tags->next_tag() ) {
     276                $tags->add_class( $matches[0] );
     277        }
     278
     279        return $tags->get_updated_html();
     280}
     281
     282add_filter( 'render_block', 'wp_render_elements_class_name', 10, 2 );
     283add_filter( 'render_block_data', 'wp_render_elements_support_styles', 10, 1 );
Note: See TracChangeset for help on using the changeset viewer.