wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_apply_border_support › WordPress Function

Since5.8.0
Deprecatedn/a
wp_apply_border_support ( $block_type, $block_attributes )
Access:
  • private
Parameters: (2)
  • (WP_Block_Type) $block_type Block type.
    Required: Yes
  • (array) $block_attributes Block attributes.
    Required: Yes
Returns:
  • (array) Border CSS classes and inline styles.
Defined at:
Codex:

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.



Source

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;
}