* @copyright 2026 webhead GmbH */ declare( strict_types=1 ); namespace Webhead\WP\SearchCloud\DTO; use Webhead\WP\SearchCloud\Frontend\Attribute_Value as Value; defined( 'ABSPATH' ) || exit; /** * @since 1.4.0 */ final class Placement_Options { /** More than a theme ever needs; a longer list is a mistake, not a theme. */ public const SELECTORS_MAX = 20; public const SELECTOR_LENGTH = 200; /** * @param bool $takeover Whether the site's own search forms are drawn as Searcio. * @param bool $openers Whether elements the selectors match open the search. * @param string[] $selectors CSS selectors of the theme's own search icon or field. * @param bool $shortcut Whether Ctrl/Cmd+K and "/" open the search. */ public function __construct( public readonly bool $takeover = false, public readonly bool $openers = false, public readonly array $selectors = [], public readonly bool $shortcut = false, public readonly Floating_Button $floating = new Floating_Button(), public readonly Search_Placement $search = new Search_Placement(), public readonly Products_Placement $products = new Products_Placement() ) {} /** * @param array $data */ public static function from_array( array $data ): self { return new self( takeover: Value::flag( $data['takeover'] ?? null, false ), openers: Value::flag( $data['openers'] ?? null, false ), selectors: self::clean_selectors( $data['selectors'] ?? [] ), shortcut: Value::flag( $data['shortcut'] ?? null, false ), floating: Floating_Button::from_array( is_array( $data['floating'] ?? null ) ? $data['floating'] : [] ), search: Search_Placement::from_array( self::with_look( is_array( $data['search'] ?? null ) ? $data['search'] : [] ) ), products: Products_Placement::from_array( is_array( $data['products'] ?? null ) ? $data['products'] : [] ) ); } /** * Settings saved before the Look editor had four colours and a radius of * their own; they are the editor's now, where a shop finds them beside the * rest. Settings saved since carry `look`, and are read as they are. * * @param array $search * @return array */ private static function with_look( array $search ): array { if ( array_key_exists( 'look', $search ) ) { return $search; } $moved = [ 'accent' => 'modal_accent', 'background' => 'modal_bg', 'text' => 'modal_text', 'icon_color' => 'trigger_icon_color', 'radius' => 'modal_radius', ]; $look = []; foreach ( $moved as $key => $control ) { if ( isset( $search[ $key ] ) && '' !== $search[ $key ] ) { $look[ $control ] = $search[ $key ]; } $search[ $key ] = 'radius' === $key ? null : ''; } $search['look'] = $look; return $search; } /** * @return array */ public function to_array(): array { return [ 'takeover' => $this->takeover, 'openers' => $this->openers, 'selectors' => $this->selectors, 'shortcut' => $this->shortcut, 'floating' => $this->floating->to_array(), 'search' => $this->search->to_array(), 'products' => $this->products->to_array(), ]; } /** * Whether the search has to be on every page, whether or not the page * holds a block, a widget or a shortcode. A form Searcio takes over loads * the search where it is drawn, so it is not one of these. */ public function needs_every_page(): bool { return ( $this->openers && [] !== $this->selectors ) || $this->shortcut || $this->floating->enabled; } /** * One selector per line or per list item, as the admin typed them. Markup, * `<`, a rule's braces, a `;` and a backslash are taken out, which leaves * nothing that could end the selector or the script it is printed in; `>`, * quotes and brackets stay, for `#menu-item-search > a` and * `a[href="?s="]`. One the browser cannot read is skipped there, so a * typing mistake turns one selector off and not the others. * * @return string[] */ public static function clean_selectors( mixed $value ): array { if ( is_string( $value ) ) { $value = preg_split( '/\R/', $value ) ?: []; } if ( ! is_array( $value ) ) { return []; } $selectors = []; foreach ( $value as $selector ) { if ( ! is_string( $selector ) ) { continue; } $selector = trim( (string) preg_replace( '/[{};<\\\\]|\s+/', ' ', wp_strip_all_tags( $selector ) ) ); $selector = trim( (string) preg_replace( '/\s{2,}/', ' ', $selector ), " \t," ); if ( '' === $selector || strlen( $selector ) > self::SELECTOR_LENGTH ) { continue; } $selectors[ $selector ] = $selector; } return array_slice( array_values( $selectors ), 0, self::SELECTORS_MAX ); } }