callbacks as $priority => $callbacks ) {
foreach ( $callbacks as $cb ) {
$fn = $cb['function'] ?? null;
if ( 'woocommerce_template_single_add_to_cart' === $fn && null === $cart ) {
$cart = (int) $priority;
} elseif ( is_array( $fn ) && 'render_hook' === ( $fn[1] ?? '' ) && __CLASS__ === ( is_object( $fn[0] ?? null ) ? get_class( $fn[0] ) : (string) ( $fn[0] ?? '' ) ) ) {
$mine[] = (int) $priority;
}
}
}
if ( null === $cart || ! $mine || array( $cart ) === $mine ) { return; }
foreach ( $mine as $priority ) {
remove_action( $hook, array( __CLASS__, 'render_hook' ), $priority );
}
add_action( $hook, array( __CLASS__, 'render_hook' ), $cart );
}
public static function read_template( $template ) {
global $_wp_current_template_content;
self::$template = null;
if ( ! function_exists( 'is_singular' ) || ! is_singular( 'product' ) ) { return $template; }
$found = array( 'block' => false, 'bridge' => false, 'cart' => false, 'under' => false );
$content = is_string( $_wp_current_template_content ) ? $_wp_current_template_content : '';
if ( '' !== $content ) {
$bridge = class_exists( 'WP_Block_Type_Registry' ) && WP_Block_Type_Registry::get_instance()->is_registered( 'aieo/dmm-bridge' );
$walk = static function ( array $blocks ) use ( &$walk, &$found, $bridge ) {
foreach ( $blocks as $block ) {
$name = (string) ( $block['blockName'] ?? '' );
if ( 'aieo/product-request' === $name ) {
$found['block'] = true;
} elseif ( 'aieo/dmm-bridge' === $name ) {
$found['bridge'] = $found['bridge'] || ( $bridge && self::ROW === str_replace( '-', '_', sanitize_key( (string) ( $block['attrs']['moduleId'] ?? '' ) ) ) );
} elseif ( in_array( $name, array( 'core/group', 'woocommerce/add-to-cart-with-options', 'woocommerce/add-to-cart-form' ), true ) && self::has_class( $block, self::CART_ROW_CLASSES ) ) {
$found['cart'] = true;
} elseif ( 'core/group' === $name && self::has_class( $block, self::UNDER_CLASSES ) ) {
$found['under'] = true;
}
if ( ! empty( $block['innerBlocks'] ) ) {
$walk( $block['innerBlocks'] );
}
}
};
if ( class_exists( 'AIEO_DMM_Template_Blocks' ) ) {
$walk( AIEO_DMM_Template_Blocks::resolved( $content ) );
} else {
$blocks = parse_blocks( $content );
$walk( function_exists( 'resolve_pattern_blocks' ) ? resolve_pattern_blocks( $blocks ) : $blocks );
}
}
self::$template = $found;
return $template;
}
public static function block_place( $product ) {
$t = is_array( self::$template ) ? self::$template : array( 'block' => false, 'bridge' => false, 'cart' => false, 'under' => false );
if ( $t['block'] ) { return 'none'; }
if ( self::cannot_buy( $product ) || ! AIEO_Product_Request_Settings::should_display( $product ) ) { return 'cart'; }
if ( $t['bridge'] ) { return 'bridge'; }
if ( $t['under'] && 'under' === self::zone() ) { return 'under'; }
return $t['cart'] ? 'row' : 'cart';
}
public static function cannot_buy( $product ) {
if ( ! $product || ! is_object( $product ) || ! method_exists( $product, 'is_purchasable' ) ) { return false; }
if ( class_exists( 'AIEO_Catalogue_Mode' ) && AIEO_Catalogue_Mode::cart_off( $product ) ) { return true; }
return ! $product->is_purchasable() || '' === trim( (string) $product->get_price() );
}
private static function zone() {
$zones = get_option( self::ZONES_OPTION, array() );
return is_array( $zones ) && 'under' === ( $zones[ self::ROW ] ?? '' ) ? 'under' : 'info';
}
private static function has_class( $block, array $classes ) {
$own = preg_split( '/\s+/', (string) ( $block['attrs']['className'] ?? '' ), -1, PREG_SPLIT_NO_EMPTY );
return $own && array_intersect( $classes, $own );
}
private static function page_product() {
$id = function_exists( 'is_singular' ) && is_singular( 'product' ) ? (int) get_queried_object_id() : 0;
$product = $id > 0 && function_exists( 'wc_get_product' ) ? wc_get_product( $id ) : null;
return $product instanceof WC_Product ? $product : null;
}
private static function row_render() {
self::$automatic = true;
return self::render( array( 'explicit' => false ) );
}
private static function row_part() {
$html = self::row_render();
if ( '' === trim( $html ) ) { return ''; }
return '
' . $html . '
';
}
public static function after_cart_block( $content, $block = array() ) {
if ( self::$explicit || self::$automatic ) { return $content; }
$product = self::page_product();
if ( ! $product ) { return $content; }
$place = self::block_place( $product );
if ( 'row' === $place && self::has_class( $block, self::CART_ROW_CLASSES ) ) {
return $content . self::row_part();
}
return 'cart' === $place ? $content . self::row_render() : $content;
}
public static function row_group( $content, $block = array() ) {
if ( null === self::$template || self::$explicit || self::$automatic ) { return $content; }
$cart = self::has_class( $block, self::CART_ROW_CLASSES );
$under = ! $cart && self::has_class( $block, self::UNDER_CLASSES );
if ( ! $cart && ! $under ) { return $content; }
$product = self::page_product();
if ( ! $product ) { return $content; }
$place = self::block_place( $product );
if ( $cart && 'row' === $place ) {
return $content . self::row_part();
}
if ( $under && 'under' === $place ) {
$tag = sanitize_key( (string) ( $block['attrs']['tagName'] ?? 'div' ) );
$end = strrpos( $content, '' . ( '' === $tag ? 'div' : $tag ) . '>' );
return false === $end ? $content . self::row_part() : substr( $content, 0, $end ) . self::row_part() . substr( $content, $end );
}
return $content;
}
public static function bridge_card() {
if ( null === self::$template ) {
return self::render( array( 'check_rule' => true ) );
}
if ( self::$explicit || self::$automatic ) { return ''; }
$product = self::page_product();
if ( ! $product || 'bridge' !== self::block_place( $product ) ) { return ''; }
return self::row_render();
}
public static function shortcode_form( $atts = array() ) {
$a = shortcode_atts( array(
'type' => '',
'product_id' => 0,
'layout' => 'compact',
'title' => '',
'button' => '',
'fields' => '',
), $atts, 'aieo_product_request' );
return self::render( array(
'product_id' => (int) $a['product_id'],
'type' => (string) $a['type'],
'layout' => (string) $a['layout'],
'title' => (string) $a['title'],
'button' => (string) $a['button'],
'fields' => (string) $a['fields'],
'check_rule' => 0 === (int) $a['product_id'],
) );
}
public static function shortcode_basket( $atts = array() ) {
$a = shortcode_atts( array( 'title' => '' ), $atts, 'aieo_quote_basket' );
return self::render_basket( array( 'title' => (string) $a['title'] ) );
}
public static function action_render( $product_id = 0, $type = '' ) {
echo self::render( array(
'product_id' => (int) $product_id,
'type' => (string) $type,
'check_rule' => false,
) );
}
public static function enqueue_assets() {
if ( self::$enqueued ) { return; }
self::$enqueued = true;
$base = defined( 'AIEO_FILE' ) ? AIEO_FILE : __FILE__;
$dir = defined( 'AIEO_DIR' ) ? AIEO_DIR : trailingslashit( dirname( __FILE__ ) );
$css_rel = 'assets/css/aieo-product-request.css';
$js_rel = 'assets/js/aieo-product-request.js';
$css_ver = file_exists( $dir . $css_rel ) ? filemtime( $dir . $css_rel ) : ( defined( 'AIEO_VERSION' ) ? AIEO_VERSION : '1' );
$js_ver = file_exists( $dir . $js_rel ) ? filemtime( $dir . $js_rel ) : ( defined( 'AIEO_VERSION' ) ? AIEO_VERSION : '1' );
$deps = array();
if ( class_exists( 'AIEO_Form_Fields' ) ) {
AIEO_Form_Fields::register_assets();
$deps = array( AIEO_Form_Fields::STYLE );
if ( class_exists( 'AIEO_Text_Link' ) ) {
$deps = array_merge( $deps, AIEO_Text_Link::dependency() );
}
}
wp_register_style( self::HANDLE, plugins_url( $css_rel, $base ), $deps, $css_ver );
wp_add_inline_style( self::HANDLE, self::appearance_css() );
if ( method_exists( 'AIEO_Form_Fields', 'style_here' ) ) {
self::$sheet = AIEO_Form_Fields::style_here( self::HANDLE );
} else {
wp_enqueue_style( self::HANDLE );
}
wp_enqueue_script( self::HANDLE, plugins_url( $js_rel, $base ), class_exists( 'AIEO_Form_Fields' ) ? array( AIEO_Form_Fields::SCRIPT ) : array(), $js_ver, true );
$s = AIEO_Product_Request_Settings::get();
wp_localize_script( self::HANDLE, 'aieoProductRequest', array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( self::NONCE ),
) + self::client_state( $s ) + array(
'minTime' => 2,
'i18n' => array(
'invalidEmail' => __( 'Please enter a valid email address.', 'ai-eshop-optimizer' ),
'invalidPhone' => __( 'Please enter a phone number we can reach you on.', 'ai-eshop-optimizer' ),
'genericError' => __( 'Something went wrong. Please try again.', 'ai-eshop-optimizer' ),
'addFirst' => (string) $s['add_button_text_first'],
'addMore' => (string) $s['add_button_text'],
'added' => (string) $s['added_text'],
'removeBtn' => (string) $s['remove_button_text'],
'update' => __( 'Update my quote', 'ai-eshop-optimizer' ),
'review' => (string) $s['review_text'],
'empty' => (string) $s['basket_empty_text'],
'remove' => (string) $s['remove_text'],
'consent' => __( 'Please accept the privacy policy to continue.', 'ai-eshop-optimizer' ),
'pickTitle' => __( 'Select variations', 'ai-eshop-optimizer' ),
'pickOption' => __( '%d option', 'ai-eshop-optimizer' ),
'pickOptions' => __( '%d options', 'ai-eshop-optimizer' ),
'pickItem' => __( '%d item', 'ai-eshop-optimizer' ),
'pickItems' => __( '%d items', 'ai-eshop-optimizer' ),
'pickAskOne' => __( 'Request a quote for %d option', 'ai-eshop-optimizer' ),
'pickAsk' => __( 'Request a quote for %d options', 'ai-eshop-optimizer' ),
'pickAskNone' => (string) $s[ AIEO_Product_Request_Settings::type_profile( 'quote' )['button_key'] ],
'picked' => __( 'Your selection: %s', 'ai-eshop-optimizer' ),
'pickedOption' => __( '%1$s, %2$s', 'ai-eshop-optimizer' ),
'pickedQty' => __( '%1$s × %2$d', 'ai-eshop-optimizer' ),
'pickedMore' => __( 'and %d more', 'ai-eshop-optimizer' ),
'back' => __( 'Back', 'ai-eshop-optimizer' ),
'close' => __( 'Close', 'ai-eshop-optimizer' ),
'done' => __( 'Done', 'ai-eshop-optimizer' ),
'quoteAdded' => __( 'Added to your quote.', 'ai-eshop-optimizer' ),
),
) );
}
private static function contact_row( array $s ) {
if ( ! class_exists( 'AIEO_Location_Contact' ) ) {
return '';
}
$channels = AIEO_Location_Contact::parse( (string) ( $s['contact_channels'] ?? '' ) );
if ( ! $channels ) {
return '';
}
$loc = AIEO_Location_Contact::location( (string) ( $s['contact_location'] ?? '' ) );
$buttons = AIEO_Location_Contact::buttons( $loc, $channels, true, 'yes' === (string) ( $s['contact_call_hours'] ?? 'no' ), (string) ( $s['contact_call_hours_display'] ?? 'inline' ) );
if ( '' === $buttons ) {
return '';
}
AIEO_Location_Contact::style();
return '' . $buttons . '
';
}
private static function appearance_css() {
$s = AIEO_Product_Request_Settings::get( true );
$card = AIEO_Product_Request_Settings::card_colours( $s );
$btn_bg = trim( (string) $s['btn_bg_color'] );
$btn_text = trim( (string) $s['btn_text_color'] );
$btn_border = trim( (string) $s['btn_border_color'] );
$btn_bw = (string) $s['btn_border_width'];
return sprintf(
'.aieo-pr,.aieo-pr-add,.aieo-pr-basket,.aieo-pr-pick{'
. '--aieo-pr-bg:%s;'
. '--aieo-pr-border:%s;'
. '--aieo-pr-border-w:%dpx;'
. '--aieo-pr-title-color:%s;'
. '--aieo-pr-text-color:%s;'
. '--aieo-pr-title-size:%dpx;'
. '--aieo-pr-text-size:%dpx;'
. '--aieo-pr-btn-bg:%s;'
. '--aieo-pr-btn-text:%s;'
. '--aieo-pr-btn-border:%s;'
. '--aieo-pr-btn-border-w:%s;'
. '}',
esc_attr( $card['bg'] ),
esc_attr( $card['border'] ),
(int) $s['border_width'],
esc_attr( $card['title'] ),
esc_attr( $card['text'] ),
(int) $s['title_size'],
(int) $s['text_size'],
'' !== $btn_bg ? esc_attr( $btn_bg ) : 'var(--aieo-btn-bg,var(--wp--custom--rooster--button--background,var(--wp--preset--color--accent,CanvasText)))',
'' !== $btn_text ? esc_attr( $btn_text ) : 'var(--aieo-btn-fg,var(--wp--custom--rooster--button--text,var(--wp--preset--color--on-accent,var(--wp--preset--color--base,Canvas))))',
'' !== $btn_border ? esc_attr( $btn_border ) : 'var(--aieo-btn-border,var(--wp--custom--rooster--button--background,var(--wp--preset--color--accent,currentColor)))',
'' !== $btn_bw ? ( (int) $btn_bw . 'px' ) : 'var(--aieo-btn-border-w,0px)'
);
}
public static function render( $args = array() ) {
$args = wp_parse_args( $args, array(
'product_id' => 0,
'type' => '',
'layout' => 'compact',
'title' => '',
'button' => '',
'fields' => '',
'check_rule' => true,
'explicit' => true,
) );
if ( ! empty( $args['explicit'] ) ) {
self::$explicit = true;
if ( self::$row_card && function_exists( 'is_singular' ) && is_singular( 'product' ) ) { return ''; }
}
if ( ! AIEO_Product_Request_Settings::frontend_is_active() ) { return ''; }
if ( ! AIEO_Product_Request_Store::ready() ) { return ''; }
$s = AIEO_Product_Request_Settings::get();
$product_id = absint( $args['product_id'] );
if ( $product_id <= 0 ) {
$product_id = self::current_product_id();
}
$product = $product_id > 0 && function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : null;
$types = AIEO_Product_Request_Settings::clean_types( $s['request_types'] );
$type = sanitize_key( (string) $args['type'] );
if ( ! in_array( $type, $types, true ) ) { $type = AIEO_Product_Request_Settings::product_type( $product, $s ); }
if ( 'quote' === $type && ! $product ) { return ''; }
if ( $args['check_rule'] && $product && ! AIEO_Product_Request_Settings::should_display( $product, $s ) ) {
return 'quote' === $type ? self::follow_prompt( $product_id, $s ) : '';
}
self::enqueue_assets();
if ( empty( $args['explicit'] ) ) {
self::$row_card = true;
}
self::$uid++;
$uid = self::$uid;
$layout = ( 'full' === $args['layout'] ) ? 'full' : 'compact';
$profile = AIEO_Product_Request_Settings::type_profile( $type );
$fields = self::resolve_fields( $args['fields'], $s, $type );
$title = '' !== trim( (string) $args['title'] ) ? (string) $args['title'] : (string) $s[ $profile['title_key'] ];
$button = '' !== trim( (string) $args['button'] ) ? (string) $args['button'] : (string) $s[ $profile['button_key'] ];
$shades = ( 'quote' === $type && $product_id > 0 && ! self::$list_drawn ) ? self::shades_picker( $product_id ) : '';
$list = ( '' === $shades && 'quote' === $type && $product_id > 0 && ! self::$list_drawn ) ? self::variation_list( $product_id ) : array();
if ( '' !== $shades ) {
$fields['qty'] = false;
}
ob_start();
echo self::take_sheet();
if ( '' !== $shades ) {
self::$list_drawn = true;
echo 1 === (int) $uid ? (string) preg_replace( '/^ 0 && $quote ) {
self::render_add_prompt( $product_id, $s, false, '' !== $shades );
}
echo self::first_paint_script( $s, '' !== $shades ? 'shades' : ( $list ? 'list' : ( $product && $product->is_type( 'variable' ) ? 'chips' : '' ) ) );
if ( '' !== $shades ) {
echo self::contact_row( $s );
}
if ( $product_id > 0 && $quote ) {
if ( ! self::$basket_drawn ) {
echo self::render_basket();
}
}
return (string) ob_get_clean();
}
private static function first_paint_script( array $s, $kept = '' ) {
$shades = 'shades' === $kept;
$js = '(function(d){var n=d.currentScript,p=n&&n.previousElementSibling,f,t="",s,e,m,c,r,v;'
. 'if(p&&p.classList.contains("aieo-pr-add")){f=p.previousElementSibling;}else{f=p;p=null;}'
. 'if(!f||!f.classList.contains("aieo-pr"))return;'
. self::kept_ticks_js( (string) $kept )
. ( $shades ? self::picker_words_js( $s ) : '' )
. 'function g(){'
. self::token_js( $s )
. ( $shades ? 'w(/^[a-f0-9]{32}$/.test(t));' : '' )
. 'if(p&&/^[a-f0-9]{32}$/.test(t)){'
. 'f.hidden=true;c=f.querySelector(".aieo-pr__variations");r=f.querySelector(".aieo-pr__contact");'
. 'window.aieoPrHome={chips:c?{parent:c.parentNode,next:c.nextSibling}:null,contact:r?{parent:r.parentNode,next:r.nextSibling}:null};'
. 'if(c)p.insertBefore(c,p.firstChild);if(r)p.appendChild(r);'
. 'v=p.querySelector(".aieo-pr-add__review");if(v)v.hidden=false;'
. 'v=p.querySelector(".aieo-pr-add__btn");m=[];try{m=JSON.parse(s.getItem("aieo_qr_ids")||"[]")||[];}catch(x){m=[];}'
. 'if(v&&!c&&!d.querySelector(".aieo-pr-pick")&&m.length){if(m.indexOf(parseInt(p.getAttribute("data-product"),10))>-1){v.textContent=' . wp_json_encode( (string) $s['remove_button_text'], JSON_HEX_TAG | JSON_HEX_AMP ) . ';v.title=' . wp_json_encode( (string) $s['added_text'], JSON_HEX_TAG | JSON_HEX_AMP ) . ';v.classList.add("is-in");}else{v.textContent=' . wp_json_encode( (string) $s['add_button_text'], JSON_HEX_TAG | JSON_HEX_AMP ) . ';}}'
. 'p.hidden=false;'
. ( $shades ? '}' : '}else{f.hidden=false;}' )
. '}g();'
. 'if(d.prerendering)d.addEventListener("prerenderingchange",function(){if(p&&p.hidden)g();' . ( $shades ? 'else w(/^[a-f0-9]{32}$/.test(t));' : '' ) . '},{once:true});'
. '})(document);';
return '';
}
private static function picker_words_js( array $s ) {
$flags = JSON_HEX_TAG | JSON_HEX_AMP;
$ask = class_exists( 'AIEO_Catalogue_Mode' ) ? (string) AIEO_Catalogue_Mode::request_label() : '';
$words = array(
'' !== $ask ? $ask : __( 'Request a quote', 'ai-eshop-optimizer' ),
__( 'Request a quote for %d option', 'ai-eshop-optimizer' ),
__( 'Request a quote for %d options', 'ai-eshop-optimizer' ),
'' !== trim( (string) $s['add_button_text'] ) ? (string) $s['add_button_text'] : __( 'Add to my quote as well', 'ai-eshop-optimizer' ),
__( 'Add %d option to my quote', 'ai-eshop-optimizer' ),
__( 'Add %d options to my quote', 'ai-eshop-optimizer' ),
);
return 'function w(o){var q=d.querySelector("form[data-aieo-quote]"),b,k,x=' . wp_json_encode( $words, $flags ) . ',j;if(!q)return;'
. 'q.setAttribute("data-quote-open",o?"1":"0");b=q.querySelector("[data-aieo-quote-ask]");if(!b)return;'
. 'k=q.querySelectorAll(".om-variation-input:checked").length;j=(o?3:0)+(k?(k===1?1:2):0);b.textContent=x[j].replace(/%[sd]/,k);}';
}
private static function token_js( array $s ) {
$storage = ( 'local' === (string) $s['basket_storage'] ) ? 'localStorage' : 'sessionStorage';
return 'try{s=window[' . wp_json_encode( $storage, JSON_HEX_TAG | JSON_HEX_AMP ) . '];t=s.getItem("aieo_qr")||"";'
. ( 'localStorage' === $storage ? 'e=parseInt(s.getItem("aieo_qr_exp")||"0",10);if(t&&e&&Date.now()>e)t="";' : '' )
. '}catch(x){t="";}'
. 'if(!t){m=d.cookie.match(new RegExp("(^|;)\\\\s*"+' . wp_json_encode( AIEO_Product_Request_Store::COOKIE, JSON_HEX_TAG | JSON_HEX_AMP ) . '+"\\\\s*=\\\\s*([^;]+)"));t=m?decodeURIComponent(m[2]):"";}';
}
private static function kept_ticks_js( $kept ) {
$flags = JSON_HEX_TAG | JSON_HEX_AMP;
$run = '(function(k){k();if(d.prerendering)d.addEventListener("prerenderingchange",k,{once:true});})';
if ( 'chips' === $kept ) {
return $run . '(function(){var k=null,i,q;try{k=JSON.parse(sessionStorage.getItem("aieo_qp_add_"+f.getAttribute("data-product"))||"null");}catch(x){k=null;}'
. 'if(!k||typeof k!=="object")return;'
. 'for(i in k){q=d.querySelector("input[name=\'variations[]\'][value=\'"+(parseInt(i,10)||0)+"\']");if(q)q.checked=true;}});';
}
if ( 'shades' === $kept ) {
return $run . '(function(){var k=null,i,q;try{k=JSON.parse(sessionStorage.getItem("aieo_qp_add_"+f.getAttribute("data-product"))||"null");}catch(x){k=null;}'
. 'if(!k||typeof k!=="object")return;'
. 'for(i in k){q=d.querySelector("form[data-aieo-quote] .om-variation-item[data-vid=\'"+(parseInt(i,10)||0)+"\'] > .om-variation-input");if(q&&!q.disabled)q.checked=true;}});';
}
if ( 'list' !== $kept ) {
return '';
}
$one = wp_json_encode( __( '%d option', 'ai-eshop-optimizer' ), $flags );
$many = wp_json_encode( __( '%d options', 'ai-eshop-optimizer' ), $flags );
$item = wp_json_encode( __( '%d item', 'ai-eshop-optimizer' ), $flags );
$items = wp_json_encode( __( '%d items', 'ai-eshop-optimizer' ), $flags );
return $run . '(function(){var g=d.querySelector(".aieo-pr-pick"),o={},k=0,q=0,y,i,h,w;if(!g)return;y=g.getAttribute("data-qty")==="1";'
. '["aieo_qp_","aieo_qp_add_"].forEach(function(x){var j=null,a;try{j=JSON.parse(sessionStorage.getItem(x+g.getAttribute("data-product"))||"null");}catch(z){j=null;}'
. 'if(j&&typeof j==="object"){for(a in j){if((parseInt(a,10)||0)>0&&(parseInt(j[a],10)||0)>0)o[a]=parseInt(j[a],10);}}});'
. 'for(i in o){k++;q+=o[i];}if(!k)return;'
. 'w=(k===1?' . $one . ':' . $many . ').replace("%d",k);'
. 'h=g.querySelector(".aieo-pick__title");if(h)h.textContent=' . wp_json_encode( __( 'Select variations', 'ai-eshop-optimizer' ), $flags ) . '+" · "+w;'
. 'h=f.querySelector(".aieo-pr__picked");if(h){h.textContent=' . wp_json_encode( __( 'Your selection: %s', 'ai-eshop-optimizer' ), $flags ) . '.replace("%s",w+(y?" · "+(q===1?' . $item . ':' . $items . ').replace("%d",q):""));h.hidden=false;}'
. 'h=f.querySelector(".aieo-pr__picked-list");if(h){h.style.minHeight=(Math.min(k,3)+(k>3?1:0))*1.5+"em";h.hidden=false;}'
. 'h=f.querySelector(".aieo-pr__field--qty");if(h&&y)h.hidden=true;'
. 'g.classList.add("has-picks");});';
}
private static function follow_prompt( $product_id, array $s ) {
$product_id = (int) $product_id;
if ( $product_id <= 0 || 'hide' === (string) get_post_meta( $product_id, AIEO_Product_Request_Settings::OVERRIDE_META, true ) ) {
return '';
}
if ( ! (bool) apply_filters( 'aieo_product_request_follow_quote', true, $product_id ) ) {
return '';
}
self::enqueue_assets();
self::$uid++;
ob_start();
echo self::take_sheet();
self::render_add_prompt( $product_id, $s, true );
echo self::follow_paint_script( $s );
if ( ! self::$basket_drawn ) {
echo self::render_basket();
}
return (string) ob_get_clean();
}
private static function follow_paint_script( array $s ) {
$flags = JSON_HEX_TAG | JSON_HEX_AMP;
$js = '(function(d){var n=d.currentScript,p=n&&n.previousElementSibling,t="",s,e,m,v;'
. 'if(!p||!p.classList.contains("aieo-pr-add"))return;'
. 'function g(){'
. self::token_js( $s )
. 'if(!/^[a-f0-9]{32}$/.test(t))return;'
. 'v=p.querySelector(".aieo-pr-add__review");if(v)v.hidden=false;'
. 'v=p.querySelector(".aieo-pr-add__btn");m=[];try{m=JSON.parse(s.getItem("aieo_qr_ids")||"[]")||[];}catch(x){m=[];}'
. 'if(v&&m.indexOf(parseInt(p.getAttribute("data-product"),10))>-1){v.textContent=' . wp_json_encode( (string) $s['remove_button_text'], $flags ) . ';v.title=' . wp_json_encode( (string) $s['added_text'], $flags ) . ';v.classList.add("is-in");}'
. 'p.hidden=false;'
. '}g();'
. 'if(d.prerendering)d.addEventListener("prerenderingchange",function(){if(p.hidden)g();},{once:true});'
. '})(document);';
return '';
}
public static function window_form( $product_id ) {
$product_id = (int) $product_id;
if ( $product_id <= 0 || ! AIEO_Product_Request_Settings::frontend_is_active() || ! AIEO_Product_Request_Store::ready() || ! AIEO_Product_Request_Settings::quote_only( $product_id ) ) {
return array();
}
$s = AIEO_Product_Request_Settings::get();
$profile = AIEO_Product_Request_Settings::type_profile( 'quote' );
$fields = self::resolve_fields( '', $s, 'quote' );
$fields['qty'] = false;
ob_start();
self::render_request_card( 0, $product_id, 'quote', 'full', (string) $s[ $profile['title_key'] ], (string) $s[ $profile['button_key'] ], $fields, $s, true, true );
$html = trim( (string) ob_get_clean() );
$dir = defined( 'AIEO_DIR' ) ? AIEO_DIR : trailingslashit( dirname( __FILE__ ) );
$base = defined( 'AIEO_FILE' ) ? AIEO_FILE : __FILE__;
$rel = file_exists( $dir . 'assets/css/aieo-product-request.min.css' ) ? 'assets/css/aieo-product-request.min.css' : 'assets/css/aieo-product-request.css';
$fields_rel = file_exists( $dir . 'assets/css/aieo-fields.min.css' ) ? 'assets/css/aieo-fields.min.css' : 'assets/css/aieo-fields.css';
$out = array(
'html' => $html,
'style' => self::appearance_css(),
'css' => add_query_arg( 'ver', file_exists( $dir . $rel ) ? (string) filemtime( $dir . $rel ) : '1', plugins_url( $rel, $base ) ),
'fields_css' => add_query_arg( 'ver', file_exists( $dir . 'assets/css/aieo-fields.css' ) ? (string) filemtime( $dir . 'assets/css/aieo-fields.css' ) : '1', plugins_url( $fields_rel, $base ) ),
'cfg' => self::client_state( $s ) + array(
'i18n' => array(
'invalidEmail' => __( 'Please enter a valid email address.', 'ai-eshop-optimizer' ),
'invalidPhone' => __( 'Please enter a phone number we can reach you on.', 'ai-eshop-optimizer' ),
'genericError' => __( 'Something went wrong. Please try again.', 'ai-eshop-optimizer' ),
'consent' => __( 'Please accept the privacy policy to continue.', 'ai-eshop-optimizer' ),
),
),
);
$captcha = false !== strpos( $html, 'data-aieo-captcha-action' ) ? self::captcha_script() : array();
if ( $captcha ) {
$out['captcha'] = $captcha;
}
return $out;
}
private static function take_sheet() {
$sheet = self::$sheet;
self::$sheet = '';
return $sheet;
}
private static function client_state( array $s ) {
return array(
'storage' => ( 'local' === (string) $s['basket_storage'] ) ? 'local' : 'session',
'ttl' => max( 1, (int) $s['basket_local_hours'] ) * 3600,
'cookie' => AIEO_Product_Request_Store::COOKIE,
'site' => substr( md5( home_url() ), 0, 8 ),
);
}
private static function captcha_script() {
if ( ! class_exists( 'AIEO_Form_Captcha' ) ) {
return array();
}
$cls = AIEO_Form_Captcha::captcha_class();
if ( '' === $cls || ! method_exists( $cls, 'register_script' ) || ! defined( $cls . '::HANDLE' ) ) {
return array();
}
$handle = (string) constant( $cls . '::HANDLE' );
if ( ! wp_script_is( $handle, 'registered' ) ) {
call_user_func( array( $cls, 'register_script' ) );
}
$reg = wp_scripts()->query( $handle, 'registered' );
if ( ! $reg || empty( $reg->src ) ) {
return array();
}
$cfg = array();
if ( preg_match( '/=\s*(\{.*\})\s*;?\s*$/s', (string) wp_scripts()->get_data( $handle, 'data' ), $m ) ) {
$cfg = json_decode( $m[1], true );
}
return array(
'src' => (string) add_query_arg( 'ver', (string) $reg->ver, (string) $reg->src ),
'cfg' => is_array( $cfg ) ? $cfg : array(),
);
}
private static function render_request_card( $uid, $product_id, $type, $layout, $title, $button, array $fields, array $s, $listed = false, $window = false, $shades = false ) {
$id_prefix = $window ? 'aieo-prw-' . (int) $product_id : 'aieo-pr-' . (int) $uid;
$consent_url = '' !== trim( (string) $s['consent_url'] )
? (string) $s['consent_url']
: ( function_exists( 'get_privacy_policy_url' ) ? get_privacy_policy_url() : '' );
$identity = AIEO_Product_Request_Settings::identity_field( $type );
$identity = ( 'phone' === $identity ) ? 'phone' : 'email';
$id_meta = 'phone' === $identity
? array( 'type' => 'tel', 'autocomplete' => 'tel', 'label' => (string) $s['field_phone_label'] )
: array( 'type' => 'email', 'autocomplete' => 'email', 'label' => (string) $s['field_email_label'] );
?>
data-product=""
0 && function_exists( 'wc_get_product' ) ? wc_get_product( (int) $product_id ) : null;
echo $named ? 'data-product-name="' . esc_attr( $named->get_name() ) . '"' : '';
?>
data-type=""
data-identity=""
>
quote_form_html( (int) $product_id ) : '';
}
public static function variation_list( $product_id ) {
$product = $product_id > 0 && function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : null;
if ( ! $product || ! $product->is_type( 'variable' ) || ! class_exists( 'AIEO_DMM_Swatches_Runtime' ) || ! class_exists( 'AIEO_Card' ) || ! class_exists( 'AIEO_Pick_List' ) ) {
return array();
}
$count = count( (array) $product->get_children() );
if ( $count < 1 ) {
return array();
}
$runtime = AIEO_DMM_Swatches_Runtime::instance();
$multi = method_exists( $runtime, 'multi_select_on' ) && $runtime->multi_select_on( (int) $product_id );
$ps = ( $multi && method_exists( $runtime, 'product_settings' ) ) ? (array) $runtime->product_settings( (int) $product_id ) : array();
$list = ( $multi || $count > self::MAX_CHOICES ) ? array( 'qty' => $multi ? ! empty( $ps['show_quantity'] ) : true ) : array();
$list = apply_filters( 'aieo_product_request_variation_list', $list, $product );
return is_array( $list ) && $list ? array( 'qty' => ! empty( $list['qty'] ) ) : array();
}
private static function render_variation_list( $product_id, array $list, $button ) {
echo AIEO_Pick_List::html(
(int) $product_id,
array(
'id' => 'aieo-pr-pick-' . (int) $product_id,
'qty' => ! empty( $list['qty'] ),
'class' => 'aieo-pr-pick',
'layout' => true,
'title' => 'caller',
'msg' => true,
'foot' => '',
)
);
}
const MAX_CHOICES = 60;
public static function variation_choices( $product_id ) {
$product = $product_id > 0 && function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : null;
if ( ! $product || ! $product->is_type( 'variable' ) ) {
return array();
}
$ids = $product->get_children();
if ( ! $ids || count( $ids ) > self::MAX_CHOICES ) {
return array();
}
$out = array();
$mode = method_exists( 'AIEO_DMM_Swatches_Runtime', 'shade_mode' ) ? AIEO_DMM_Swatches_Runtime::instance()->shade_mode() : 'desc_term';
foreach ( $ids as $vid ) {
$v = wc_get_product( $vid );
if ( ! $v || 'publish' !== $v->get_status() ) {
continue;
}
$name = trim( wp_strip_all_tags( wc_get_formatted_variation( $v, true, false, false ) ) );
$name = '' !== $name && method_exists( 'AIEO_DMM_Swatches_Runtime', 'shade_label' ) ? AIEO_DMM_Swatches_Runtime::shade_label( $name, (string) $v->get_description(), $mode ) : $name;
$out[] = array( 'id' => (int) $vid, 'name' => '' !== $name ? $name : (string) $v->get_name() ) + self::swatch( $v );
}
return $out;
}
private static function swatch( $variation ) {
foreach ( $variation->get_variation_attributes( false ) as $taxonomy => $slug ) {
if ( '' === (string) $slug || ! taxonomy_exists( (string) $taxonomy ) ) {
continue;
}
$term = get_term_by( 'slug', (string) $slug, (string) $taxonomy );
if ( ! $term ) {
break;
}
$pic = (int) get_term_meta( $term->term_id, '_aieo_swatch_image_id', true ) ?: (int) get_term_meta( $term->term_id, 'product_attribute_image', true );
if ( $pic > 0 ) {
return array( 'swatch' => (string) wp_get_attachment_image_url( $pic, 'thumbnail' ), 'color' => '' );
}
$color = (string) sanitize_hex_color( (string) ( get_term_meta( $term->term_id, 'color', true ) ?: get_term_meta( $term->term_id, 'product_attribute_color', true ) ) );
if ( '' !== $color ) {
return array( 'swatch' => '', 'color' => $color );
}
break;
}
$own = (int) $variation->get_image_id( 'edit' );
return array( 'swatch' => $own > 0 ? (string) wp_get_attachment_image_url( $own, 'thumbnail' ) : '', 'color' => '' );
}
private static function render_add_prompt( $product_id, array $s, $follow = false, $shades = false ) {
?>
data-product="" hidden>
'' ) );
$title = '' !== trim( (string) $args['title'] ) ? (string) $args['title'] : (string) $s['basket_title'];
$finish_on = 'yes' === (string) $s['finish_enabled'];
ob_start();
echo self::take_sheet();
?>
', esc_url( $url ) );
$close = '';
$link_text = trim( (string) $link_text );
if ( '' !== $link_text ) {
$pos = function_exists( 'mb_strpos' ) ? mb_strpos( $text, $link_text ) : strpos( $text, $link_text );
if ( false !== $pos ) {
$len = function_exists( 'mb_strlen' ) ? mb_strlen( $link_text ) : strlen( $link_text );
$before = function_exists( 'mb_substr' ) ? mb_substr( $text, 0, $pos ) : substr( $text, 0, $pos );
$after = function_exists( 'mb_substr' ) ? mb_substr( $text, $pos + $len ) : substr( $text, $pos + $len );
return esc_html( $before ) . $open . esc_html( $link_text ) . $close . esc_html( $after );
}
}
return $open . esc_html( $text ) . $close;
}
private static function current_product_id() {
global $product;
if ( $product && is_object( $product ) && method_exists( $product, 'get_id' ) ) {
return (int) $product->get_id();
}
if ( function_exists( 'is_singular' ) && is_singular( 'product' ) ) {
return (int) get_the_ID();
}
return 0;
}
}
}