config(); $mode = isset( $c['openai_mode'] ) ? (string) $c['openai_mode'] : 'off'; return in_array( $mode, array( 'browser', 'both' ), true ) && ! empty( $c['openai_pixel_id'] ); } private function pixel_id() { $c = $this->config(); return isset( $c['openai_pixel_id'] ) ? (string) $c['openai_pixel_id'] : ''; } public function enqueue_base() { if ( ! $this->browser_enabled() ) { return; } $pixel = $this->pixel_id(); if ( '' === $pixel ) { return; } wp_register_script( self::HANDLE, '', array(), defined( 'AIEO_VERSION' ) ? AIEO_VERSION : '1.0', true ); wp_enqueue_script( self::HANDLE ); $boot = "(function(w,d,s,u){" . "if(w.oaiq){w.aieoOpenAIStoodDown=1;return;}" . "var q=function(){q.q.push(arguments);};q.q=[];w.oaiq=q;" . "var js=d.createElement(s);js.async=true;js.src=u;" . "var f=d.getElementsByTagName(s)[0];f.parentNode.insertBefore(js,f);" . "}(window,document,'script'," . wp_json_encode( self::SDK_SRC, JSON_UNESCAPED_SLASHES ) . "));"; $init = "if(window.oaiq&&!window.aieoOpenAIStoodDown){" . "oaiq('init',{pixelId:" . wp_json_encode( $pixel ) . "});" . "oaiq('consent',true);" . "oaiq('measureSingle'," . wp_json_encode( $pixel ) . ",'page_viewed',{type:'customer_action'});" . "}"; $gated = '(function aieoOpenAIRun(){if(window.aieoOpenAIStarted){return;}' . "if(typeof window.aieoConsentFor!=='function'||!window.aieoConsentFor('marketing')){" . "if(typeof window.aieoOnConsent==='function'&&!aieoOpenAIRun.waiting){aieoOpenAIRun.waiting=1;window.aieoOnConsent(aieoOpenAIRun);}return;}" . 'window.aieoOpenAIStarted=1;' . $boot . $init . '})();'; wp_add_inline_script( self::HANDLE, $gated ); } public function on_thankyou( $order_id ) { if ( ! $this->browser_enabled() || ! function_exists( 'wc_get_order' ) ) { return; } $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $pixel = $this->pixel_id(); if ( '' === $pixel ) { return; } $currency = $order->get_currency(); $eid = (string) $order->get_meta( AIEO_OpenAI_Events_Sender::EVENT_ID_META ); if ( '' === $eid ) { $eid = wp_generate_uuid4(); $order->update_meta_data( AIEO_OpenAI_Events_Sender::EVENT_ID_META, $eid ); $order->save(); } $contents = array(); foreach ( $order->get_items() as $item ) { $product = method_exists( $item, 'get_product' ) ? $item->get_product() : null; $qty = (int) $item->get_quantity(); $line = (float) $item->get_total(); $contents[] = array( 'id' => $product ? (string) $product->get_id() : '', 'name' => (string) $item->get_name(), 'content_type' => 'product', 'quantity' => $qty, 'amount' => AIEO_OpenAI_Money::to_minor( $qty > 0 ? $line / $qty : $line, $currency ), 'currency' => $currency, ); } $data = array( 'type' => 'contents', 'amount' => AIEO_OpenAI_Money::to_minor( $order->get_total(), $currency ), 'currency' => $currency, 'contents' => $contents, ); $js = "if(window.oaiq&&!window.aieoOpenAIStoodDown){" . "oaiq('measureSingle'," . wp_json_encode( $pixel ) . ",'order_created'," . wp_json_encode( $data ) . ",{event_id:" . wp_json_encode( $eid ) . "});" . "}"; wp_add_inline_script( self::HANDLE, $js ); } } }