'#f8f9fa', 'border_color' => '#e9ecef', 'title_color' => '#1d2327', 'text_color' => '#50575e' ); $set = array(); foreach ( $retired as $k => $old ) { $c = sanitize_hex_color( trim( (string) ( $s[ $k ] ?? '' ) ) ); $set[ $k ] = ( $c && 0 !== strcasecmp( $c, $old ) ) ? $c : ''; } if ( '' !== $set['bg_color'] ) { $ink = 'oklch(from ' . $set['bg_color'] . ' clamp(0,(0.62 - l) * 999,1) 0 0)'; foreach ( array( 'title_color', 'text_color' ) as $k ) { if ( '' === $set[ $k ] ) { $set[ $k ] = $ink; } } } return array( 'bg' => '' !== $set['bg_color'] ? $set['bg_color'] : 'var(--wp--preset--color--subtle,color-mix(in srgb,currentColor 6%,transparent))', 'border' => '' !== $set['border_color'] ? $set['border_color'] : 'var(--wp--preset--color--line,color-mix(in srgb,currentColor 16%,transparent))', 'title' => '' !== $set['title_color'] ? $set['title_color'] : 'var(--wp--preset--color--fg,currentColor)', 'text' => '' !== $set['text_color'] ? $set['text_color'] : 'var(--wp--preset--color--fg,currentColor)', ); } public static function defaults() { return array( 'enabled' => 'no', 'request_types' => array( 'quote' ), 'recipients' => '', 'recipients_quote' => '', 'recipients_contact' => '', 'recipients_callback' => '', 'notify_delay_minutes' => 0, 'ack_enabled' => 'no', 'ack_subject' => '', 'ack_message' => '', 'trail_lookback_hours' => 6, 'basket_ttl_hours' => 24, 'basket_storage' => 'session', 'basket_local_hours' => 24, 'finish_enabled' => 'no', 'max_items' => 20, 'frontend_enabled' => 'no', 'display_rule' => 'not_purchasable', 'rule_terms' => array(), 'placement' => 'automatic', 'form_title' => '', 'form_intro' => '', 'form_button_text' => '', 'title_contact' => '', 'title_callback' => '', 'button_contact' => '', 'button_callback' => '', 'intro_contact' => '', 'intro_callback' => '', 'form_success_message' => '', 'field_email_label' => '', 'field_name_label' => '', 'field_phone_label' => '', 'field_qty_label' => '', 'field_message_label' => '', 'consent_text' => '', 'consent_link_text' => '', 'consent_url' => '', 'add_button_text_first' => '', 'add_button_text' => '', 'added_text' => '', 'remove_button_text' => '', 'review_text' => '', 'basket_title' => '', 'basket_empty_text' => '', 'remove_text' => '', 'finish_button_text' => '', 'contact_channels' => '', 'contact_location' => '', 'contact_call_hours' => 'no', 'contact_call_hours_display' => 'inline', 'field_name' => 'yes', 'field_phone' => 'yes', 'field_qty' => 'yes', 'field_message' => 'yes', 'field_consent' => 'yes', 'title_size' => 16, 'text_size' => 14, 'bg_color' => '', 'title_color' => '', 'text_color' => '', 'border_color' => '', 'border_width' => 1, 'btn_bg_color' => '', 'btn_text_color' => '', 'btn_border_color' => '', 'btn_border_width' => '', ); } private static function i18n_labels() { return array( 'form_title' => __( 'Ask us about this product', 'ai-eshop-optimizer' ), 'form_intro' => __( 'Tell us what you need and we will get back to you with a price and availability.', 'ai-eshop-optimizer' ), 'form_button_text' => __( 'Request a quote', 'ai-eshop-optimizer' ), 'title_contact' => __( 'Ask us about this product', 'ai-eshop-optimizer' ), 'button_contact' => __( 'Send my question', 'ai-eshop-optimizer' ), 'title_callback' => __( 'Ask us to call you back', 'ai-eshop-optimizer' ), 'button_callback' => __( 'Call me back', 'ai-eshop-optimizer' ), 'intro_contact' => __( 'Write to us about this product and we will reply by email.', 'ai-eshop-optimizer' ), 'intro_callback' => __( 'Leave your number and we will call you within one working day.', 'ai-eshop-optimizer' ), 'form_success_message' => __( 'Thank you. Your request has reached us and we will be in touch shortly.', 'ai-eshop-optimizer' ), 'field_email_label' => __( 'Your email address', 'ai-eshop-optimizer' ), 'field_name_label' => __( 'Your name', 'ai-eshop-optimizer' ), 'field_phone_label' => __( 'Phone', 'ai-eshop-optimizer' ), 'field_qty_label' => __( 'Quantity', 'ai-eshop-optimizer' ), 'field_message_label' => __( 'Your message', 'ai-eshop-optimizer' ), 'consent_text' => __( 'I accept the privacy policy', 'ai-eshop-optimizer' ), 'consent_link_text' => __( 'privacy policy', 'ai-eshop-optimizer' ), 'add_button_text_first' => __( 'Send me a quote for this product', 'ai-eshop-optimizer' ), 'add_button_text' => __( 'Add to my quote as well', 'ai-eshop-optimizer' ), 'added_text' => __( 'In your quote', 'ai-eshop-optimizer' ), 'remove_button_text' => __( 'Remove from my quote', 'ai-eshop-optimizer' ), 'review_text' => __( 'Review my quote', 'ai-eshop-optimizer' ), 'basket_title' => __( 'Products in your quote', 'ai-eshop-optimizer' ), 'basket_empty_text' => __( 'Your quote is empty.', 'ai-eshop-optimizer' ), 'remove_text' => __( 'Remove', 'ai-eshop-optimizer' ), 'finish_button_text' => __( 'Send my list', 'ai-eshop-optimizer' ), 'ack_subject' => __( 'We received your request', 'ai-eshop-optimizer' ), 'ack_message' => __( 'Thank you for getting in touch. We have received your request and one of our team will reply with a price and availability shortly.', 'ai-eshop-optimizer' ), ); } private static function fill_i18n_defaults( array $s ) { if ( ! did_action( 'init' ) ) { return $s; } foreach ( self::i18n_labels() as $k => $v ) { if ( ! isset( $s[ $k ] ) || '' === trim( (string) $s[ $k ] ) ) { $s[ $k ] = $v; } } return $s; } public static function default_ack_message( $type ) { switch ( (string) $type ) { case 'contact': return __( 'Thank you for getting in touch. We have received your question and one of our team will reply shortly.', 'ai-eshop-optimizer' ); case 'callback': return __( 'Thank you for getting in touch. We have received your request and one of our team will call you back shortly.', 'ai-eshop-optimizer' ); } return __( 'Thank you for getting in touch. We have received your request and one of our team will reply with a price and availability shortly.', 'ai-eshop-optimizer' ); } public static function types() { return array( 'quote' => __( 'Quote request', 'ai-eshop-optimizer' ), 'contact' => __( 'Contact request', 'ai-eshop-optimizer' ), 'callback' => __( 'Call-back request', 'ai-eshop-optimizer' ), ); } public static function type_profile( $type ) { $map = array( 'quote' => array( 'identity' => 'email', 'fields' => array( 'name', 'phone', 'qty', 'message', 'consent' ), 'title_key' => 'form_title', 'button_key' => 'form_button_text', 'intro_key' => 'form_intro', ), 'contact' => array( 'identity' => 'email', 'fields' => array( 'name', 'phone', 'message', 'consent' ), 'title_key' => 'title_contact', 'intro_key' => 'intro_contact', 'button_key' => 'button_contact', ), 'callback' => array( 'identity' => 'phone', 'fields' => array( 'name', 'email', 'message', 'consent' ), 'title_key' => 'title_callback', 'button_key' => 'button_callback', 'intro_key' => 'intro_callback', ), ); $type = sanitize_key( (string) $type ); return isset( $map[ $type ] ) ? $map[ $type ] : $map['quote']; } public static function identity_field( $type ) { $p = self::type_profile( $type ); return (string) $p['identity']; } public static function placement_is_automatic( $s = null ) { if ( ! is_array( $s ) ) { $s = self::get( true ); } return 'manual' !== (string) ( isset( $s['placement'] ) ? $s['placement'] : 'automatic' ); } public static function own_type( $product, $s = null ) { $id = is_numeric( $product ) ? (int) $product : ( is_object( $product ) && method_exists( $product, 'get_id' ) ? (int) $product->get_id() : 0 ); if ( $id <= 0 ) { return ''; } if ( 'product_variation' === get_post_type( $id ) ) { $id = (int) wp_get_post_parent_id( $id ); } $type = $id > 0 ? sanitize_key( (string) get_post_meta( $id, self::TYPE_META, true ) ) : ''; if ( '' === $type ) { return ''; } if ( ! is_array( $s ) ) { $s = self::get( true ); } $types = self::clean_types( $s['request_types'] ); return $type !== $types[0] && in_array( $type, $types, true ) ? $type : ''; } public static function product_type( $product, $s = null ) { if ( ! is_array( $s ) ) { $s = self::get( true ); } $own = self::own_type( $product, $s ); return '' !== $own ? $own : self::clean_types( $s['request_types'] )[0]; } public static function clean_types( $types ) { $valid = array_keys( self::types() ); $out = array(); foreach ( (array) $types as $t ) { $t = sanitize_key( (string) $t ); if ( in_array( $t, $valid, true ) && ! in_array( $t, $out, true ) ) { $out[] = $t; } } return $out ? $out : array( 'quote' ); } public static function frontend_is_active() { $s = self::get( true ); return 'yes' === (string) $s['enabled'] && 'yes' === (string) $s['frontend_enabled']; } public static function should_display( $product, $s = null ) { if ( is_numeric( $product ) ) { $product = function_exists( 'wc_get_product' ) ? wc_get_product( (int) $product ) : null; } if ( ! $product || ! is_object( $product ) || ! method_exists( $product, 'get_id' ) ) { return false; } if ( ! is_array( $s ) ) { $s = self::get( true ); } $override = (string) get_post_meta( $product->get_id(), self::OVERRIDE_META, true ); if ( 'show' === $override ) { return true; } if ( 'hide' === $override ) { return false; } if ( '' !== self::own_type( $product, $s ) ) { return true; } $rule = (string) $s['display_rule']; $show = false; switch ( $rule ) { case 'always': $show = true; break; case 'out_of_stock': $show = method_exists( $product, 'is_in_stock' ) ? ! $product->is_in_stock() : false; break; case 'taxonomy': $show = self::in_selected_terms( $product, $s['rule_terms'] ); break; case 'not_purchasable': default: $purchasable = method_exists( $product, 'is_purchasable' ) ? (bool) $product->is_purchasable() : true; $price = method_exists( $product, 'get_price' ) ? (string) $product->get_price() : ''; $show = ( ! $purchasable ) || '' === trim( $price ); break; } return (bool) apply_filters( 'aieo_product_request_should_display', $show, $product, $s ); } public static function quote_only( $product, $s = null ) { if ( ! self::frontend_is_active() ) { return false; } if ( is_numeric( $product ) ) { $product = function_exists( 'wc_get_product' ) ? wc_get_product( (int) $product ) : null; } if ( ! $product || ! is_object( $product ) || ! method_exists( $product, 'get_id' ) ) { return false; } if ( method_exists( $product, 'get_parent_id' ) && (int) $product->get_parent_id() > 0 && $product->is_type( 'variation' ) ) { $product = wc_get_product( (int) $product->get_parent_id() ); if ( ! $product ) { return false; } } if ( ! is_array( $s ) ) { $s = self::get( true ); } if ( 'quote' !== self::product_type( $product, $s ) ) { return false; } if ( method_exists( $product, 'is_purchasable' ) && $product->is_purchasable() ) { return false; } return self::should_display( $product, $s ); } private static function in_selected_terms( $product, $term_ids ) { $term_ids = array_values( array_filter( array_map( 'absint', (array) $term_ids ) ) ); if ( ! $term_ids ) { return false; } $pid = (int) $product->get_id(); if ( method_exists( $product, 'get_parent_id' ) && $product->get_parent_id() ) { $pid = (int) $product->get_parent_id(); } foreach ( array( 'product_cat', 'product_brand', 'product_tag' ) as $tax ) { if ( ! taxonomy_exists( $tax ) ) { continue; } if ( has_term( $term_ids, $tax, $pid ) ) { return true; } } return false; } public static function max_items( $s = null ) { if ( ! is_array( $s ) ) { $s = self::get( true ); } return max( 1, min( self::MAX_ITEMS_CEILING, (int) $s['max_items'] ) ); } public static function basket_ttl( $s = null ) { if ( ! is_array( $s ) ) { $s = self::get( true ); } return max( HOUR_IN_SECONDS, min( 30 * DAY_IN_SECONDS, (int) $s['basket_ttl_hours'] * HOUR_IN_SECONDS ) ); } public static function trail_window( $s = null ) { if ( ! is_array( $s ) ) { $s = self::get( true ); } return max( 900, min( 7 * DAY_IN_SECONDS, (int) $s['trail_lookback_hours'] * HOUR_IN_SECONDS ) ); } } }