Changeset 54211
- Timestamp:
- 09/19/2022 08:12:02 PM (2 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-supports/border.php
r53240 r54211 12 12 * 13 13 * @since 5.8.0 14 * @since 6.1.0 Improved conditional blocks optimization. 14 15 * @access private 15 16 * … … 17 18 */ 18 19 function wp_register_border_support( $block_type ) { 19 // Determine if any border related features are supported.20 $has_border_support = block_has_support( $block_type, array( '__experimentalBorder' ) );21 $has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );22 23 20 // Setup attributes and styles within that if needed. 24 21 if ( ! $block_type->attributes ) { … … 26 23 } 27 24 28 if ( $has_border_support&& ! array_key_exists( 'style', $block_type->attributes ) ) {25 if ( block_has_support( $block_type, array( '__experimentalBorder' ) ) && ! array_key_exists( 'style', $block_type->attributes ) ) { 29 26 $block_type->attributes['style'] = array( 30 27 'type' => 'object', … … 32 29 } 33 30 34 if ( $has_border_color_support&& ! array_key_exists( 'borderColor', $block_type->attributes ) ) {31 if ( wp_has_border_feature_support( $block_type, 'color' ) && ! array_key_exists( 'borderColor', $block_type->attributes ) ) { 35 32 $block_type->attributes['borderColor'] = array( 36 33 'type' => 'string', … … 44 41 * 45 42 * @since 5.8.0 43 * @since 6.1.0 Implemented the style engine to generate CSS and classnames. 46 44 * @access private 47 45 * … … 55 53 } 56 54 57 $classes = array(); 58 $styles = array(); 55 $border_block_styles = array(); 56 $has_border_color_support = wp_has_border_feature_support( $block_type, 'color' ); 57 $has_border_width_support = wp_has_border_feature_support( $block_type, 'width' ); 59 58 60 59 // Border radius. … … 66 65 $border_radius = $block_attributes['style']['border']['radius']; 67 66 68 if ( is_array( $border_radius ) ) { 69 // We have individual border radius corner values. 70 foreach ( $border_radius as $key => $radius ) { 71 // Convert CamelCase corner name to kebab-case. 72 $corner = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) ); 73 $styles[] = sprintf( 'border-%s-radius: %s;', $corner, $radius ); 74 } 75 } else { 76 // This check handles original unitless implementation. 77 if ( is_numeric( $border_radius ) ) { 78 $border_radius .= 'px'; 79 } 67 if ( is_numeric( $border_radius ) ) { 68 $border_radius .= 'px'; 69 } 80 70 81 $styles[] = sprintf( 'border-radius: %s;', $border_radius ); 82 } 71 $border_block_styles['radius'] = $border_radius; 83 72 } 84 73 … … 89 78 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) 90 79 ) { 91 $border_style = $block_attributes['style']['border']['style']; 92 $styles[] = sprintf( 'border-style: %s;', $border_style ); 80 $border_block_styles['style'] = $block_attributes['style']['border']['style']; 93 81 } 94 82 95 83 // Border width. 96 84 if ( 97 wp_has_border_feature_support( $block_type, 'width' )&&85 $has_border_width_support && 98 86 isset( $block_attributes['style']['border']['width'] ) && 99 87 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) … … 106 94 } 107 95 108 $ styles[] = sprintf( 'border-width: %s;', $border_width );96 $border_block_styles['width'] = $border_width; 109 97 } 110 98 111 99 // Border color. 112 100 if ( 113 wp_has_border_feature_support( $block_type, 'color' )&&101 $has_border_color_support && 114 102 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) 115 103 ) { 116 $has_named_border_color = array_key_exists( 'borderColor', $block_attributes ); 117 $has_custom_border_color = isset( $block_attributes['style']['border']['color'] ); 104 $preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null; 105 $custom_border_color = _wp_array_get( $block_attributes, array( 'style', 'border', 'color' ), null ); 106 $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color; 107 } 118 108 119 if ( $has_named_border_color || $has_custom_border_color ) { 120 $classes[] = 'has-border-color'; 121 } 122 123 if ( $has_named_border_color ) { 124 $classes[] = sprintf( 'has-%s-border-color', $block_attributes['borderColor'] ); 125 } elseif ( $has_custom_border_color ) { 126 $border_color = $block_attributes['style']['border']['color']; 127 $styles[] = sprintf( 'border-color: %s;', $border_color ); 109 // Generates styles for individual border sides. 110 if ( $has_border_color_support || $has_border_width_support ) { 111 foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { 112 $border = _wp_array_get( $block_attributes, array( 'style', 'border', $side ), null ); 113 $border_side_values = array( 114 'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null, 115 'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null, 116 'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null, 117 ); 118 $border_block_styles[ $side ] = $border_side_values; 128 119 } 129 120 } … … 131 122 // Collect classes and styles. 132 123 $attributes = array(); 124 $styles = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) ); 133 125 134 if ( ! empty( $ classes) ) {135 $attributes['class'] = implode( ' ', $classes );126 if ( ! empty( $styles['classnames'] ) ) { 127 $attributes['class'] = $styles['classnames']; 136 128 } 137 129 138 if ( ! empty( $styles ) ) {139 $attributes['style'] = implode( ' ', $styles );130 if ( ! empty( $styles['css'] ) ) { 131 $attributes['style'] = $styles['css']; 140 132 } 141 133 -
trunk/src/wp-includes/block-supports/colors.php
r53076 r54211 11 11 * 12 12 * @since 5.6.0 13 * @since 6.1.0 Improved $color_support assignment optimization. 13 14 * @access private 14 15 * … … 16 17 */ 17 18 function 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; 22 20 $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) ); 23 21 $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) ); … … 64 62 * 65 63 * @since 5.6.0 64 * @since 6.1.0 Implemented the style engine to generate CSS and classnames. 66 65 * @access private 67 66 * … … 84 83 $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) ); 85 84 $has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false ); 86 $classes = array(); 87 $styles = array(); 85 $color_block_styles = array(); 88 86 89 87 // Text colors. 90 // Check support for text colors.91 88 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; 105 92 } 106 93 107 94 // Background colors. 108 95 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; 122 99 } 123 100 124 101 // Gradients. 125 102 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; 138 106 } 139 107 140 108 $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']; 143 113 } 144 if ( ! empty( $styles ) ) { 145 $attributes['style'] = implode( ' ', $styles ); 114 115 if ( ! empty( $styles['css'] ) ) { 116 $attributes['style'] = $styles['css']; 146 117 } 147 118 -
trunk/src/wp-includes/block-supports/elements.php
r53260 r54211 90 90 * 91 91 * @since 6.0.0 92 * @since 6.1.0 Implemented the style engine to generate CSS and classnames. 92 93 * @access private 93 94 * … … 98 99 */ 99 100 function wp_render_elements_support_styles( $pre_render, $block ) { 100 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 101 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 102 $element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null; 103 104 /* 105 * For now we only care about link color. 106 */ 101 107 $skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ); 108 102 109 if ( $skip_link_color_serialization ) { 103 110 return null; 104 111 } 112 $class_name = wp_get_elements_class_name( $block ); 113 $link_block_styles = isset( $element_block_styles['link'] ) ? $element_block_styles['link'] : null; 105 114 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 ); 115 wp_style_engine_get_styles( 116 $link_block_styles, 117 array( 118 'selector' => ".$class_name a", 119 'context' => 'block-supports', 120 ) 121 ); 134 122 135 123 return null; -
trunk/src/wp-includes/block-supports/spacing.php
r53076 r54211 38 38 * 39 39 * @since 5.8.0 40 * @since 6.1.0 Implemented the style engine to generate CSS and classnames. 40 41 * @access private 41 42 * … … 49 50 } 50 51 52 $attributes = array(); 51 53 $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false ); 52 54 $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' ); 55 $styles = array(); 55 $block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null; 56 56 57 if ( $has_padding_support && ! $skip_padding ) { 58 $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null ); 59 if ( is_array( $padding_value ) ) { 60 foreach ( $padding_value as $key => $value ) { 61 $styles[] = sprintf( 'padding-%s: %s;', $key, $value ); 62 } 63 } elseif ( null !== $padding_value ) { 64 $styles[] = sprintf( 'padding: %s;', $padding_value ); 65 } 57 if ( ! $block_styles ) { 58 return $attributes; 66 59 } 67 60 68 if ( $has_margin_support && ! $skip_margin ) {69 $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null);70 if ( is_array( $margin_value ) ) {71 foreach ( $margin_value as $key => $value ) {72 $styles[] = sprintf( 'margin-%s: %s;', $key, $value );73 }74 } elseif ( null !== $margin_value ) { 75 $styles[] = sprintf( 'margin: %s;', $margin_value );76 }61 $skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' ); 62 $skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' ); 63 $spacing_block_styles = array(); 64 $spacing_block_styles['padding'] = $has_padding_support && ! $skip_padding ? _wp_array_get( $block_styles, array( 'spacing', 'padding' ), null ) : null; 65 $spacing_block_styles['margin'] = $has_margin_support && ! $skip_margin ? _wp_array_get( $block_styles, array( 'spacing', 'margin' ), null ) : null; 66 $styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) ); 67 68 if ( ! empty( $styles['css'] ) ) { 69 $attributes['style'] = $styles['css']; 77 70 } 78 71 79 return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );72 return $attributes; 80 73 } 81 74 -
trunk/tests/phpunit/tests/block-supports/border.php
r53680 r54211 65 65 $expected = array( 66 66 'class' => 'has-border-color has-red-border-color', 67 'style' => 'border-radius: 10px; border-style: dashed; border-width:1px;',67 'style' => 'border-radius:10px;border-style:dashed;border-width:1px;', 68 68 ); 69 69 … … 155 155 $actual = wp_apply_border_support( $block_type, $block_atts ); 156 156 $expected = array( 157 'style' => 'border-style: dotted; border-width:1px;',157 'style' => 'border-style:dotted;border-width:1px;', 158 158 ); 159 159 -
trunk/tests/phpunit/tests/block-supports/colors.php
r53680 r54211 61 61 62 62 $actual = wp_apply_colors_support( $block_type, $block_atts ); 63 $expected = array( 'class' => 'has-text-color has-fg-1-color has-background has-bg-2-background-color has- background has-gr-3-gradient-background' );63 $expected = array( 'class' => 'has-text-color has-fg-1-color has-background has-bg-2-background-color has-gr-3-gradient-background' ); 64 64 65 65 $this->assertSame( $expected, $actual ); … … 144 144 $expected = array( 145 145 'class' => 'has-text-color', 146 'style' => 'color: 146 'style' => 'color:#d92828;', 147 147 ); 148 148 -
trunk/tests/phpunit/tests/block-supports/spacing.php
r53680 r54211 64 64 $actual = wp_apply_spacing_support( $block_type, $block_atts ); 65 65 $expected = array( 66 'style' => 'padding: 111px; margin-top: 1px; margin-right: 2px; margin-bottom: 3px; margin-left:4px;',66 'style' => 'padding:111px;margin-top:1px;margin-right:2px;margin-bottom:3px;margin-left:4px;', 67 67 ); 68 68 … … 160 160 $actual = wp_apply_spacing_support( $block_type, $block_atts ); 161 161 $expected = array( 162 'style' => 'padding-top: 1px; padding-right: 2px; padding-bottom: 3px; padding-left:4px;',162 'style' => 'padding-top:1px;padding-right:2px;padding-bottom:3px;padding-left:4px;', 163 163 ); 164 164 -
trunk/tests/phpunit/tests/blocks/supportedStyles.php
r51657 r54211 221 221 ); 222 222 223 $expected_styles = 'test: style; color: #000; background-color:#fff;';223 $expected_styles = 'test: style;color:#000;background-color:#fff;'; 224 224 $expected_classes = 'foo-bar-class wp-block-example has-text-color has-background'; 225 225 … … 284 284 285 285 $expected_classes = 'foo-bar-class wp-block-example has-background'; 286 $expected_styles = 'test: style; background: 286 $expected_styles = 'test: style; background:some-gradient-style;'; 287 287 288 288 $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles ); … … 564 564 565 565 $expected_classes = 'foo-bar-class wp-block-example has-text-color has-background alignwide'; 566 $expected_styles = 'test: style; color: #000; background-color:#fff; font-size: 10px; line-height: 20;';566 $expected_styles = 'test: style; color:#000; background-color:#fff; font-size: 10px; line-height: 20;'; 567 567 568 568 $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
Note: See TracChangeset
for help on using the changeset viewer.