Make WordPress Core

Changeset 53076


Ignore:
Timestamp:
04/05/2022 12:06:48 PM (5 years ago)
Author:
gziolo
Message:

Editor: Backport block support changes from the Gutenberg plugin

Migrate spacing, border, color, dimensions, elements and typography and associated tests for block supports in the block editor.

Related changes in Gutenberg:

Props ramonopoly, aaronrobertshaw.
See #55505.

Location:
trunk
Files:
3 added
9 edited

Legend:

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

    r52302 r53076  
    5151 */
    5252function wp_apply_border_support( $block_type, $block_attributes ) {
    53         if ( wp_skip_border_serialization( $block_type ) ) {
     53        if ( wp_should_skip_block_supports_serialization( $block_type, 'border' ) ) {
    5454                return array();
    5555        }
    … …  
    6161        if (
    6262                wp_has_border_feature_support( $block_type, 'radius' ) &&
    63                 isset( $block_attributes['style']['border']['radius'] )
     63                isset( $block_attributes['style']['border']['radius'] ) &&
     64                ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
    6465        ) {
    6566                $border_radius = $block_attributes['style']['border']['radius'];
    … …  
    8586        if (
    8687                wp_has_border_feature_support( $block_type, 'style' ) &&
    87                 isset( $block_attributes['style']['border']['style'] )
     88                isset( $block_attributes['style']['border']['style'] ) &&
     89                ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
    8890        ) {
    8991                $border_style = $block_attributes['style']['border']['style'];
    … …  
    9496        if (
    9597                wp_has_border_feature_support( $block_type, 'width' ) &&
    96                 isset( $block_attributes['style']['border']['width'] )
     98                isset( $block_attributes['style']['border']['width'] ) &&
     99                ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
    97100        ) {
    98101                $border_width = $block_attributes['style']['border']['width'];
    … …  
    107110
    108111        // Border color.
    109         if ( wp_has_border_feature_support( $block_type, 'color' ) ) {
     112        if (
     113                wp_has_border_feature_support( $block_type, 'color' ) &&
     114                ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
     115        ) {
    110116                $has_named_border_color  = array_key_exists( 'borderColor', $block_attributes );
    111117                $has_custom_border_color = isset( $block_attributes['style']['border']['color'] );
    … …  
    135141
    136142        return $attributes;
    137 }
    138 
    139 /**
    140  * Checks whether serialization of the current block's border properties should
    141  * occur.
    142  *
    143  * @since 5.8.0
    144  * @access private
    145  *
    146  * @param WP_Block_Type $block_type Block type.
    147  * @return bool Whether serialization of the current block's border properties
    148  *              should occur.
    149  */
    150 function wp_skip_border_serialization( $block_type ) {
    151         $border_support = _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), false );
    152 
    153         return is_array( $border_support ) &&
    154                 array_key_exists( '__experimentalSkipSerialization', $border_support ) &&
    155                 $border_support['__experimentalSkipSerialization'];
    156143}
    157144
  • trunk/src/wp-includes/block-supports/colors.php

    r52069 r53076  
    7676        if (
    7777                is_array( $color_support ) &&
    78                 array_key_exists( '__experimentalSkipSerialization', $color_support ) &&
    79                 $color_support['__experimentalSkipSerialization']
     78                wp_should_skip_block_supports_serialization( $block_type, 'color' )
    8079        ) {
    8180                return array();
    … …  
    9089        // Text colors.
    9190        // Check support for text colors.
    92         if ( $has_text_colors_support ) {
     91        if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
    9392                $has_named_text_color  = array_key_exists( 'textColor', $block_attributes );
    9493                $has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
    … …  
    107106
    108107        // Background colors.
    109         if ( $has_background_colors_support ) {
     108        if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
    110109                $has_named_background_color  = array_key_exists( 'backgroundColor', $block_attributes );
    111110                $has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
    … …  
    124123
    125124        // Gradients.
    126         if ( $has_gradients_support ) {
     125        if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) {
    127126                $has_named_gradient  = array_key_exists( 'gradient', $block_attributes );
    128127                $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
  • trunk/src/wp-includes/block-supports/dimensions.php

    r52302 r53076  
    55 * This does not include the `spacing` block support even though that visually
    66 * appears under the "Dimensions" panel in the editor. It remains in its
    7  * original `spacing.php` file for backwards compatibility.
     7 * original `spacing.php` file for compatibility with core.
    88 *
    99 * @package WordPress
    … …  
    5252 */
    5353function wp_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
    54         if ( wp_skip_dimensions_serialization( $block_type ) ) {
     54        if ( wp_should_skip_block_supports_serialization( $block_type, '__experimentalDimensions' ) ) {
    5555                return array();
    5656        }
    … …  
    6464}
    6565
    66 /**
    67  * Checks whether serialization of the current block's dimensions properties
    68  * should occur.
    69  *
    70  * @since 5.9.0
    71  * @access private
    72  *
    73  * @param WP_Block_type $block_type Block type.
    74  * @return bool Whether to serialize spacing support styles & classes.
    75  */
    76 function wp_skip_dimensions_serialization( $block_type ) {
    77         $dimensions_support = _wp_array_get( $block_type->supports, array( '__experimentalDimensions' ), false );
    78         return is_array( $dimensions_support ) &&
    79                 array_key_exists( '__experimentalSkipSerialization', $dimensions_support ) &&
    80                 $dimensions_support['__experimentalSkipSerialization'];
    81 }
    82 
    8366// Register the block support.
    8467WP_Block_Supports::get_instance()->register(
  • trunk/src/wp-includes/block-supports/elements.php

    r53012 r53076  
    1919function wp_render_elements_support( $block_content, $block ) {
    2020        if ( ! $block_content ) {
     21                return $block_content;
     22        }
     23
     24        $block_type                    = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
     25        $skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
     26
     27        if ( $skip_link_color_serialization ) {
    2128                return $block_content;
    2229        }
  • trunk/src/wp-includes/block-supports/spacing.php

    r52434 r53076  
    4545 */
    4646function wp_apply_spacing_support( $block_type, $block_attributes ) {
    47         if ( wp_skip_spacing_serialization( $block_type ) ) {
     47        if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) {
    4848                return array();
    4949        }
    … …  
    5151        $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
    5252        $has_margin_support  = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
     53        $skip_padding        = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
     54        $skip_margin         = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
    5355        $styles              = array();
    5456
    55         if ( $has_padding_support ) {
     57        if ( $has_padding_support && ! $skip_padding ) {
    5658                $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
    5759                if ( is_array( $padding_value ) ) {
    … …  
    6466        }
    6567
    66         if ( $has_margin_support ) {
     68        if ( $has_margin_support && ! $skip_margin ) {
    6769                $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
    6870                if ( is_array( $margin_value ) ) {
    … …  
    7880}
    7981
    80 /**
    81  * Checks whether serialization of the current block's spacing properties should
    82  * occur.
    83  *
    84  * @since 5.9.0
    85  * @access private
    86  *
    87  * @param WP_Block_Type $block_type Block type.
    88  * @return bool Whether to serialize spacing support styles & classes.
    89  */
    90 function wp_skip_spacing_serialization( $block_type ) {
    91         $spacing_support = _wp_array_get( $block_type->supports, array( 'spacing' ), false );
    92 
    93         return is_array( $spacing_support ) &&
    94                 array_key_exists( '__experimentalSkipSerialization', $spacing_support ) &&
    95                 $spacing_support['__experimentalSkipSerialization'];
    96 }
    97 
    9882// Register the block support.
    9983WP_Block_Supports::get_instance()->register(
  • trunk/src/wp-includes/block-supports/typography.php

    r52302 r53076  
    8282        }
    8383
    84         $skip_typography_serialization = _wp_array_get( $typography_supports, array( '__experimentalSkipSerialization' ), false );
    85         if ( $skip_typography_serialization ) {
     84        if ( wp_should_skip_block_supports_serialization( $block_type, 'typography' ) ) {
    8685                return array();
    8786        }
    … …  
    10099        $has_text_transform_support  = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
    101100
    102         if ( $has_font_size_support ) {
     101        if ( $has_font_size_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' ) ) {
    103102                $has_named_font_size  = array_key_exists( 'fontSize', $block_attributes );
    104103                $has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );
    … …  
    111110        }
    112111
    113         if ( $has_font_family_support ) {
     112        if ( $has_font_family_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' ) ) {
    114113                $has_named_font_family  = array_key_exists( 'fontFamily', $block_attributes );
    115114                $has_custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] );
    … …  
    130129        }
    131130
    132         if ( $has_font_style_support ) {
     131        if ( $has_font_style_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' ) ) {
    133132                $font_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontStyle', 'font-style' );
    134133                if ( $font_style ) {
    … …  
    137136        }
    138137
    139         if ( $has_font_weight_support ) {
     138        if ( $has_font_weight_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' ) ) {
    140139                $font_weight = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontWeight', 'font-weight' );
    141140                if ( $font_weight ) {
    … …  
    144143        }
    145144
    146         if ( $has_line_height_support ) {
     145        if ( $has_line_height_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'lineHeight' ) ) {
    147146                $has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
    148147                if ( $has_line_height ) {
    … …  
    151150        }
    152151
    153         if ( $has_text_decoration_support ) {
     152        if ( $has_text_decoration_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' ) ) {
    154153                $text_decoration_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textDecoration', 'text-decoration' );
    155154                if ( $text_decoration_style ) {
    … …  
    158157        }
    159158
    160         if ( $has_text_transform_support ) {
     159        if ( $has_text_transform_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' ) ) {
    161160                $text_transform_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textTransform', 'text-transform' );
    162161                if ( $text_transform_style ) {
    … …  
    165164        }
    166165
    167         if ( $has_letter_spacing_support ) {
     166        if ( $has_letter_spacing_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' ) ) {
    168167                $letter_spacing_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'letterSpacing', 'letter-spacing' );
    169168                if ( $letter_spacing_style ) {
  • trunk/src/wp-settings.php

    r53004 r53076  
    318318require ABSPATH . WPINC . '/block-patterns.php';
    319319require ABSPATH . WPINC . '/class-wp-block-supports.php';
     320require ABSPATH . WPINC . '/block-supports/utils.php';
    320321require ABSPATH . WPINC . '/block-supports/align.php';
    321322require ABSPATH . WPINC . '/block-supports/border.php';
  • trunk/tests/phpunit/tests/block-supports/colors.php

    r52264 r53076  
    4545                unregister_block_type( 'test/color-slug-with-numbers' );
    4646        }
     47
     48        /**
     49         * @ticket 55505
     50         */
     51        function test_color_with_skipped_serialization_block_supports() {
     52                $block_name = 'test/color-with-skipped-serialization-block-supports';
     53                register_block_type(
     54                        $block_name,
     55                        array(
     56                                'api_version' => 2,
     57                                'attributes'  => array(
     58                                        'style' => array(
     59                                                'type' => 'object',
     60                                        ),
     61                                ),
     62                                'supports'    => array(
     63                                        'color' => array(
     64                                                'text'                            => true,
     65                                                'gradients'                       => true,
     66                                                '__experimentalSkipSerialization' => true,
     67                                        ),
     68                                ),
     69                        )
     70                );
     71
     72                $registry   = WP_Block_Type_Registry::get_instance();
     73                $block_type = $registry->get_registered( $block_name );
     74                $block_atts = array(
     75                        'style' => array(
     76                                'color' => array(
     77                                        'text'     => '#d92828',
     78                                        'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 0%,rgb(223,13,13) 46%,rgb(155,81,224) 100%)',
     79                                ),
     80                        ),
     81                );
     82
     83                $actual   = wp_apply_colors_support( $block_type, $block_atts );
     84                $expected = array();
     85
     86                $this->assertSame( $expected, $actual );
     87                unregister_block_type( $block_name );
     88        }
     89
     90        /**
     91         * @ticket 55505
     92         */
     93        function test_gradient_with_individual_skipped_serialization_block_supports() {
     94                $block_name = 'test/gradient-with-individual-skipped-serialization-block-support';
     95                register_block_type(
     96                        $block_name,
     97                        array(
     98                                'api_version' => 2,
     99                                'attributes'  => array(
     100                                        'style' => array(
     101                                                'type' => 'object',
     102                                        ),
     103                                ),
     104                                'supports'    => array(
     105                                        'color' => array(
     106                                                'text'                            => true,
     107                                                'gradients'                       => true,
     108                                                '__experimentalSkipSerialization' => array( 'gradients' ),
     109                                        ),
     110                                ),
     111                        )
     112                );
     113
     114                $registry   = WP_Block_Type_Registry::get_instance();
     115                $block_type = $registry->get_registered( $block_name );
     116                $block_atts = array(
     117                        'style' => array(
     118                                'color' => array(
     119                                        'text' => '#d92828',
     120                                ),
     121                        ),
     122                );
     123
     124                $actual   = wp_apply_colors_support( $block_type, $block_atts );
     125                $expected = array(
     126                        'class' => 'has-text-color',
     127                        'style' => 'color: #d92828;',
     128                );
     129
     130                $this->assertSame( $expected, $actual );
     131                unregister_block_type( $block_name );
     132        }
    47133}
  • trunk/tests/phpunit/tests/block-supports/typography.php

    r52264 r53076  
    6363        }
    6464
     65        /**
     66         * @ticket 55505
     67         */
     68        function test_typography_with_skipped_serialization_block_supports() {
     69                $block_name = 'test/typography-with-skipped-serialization-block-supports';
     70                register_block_type(
     71                        $block_name,
     72                        array(
     73                                'api_version' => 2,
     74                                'attributes'  => array(
     75                                        'style' => array(
     76                                                'type' => 'object',
     77                                        ),
     78                                ),
     79                                'supports'    => array(
     80                                        'typography' => array(
     81                                                'fontSize'                        => true,
     82                                                'lineHeight'                      => true,
     83                                                '__experimentalFontFamily'        => true,
     84                                                '__experimentalLetterSpacing'     => true,
     85                                                '__experimentalSkipSerialization' => true,
     86                                        ),
     87                                ),
     88                        )
     89                );
     90                $registry   = WP_Block_Type_Registry::get_instance();
     91                $block_type = $registry->get_registered( $block_name );
     92                $block_atts = array(
     93                        'style' => array(
     94                                'typography' => array(
     95                                        'fontSize'      => 'serif',
     96                                        'lineHeight'    => 'serif',
     97                                        'fontFamily'    => '22px',
     98                                        'letterSpacing' => '22px',
     99                                ),
     100                        ),
     101                );
     102
     103                $actual   = wp_apply_typography_support( $block_type, $block_atts );
     104                $expected = array();
     105
     106                $this->assertSame( $expected, $actual );
     107                unregister_block_type( $block_name );
     108        }
     109
     110        /**
     111         * @ticket 55505
     112         */
     113        function test_letter_spacing_with_individual_skipped_serialization_block_supports() {
     114                $block_name = 'test/letter-spacing-with-individua-skipped-serialization-block-supports';
     115                register_block_type(
     116                        $block_name,
     117                        array(
     118                                'api_version' => 2,
     119                                'attributes'  => array(
     120                                        'style' => array(
     121                                                'type' => 'object',
     122                                        ),
     123                                ),
     124                                'supports'    => array(
     125                                        'typography' => array(
     126                                                '__experimentalLetterSpacing'     => true,
     127                                                '__experimentalSkipSerialization' => array(
     128                                                        'letterSpacing',
     129                                                ),
     130                                        ),
     131                                ),
     132                        )
     133                );
     134                $registry   = WP_Block_Type_Registry::get_instance();
     135                $block_type = $registry->get_registered( $block_name );
     136                $block_atts = array( 'style' => array( 'typography' => array( 'letterSpacing' => '22px' ) ) );
     137
     138                $actual   = wp_apply_typography_support( $block_type, $block_atts );
     139                $expected = array();
     140
     141                $this->assertSame( $expected, $actual );
     142                unregister_block_type( $block_name );
     143        }
     144
    65145        function test_font_family_with_legacy_inline_styles_using_a_css_var() {
    66146                $block_name = 'test/font-family-with-inline-styles-using-css-var';
Note: See TracChangeset for help on using the changeset viewer.