| @@ -11,24 +11,20 @@ | ||
| 11 | 11 | * |
| 12 | 12 | * @param WP_Block_Type $block_type Block Type. |
| 13 | 13 | */ |
| 14 | 14 | function gutenberg_register_border_support( $block_type ) { |
| 15 | - // Determine if any border related features are supported. | |
| 16 | - $has_border_support = gutenberg_block_has_support( $block_type, array( '__experimentalBorder' ) ); | |
| 17 | - $has_border_color_support = gutenberg_has_border_feature_support( $block_type, 'color' ); | |
| 18 | - | |
| 19 | 15 | // Setup attributes and styles within that if needed. |
| 20 | 16 | if ( ! $block_type->attributes ) { |
| 21 | 17 | $block_type->attributes = array(); |
| 22 | 18 | } |
| 23 | 19 | |
| 24 | - if ( $has_border_support && ! array_key_exists( 'style', $block_type->attributes ) ) { | |
| 20 | + if ( block_has_support( $block_type, array( '__experimentalBorder' ) ) && ! array_key_exists( 'style', $block_type->attributes ) ) { | |
| 25 | 21 | $block_type->attributes['style'] = array( |
| 26 | 22 | 'type' => 'object', |
| 27 | 23 | ); |
| 28 | 24 | } |
| 29 | 25 | |
| 30 | - if ( $has_border_color_support && ! array_key_exists( 'borderColor', $block_type->attributes ) ) { | |
| 26 | + if ( gutenberg_has_border_feature_support( $block_type, 'color' ) && ! array_key_exists( 'borderColor', $block_type->attributes ) ) { | |
| 31 | 27 | $block_type->attributes['borderColor'] = array( |
| 32 | 28 | 'type' => 'string', |
| 33 | 29 | ); |
| 34 | 30 | } |
| @@ -37,58 +33,51 @@ | ||
| 37 | 33 | /** |
| 38 | 34 | * Adds CSS classes and inline styles for border styles to the incoming |
| 39 | 35 | * attributes array. This will be applied to the block markup in the front-end. |
| 40 | 36 | * |
| 41 | - * @param WP_Block_type $block_type Block type. | |
| 37 | + * @param WP_Block_Type $block_type Block type. | |
| 42 | 38 | * @param array $block_attributes Block attributes. |
| 43 | 39 | * |
| 44 | 40 | * @return array Border CSS classes and inline styles. |
| 45 | 41 | */ |
| 46 | 42 | function gutenberg_apply_border_support( $block_type, $block_attributes ) { |
| 47 | - if ( gutenberg_skip_border_serialization( $block_type ) ) { | |
| 43 | + if ( gutenberg_should_skip_block_supports_serialization( $block_type, 'border' ) ) { | |
| 48 | 44 | return array(); |
| 49 | 45 | } |
| 50 | 46 | |
| 51 | - $classes = array(); | |
| 52 | - $styles = array(); | |
| 47 | + $border_block_styles = array(); | |
| 48 | + $has_border_color_support = gutenberg_has_border_feature_support( $block_type, 'color' ); | |
| 49 | + $has_border_width_support = gutenberg_has_border_feature_support( $block_type, 'width' ); | |
| 53 | 50 | |
| 54 | 51 | // Border radius. |
| 55 | 52 | if ( |
| 56 | 53 | gutenberg_has_border_feature_support( $block_type, 'radius' ) && |
| 57 | - isset( $block_attributes['style']['border']['radius'] ) | |
| 54 | + isset( $block_attributes['style']['border']['radius'] ) && | |
| 55 | + ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ) | |
| 58 | 56 | ) { |
| 59 | 57 | $border_radius = $block_attributes['style']['border']['radius']; |
| 60 | 58 | |
| 61 | - if ( is_array( $border_radius ) ) { | |
| 62 | - // We have individual border radius corner values. | |
| 63 | - foreach ( $border_radius as $key => $radius ) { | |
| 64 | - // Convert CamelCase corner name to kebab-case. | |
| 65 | - $corner = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) ); | |
| 66 | - $styles[] = sprintf( 'border-%s-radius: %s;', $corner, $radius ); | |
| 67 | - } | |
| 68 | - } else { | |
| 69 | - // This check handles original unitless implementation. | |
| 70 | - if ( is_numeric( $border_radius ) ) { | |
| 71 | - $border_radius .= 'px'; | |
| 72 | - } | |
| 59 | + if ( is_numeric( $border_radius ) ) { | |
| 60 | + $border_radius .= 'px'; | |
| 61 | + } | |
| 73 | 62 | |
| 74 | - $styles[] = sprintf( 'border-radius: %s;', $border_radius ); | |
| 75 | - } | |
| 63 | + $border_block_styles['radius'] = $border_radius; | |
| 76 | 64 | } |
| 77 | 65 | |
| 78 | 66 | // Border style. |
| 79 | 67 | if ( |
| 80 | 68 | gutenberg_has_border_feature_support( $block_type, 'style' ) && |
| 81 | - isset( $block_attributes['style']['border']['style'] ) | |
| 69 | + isset( $block_attributes['style']['border']['style'] ) && | |
| 70 | + ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) | |
| 82 | 71 | ) { |
| 83 | - $border_style = $block_attributes['style']['border']['style']; | |
| 84 | - $styles[] = sprintf( 'border-style: %s;', $border_style ); | |
| 72 | + $border_block_styles['style'] = $block_attributes['style']['border']['style']; | |
| 85 | 73 | } |
| 86 | 74 | |
| 87 | 75 | // Border width. |
| 88 | 76 | if ( |
| 89 | - gutenberg_has_border_feature_support( $block_type, 'width' ) && | |
| 90 | - isset( $block_attributes['style']['border']['width'] ) | |
| 77 | + $has_border_width_support && | |
| 78 | + isset( $block_attributes['style']['border']['width'] ) && | |
| 79 | + ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) | |
| 91 | 80 | ) { |
| 92 | 81 | $border_width = $block_attributes['style']['border']['width']; |
| 93 | 82 | |
| 94 | 83 | // This check handles original unitless implementation. |
| @@ -95,37 +84,44 @@ | ||
| 95 | 84 | if ( is_numeric( $border_width ) ) { |
| 96 | 85 | $border_width .= 'px'; |
| 97 | 86 | } |
| 98 | 87 | |
| 99 | - $styles[] = sprintf( 'border-width: %s;', $border_width ); | |
| 88 | + $border_block_styles['width'] = $border_width; | |
| 100 | 89 | } |
| 101 | 90 | |
| 102 | 91 | // Border color. |
| 103 | - if ( gutenberg_has_border_feature_support( $block_type, 'color' ) ) { | |
| 104 | - $has_named_border_color = array_key_exists( 'borderColor', $block_attributes ); | |
| 105 | - $has_custom_border_color = isset( $block_attributes['style']['border']['color'] ); | |
| 92 | + if ( | |
| 93 | + $has_border_color_support && | |
| 94 | + ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) | |
| 95 | + ) { | |
| 96 | + $preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null; | |
| 97 | + $custom_border_color = _wp_array_get( $block_attributes, array( 'style', 'border', 'color' ), null ); | |
| 98 | + $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color; | |
| 99 | + } | |
| 106 | 100 | |
| 107 | - if ( $has_named_border_color || $has_custom_border_color ) { | |
| 108 | - $classes[] = 'has-border-color'; | |
| 101 | + // Generate styles for individual border sides. | |
| 102 | + if ( $has_border_color_support || $has_border_width_support ) { | |
| 103 | + foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { | |
| 104 | + $border = _wp_array_get( $block_attributes, array( 'style', 'border', $side ), null ); | |
| 105 | + $border_side_values = array( | |
| 106 | + 'width' => isset( $border['width'] ) && ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null, | |
| 107 | + 'color' => isset( $border['color'] ) && ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null, | |
| 108 | + 'style' => isset( $border['style'] ) && ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null, | |
| 109 | + ); | |
| 110 | + $border_block_styles[ $side ] = $border_side_values; | |
| 109 | 111 | } |
| 110 | - | |
| 111 | - if ( $has_named_border_color ) { | |
| 112 | - $classes[] = sprintf( 'has-%s-border-color', $block_attributes['borderColor'] ); | |
| 113 | - } elseif ( $has_custom_border_color ) { | |
| 114 | - $border_color = $block_attributes['style']['border']['color']; | |
| 115 | - $styles[] = sprintf( 'border-color: %s;', $border_color ); | |
| 116 | - } | |
| 117 | 112 | } |
| 118 | 113 | |
| 119 | 114 | // Collect classes and styles. |
| 120 | 115 | $attributes = array(); |
| 116 | + $styles = gutenberg_style_engine_get_styles( array( 'border' => $border_block_styles ) ); | |
| 121 | 117 | |
| 122 | - if ( ! empty( $classes ) ) { | |
| 123 | - $attributes['class'] = implode( ' ', $classes ); | |
| 118 | + if ( ! empty( $styles['classnames'] ) ) { | |
| 119 | + $attributes['class'] = $styles['classnames']; | |
| 124 | 120 | } |
| 125 | 121 | |
| 126 | - if ( ! empty( $styles ) ) { | |
| 127 | - $attributes['style'] = implode( ' ', $styles ); | |
| 122 | + if ( ! empty( $styles['css'] ) ) { | |
| 123 | + $attributes['style'] = $styles['css']; | |
| 128 | 124 | } |
| 129 | 125 | |
| 130 | 126 | return $attributes; |
| 131 | 127 | } |
| @@ -130,24 +126,8 @@ | ||
| 130 | 126 | return $attributes; |
| 131 | 127 | } |
| 132 | 128 | |
| 133 | 129 | /** |
| 134 | - * Checks whether serialization of the current block's border properties should | |
| 135 | - * occur. | |
| 136 | - * | |
| 137 | - * @param WP_Block_type $block_type Block type. | |
| 138 | - * | |
| 139 | - * @return boolean | |
| 140 | - */ | |
| 141 | -function gutenberg_skip_border_serialization( $block_type ) { | |
| 142 | - $border_support = _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), false ); | |
| 143 | - | |
| 144 | - return is_array( $border_support ) && | |
| 145 | - array_key_exists( '__experimentalSkipSerialization', $border_support ) && | |
| 146 | - $border_support['__experimentalSkipSerialization']; | |
| 147 | -} | |
| 148 | - | |
| 149 | -/** | |
| 150 | 130 | * Checks whether the current block type supports the border feature requested. |
| 151 | 131 | * |
| 152 | 132 | * If the `__experimentalBorder` support flag is a boolean `true` all border |
| 153 | 133 | * support features are available. Otherwise, the specific feature's support |
| @@ -170,9 +150,9 @@ | ||
| 170 | 150 | } |
| 171 | 151 | |
| 172 | 152 | // Check if the specific feature has been opted into individually |
| 173 | 153 | // via nested flag under `__experimentalBorder`. |
| 174 | - return gutenberg_block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default ); | |
| 154 | + return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default ); | |
| 175 | 155 | } |
| 176 | 156 | |
| 177 | 157 | // Register the block support. |
| 178 | 158 | WP_Block_Supports::get_instance()->register( |