Make WordPress Core


Ignore:
Timestamp:
09/19/2022 08:12:02 PM (4 years ago)
Author:
audrasjb
Message:

Editor: Backport block supports (border, color, elements, spacing) from Gutenberg to WP 6.1.

This changeset backports border, color, elements and spacing block supports changes from Gutenberg to WP 6.1.

See tracking issue on Gutenberg repository: ​gutenberg#43440.

Props ramonopoly, glendaviesnz, bernhard-reiter, audrasjb, costdev.
See #56467.

File:
1 edited

Legend:

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

    r53076 r54211  
    1111 *
    1212 * @since 5.6.0
     13 * @since 6.1.0 Improved $color_support assignment optimization.
    1314 * @access private
    1415 *
    … …  
    1617 */
    1718function wp_register_colors_support( $block_type ) {
    18         $color_support = false;
    19         if ( property_exists( $block_type, 'supports' ) ) {
    20                 $color_support = _wp_array_get( $block_type->supports, array( 'color' ), false );
    21         }
     19        $color_support                 = property_exists( $block_type, 'supports' ) ? _wp_array_get( $block_type->supports, array( 'color' ), false ) : false;
    2220        $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
    2321        $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
    … …  
    6462 *
    6563 * @since 5.6.0
     64 * @since 6.1.0 Implemented the style engine to generate CSS and classnames.
    6665 * @access private
    6766 *
    … …  
    8483        $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
    8584        $has_gradients_support         = _wp_array_get( $color_support, array( 'gradients' ), false );
    86         $classes                       = array();
    87         $styles                        = array();
     85        $color_block_styles            = array();
    8886
    8987        // Text colors.
    90         // Check support for text colors.
    9188        if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
    92                 $has_named_text_color  = array_key_exists( 'textColor', $block_attributes );
    93                 $has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
    94 
    95                 // Apply required generic class.
    96                 if ( $has_custom_text_color || $has_named_text_color ) {
    97                         $classes[] = 'has-text-color';
    98                 }
    99                 // Apply color class or inline style.
    100                 if ( $has_named_text_color ) {
    101                         $classes[] = sprintf( 'has-%s-color', _wp_to_kebab_case( $block_attributes['textColor'] ) );
    102                 } elseif ( $has_custom_text_color ) {
    103                         $styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
    104                 }
     89                $preset_text_color          = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null;
     90                $custom_text_color          = _wp_array_get( $block_attributes, array( 'style', 'color', 'text' ), null );
     91                $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
    10592        }
    10693
    10794        // Background colors.
    10895        if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
    109                 $has_named_background_color  = array_key_exists( 'backgroundColor', $block_attributes );
    110                 $has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
    111 
    112                 // Apply required background class.
    113                 if ( $has_custom_background_color || $has_named_background_color ) {
    114                         $classes[] = 'has-background';
    115                 }
    116                 // Apply background color classes or styles.
    117                 if ( $has_named_background_color ) {
    118                         $classes[] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $block_attributes['backgroundColor'] ) );
    119                 } elseif ( $has_custom_background_color ) {
    120                         $styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
    121                 }
     96                $preset_background_color          = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null;
     97                $custom_background_color          = _wp_array_get( $block_attributes, array( 'style', 'color', 'background' ), null );
     98                $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
    12299        }
    123100
    124101        // Gradients.
    125102        if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) {
    126                 $has_named_gradient  = array_key_exists( 'gradient', $block_attributes );
    127                 $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
    128 
    129                 if ( $has_named_gradient || $has_custom_gradient ) {
    130                         $classes[] = 'has-background';
    131                 }
    132                 // Apply required background class.
    133                 if ( $has_named_gradient ) {
    134                         $classes[] = sprintf( 'has-%s-gradient-background', _wp_to_kebab_case( $block_attributes['gradient'] ) );
    135                 } elseif ( $has_custom_gradient ) {
    136                         $styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
    137                 }
     103                $preset_gradient_color          = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null;
     104                $custom_gradient_color          = _wp_array_get( $block_attributes, array( 'style', 'color', 'gradient' ), null );
     105                $color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color;
    138106        }
    139107
    140108        $attributes = array();
    141         if ( ! empty( $classes ) ) {
    142                 $attributes['class'] = implode( ' ', $classes );
     109        $styles     = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) );
     110
     111        if ( ! empty( $styles['classnames'] ) ) {
     112                $attributes['class'] = $styles['classnames'];
    143113        }
    144         if ( ! empty( $styles ) ) {
    145                 $attributes['style'] = implode( ' ', $styles );
     114
     115        if ( ! empty( $styles['css'] ) ) {
     116                $attributes['style'] = $styles['css'];
    146117        }
    147118
Note: See TracChangeset for help on using the changeset viewer.