* @copyright 2026 webhead GmbH */ declare( strict_types=1 ); namespace Webhead\WP\SearchCloud\Frontend; defined( 'ABSPATH' ) || exit; /** * @since 1.0.0 */ final class Trigger_Markup { private const ANIMATIONS = [ 'none', 'fade', 'slide-down', 'slide-up', 'zoom' ]; private const CARD_SOURCES = [ 'plugin', 'server' ]; private const HIGHLIGHTS_SOURCES = [ 'newest', 'featured' ]; /** * The two triggers the site prints once at the end of a page, which the * site-wide ways of opening the search open: one that draws nothing, and * the floating button. */ private const SITE_MODES = [ 'hidden', 'floating' ]; private const FLOATING_STYLES = [ 'icon', 'label' ]; /** How many filter buttons the search window's bar shows at most, unless a placement says otherwise. */ public const FACET_BAR_LIMIT = 5; /** The most a placement may ask for; more never fit beside "All filters" on a laptop. */ public const FACET_BAR_LIMIT_MAX = 8; /** * @param array{site?:string,floatingStyle?:string,mode?:string,scopeType?:string,placeholder?:string,className?:string,enableVoice?:bool,searchTrigger?:string,instanceClass?:string,iconHtml?:string,voiceIconHtml?:string,animation?:string,showCount?:bool,cardSource?:string,highlightsSource?:string,facetBarLimit?:int|string|null,editorPreview?:bool,editorQuery?:string} $attributes */ public static function render( array $attributes ): string { $site = in_array( $attributes['site'] ?? '', self::SITE_MODES, true ) ? (string) $attributes['site'] : ''; $mode = '' !== $site ? $site : ( in_array( $attributes['mode'] ?? '', [ 'box', 'icon' ], true ) ? $attributes['mode'] : 'icon' ); $scope_type = is_string( $attributes['scopeType'] ?? null ) ? trim( (string) $attributes['scopeType'] ) : ''; $placeholder = is_string( $attributes['placeholder'] ?? null ) ? trim( (string) $attributes['placeholder'] ) : ''; $class_name = is_string( $attributes['className'] ?? null ) ? trim( (string) $attributes['className'] ) : ''; $enable_voice = ! empty( $attributes['enableVoice'] ); $search_trigger = 'type' === ( $attributes['searchTrigger'] ?? '' ) ? 'type' : 'submit'; $instance_class = is_string( $attributes['instanceClass'] ?? null ) ? trim( (string) $attributes['instanceClass'] ) : ''; $icon_html = is_string( $attributes['iconHtml'] ?? null ) ? trim( (string) $attributes['iconHtml'] ) : ''; $voice_icon_html = is_string( $attributes['voiceIconHtml'] ?? null ) ? trim( (string) $attributes['voiceIconHtml'] ) : ''; $animation = in_array( $attributes['animation'] ?? '', self::ANIMATIONS, true ) ? (string) $attributes['animation'] : 'fade'; // Count is shown unless explicitly disabled. $show_count = array_key_exists( 'showCount', $attributes ) ? ! empty( $attributes['showCount'] ) : true; $card_source = in_array( $attributes['cardSource'] ?? '', self::CARD_SOURCES, true ) ? $attributes['cardSource'] : 'plugin'; // 'featured' sources the empty-query highlights carousel from // `filter[featured]=true` instead of a plain match-all. $highlights_source = in_array( $attributes['highlightsSource'] ?? '', self::HIGHLIGHTS_SOURCES, true ) ? $attributes['highlightsSource'] : 'newest'; // 0 shows "All filters" alone; an empty or unreadable value is the default. $facet_bar_limit = is_numeric( $attributes['facetBarLimit'] ?? null ) ? max( 0, min( self::FACET_BAR_LIMIT_MAX, (int) $attributes['facetBarLimit'] ) ) : self::FACET_BAR_LIMIT; // Opens the modal on its own, which only the Elementor editor asks for // (the widget checks it is rendering there), so it is omitted entirely // otherwise rather than written as an "off" value. // Only the site's own trigger carries these, so a block or a widget // prints exactly what it printed before. $site_data = '' !== $site ? sprintf( ' data-whsc-site="%1$s"%2$s', esc_attr( $site ), 'floating' === $site ? sprintf( ' data-floating="%s"', esc_attr( in_array( $attributes['floatingStyle'] ?? '', self::FLOATING_STYLES, true ) ? (string) $attributes['floatingStyle'] : 'icon' ) ) : '' ) : ''; $editor_preview = ! empty( $attributes['editorPreview'] ) ? sprintf( ' data-editor-preview="1" data-editor-query="%s"', esc_attr( is_string( $attributes['editorQuery'] ?? null ) ? trim( (string) $attributes['editorQuery'] ) : '' ) ) : ''; // The instance class is applied to the trigger itself (so Elementor's // Style-tab selectors, which target the trigger's ancestor wrapper, // still resolve) and is also read by the front-end JS via // `data-instance-class` so it can be re-applied to the modal - the // modal is portalled to `document.body`, so it is not a DOM descendant // of this trigger and needs the class copied onto it explicitly for // per-instance CSS custom properties to reach it. // Join only the non-empty parts so an empty instance/custom class never // leaves a doubled space in the class attribute. $classes = implode( ' ', array_filter( [ 'searcio-trigger', $instance_class, $class_name ], static fn ( string $part ): bool => '' !== $part ) ); // `data-webhead-frontend-root` scopes the shared frontend reset // (`src/components/Default/frontend-reset.scss`) to this trigger. The // portalled modal (rendered separately under `document.body`, see // `index.js`) carries the same attribute on its own mount element. return sprintf( '
', esc_attr( $classes ), esc_attr( $mode ), esc_attr( $scope_type ), esc_attr( $placeholder ), esc_attr( $enable_voice ? '1' : '' ), esc_attr( $search_trigger ), esc_attr( $instance_class ), esc_attr( $animation ), esc_attr( $show_count ? '1' : '0' ), // Both icon markups are rendered by Elementor's Icons_Manager // (trusted, sanitized). esc_attr keeps them safely contained inside // the attribute; the front-end JS decodes and renders them. esc_attr( $icon_html ), esc_attr( $voice_icon_html ), esc_attr( $card_source ), esc_attr( $highlights_source ), $facet_bar_limit, $editor_preview, $site_data ); } }