'product',
'classes' => array(),
'tooltip' => Settings::tooltip(),
'tooltip_image' => Settings::tooltip_image(),
),
$args
);
++self::$count;
$type = (string) $swatches['type'];
$group = 'swatchline_' . self::$count . '_' . sanitize_key( (string) $swatches['name'] );
$classes = array_merge(
array(
'swatchline-swatches',
'swatchline-swatches--' . $type,
'swatchline-swatches--' . sanitize_html_class( (string) $args['context'] ),
'swatchline-shape-' . Settings::shape(),
'swatchline-label-' . Settings::label_style(),
),
(array) $args['classes']
);
$html = '
';
foreach ( $swatches['options'] as $n => $option ) {
$html .= self::option( (array) $option, $type, $group . '_' . $n, $group, $selected, $args );
}
$html .= '
';
/**
* The markup of a group of swatches.
*
* @param string $html
* @param array $swatches
* @param string $selected
* @param array $args
*/
return (string) apply_filters( 'swatchline_swatches_html', $html, $swatches, $selected, $args );
}
private static function option( array $option, string $type, string $id, string $group, string $selected, array $args ): string {
$value = (string) ( $option['value'] ?? '' );
$label = (string) ( $option['label'] ?? $value );
$colors = array_values( array_filter( array_map( 'sanitize_hex_color', (array) ( $option['colors'] ?? array() ) ) ) );
$image = (int) ( $option['image'] ?? 0 );
$chip = 'label' === $type ? '' : self::chip( $type, $colors, $image );
$empty = 'label' !== $type && '' === $chip;
$classes = array( 'swatchline-swatch' );
if ( $empty ) {
$classes[] = 'swatchline-swatch--empty';
}
if ( count( $colors ) > 1 && 'color' === $type ) {
$classes[] = 'swatchline-swatch--dual';
}
$html = '';
$html .= '';
$html .= '';
return $html;
}
/** A colour (one or two, split diagonally) as an SVG, or an image; '' when there's neither. */
public static function chip( string $type, array $colors, int $image ): string {
if ( 'image' === $type && $image ) {
return (string) wp_get_attachment_image(
$image,
'thumbnail',
false,
array(
'class' => 'swatchline-swatch__image',
'alt' => '',
'loading' => 'lazy',
)
);
}
if ( 'color' !== $type || ! $colors ) {
return '';
}
$svg = '';
}
}