* @copyright 2026 webhead GmbH */ declare( strict_types=1 ); namespace Webhead\WP\SearchCloud\Frontend; use Webhead\WP\SearchCloud\DTO\Storefront_Facet; use Webhead\WP\SearchCloud\Indexing\Facet_Config; use Webhead\WP\SearchCloud\Indexing\Index_Config; use Webhead\WP\SearchCloud\Indexing\Language_Config; use Webhead\WP\SearchCloud\Indexing\Term_Names; use Webhead\WP\SearchCloud\Settings\Plugin_Settings; use Webhead\WP\SearchCloud\Dependencies\Webhead\WP\Framework\Settings\Settings_Manager; use Webhead\WP\SearchCloud\Dependencies\Webhead\WP\Framework\Support\Language; defined( 'ABSPATH' ) || exit; /** * @since 1.0.0 */ final class Frontend_Config { /** The webpack entry every storefront surface of this plugin is drawn by. */ public const ENTRY = 'bundle.search'; /** The global the entry's script reads this configuration from. */ public const OBJECT_NAME = 'searcioFrontend'; /** * The query parameter each page builder's editor canvas is loaded with: * Elementor, Divi's Visual Builder, Beaver Builder, Oxygen, WPBakery's * frontend editor and Bricks. */ private const EDITOR_QUERY_ARGS = [ 'elementor-preview', 'et_fb', 'fl_builder', 'ct_builder', 'vc_editable', 'bricks' ]; public function __construct( private Settings_Manager $settings, private Index_Config $index_config, private Language $language, private Language_Config $language_config, private Facet_Config $facet_config, private ?Placement_Config $placement_config = null ) {} /** * Whether this page is a page builder's editor or the Customizer's * preview. Its searches are the person editing it, not a visitor, so the * storefront marks them `preview`, which Searcio meters but does not count * in the site's search statistics. * * Filterable through `searcio_storefront_is_preview` for an * editor this list does not know. */ private function is_preview(): bool { $editor = is_customize_preview(); foreach ( self::EDITOR_QUERY_ARGS as $arg ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only reads which page this is. if ( isset( $_GET[ $arg ] ) ) { $editor = true; } } return (bool) apply_filters( 'searcio_storefront_is_preview', $editor ); } /** * Currency symbol, position and decimal count, sourced from WooCommerce so * result-card prices (returned by the Searcio API as bare numbers) are * formatted client-side to match the store. Falls back to no symbol when * WooCommerce is inactive. * * @return array */ private function currency_config(): array { if ( ! function_exists( 'get_woocommerce_currency_symbol' ) ) { return [ 'symbol' => '', 'position' => 'left', 'decimals' => 2, 'decimalSep' => '.', 'thousandSep' => ',', 'suffix' => '' ]; } return [ 'symbol' => html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' ), 'position' => (string) get_option( 'woocommerce_currency_pos', 'left' ), 'decimals' => function_exists( 'wc_get_price_decimals' ) ? (int) wc_get_price_decimals() : 2, // A space cannot be saved as a separator, so a shop types " ", // which WooCommerce prints into HTML and the search window as text. 'decimalSep' => function_exists( 'wc_get_price_decimal_separator' ) ? Term_Names::decode( wc_get_price_decimal_separator() ) : '.', 'thousandSep' => function_exists( 'wc_get_price_thousand_separator' ) ? Term_Names::decode( wc_get_price_thousand_separator() ) : ',', // The store's price suffix, e.g. "inkl. MwSt. zzgl. Versandkosten". // Only used when it is plain text (no {price_*} placeholders, which // need a computed per-product value we don't have client-side). 'suffix' => $this->price_suffix(), ]; } /** * The Searcio logo the search window shows in its bottom corner, linked to * searcio.com, or null when it shows none: only when the shop turned * Support Searcio on, whatever its plan. * * @return array{url:string}|null */ private function credit(): ?array { return (bool) $this->settings->get( Plugin_Settings::SUPPORT_SEARCIO ) ? [ 'url' => 'https://searcio.com/' ] : null; } /** * The suffix as text: WooCommerce prints it as HTML, so a shop may have * written an entity or a tag into it. */ private function price_suffix(): string { $suffix = (string) get_option( 'woocommerce_price_display_suffix', '' ); return str_contains( $suffix, '{' ) ? '' : trim( Term_Names::decode( wp_strip_all_tags( $suffix ) ) ); } /** * Called by the asset registry when it registers the entry, which is the * first time a surface on the page asks for it. * * @return array */ public function to_array(): array { $host = defined( 'Webhead\WP\SearchCloud\SERVICE_URL' ) ? \Webhead\WP\SearchCloud\SERVICE_URL : ''; return [ // Key-relative: the search key alone resolves the site server-side // (see docs/api/README.md "Key-relative endpoints"), so no site id // needs to be baked into the endpoint URL. 'endpoint' => rtrim( (string) $host, '/' ) . '/api/v1', 'searchKey' => (string) $this->settings->get( Plugin_Settings::SEARCH_KEY ), 'siteHome' => home_url( '/' ), // Read through the setting's own cleaning, for a list saved before it decoded. 'popularSearches' => Plugin_Settings::plain_text_list( $this->settings->get( Plugin_Settings::POPULAR_SEARCHES ) ), 'typeLabels' => $this->index_config->indexable_post_type_labels(), // Facet headings, comparison rows and quiz answers for the fields the // site names itself (WooCommerce attributes, mapped taxonomies), in // the language of the page being built - which is why this is read // here, per request, and not stored with the index configuration. 'fieldLabels' => $this->index_config->field_labels(), // The filters of the search window in the order Search Config lists // them, with how each is drawn and the colours of a coloured // attribute's values. Named from `fieldLabels` above. 'facets' => array_map( static fn ( Storefront_Facet $facet ): array => $facet->to_array(), $this->facet_config->storefront() ), 'currency' => $this->currency_config(), 'credit' => $this->credit(), // Both gated by is_multilingual(), not just currentLang: Language_Config's // mirror only refreshes when an admin opens/saves Search Config, so a site // that deactivated WPML/Polylang after restricting languages would // otherwise keep serving a stale enabledLanguages with no currentLang to // ever match it - sending every visitor to native-search fallback. 'currentLang' => $this->language->is_multilingual() ? $this->language->current_language() : '', 'enabledLanguages' => $this->language->is_multilingual() ? $this->language_config->enabled_languages() : [], 'placement' => $this->placement(), 'preview' => $this->is_preview(), 'i18n' => [ 'placeholder' => __( 'Search…', 'searcio' ), 'noResults' => __( 'No results found.', 'searcio' ), 'searching' => __( 'Searching…', 'searcio' ), /* translators: shown before a variable product's lowest price, e.g. "from €42". */ 'priceFrom' => _x( 'from', 'variable product price prefix', 'searcio' ), 'onSale' => __( 'Sale', 'searcio' ), 'error' => __( 'Search is unavailable right now.', 'searcio' ), 'recentSearches' => __( 'Recent searches', 'searcio' ), 'popularSearches' => __( 'Popular searches', 'searcio' ), 'clear' => __( 'Clear', 'searcio' ), 'close' => __( 'Close search', 'searcio' ), 'voiceSearch' => __( 'Search by voice', 'searcio' ), 'allTypes' => __( 'All', 'searcio' ), /* translators: %s: the name of the search service, "Searcio". */ 'credit' => sprintf( __( 'Search by %s (opens in a new tab)', 'searcio' ), 'Searcio' ), 'search' => __( 'Search', 'searcio' ), ], ]; } /** * What opens the search besides its own triggers: the theme's elements * the Placement tab names, and whether the keyboard shortcut is on. A link * to `#searcio` and the class `searcio-open` always do, and need nothing. * * @return array{selectors:string[],shortcut:bool} */ private function placement(): array { $options = $this->placement_config?->options(); return [ 'selectors' => null !== $options && $options->openers ? $options->selectors : [], 'shortcut' => null !== $options && $options->shortcut, ]; } }