Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 93 additions & 58 deletions lib/block-supports/border.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,76 +44,111 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
return array();
}

$border_block_styles = array();
$has_border_color_support = gutenberg_has_border_feature_support( $block_type, 'color' );
$has_border_width_support = gutenberg_has_border_feature_support( $block_type, 'width' );

// Border radius.
if (
gutenberg_has_border_feature_support( $block_type, 'radius' ) &&
isset( $block_attributes['style']['border']['radius'] ) &&
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
) {
$border_radius = $block_attributes['style']['border']['radius'];

if ( is_numeric( $border_radius ) ) {
$border_radius .= 'px';
}
$has_border_color_support = gutenberg_has_border_feature_support( $block_type, 'color' );
$has_border_radius_support = gutenberg_has_border_feature_support( $block_type, 'radius' );
$has_border_style_support = gutenberg_has_border_feature_support( $block_type, 'style' );
$has_border_width_support = gutenberg_has_border_feature_support( $block_type, 'width' );
$skip_color = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' );
$skip_radius = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' );
$skip_style = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' );
$skip_width = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' );

// The unset() calls below fail on a non-array style or border.
if ( ! isset( $block_attributes['style']['border'] ) || ! is_array( $block_attributes['style']['border'] ) ) {
unset( $block_attributes['style'] );
}

if ( ! $has_border_radius_support || $skip_radius ) {
unset( $block_attributes['style']['border']['radius'] );
}

$border_block_styles['radius'] = $border_radius;
if ( ! $has_border_style_support || $skip_style ) {
unset( $block_attributes['style']['border']['style'] );
}

// Border style.
if (
gutenberg_has_border_feature_support( $block_type, 'style' ) &&
isset( $block_attributes['style']['border']['style'] ) &&
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
) {
$border_block_styles['style'] = $block_attributes['style']['border']['style'];
if ( ! $has_border_width_support || $skip_width ) {
unset( $block_attributes['style']['border']['width'] );
}

// Border width.
if (
$has_border_width_support &&
isset( $block_attributes['style']['border']['width'] ) &&
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
) {
$border_width = $block_attributes['style']['border']['width'];
if ( ! $has_border_color_support || $skip_color ) {
unset( $block_attributes['style']['border']['color'], $block_attributes['borderColor'] );
}

// Side values follow skipped serialization only, as on trunk.
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
$has_side = isset( $block_attributes['style']['border'][ $side ] ) && is_array( $block_attributes['style']['border'][ $side ] );

if ( ! $has_side || ( ! $has_border_color_support && ! $has_border_width_support ) ) {
unset( $block_attributes['style']['border'][ $side ] );
continue;
}

if ( $skip_width ) {
unset( $block_attributes['style']['border'][ $side ]['width'] );
}

// This check handles original unitless implementation.
if ( is_numeric( $border_width ) ) {
$border_width .= 'px';
if ( $skip_color ) {
unset( $block_attributes['style']['border'][ $side ]['color'] );
}

$border_block_styles['width'] = $border_width;
}

// Border color.
if (
$has_border_color_support &&
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
) {
$preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
$custom_border_color = $block_attributes['style']['border']['color'] ?? null;
$border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
}

// Generate styles for individual border sides.
if ( $has_border_color_support || $has_border_width_support ) {
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
$border = $block_attributes['style']['border'][ $side ] ?? null;
$border_side_values = array(
'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null,
);
$border_block_styles[ $side ] = $border_side_values;
if ( $skip_style ) {
unset( $block_attributes['style']['border'][ $side ]['style'] );
}
}

// Collect classes and styles.
return gutenberg_get_border_classes_and_styles( $block_attributes );
}

/**
* Returns border classes and inline styles for block attributes, like the JS
* `getBorderClassesAndStyles()`. Does not check block support or skipped
* serialization.
*
* @since 7.3.0
*
* @param array $block_attributes Block attributes.
* @return array Array with `class` and `style` keys, each present only when non-empty.
*/
function gutenberg_get_border_classes_and_styles( $block_attributes ) {
if ( ! is_array( $block_attributes ) ) {
return array();
}

$border = isset( $block_attributes['style']['border'] ) && is_array( $block_attributes['style']['border'] )
? $block_attributes['style']['border']
: array();
$border_styles = array();

// Unitless radius and width are from the original implementation.
if ( isset( $border['radius'] ) ) {
$border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius'];
}

if ( isset( $border['style'] ) ) {
$border_styles['style'] = $border['style'];
}

if ( isset( $border['width'] ) ) {
$border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width'];
}

// Preset slugs are strings in theme.json and the editor.
$preset_slug = $block_attributes['borderColor'] ?? null;
$border_styles['color'] = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|color|{$preset_slug}" : ( $border['color'] ?? null );

foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
if ( ! isset( $border[ $side ] ) || ! is_array( $border[ $side ] ) ) {
continue;
}
$border_styles[ $side ] = array(
'width' => $border[ $side ]['width'] ?? null,
'color' => $border[ $side ]['color'] ?? null,
'style' => $border[ $side ]['style'] ?? null,
);
}

$attributes = array();
$styles = gutenberg_style_engine_get_styles( array( 'border' => $border_block_styles ) );
$styles = gutenberg_style_engine_get_styles( array( 'border' => $border_styles ) );

if ( ! empty( $styles['classnames'] ) ) {
$attributes['class'] = $styles['classnames'];
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"prefixFunctions": [
"wp_apply_colors_support",
"wp_get_color_classes_and_styles",
"wp_get_border_classes_and_styles",
"wp_enqueue_block_support_styles",
"wp_get_typography_font_size_value",
"wp_get_shadow_classes_and_styles",
Expand Down
43 changes: 1 addition & 42 deletions packages/block-library/src/avatar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,48 +108,7 @@ function render_block_core_avatar( $attributes, $content, $block ) {
* @return array The border-related classnames and styles for the block.
*/
function get_block_core_avatar_border_attributes( $attributes ) {
$border_styles = array();
$sides = array( 'top', 'right', 'bottom', 'left' );

// Border radius.
if ( isset( $attributes['style']['border']['radius'] ) ) {
$border_styles['radius'] = $attributes['style']['border']['radius'];
}

// Border style.
if ( isset( $attributes['style']['border']['style'] ) ) {
$border_styles['style'] = $attributes['style']['border']['style'];
}

// Border width.
if ( isset( $attributes['style']['border']['width'] ) ) {
$border_styles['width'] = $attributes['style']['border']['width'];
}

// Border color.
$preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
$custom_color = $attributes['style']['border']['color'] ?? null;
$border_styles['color'] = $preset_color ? $preset_color : $custom_color;

// Individual border styles e.g. top, left etc.
foreach ( $sides as $side ) {
$border = $attributes['style']['border'][ $side ] ?? null;
$border_styles[ $side ] = array(
'color' => $border['color'] ?? null,
'style' => $border['style'] ?? null,
'width' => $border['width'] ?? null,
);
}

$styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
$attributes = array();
if ( ! empty( $styles['classnames'] ) ) {
$attributes['class'] = $styles['classnames'];
}
if ( ! empty( $styles['css'] ) ) {
$attributes['style'] = $styles['css'];
}
return $attributes;
return wp_get_border_classes_and_styles( $attributes );
}

/**
Expand Down
48 changes: 12 additions & 36 deletions packages/block-library/src/icon/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,6 @@ function render_block_core_icon( $attributes ) {
}
$background_styles = wp_get_background_classes_and_styles( array( 'style' => array( 'background' => $background ) ) );

// Border.
$border_styles = array();
$sides = array( 'top', 'right', 'bottom', 'left' );

if ( isset( $attributes['style']['border']['radius'] ) ) {
$border_styles['radius'] = $attributes['style']['border']['radius'];
}
if ( isset( $attributes['style']['border']['style'] ) ) {
$border_styles['style'] = $attributes['style']['border']['style'];
}
if ( isset( $attributes['style']['border']['width'] ) ) {
$border_styles['width'] = $attributes['style']['border']['width'];
}

$preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
$custom_color = $attributes['style']['border']['color'] ?? null;
$border_styles['color'] = $preset_color ? $preset_color : $custom_color;

foreach ( $sides as $side ) {
$border = $attributes['style']['border'][ $side ] ?? null;
$border_styles[ $side ] = array(
'color' => $border['color'] ?? null,
'style' => $border['style'] ?? null,
'width' => $border['width'] ?? null,
);
}

// Shadow.
$shadow_styles = $attributes['style']['shadow'] ?? null;

Expand All @@ -84,16 +57,16 @@ function render_block_core_icon( $attributes ) {
}
}

// Generate styles and classes.
$styles = wp_style_engine_get_styles(
// Border sits between color and shadow in the style engine's order.
$styles_before_border = wp_style_engine_get_styles( array( 'color' => $color_styles ) );
$border_styles = wp_get_border_classes_and_styles( $attributes );
$styles_after_border = wp_style_engine_get_styles(
array(
'color' => $color_styles,
'border' => $border_styles,
'shadow' => $shadow_styles,
'dimensions' => $dimensions_styles,
),
)
);
$spacing_styles = wp_get_spacing_classes_and_styles(
$spacing_styles = wp_get_spacing_classes_and_styles(
array(
'style' => array(
'spacing' => array(
Expand All @@ -102,17 +75,20 @@ function render_block_core_icon( $attributes ) {
),
)
);
$styles['css'] = ( $styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' );
$styles = array(
'classnames' => implode( ' ', array_filter( array( $styles_before_border['classnames'] ?? '', $border_styles['class'] ?? '', $styles_after_border['classnames'] ?? '' ) ) ),
'css' => ( $styles_before_border['css'] ?? '' ) . ( $border_styles['style'] ?? '' ) . ( $styles_after_border['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' ),
);

// The style engine outputs background before the other groups, so it leads here too.
$css = ( $background_styles['style'] ?? '' ) . ( $styles['css'] ?? '' );
$css = ( $background_styles['style'] ?? '' ) . $styles['css'];

$svg = wp_get_icon(
$attributes['icon'],
array(
// Width is applied via the dimensions block support styles below.
'size' => null,
'class' => $styles['classnames'] ?? '',
'class' => $styles['classnames'],
'label' => $attributes['ariaLabel'] ?? '',
)
);
Expand Down
43 changes: 1 addition & 42 deletions packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,48 +209,7 @@ function get_block_core_post_featured_image_overlay_element_markup( $attributes
* @return array The border-related classnames and styles for the block.
*/
function get_block_core_post_featured_image_border_attributes( $attributes ) {
$border_styles = array();
$sides = array( 'top', 'right', 'bottom', 'left' );

// Border radius.
if ( isset( $attributes['style']['border']['radius'] ) ) {
$border_styles['radius'] = $attributes['style']['border']['radius'];
}

// Border style.
if ( isset( $attributes['style']['border']['style'] ) ) {
$border_styles['style'] = $attributes['style']['border']['style'];
}

// Border width.
if ( isset( $attributes['style']['border']['width'] ) ) {
$border_styles['width'] = $attributes['style']['border']['width'];
}

// Border color.
$preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
$custom_color = $attributes['style']['border']['color'] ?? null;
$border_styles['color'] = $preset_color ? $preset_color : $custom_color;

// Individual border styles e.g. top, left etc.
foreach ( $sides as $side ) {
$border = $attributes['style']['border'][ $side ] ?? null;
$border_styles[ $side ] = array(
'color' => $border['color'] ?? null,
'style' => $border['style'] ?? null,
'width' => $border['width'] ?? null,
);
}

$styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
$attributes = array();
if ( ! empty( $styles['classnames'] ) ) {
$attributes['class'] = $styles['classnames'];
}
if ( ! empty( $styles['css'] ) ) {
$attributes['style'] = $styles['css'];
}
return $attributes;
return wp_get_border_classes_and_styles( $attributes );
}

/**
Expand Down
Loading
Loading