| @@ -5,17 +5,17 @@ | ||
| 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 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' ); | |
| 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 | 18 | |
| 19 | 19 | // Setup attributes and styles within that if needed. |
| 20 | 20 | if ( ! $block_type->attributes ) { |
| 21 | 21 | $block_type->attributes = array(); |
| @@ -20,19 +20,13 @@ | ||
| 20 | 20 | if ( ! $block_type->attributes ) { |
| 21 | 21 | $block_type->attributes = array(); |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - if ( $has_border_support && ! array_key_exists( 'style', $block_type->attributes ) ) { | |
| 24 | + if ( $has_border_radius_support && ! array_key_exists( 'style', $block_type->attributes ) ) { | |
| 25 | 25 | $block_type->attributes['style'] = array( |
| 26 | 26 | 'type' => 'object', |
| 27 | 27 | ); |
| 28 | 28 | } |
| 29 | - | |
| 30 | - if ( $has_border_color_support && ! array_key_exists( 'borderColor', $block_type->attributes ) ) { | |
| 31 | - $block_type->attributes['borderColor'] = array( | |
| 32 | - 'type' => 'string', | |
| 33 | - ); | |
| 34 | - } | |
| 35 | 29 | } |
| 36 | 30 | |
| 37 | 31 | /** |
| 38 | 32 | * Adds CSS classes and inline styles for border styles to the incoming |
| @@ -43,87 +37,24 @@ | ||
| 43 | 37 | * |
| 44 | 38 | * @return array Border CSS classes and inline styles. |
| 45 | 39 | */ |
| 46 | 40 | function gutenberg_apply_border_support( $block_type, $block_attributes ) { |
| 47 | - if ( gutenberg_skip_border_serialization( $block_type ) ) { | |
| 48 | - return array(); | |
| 49 | - } | |
| 41 | + // Arrays used to ease addition of further border related features in future. | |
| 42 | + $styles = array(); | |
| 50 | 43 | |
| 51 | - $classes = array(); | |
| 52 | - $styles = array(); | |
| 53 | - | |
| 54 | - // Border radius. | |
| 55 | - if ( | |
| 56 | - gutenberg_has_border_feature_support( $block_type, 'radius' ) && | |
| 57 | - isset( $block_attributes['style']['border']['radius'] ) | |
| 58 | - ) { | |
| 59 | - $border_radius = $block_attributes['style']['border']['radius']; | |
| 60 | - | |
| 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 | - } | |
| 73 | - | |
| 74 | - $styles[] = sprintf( 'border-radius: %s;', $border_radius ); | |
| 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 ); | |
| 75 | 49 | } |
| 76 | 50 | } |
| 77 | 51 | |
| 78 | - // Border style. | |
| 79 | - if ( | |
| 80 | - gutenberg_has_border_feature_support( $block_type, 'style' ) && | |
| 81 | - isset( $block_attributes['style']['border']['style'] ) | |
| 82 | - ) { | |
| 83 | - $border_style = $block_attributes['style']['border']['style']; | |
| 84 | - $styles[] = sprintf( 'border-style: %s;', $border_style ); | |
| 85 | - } | |
| 52 | + // Border width, style etc can be added here. | |
| 86 | 53 | |
| 87 | - // Border width. | |
| 88 | - if ( | |
| 89 | - gutenberg_has_border_feature_support( $block_type, 'width' ) && | |
| 90 | - isset( $block_attributes['style']['border']['width'] ) | |
| 91 | - ) { | |
| 92 | - $border_width = $block_attributes['style']['border']['width']; | |
| 93 | - | |
| 94 | - // This check handles original unitless implementation. | |
| 95 | - if ( is_numeric( $border_width ) ) { | |
| 96 | - $border_width .= 'px'; | |
| 97 | - } | |
| 98 | - | |
| 99 | - $styles[] = sprintf( 'border-width: %s;', $border_width ); | |
| 100 | - } | |
| 101 | - | |
| 102 | - // 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'] ); | |
| 106 | - | |
| 107 | - if ( $has_named_border_color || $has_custom_border_color ) { | |
| 108 | - $classes[] = 'has-border-color'; | |
| 109 | - } | |
| 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 | - } | |
| 118 | - | |
| 119 | 54 | // Collect classes and styles. |
| 120 | 55 | $attributes = array(); |
| 121 | 56 | |
| 122 | - if ( ! empty( $classes ) ) { | |
| 123 | - $attributes['class'] = implode( ' ', $classes ); | |
| 124 | - } | |
| 125 | - | |
| 126 | 57 | if ( ! empty( $styles ) ) { |
| 127 | 58 | $attributes['style'] = implode( ' ', $styles ); |
| 128 | 59 | } |
| 129 | 60 | |
| @@ -130,31 +61,10 @@ | ||
| 130 | 61 | return $attributes; |
| 131 | 62 | } |
| 132 | 63 | |
| 133 | 64 | /** |
| 134 | - * Checks whether serialization of the current block's border properties should | |
| 135 | - * occur. | |
| 65 | + * Checks whether the current block type supports the feature requested. | |
| 136 | 66 | * |
| 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 | - * Checks whether the current block type supports the border feature requested. | |
| 151 | - * | |
| 152 | - * If the `__experimentalBorder` support flag is a boolean `true` all border | |
| 153 | - * support features are available. Otherwise, the specific feature's support | |
| 154 | - * flag nested under `experimentalBorder` must be enabled for the feature | |
| 155 | - * to be opted into. | |
| 156 | - * | |
| 157 | 67 | * @param WP_Block_Type $block_type Block type to check for support. |
| 158 | 68 | * @param string $feature Name of the feature to check support for. |
| 159 | 69 | * @param mixed $default Fallback value for feature support, defaults to false. |
| 160 | 70 | * |
| @@ -159,20 +69,15 @@ | ||
| 159 | 69 | * @param mixed $default Fallback value for feature support, defaults to false. |
| 160 | 70 | * |
| 161 | 71 | * @return boolean Whether or not the feature is supported. |
| 162 | 72 | */ |
| 163 | -function gutenberg_has_border_feature_support( $block_type, $feature, $default = false ) { | |
| 164 | - // Check if all border support features have been opted into via `"__experimentalBorder": true`. | |
| 165 | - if ( | |
| 166 | - property_exists( $block_type, 'supports' ) && | |
| 167 | - ( true === _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default ) ) | |
| 168 | - ) { | |
| 169 | - return true; | |
| 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 ); | |
| 170 | 77 | } |
| 171 | 78 | |
| 172 | - // Check if the specific feature has been opted into individually | |
| 173 | - // via nested flag under `__experimentalBorder`. | |
| 174 | - return gutenberg_block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default ); | |
| 79 | + return true === $block_support || ( is_array( $block_support ) && gutenberg_experimental_get( $block_support, array( $feature ), false ) ); | |
| 175 | 80 | } |
| 176 | 81 | |
| 177 | 82 | // Register the block support. |
| 178 | 83 | WP_Block_Supports::get_instance()->register( |