wp_apply_border_support

The timeline below displays how wordpress function wp_apply_border_support has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Adds CSS classes and inline styles for border styles to the incoming
 * attributes array. This will be applied to the block markup in the front-end.
 *
 * @since 5.8.0
 * @since 6.1.0 Implemented the style engine to generate CSS and classnames.
 * @access private
 *
 * @param WP_Block_Type $block_type       Block type.
 * @param array         $block_attributes Block attributes.
 * @return array Border CSS classes and inline styles.
 */
function wp_apply_border_support($block_type, $block_attributes)
{
    if (wp_should_skip_block_supports_serialization($block_type, 'border')) {
        return array();
    }
    $border_block_styles = array();
    $has_border_color_support = wp_has_border_feature_support($block_type, 'color');
    $has_border_width_support = wp_has_border_feature_support($block_type, 'width');
    // Border radius.
    if (wp_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';
        }
        $border_block_styles['radius'] = $border_radius;
    }
    // Border style.
    if (wp_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'];
    }
    // 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'];
        // This check handles original unitless implementation.
        if (is_numeric($border_width)) {
            $border_width .= 'px';
        }
        $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 = isset($block_attributes['style']['border']['color']) ? $block_attributes['style']['border']['color'] : null;
        $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
    }
    // Generates styles for individual border sides.
    if ($has_border_color_support || $has_border_width_support) {
        foreach (array('top', 'right', 'bottom', 'left') as $side) {
            $border = isset($block_attributes['style']['border'][$side]) ? $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;
        }
    }
    // Collect classes and styles.
    $attributes = array();
    $styles = wp_style_engine_get_styles(array('border' => $border_block_styles));
    if (!empty($styles['classnames'])) {
        $attributes['class'] = $styles['classnames'];
    }
    if (!empty($styles['css'])) {
        $attributes['style'] = $styles['css'];
    }
    return $attributes;
}

WordPress Version: 6.1

/**
 * Adds CSS classes and inline styles for border styles to the incoming
 * attributes array. This will be applied to the block markup in the front-end.
 *
 * @since 5.8.0
 * @since 6.1.0 Implemented the style engine to generate CSS and classnames.
 * @access private
 *
 * @param WP_Block_Type $block_type       Block type.
 * @param array         $block_attributes Block attributes.
 * @return array Border CSS classes and inline styles.
 */
