* @copyright 2026 webhead GmbH */ declare( strict_types=1 ); namespace Webhead\WP\SearchCloud\Frontend; defined( 'ABSPATH' ) || exit; /** * @since 1.4.0 */ final class Style_Tokens { /** Block attribute => custom property, for the attributes that hold a colour. */ public const COLORS = [ 'triggerIconColor' => '--whsc-trigger-icon', 'triggerIconColorHover' => '--whsc-trigger-icon-hover', 'triggerBg' => '--whsc-trigger-bg', 'triggerBgHover' => '--whsc-trigger-bg-hover', 'bgColor' => '--whsc-bg', 'textColor' => '--whsc-text', 'accentColor' => '--whsc-accent', 'inputBgColor' => '--whsc-input-bg', 'inputTextColor' => '--whsc-input-text', 'inputIconColor' => '--whsc-input-icon', 'inputPlaceholderColor' => '--whsc-input-placeholder', 'inputBorderColor' => '--whsc-input-border', 'inputBorderFocusColor' => '--whsc-input-border-focus', 'btnBg' => '--whsc-btn-bg', 'btnBgHover' => '--whsc-btn-bg-hover', 'btnText' => '--whsc-btn-text', 'btnTextHover' => '--whsc-btn-text-hover', 'iconColor' => '--whsc-icon-color', 'iconColorHover' => '--whsc-icon-color-hover', 'iconBgHover' => '--whsc-icon-bg-hover', 'voiceColor' => '--whsc-voice-color', 'voiceColorHover' => '--whsc-voice-color-hover', 'voiceBg' => '--whsc-voice-bg', 'voiceBgHover' => '--whsc-voice-bg-hover', 'closeColor' => '--whsc-close-color', 'closeColorHover' => '--whsc-close-color-hover', 'closeBg' => '--whsc-close-bg', 'closeBgHover' => '--whsc-close-bg-hover', 'clearBtnColor' => '--whsc-clearbtn-color', 'clearBtnColorHover' => '--whsc-clearbtn-color-hover', 'clearBtnBg' => '--whsc-clearbtn-bg', 'clearBtnBgHover' => '--whsc-clearbtn-bg-hover', 'panelBg' => '--whsc-panel-bg', 'panelBorder' => '--whsc-panel-border', 'suggestionText' => '--whsc-suggestion-text', 'suggestionTextHover' => '--whsc-suggestion-text-hover', 'suggestionBgHover' => '--whsc-suggestion-bg-hover', 'titleColor' => '--whsc-title-color', 'chipBg' => '--whsc-chip-bg', 'chipText' => '--whsc-chip-text', 'chipBorder' => '--whsc-chip-border', 'chipBgHover' => '--whsc-chip-bg-hover', 'chipTextHover' => '--whsc-chip-text-hover', 'chipBorderHover' => '--whsc-chip-border-hover', 'clearText' => '--whsc-clear-text', 'clearTextHover' => '--whsc-clear-text-hover', 'clearBgHover' => '--whsc-clear-bg-hover', 'clearTextActive' => '--whsc-clear-text-active', 'popularText' => '--whsc-popular-text', 'popularTextHover' => '--whsc-popular-text-hover', 'popularBgHover' => '--whsc-popular-bg-hover', 'facetBg' => '--whsc-facet-bg', 'facetText' => '--whsc-facet-text', 'facetBgHover' => '--whsc-facet-bg-hover', 'facetTextHover' => '--whsc-facet-text-hover', 'facetBgActive' => '--whsc-facet-bg-active', 'facetTextActive' => '--whsc-facet-text-active', 'facetCount' => '--whsc-facet-count', 'filterBadgeBg' => '--whsc-filter-badge-bg', 'filterBadgeText' => '--whsc-filter-badge-text', 'visualColor' => '--whsc-visual-color', 'visualColorHover' => '--whsc-visual-color-hover', 'visualBg' => '--whsc-visual-bg', 'visualBgHover' => '--whsc-visual-bg-hover', 'tabText' => '--whsc-tab-text', 'tabTextActive' => '--whsc-tab-text-active', 'tabIndicator' => '--whsc-tab-indicator', 'tabBorder' => '--whsc-tab-border', 'assistantAccent' => '--whsc-assistant-accent', 'assistantText' => '--whsc-assistant-text', 'assistantUserBg' => '--whsc-assistant-user-bg', 'assistantUserText' => '--whsc-assistant-user-text', 'assistantRowBg' => '--whsc-assistant-row-bg', 'bannerBg' => '--whsc-banner-bg', 'bannerText' => '--whsc-banner-text', 'quizQuestion' => '--whsc-quiz-question', 'quizOptionBg' => '--whsc-quiz-option-bg', 'quizOptionText' => '--whsc-quiz-option-text', 'quizOptionBorder' => '--whsc-quiz-option-border', 'quizOptionBgHover' => '--whsc-quiz-option-bg-hover', 'quizOptionTextHover' => '--whsc-quiz-option-text-hover', 'quizOptionBorderHover' => '--whsc-quiz-option-border-hover', 'cardBg' => '--whsc-card-bg', 'cardBorder' => '--whsc-card-border', 'cardBorderHover' => '--whsc-card-border-hover', 'cardTitle' => '--whsc-card-title', 'priceColor' => '--whsc-price-color', 'stockInColor' => '--whsc-stock-in-color', 'stockOutColor' => '--whsc-stock-out-color', 'arrowColor' => '--whsc-arrow-color', 'arrowBg' => '--whsc-arrow-bg', 'dotColor' => '--whsc-dot-color', 'dotColorActive' => '--whsc-dot-color-active', ]; /** * Block attribute => custom property, for the attributes that hold a length * in px. The last two are no block's: a shortcode's box width and the * floating button's distance from the bottom edge. */ public const DIMENSIONS = [ 'triggerRadius' => '--whsc-trigger-radius', 'triggerIconSize' => '--whsc-trigger-icon-size', 'triggerBtnSize' => '--whsc-trigger-size', 'radiusPanel' => '--whsc-radius-panel', 'modalPadding' => '--whsc-padding', 'modalGap' => '--whsc-gap', 'inputRadius' => '--whsc-input-radius', 'btnRadius' => '--whsc-btn-radius', 'panelRadius' => '--whsc-panel-radius', 'chipRadius' => '--whsc-chip-radius', 'facetRadius' => '--whsc-facet-radius', 'facetsMaxHeight' => '--whsc-facets-max-height', 'filterHeight' => '--whsc-filter-height', 'popoverWidth' => '--whsc-popover-width', 'popoverRadius' => '--whsc-popover-radius', 'drawerWidth' => '--whsc-drawer-width', 'sheetRadius' => '--whsc-sheet-radius', 'assistantInputRadius' => '--whsc-assistant-input-radius', 'quizOptionRadius' => '--whsc-quiz-option-radius', 'cardRadius' => '--whsc-card-radius', 'cardPadding' => '--whsc-card-padding', 'carouselGap' => '--whsc-carousel-gap', 'popularGap' => '--whsc-popular-gap', 'popularRadius' => '--whsc-popular-radius', 'triggerBoxWidth' => '--whsc-trigger-box-width', 'floatingOffset' => '--whsc-floating-offset', ]; /** * What a colour may be written with: a hex value, a colour function, a * name. Anything that could close the declaration or the rule is taken out. */ public static function color( mixed $value ): string { if ( ! is_string( $value ) ) { return ''; } return trim( (string) preg_replace( '/[^#a-zA-Z0-9(),.%\s-]/', '', $value ) ); } /** * The declarations the attributes ask for, joined, without the selector. * * @param array $attributes Block attributes, by their block names. */ public static function declarations( array $attributes ): string { $declarations = []; foreach ( self::COLORS as $attribute => $property ) { $value = self::color( $attributes[ $attribute ] ?? '' ); if ( '' !== $value ) { $declarations[] = sprintf( '%s:%s', $property, $value ); } } foreach ( self::DIMENSIONS as $attribute => $property ) { $value = $attributes[ $attribute ] ?? null; if ( is_numeric( $value ) ) { $declarations[] = sprintf( '%s:%dpx', $property, (int) $value ); } } if ( is_numeric( $attributes['animationDuration'] ?? null ) ) { $declarations[] = sprintf( '--whsc-anim-duration:%dms', (int) $attributes['animationDuration'] ); } return implode( ';', $declarations ); } /** * The rule that sets the declarations on one instance class, or nothing * when the instance has no class or sets nothing. It is printed through * {@see Inline_Styles}. * * The search window is drawn in `body`, outside the trigger, so it takes the * properties from the same class copied onto it, not by inheritance. * * @param array $attributes */ public static function rule( string $instance_class, array $attributes ): string { $instance_class = (string) preg_replace( '/[^a-zA-Z0-9_-]/', '', $instance_class ); $declarations = self::declarations( $attributes ); if ( '' === $instance_class || '' === $declarations ) { return ''; } return sprintf( '.%1$s{%2$s}', $instance_class, $declarations ); } }