Make WordPress Core

Changeset 53260


Ignore:
Timestamp:
04/25/2022 04:35:16 PM (3 years ago)
Author:
gziolo
Message:

Editor: Fix styles for nested elements (link color)

This fixes an issue by which link color behaves differently in the editor and front end.

Related Gutenberg PR: https://github.com/WordPress/gutenberg/pull/37728.

Props oandregal.
See #55567.

Location:
trunk
Files:
2 edited

Legend:

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

    r53076 r53260  
    88
    99/**
    10  * Render the elements stylesheet.
     10 * Get the elements class names.
     11 *
     12 * @since 6.0.0
     13 * @access private
     14 *
     15 * @param array $block Block object.
     16 * @return string      The unique class name.
     17 */
     18function wp_get_elements_class_name( $block ) {
     19    return 'wp-elements-' . md5( serialize( $block ) );
     20}
     21
     22/**
     23 * Update the block content with elements class names.
    1124 *
    1225 * @since 5.8.0
     
    4457    }
    4558
    46     $class_name = wp_unique_id( 'wp-elements-' );
    47 
    48     if ( strpos( $link_color, 'var:preset|color|' ) !== false ) {
    49         // Get the name from the string and add proper styles.
    50         $index_to_splice = strrpos( $link_color, '|' ) + 1;
    51         $link_color_name = substr( $link_color, $index_to_splice );
    52         $link_color      = "var(--wp--preset--color--$link_color_name)";
    53     }
    54     $link_color_declaration = esc_html( safecss_filter_attr( "color: $link_color" ) );
    55 
    56     $style = ".$class_name a{" . $link_color_declaration . ';}';
     59    $class_name = wp_get_elements_class_name( $block );
    5760
    5861    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
     
    7679    }
    7780
    78     wp_enqueue_block_support_styles( $style );
    79 
    8081    return $content;
    8182}
    8283
     84/**
     85 * Render the elements stylesheet.
     86 *
     87 * In the case of nested blocks we want the parent element styles to be rendered before their descendants.
     88 * This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
     89 * we want the descendant style to take priority, and this is done by loading it after, in DOM order.
     90 *
     91 * @since 6.0.0
     92 * @access private
     93 *
     94 * @param string|null $pre_render   The pre-rendered content. Default null.
     95 * @param array       $block        The block being rendered.
     96 *
     97 * @return null
     98 */
     99function wp_render_elements_support_styles( $pre_render, $block ) {
     100    $block_type                    = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
     101    $skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
     102    if ( $skip_link_color_serialization ) {
     103        return null;
     104    }
     105
     106    $link_color = null;
     107    if ( ! empty( $block['attrs'] ) ) {
     108        $link_color = _wp_array_get( $block['attrs'], array( 'style', 'elements', 'link', 'color', 'text' ), null );
     109    }
     110
     111    /*
     112    * For now we only care about link color.
     113    * This code in the future when we have a public API
     114    * should take advantage of WP_Theme_JSON::compute_style_properties
     115    * and work for any element and style.
     116    */
     117    if ( null === $link_color ) {
     118        return null;
     119    }
     120
     121    $class_name = wp_get_elements_class_name( $block );
     122
     123    if ( strpos( $link_color, 'var:preset|color|' ) !== false ) {
     124        // Get the name from the string and add proper styles.
     125        $index_to_splice = strrpos( $link_color, '|' ) + 1;
     126        $link_color_name = substr( $link_color, $index_to_splice );
     127        $link_color      = "var(--wp--preset--color--$link_color_name)";
     128    }
     129    $link_color_declaration = esc_html( safecss_filter_attr( "color: $link_color" ) );
     130
     131    $style = ".$class_name a{" . $link_color_declaration . ';}';
     132
     133    wp_enqueue_block_support_styles( $style );
     134
     135    return null;
     136}
     137
    83138add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
     139add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 );
  • trunk/tests/phpunit/tests/block-supports/elements.php

    r53128 r53260  
    1212     */
    1313    private static function make_unique_id_one( $string ) {
    14         return preg_replace( '/wp-elements-\d+/', 'wp-elements-1', $string );
     14        return preg_replace( '/wp-elements-[a-zA-Z0-9]+/', 'wp-elements-1', $string );
    1515    }
    1616
Note: See TracChangeset for help on using the changeset viewer.