'currentColor', 'muted' => 'var(--wp--preset--color--muted)', 'line' => 'var(--wp--preset--color--line)', 'accent' => 'var(--wp--preset--color--accent)', 'bg' => 'var(--wp--preset--color--bg)', 'subtle' => 'var(--wp--preset--color--subtle)', 'transparent' => 'transparent', ); const CHOICES = array( 'border_color' => array( '', 'text', 'muted', 'line', 'accent', 'custom' ), 'background' => array( '', 'transparent', 'bg', 'subtle', 'custom' ), 'placeholder_color' => array( '', 'text', 'custom' ), ); const WIDTH_MAX = 6; const RADIUS_MAX = 40; const STYLE_ID = 'aieo-header-search-look'; private static $booted = false; public static function boot() { if ( self::$booted ) { return; } self::$booted = true; add_action( 'wp_head', array( __CLASS__, 'print_css' ), 8 ); add_action( 'aieo_dmm_header_actions_cards', array( __CLASS__, 'card' ) ); add_action( 'wp_ajax_aieo_header_search_look_save', array( __CLASS__, 'ajax_save' ) ); add_filter( 'aieo_dmm_admin_navigator_cards', array( __CLASS__, 'navigator' ) ); } public static function defaults() { return array( 'border_top' => true, 'border_right' => true, 'border_bottom' => true, 'border_left' => true, 'border_width' => '', 'border_color' => '', 'border_color_custom' => '', 'background' => '', 'background_custom' => '', 'radius' => '', 'placeholder_color' => '', 'placeholder_color_custom' => '', 'align' => '', ); } public static function sanitize( $value ) { $value = is_array( $value ) ? $value : array(); $out = self::defaults(); foreach ( self::SIDES as $side ) { if ( array_key_exists( 'border_' . $side, $value ) ) { $out[ 'border_' . $side ] = (bool) filter_var( $value[ 'border_' . $side ], FILTER_VALIDATE_BOOLEAN ); } } $out['border_width'] = self::length( $value['border_width'] ?? '', self::WIDTH_MAX, 0.5 ); $out['radius'] = self::length( $value['radius'] ?? '', self::RADIUS_MAX, 1 ); foreach ( self::CHOICES as $key => $roles ) { $role = is_scalar( $value[ $key ] ?? null ) ? strtolower( trim( (string) $value[ $key ] ) ) : ''; $out[ $key ] = in_array( $role, $roles, true ) ? $role : ''; $out[ $key . '_custom' ] = self::colour( $value[ $key . '_custom' ] ?? '' ); } $out['align'] = 'center' === ( is_scalar( $value['align'] ?? null ) ? strtolower( trim( (string) $value['align'] ) ) : '' ) ? 'center' : ''; return $out; } public static function settings() { return self::sanitize( get_option( self::OPTION, array() ) ); } private static function length( $value, $max, $step ) { if ( ! is_scalar( $value ) || '' === trim( (string) $value ) || ! is_numeric( trim( (string) $value ) ) ) { return ''; } $number = round( min( max( (float) $value, 0 ), $max ) / $step ) * $step; return rtrim( rtrim( number_format( $number, 1, '.', '' ), '0' ), '.' ); } public static function colour( $value ) { $value = is_scalar( $value ) ? trim( (string) $value ) : ''; if ( 1 === preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $value ) ) { return strtolower( $value ); } if ( 1 === preg_match( '/^(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch)\(\s*[0-9.%\s,\/+-]*(?:deg|turn|rad|none)?[0-9.%\s,\/+-]*\)$/i', $value ) && strlen( $value ) <= 64 ) { return strtolower( $value ); } if ( 0 === strcasecmp( $value, 'transparent' ) ) { return 'transparent'; } return 0 === strcasecmp( $value, 'currentColor' ) ? 'currentColor' : ''; } private static function value( array $settings, $key ) { $role = (string) ( $settings[ $key ] ?? '' ); if ( 'custom' === $role ) { return (string) ( $settings[ $key . '_custom' ] ?? '' ); } return '' !== $role && isset( self::ROLES[ $role ] ) ? self::ROLES[ $role ] : ''; } public static function css( $settings = null ) { $s = null === $settings ? self::settings() : self::sanitize( $settings ); $vars = array(); $all = true; foreach ( self::SIDES as $side ) { $all = $all && ! empty( $s[ 'border_' . $side ] ); } $own = false; if ( ! $all || '' !== $s['border_width'] ) { $width = '' !== $s['border_width'] ? $s['border_width'] . 'px' : '1px'; $sides = array(); foreach ( self::SIDES as $side ) { $sides[] = ! empty( $s[ 'border_' . $side ] ) && '0px' !== $width ? $width : '0'; } $vars['--rooster-search-border-width'] = implode( ' ', $sides ); $own = true; } foreach ( array( 'border_color' => '--rooster-search-border-color', 'background' => '--rooster-search-background', 'placeholder_color' => '--rooster-search-placeholder' ) as $key => $property ) { $colour = self::value( $s, $key ); if ( '' !== $colour ) { $vars[ $property ] = $colour; $own = $own || 'placeholder_color' !== $key; } } if ( '' !== $s['radius'] ) { $vars['--rooster-search-radius'] = $s['radius'] . 'px'; } if ( 'center' === $s['align'] ) { $vars['--rooster-search-align'] = 'center'; $vars['--aieo-fibo-pad-start'] = '2.5rem'; } if ( $own ) { $vars['--rooster-search-focus'] = 'currentColor'; $vars['--rooster-search-focus-width'] = '1px'; } if ( array() === $vars ) { return ''; } $out = array(); foreach ( $vars as $property => $css ) { $out[] = $property . ':' . $css; } return '.rooster-header__search-field{' . implode( ';', $out ) . '}'; } public static function print_css() { $css = self::css(); if ( '' !== $css ) { echo '\n"; } } private static function choice_names( $key ) { $names = array( '' => __( 'The theme\'s', 'ai-eshop-optimizer' ), 'text' => __( 'The row\'s text colour', 'ai-eshop-optimizer' ), 'muted' => __( 'The muted text colour', 'ai-eshop-optimizer' ), 'line' => __( 'The line colour', 'ai-eshop-optimizer' ), 'accent' => __( 'The accent', 'ai-eshop-optimizer' ), 'bg' => __( 'The page\'s ground', 'ai-eshop-optimizer' ), 'subtle' => __( 'The subtle ground', 'ai-eshop-optimizer' ), 'transparent' => __( 'None: transparent', 'ai-eshop-optimizer' ), 'custom' => __( 'A colour of your own', 'ai-eshop-optimizer' ), ); $out = array(); foreach ( self::CHOICES[ $key ] as $role ) { $out[ $role ] = $names[ $role ]; } return $out; } public static function card() { if ( ! class_exists( 'AIEO_DMM_UI' ) || ! class_exists( '\Rooster\Admin\Ui' ) || ! class_exists( '\Rooster\Admin\Fields' ) ) { return; } $s = self::settings(); $ui = '\Rooster\Admin\Ui'; $sides = array( 'top' => __( 'Top', 'ai-eshop-optimizer' ), 'right' => __( 'Right', 'ai-eshop-optimizer' ), 'bottom' => __( 'Bottom', 'ai-eshop-optimizer' ), 'left' => __( 'Left', 'ai-eshop-optimizer' ), ); $on = array(); foreach ( self::SIDES as $side ) { if ( ! empty( $s[ 'border_' . $side ] ) ) { $on[] = $side; } } $colour = static function ( $key, $label, $help ) use ( $s, $ui ) { $id = 'aieo-hsl-' . str_replace( '_', '-', $key ); return $ui::field_row( array( 'id' => $id, 'label' => $label, 'control' => $ui::select( $key, self::choice_names( $key ), (string) $s[ $key ], array( 'id' => $id, 'short' => true ) ) . ' ' . \Rooster\Admin\Fields::color( $key . '_custom', (string) $s[ $key . '_custom' ], array( 'id' => $id . '-custom', 'alpha' => true, 'label' => $label, 'aria-label' => sprintf( __( '%s: a colour of your own', 'ai-eshop-optimizer' ), $label ) ) ), 'help' => $help, ) ); }; $html = '