| @@ -5,30 +5,28 @@ | ||
| 5 | 5 | * @package gutenberg |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * Registers the style attribute used by the border feature if needed for block | |
| 10 | - * types that support borders. | |
| 9 | + * Registers the style attribute used by the border feature if needed for block types that | |
| 10 | + * support borders. | |
| 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 border related features supported. | |
| 16 | + // Border width, style etc can be added in the future. | |
| 17 | + $has_border_radius_support = gutenberg_has_border_support( $block_type, 'radius' ); | |
| 18 | + | |
| 15 | 19 | // Setup attributes and styles within that if needed. |
| 16 | 20 | if ( ! $block_type->attributes ) { |
| 17 | 21 | $block_type->attributes = array(); |
| 18 | 22 | } |
| 19 | 23 | |
| 20 | - if ( block_has_support( $block_type, array( '__experimentalBorder' ) ) && ! array_key_exists( 'style', $block_type->attributes ) ) { | |
| 24 | + if ( $has_border_radius_support && ! array_key_exists( 'style', $block_type->attributes ) ) { | |
| 21 | 25 | $block_type->attributes['style'] = array( |
| 22 | 26 | 'type' => 'object', |
| 23 | 27 | ); |
| 24 | 28 | } |
| 25 | - | |
| 26 | - if ( gutenberg_has_border_feature_support( $block_type, 'color' ) && ! array_key_exists( 'borderColor', $block_type->attributes ) ) { | |
| 27 | - $block_type->attributes['borderColor'] = array( | |
| 28 | - 'type' => 'string', | |
| 29 | - ); | |
| 30 | - } | |
| 31 | 29 | } |
| 32 | 30 | |
| 33 | 31 | /** |
| 34 | 32 | * Adds CSS classes and inline styles for border styles to the incoming |
| @@ -33,126 +31,53 @@ | ||
| 33 | 31 | /** |
| 34 | 32 | * Adds CSS classes and inline styles for border styles to the incoming |
| 35 | 33 | * attributes array. This will be applied to the block markup in the front-end. |
| 36 | 34 | * |
| 37 | - * @param WP_Block_Type $block_type Block type. | |
| 35 | + * @param WP_Block_type $block_type Block type. | |
| 38 | 36 | * @param array $block_attributes Block attributes. |
| 39 | 37 | * |
| 40 | 38 | * @return array Border CSS classes and inline styles. |
| 41 | 39 | */ |
| 42 | 40 | function gutenberg_apply_border_support( $block_type, $block_attributes ) { |
| 43 | - if ( wp_should_skip_block_supports_serialization( $block_type, 'border' ) ) { | |
| 44 | - return array(); | |
| 45 | - } | |
| 41 | + // Arrays used to ease addition of further border related features in future. | |
| 42 | + $styles = array(); | |
| 46 | 43 | |
| 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' ); | |
| 50 | - | |
| 51 | - // Border radius. | |
| 52 | - if ( | |
| 53 | - gutenberg_has_border_feature_support( $block_type, 'radius' ) && | |
| 54 | - isset( $block_attributes['style']['border']['radius'] ) && | |
| 55 | - ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ) | |
| 56 | - ) { | |
| 57 | - $border_radius = $block_attributes['style']['border']['radius']; | |
| 58 | - | |
| 59 | - if ( is_numeric( $border_radius ) ) { | |
| 60 | - $border_radius .= 'px'; | |
| 44 | + // Border Radius. | |
| 45 | + if ( gutenberg_has_border_support( $block_type, 'radius' ) ) { | |
| 46 | + if ( isset( $block_attributes['style']['border']['radius'] ) ) { | |
| 47 | + $border_radius = intval( $block_attributes['style']['border']['radius'] ); | |
| 48 | + $styles[] = sprintf( 'border-radius: %dpx;', $border_radius ); | |
| 61 | 49 | } |
| 62 | - | |
| 63 | - $border_block_styles['radius'] = $border_radius; | |
| 64 | 50 | } |
| 65 | 51 | |
| 66 | - // Border style. | |
| 67 | - if ( | |
| 68 | - gutenberg_has_border_feature_support( $block_type, 'style' ) && | |
| 69 | - isset( $block_attributes['style']['border']['style'] ) && | |
| 70 | - ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) | |
| 71 | - ) { | |
| 72 | - $border_block_styles['style'] = $block_attributes['style']['border']['style']; | |
| 73 | - } | |
| 52 | + // Border width, style etc can be added here. | |
| 74 | 53 | |
| 75 | - // Border width. | |
| 76 | - if ( | |
| 77 | - $has_border_width_support && | |
| 78 | - isset( $block_attributes['style']['border']['width'] ) && | |
| 79 | - ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) | |
| 80 | - ) { | |
| 81 | - $border_width = $block_attributes['style']['border']['width']; | |
| 82 | - | |
| 83 | - // This check handles original unitless implementation. | |
| 84 | - if ( is_numeric( $border_width ) ) { | |
| 85 | - $border_width .= 'px'; | |
| 86 | - } | |
| 87 | - | |
| 88 | - $border_block_styles['width'] = $border_width; | |
| 89 | - } | |
| 90 | - | |
| 91 | - // Border color. | |
| 92 | - if ( | |
| 93 | - $has_border_color_support && | |
| 94 | - ! wp_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 = $block_attributes['style']['border']['color'] ?? null; | |
| 98 | - $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color; | |
| 99 | - } | |
| 100 | - | |
| 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 = $block_attributes['style']['border'][ $side ] ?? null; | |
| 105 | - $border_side_values = array( | |
| 106 | - 'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null, | |
| 107 | - 'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null, | |
| 108 | - 'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null, | |
| 109 | - ); | |
| 110 | - $border_block_styles[ $side ] = $border_side_values; | |
| 111 | - } | |
| 112 | - } | |
| 113 | - | |
| 114 | 54 | // Collect classes and styles. |
| 115 | 55 | $attributes = array(); |
| 116 | - $styles = gutenberg_style_engine_get_styles( array( 'border' => $border_block_styles ) ); | |
| 117 | 56 | |
| 118 | - if ( ! empty( $styles['classnames'] ) ) { | |
| 119 | - $attributes['class'] = $styles['classnames']; | |
| 57 | + if ( ! empty( $styles ) ) { | |
| 58 | + $attributes['style'] = implode( ' ', $styles ); | |
| 120 | 59 | } |
| 121 | 60 | |
| 122 | - if ( ! empty( $styles['css'] ) ) { | |
| 123 | - $attributes['style'] = $styles['css']; | |
| 124 | - } | |
| 125 | - | |
| 126 | 61 | return $attributes; |
| 127 | 62 | } |
| 128 | 63 | |
| 129 | 64 | /** |
| 130 | - * Checks whether the current block type supports the border feature requested. | |
| 65 | + * Checks whether the current block type supports the feature requested. | |
| 131 | 66 | * |
| 132 | - * If the `__experimentalBorder` support flag is a boolean `true` all border | |
| 133 | - * support features are available. Otherwise, the specific feature's support | |
| 134 | - * flag nested under `experimentalBorder` must be enabled for the feature | |
| 135 | - * to be opted into. | |
| 67 | + * @param WP_Block_Type $block_type Block type to check for support. | |
| 68 | + * @param string $feature Name of the feature to check support for. | |
| 69 | + * @param mixed $default Fallback value for feature support, defaults to false. | |
| 136 | 70 | * |
| 137 | - * @param WP_Block_Type $block_type Block type to check for support. | |
| 138 | - * @param string $feature Name of the feature to check support for. | |
| 139 | - * @param mixed $default_value Fallback value for feature support, defaults to false. | |
| 140 | - * | |
| 141 | 71 | * @return boolean Whether or not the feature is supported. |
| 142 | 72 | */ |
| 143 | -function gutenberg_has_border_feature_support( $block_type, $feature, $default_value = false ) { | |
| 144 | - // Check if all border support features have been opted into via `"__experimentalBorder": true`. | |
| 145 | - if ( $block_type instanceof WP_Block_Type ) { | |
| 146 | - $block_type_supports_border = $block_type->supports['__experimentalBorder'] ?? $default_value; | |
| 147 | - if ( true === $block_type_supports_border ) { | |
| 148 | - return true; | |
| 149 | - } | |
| 73 | +function gutenberg_has_border_support( $block_type, $feature, $default = false ) { | |
| 74 | + $block_support = false; | |
| 75 | + if ( property_exists( $block_type, 'supports' ) ) { | |
| 76 | + $block_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalBorder' ), $default ); | |
| 150 | 77 | } |
| 151 | 78 | |
| 152 | - // Check if the specific feature has been opted into individually | |
| 153 | - // via nested flag under `__experimentalBorder`. | |
| 154 | - return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value ); | |
| 79 | + return true === $block_support || ( is_array( $block_support ) && gutenberg_experimental_get( $block_support, array( $feature ), false ) ); | |
| 155 | 80 | } |
| 156 | 81 | |
| 157 | 82 | // Register the block support. |
| 158 | 83 | WP_Block_Supports::get_instance()->register( |