true ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- inline only, nothing to cache-bust. wp_enqueue_script( self::HANDLE ); wp_add_inline_script( self::HANDLE, $loader ); } catch ( \Throwable $e ) { if ( ! self::$logged ) { self::$logged = true; try { Log::error( 'Pixel left off for this page: ' . $e->getMessage() ); } catch ( \Throwable $ignored ) { // A broken logger must not fatal the storefront either: fail closed means no pixel, quietly. unset( $ignored ); } } } } /** * Connected with a pixel id, switched on, not vetoed by the cortexcommerce_pixel_enabled filter, and not a page * that handles payment or account details. */ public static function should_load(): bool { if ( is_admin() || ! Options::is_connected() || '' === Options::pixel_id() || ! Options::settings()['pixel_enabled'] ) { return false; } if ( self::excluded() ) { return false; } return (bool) apply_filters( 'cortexcommerce_pixel_enabled', true ); } /** * Checkout (including order received and order pay), and account pages (including login and registration). */ public static function excluded(): bool { return ( function_exists( 'is_checkout' ) && is_checkout() ) || ( function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url( 'order-pay' ) ) || ( function_exists( 'is_account_page' ) && is_account_page() ) || is_customize_preview(); } /** * @return array{v: int, id: string, endpoint: string, pt: string, p?: string, t?: string, q?: string} */ public static function config(): array { $type = self::page_type(); $config = array( 'v' => 2, 'id' => Options::pixel_id(), 'endpoint' => Config::api_base() . '/pixel/v2/e', 'pt' => $type, ); if ( 'product' === $type ) { $config['p'] = (string) get_queried_object_id(); } if ( 'category' === $type || 'tag' === $type ) { $config['t'] = (string) get_queried_object_id(); } if ( 'search' === $type ) { $query = trim( (string) get_search_query( false ) ); if ( '' !== $query ) { $config['q'] = mb_substr( $query, 0, 100 ); } } return $config; } public static function page_type(): string { if ( is_front_page() ) { return 'home'; } if ( is_search() ) { return 'search'; } if ( function_exists( 'is_shop' ) && is_shop() ) { return 'shop'; } if ( function_exists( 'is_product' ) && is_product() ) { return 'product'; } if ( function_exists( 'is_product_category' ) && is_product_category() ) { return 'category'; } if ( function_exists( 'is_product_tag' ) && is_product_tag() ) { return 'tag'; } if ( function_exists( 'is_cart' ) && is_cart() ) { return 'cart'; } if ( is_page() ) { return 'page'; } if ( is_singular( 'post' ) ) { return 'post'; } return 'other'; } /** * The inline script: the config, then a loader that adds the hosted script once the page has loaded. */ public static function loader(): string { $flags = JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT; $config = (string) wp_json_encode( self::config(), $flags ); $src = (string) wp_json_encode( Config::pixel_src(), $flags ); return 'window.cortexcommercePixel = ' . $config . ';' . '(function(w,d,s){function go(){try{var e=d.createElement("script");e.src=s;e.async=true;e.id="cortexcommerce-pixel-src";' . '(d.head||d.documentElement).appendChild(e);}catch(x){}}' . 'if(d.readyState==="complete"){w.setTimeout(go,0);}else{w.addEventListener("load",function(){w.setTimeout(go,0);});}' . '})(window,document,' . $src . ');'; } }