Automattic\WooCommerce\Internal\ProductAttributes

VisualAttributeTermMeta::get_swatch_stylepublic staticWC 10.9.0

Build an inline swatch style from a normalized visual value.

Method of the class: VisualAttributeTermMeta{}

No Hooks.

Returns

String.

Usage

$result = VisualAttributeTermMeta::get_swatch_style( $visual ): string;
$visual(array) (required)
.

Changelog

Since 10.9.0 Introduced.

VisualAttributeTermMeta::get_swatch_style() code WC 10.9.1

public static function get_swatch_style( array $visual ): string {
	$type  = isset( $visual['type'] ) ? (string) $visual['type'] : self::TYPE_NONE;
	$value = isset( $visual['value'] ) ? (string) $visual['value'] : '';

	if ( self::TYPE_IMAGE === $type ) {
		$image = esc_url_raw( $value );

		if ( $image ) {
			return sprintf( "background-image:url('%s')", str_replace( "'", '%27', $image ) );
		}
	}

	if ( self::TYPE_COLOR === $type ) {
		$color = sanitize_hex_color( $value );

		if ( $color ) {
			return sprintf( 'background-color:%s', $color );
		}
	}

	return '';
}