status = $status; } /** * Register hooks. * * @return void */ public function init() { add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) ); // HARDENING (DESIGN decision #19/#20): enqueue the un-delayable bootstrap + // inline config UNCONDITIONALLY on eligible product pages in // wp_enqueue_scripts, independent of when (or whether) the trigger markup is // rendered. Page builders like Elementor (the 3sides.it stack) render the // product template from their own hooks/templates, so relying on the // render-path to enqueue (the old behaviour) could miss the loader entirely. // We register at the default priority and enqueue late so is_product() and // the global $product are resolved. The render path only prints trigger markup. add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_on_product' ), 20 ); // Set the durable first-party visitor cookie as early as possible (before any // output) on product pages, so attribution events carry a stable visitorKey. // We hook several increasingly-late points and let the method no-op once the // cookie is persisted: 'send_headers' / 'init' fire before most theme output // (shrinking the headers_sent() race window where the cookie can't be set), // and 'template_redirect' is the backstop once the main query is resolved and // is_product() is reliable. The earliest hook at which is_product() is true and // headers are still open wins; the rest no-op. add_action( 'send_headers', array( $this, 'maybe_set_visitor_cookie' ) ); add_action( 'init', array( $this, 'maybe_set_visitor_cookie' ) ); add_action( 'template_redirect', array( $this, 'maybe_set_visitor_cookie' ) ); // Auto placement: render after the Add to cart button on simple/variable products. add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'maybe_auto_render' ) ); // Shortcode (universal fallback). add_shortcode( 'aisthetix_tryon', array( $this, 'shortcode' ) ); // Optimizer compatibility exclusions. $this->register_optimizer_exclusions(); } /** * Read the well-formed first-party `aisthetix_vid` cookie value, or '' if absent * or malformed. * * The value is a random 32-hex token. We deliberately do not trust an arbitrary * cookie value verbatim — only accept the expected shape — so a hostile cookie * can't inject markup or oversized keys downstream. * * @return string The validated cookie value, or '' when none. */ private function read_visitor_cookie() { if ( ! isset( $_COOKIE[ self::VISITOR_COOKIE ] ) ) { return ''; } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- validated by the strict regex below. $candidate = wp_unslash( $_COOKIE[ self::VISITOR_COOKIE ] ); if ( is_string( $candidate ) && preg_match( '/^[a-f0-9]{32}$/', $candidate ) ) { return $candidate; } return ''; } /** * Resolve the durable visitor key for this request: read the first-party * `aisthetix_vid` cookie if present and well-formed, otherwise mint a fresh one. * * Used by the cookie-setting path (the minted value WILL be persisted there). For * the client config use {@see config_visitor_key()} instead, which never advertises * an ephemeral (un-persistable) key. * * @return string */ public function get_visitor_key() { if ( null !== $this->visitor_key ) { return $this->visitor_key; } $existing = $this->read_visitor_cookie(); if ( '' === $existing ) { $existing = md5( wp_generate_password( 32, false, false ) . microtime() . wp_rand() ); } $this->visitor_key = $existing; return $this->visitor_key; } /** * Resolve the visitorKey to advertise in window.AISTHETIX_CONFIG. * * CONSISTENCY GUARANTEE: the value placed in the client config must equal the value * the later server-side order event will read from $_COOKIE — otherwise the client * try-on/view events and the server checkout event are filed under two different * visitor keys and never correlate. * * - If the cookie was (or could be) persisted this request, the resolved key is * durable → use it. * - If the cookie could NOT be set (headers already sent and no prior cookie), * minting a brand-new key here would be ephemeral: it would live only in this * page's JS and never reach the order-event cookie read. So instead we fall back * to the existing (validated) cookie value, or null. null is honest — the brain * then attributes via customerId for logged-in shoppers and simply can't tie * anonymous events from this single un-cookied page view. * * @return string|null */ public function config_visitor_key() { // A real (persistable) cookie value already exists on the request. $existing = $this->read_visitor_cookie(); if ( '' !== $existing ) { return $existing; } // No cookie yet. Only advertise a freshly minted key if it was actually // persisted (Set-Cookie emitted this request); otherwise it can't survive to // the order event, so report null rather than a value that won't agree. if ( $this->cookie_persisted ) { return $this->get_visitor_key(); } return null; } /** * Set (or refresh) the first-party visitor cookie on product pages, server-side. * * SameSite=Lax, ~13-month expiry, Secure when the site is HTTPS, HttpOnly=false * because the widget's browser JS must read it to attach visitorKey to client * events (it is a non-secret analytics id, not a credential). Runs on * template_redirect (before output) so the Set-Cookie header is valid. * * @return void */ public function maybe_set_visitor_cookie() { // Idempotent: this runs on send_headers + init + template_redirect; the first // eligible invocation persists the cookie and the rest no-op. if ( $this->cookie_persisted ) { return; } if ( headers_sent() ) { return; } if ( ! Plugin::is_connected() ) { return; } if ( ! function_exists( 'is_product' ) || ! is_product() ) { return; } // Opt-in analytics: do NOT set the durable visitor cookie unless the merchant // has enabled anonymous analytics (default OFF). See Plugin::analytics_enabled(). if ( ! Plugin::analytics_enabled() ) { return; } $value = $this->get_visitor_key(); // Refresh the cookie each product-page view to keep the 13-month window // rolling for active shoppers. $args = array( 'expires' => time() + self::VISITOR_COOKIE_TTL, 'path' => defined( 'COOKIEPATH' ) && COOKIEPATH ? COOKIEPATH : '/', 'domain' => defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : '', 'secure' => is_ssl(), 'httponly' => false, 'samesite' => 'Lax', ); if ( PHP_VERSION_ID >= 70300 ) { setcookie( self::VISITOR_COOKIE, $value, $args ); } else { // Older PHP: smuggle SameSite via the path argument (a well-known shim). setcookie( self::VISITOR_COOKIE, $value, $args['expires'], $args['path'] . '; samesite=Lax', $args['domain'], $args['secure'], $args['httponly'] ); } // Make the just-set value readable within this same request (so the order // event and config_visitor_key() see the same value the browser will return). $_COOKIE[ self::VISITOR_COOKIE ] = $value; $this->cookie_persisted = true; } /** * Enqueue the bootstrap + inline config on eligible product pages, regardless of * whether/when the trigger button is rendered. See init() for the rationale. * * @return void */ public function maybe_enqueue_on_product() { if ( ! $this->can_render() ) { return; } $this->ensure_assets( $this->resolve_product() ); } /** * Resolve the current product context. * * The global $product is frequently NOT set yet at wp_enqueue_scripts time (the * loop has not run), and on block themes the enqueue pass can run before the * product block renders. Fall back to the queried product so the injected config * is always complete (publishableKey + productId + productImage), never empty. * * @return \WC_Product|null */ private function resolve_product() { global $product; if ( $product instanceof \WC_Product ) { return $product; } if ( function_exists( 'wc_get_product' ) ) { $queried = wc_get_product( get_queried_object_id() ); if ( $queried instanceof \WC_Product ) { return $queried; } } return null; } /** * Whether the try-on entry point is allowed to render on the current request. * * @return bool */ public function can_render() { if ( ! Plugin::is_connected() ) { return false; } // Only on single product pages where we have a product image to try on. if ( ! function_exists( 'is_product' ) || ! is_product() ) { return false; } // Cached status gate: active && withinQuota. return $this->status->should_render(); } /** * Register (do not yet enqueue) the bootstrap loader. * * The bootstrap is deliberately tiny and synchronous-friendly so JS-delay * optimizers cannot break it. The heavy widget bundle is lazy-loaded by the * bootstrap itself on first intent (click/hover). * * @return void */ public function register_assets() { wp_register_script( self::BOOTSTRAP_HANDLE, AISTHETIX_TRYON_URL . 'assets/bootstrap.js', array(), AISTHETIX_TRYON_VERSION, true ); // Tell optimizers (and the browser) to leave this script alone. WordPress // core does not render arbitrary data-* keys from wp_script_add_data into // the