Automattic\WooCommerce\Blocks\Utils

StyleAttributesUtils::get_border_color_class_and_style()public staticWC 1.0

Get class and style for border-color from attributes.

Data passed to this function is not always consistent. It can be: Linked - preset color: $attributes['borderColor'] => 'luminous-vivid-orange'. Linked - custom color: $attributes['style']['border']['color'] => '#681228'. Unlinked - preset color: $attributes['style']['border']['top']['color'] => 'var:preset|color|luminous-vivid-orange' Unlinked - custom color: $attributes['style']['border']['top']['color'] => '#681228'.

Method of the class: StyleAttributesUtils{}

No Hooks.

Return

Array.

Usage

$result = StyleAttributesUtils::get_border_color_class_and_style( $attributes );
$attributes(array) (required)
Block attributes.

StyleAttributesUtils::get_border_color_class_and_style() code WC 9.4.2

public static function get_border_color_class_and_style( $attributes ) {
	$border_color_linked_preset = $attributes['borderColor'] ?? '';
	$border_color_linked_custom = $attributes['style']['border']['color'] ?? '';
	$custom_border              = $attributes['style']['border'] ?? '';

	$class = '';
	$style = '';
	$value = '';

	if ( $border_color_linked_preset ) {
		// Linked preset color.
		$class = sprintf( 'has-border-color has-%s-border-color', $border_color_linked_preset );
		$value = self::get_preset_value( $border_color_linked_preset );
		$style = 'border-color:' . $value . ';';
	} elseif ( $border_color_linked_custom ) {
		// Linked custom color.
		$style .= 'border-color:' . $border_color_linked_custom . ';';
		$value  = $border_color_linked_custom;
	} elseif ( is_array( $custom_border ) ) {
		// Unlinked.
		foreach ( $custom_border as $border_color_key => $border_color_value ) {
			if ( is_array( $border_color_value ) && array_key_exists( 'color', ( $border_color_value ) ) ) {
				$style .= 'border-' . $border_color_key . '-color:' . self::get_color_value( $border_color_value['color'] ) . ';';
			}
		}
	}

	if ( ! $class && ! $style ) {
		return self::EMPTY_STYLE;
	}

	return array(
		'class' => $class,
		'style' => $style,
		'value' => $value,
	);
}