function wp_apply_border_support($block_type, $block_attributes)
{
    if (wp_should_skip_block_supports_serialization($block_type, 'border')) {
        return array();
    }
    $border_block_styles = array();
    $has_border_color_support = wp_has_border_feature_support($block_type, 'color');
    $has_border_width_support = wp_has_border_feature_support($block_type, 'width');
    // Border radius.
    if (wp_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';
        }
        $border_block_styles['radius'] = $border_radius;
    }
    // Border style.
    if (wp_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'];
    }
    // 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'];
        // This check handles original unitless implementation.
        if (is_numeric($border_width)) {
            $border_width .= 'px';
        }
        $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 = _wp_array_get($block_attributes, array('style', 'border', 'color'), null);
        $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
    }
    // Generates styles for individual border sides.
    if ($has_border_color_support || $has_border_width_support) {
        foreach (array('top', 'right', 'bottom', 'left') as $side) {
            $border = _wp_array_get($block_attributes, array('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;
        }
    }
    // Collect classes and styles.
    $attributes = array();
    $styles = wp_style_engine_get_styles(array('border' => $border_block_styles));
    if (!empty($styles['classnames'])) {
        $attributes['class'] = $styles['classnames'];
    }
    if (!empty($styles['css'])) {
        $attributes['style'] = $styles['css'];
    }
    return $attributes;
}

WordPress Version: 5.9

/**
 * Adds CSS classes and inline styles for border styles to the incoming
 * attributes array. This will be applied to the block markup in the front-end.
 *
 * @since 5.8.0
 * @access private
 *
 * @param WP_Block_Type $block_type       Block type.
 * @param array         $block_attributes Block attributes.
 * @return array Border CSS classes and inline styles.
 */
function wp_apply_border_support($block_type, $block_attributes)
{
    if (wp_skip_border_serialization($block_type)) {
        return array();
    }
    $classes = array();
    $styles = array();
    // Border radius.
    if (wp_has_border_feature_support($block_type, 'radius') && isset($block_attributes['style']['border']['radius'])) {
        $border_radius = $block_attributes['style']['border']['radius'];
        if (is_array($border_radius)) {
            // We have individual border radius corner values.
            foreach ($border_radius as $key => $radius) {
                // Convert CamelCase corner name to kebab-case.
                $corner = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $key));
                $styles[] = sprintf('border-%s-radius: %s;', $corner, $radius);
            }
        } else {
            // This check handles original unitless implementation.
            if (is_numeric($border_radius)) {
                $border_radius .= 'px';
            }
            $styles[] = sprintf('border-radius: %s;', $border_radius);
        }
    }
    // Border style.
    if (wp_has_border_feature_support($block_type, 'style') && isset($block_attributes['style']['border']['style'])) {
        $border_style = $block_attributes['style']['border']['style'];
        $styles[] = sprintf('border-style: %s;', $border_style);
    }
    // Border width.
    if (wp_has_border_feature_support($block_type, 'width') && isset($block_attributes['style']['border']['width'])) {
        $border_width = $block_attributes['style']['border']['width'];
        // This check handles original unitless implementation.
        if (is_numeric($border_width)) {
            $border_width .= 'px';
        }
        $styles[] = sprintf('border-width: %s;', $border_width);
    }
    // Border color.
    if (wp_has_border_feature_support($block_type, 'color')) {
        $has_named_border_color = array_key_exists('borderColor', $block_attributes);
        $has_custom_border_color = isset($block_attributes['style']['border']['color']);
        if ($has_named_border_color || $has_custom_border_color) {
            $classes[] = 'has-border-color';
        }
        if ($has_named_border_color) {
            $classes[] = sprintf('has-%s-border-color', $block_attributes['borderColor']);
        } elseif ($has_custom_border_color) {
            $border_color = $block_attributes['style']['border']['color'];
            $styles[] = sprintf('border-color: %s;', $border_color);
        }
    }
    // Collect classes and styles.
    $attributes = array();
    if (!empty($classes)) {
        $attributes['class'] = implode(' ', $classes);
    }
    if (!empty($styles)) {
        $attributes['style'] = implode(' ', $styles);
    }
    return $attributes;
}

WordPress Version: 5.8

/**
 * Adds CSS classes and inline styles for border styles to the incoming
 * attributes array. This will be applied to the block markup in the front-end.
 *
 * @since 5.8.0
 * @access private
 *
 * @param WP_Block_Type $block_type       Block type.
 * @param array         $block_attributes Block attributes.
 *
 * @return array Border CSS classes and inline styles.
 */
function wp_apply_border_support($block_type, $block_attributes)
{
    if (wp_skip_border_serialization($block_type)) {
        return array();
    }
    $classes = array();
    $styles = array();
    // Border radius.
    if (wp_has_border_feature_support($block_type, 'radius') && isset($block_attributes['style']['border']['radius'])) {
        $border_radius = (int) $block_attributes['style']['border']['radius'];
        $styles[] = sprintf('border-radius: %dpx;', $border_radius);
    }
    // Border style.
    if (wp_has_border_feature_support($block_type, 'style') && isset($block_attributes['style']['border']['style'])) {
        $border_style = $block_attributes['style']['border']['style'];
        $styles[] = sprintf('border-style: %s;', $border_style);
    }
    // Border width.
    if (wp_has_border_feature_support($block_type, 'width') && isset($block_attributes['style']['border']['width'])) {
        $border_width = intval($block_attributes['style']['border']['width']);
        $styles[] = sprintf('border-width: %dpx;', $border_width);
    }
    // Border color.
    if (wp_has_border_feature_support($block_type, 'color')) {
        $has_named_border_color = array_key_exists('borderColor', $block_attributes);
        $has_custom_border_color = isset($block_attributes['style']['border']['color']);
        if ($has_named_border_color || $has_custom_border_color) {
            $classes[] = 'has-border-color';
        }
        if ($has_named_border_color) {
            $classes[] = sprintf('has-%s-border-color', $block_attributes['borderColor']);
        } elseif ($has_custom_border_color) {
            $border_color = $block_attributes['style']['border']['color'];
            $styles[] = sprintf('border-color: %s;', $border_color);
        }
    }
    // Collect classes and styles.
    $attributes = array();
    if (!empty($classes)) {
        $attributes['class'] = implode(' ', $classes);
    }
    if (!empty($styles)) {
        $attributes['style'] = implode(' ', $styles);
    }
    return $attributes;
}