Cookie consent). Basket and checkout activity uses WooCommerce's own * basket session. Shop staff and bots are ignored, and a basket only counts once the browser shows a real * person is there (a mouse move, tap, scroll or key press). * * @package Basketwise */ defined( 'ABSPATH' ) || exit; // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- the plugin's own tables; values are always prepared. /** Should this request be ignored (bots and, if set, shop staff)? */ function brai_skip() { static $skip = null; if ( null === $skip ) { $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; $skip = '' === $ua || (bool) preg_match( '/bot|crawl|spider|slurp|preview|lighthouse|headless|curl|wget|python|monitor|uptime|facebookexternalhit|pingdom/i', $ua ) || ( brai_setting( 'staff_skip' ) && is_user_logged_in() && current_user_can( 'edit_shop_orders' ) && empty( $_COOKIE['brai_test'] ) ); } return $skip; } /** Has the browser shown a real person is there? (Set by the tracker on the first mouse move, tap, scroll or key press.) */ function brai_human() { return is_user_logged_in() || ( isset( $_COOKIE['brai_h'] ) && '1' === $_COOKIE['brai_h'] ); } /** The browsing cookie, only set in the browser once analytics cookies are accepted. */ function brai_cookie_vid() { $v = isset( $_COOKIE['brai_vid'] ) ? sanitize_key( wp_unslash( $_COOKIE['brai_vid'] ) ) : ''; return preg_match( '/^[a-f0-9]{24}$/', $v ) ? $v : ''; } /** * The key for this visitor. A basket started before the browsing cookie existed is merged into it. * * @param bool $fresh Work it out again. * @return string */ function brai_vid( $fresh = false ) { static $vid = null; if ( null !== $vid && ! $fresh ) { return $vid; } $c = brai_cookie_vid(); $s = ''; if ( function_exists( 'WC' ) && WC()->session && ( WC()->session->has_session() || ( WC()->cart && ! WC()->cart->is_empty() ) ) ) { $s = 's' . substr( md5( WC()->session->get_customer_id() . '|' . wp_salt( 'nonce' ) ), 0, 23 ); } if ( $c && $s ) { brai_merge( $s, $c ); } $vid = $c ? $c : $s; return $vid; } /** * How far along a status is. * * @param string $status Status. * @return int */ function brai_rank( $status ) { return (int) array_search( $status, array( 'browsing', 'basket', 'checkout', 'ordered' ), true ); } /** * Merges one visitor record into another. * * @param string $from Visitor to merge away. * @param string $to Visitor to keep. */ function brai_merge( $from, $to ) { global $wpdb; $v = brai_table( 'visitors' ); $a = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE vid = %s', $v, $from ), ARRAY_A ); if ( ! $a ) { return; } $wpdb->update( brai_table( 'events' ), array( 'vid' => $to ), array( 'vid' => $from ) ); $wpdb->update( brai_table( 'messages' ), array( 'vid' => $to ), array( 'vid' => $from ) ); $b = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE vid = %s', $v, $to ), ARRAY_A ); if ( ! $b ) { $wpdb->update( $v, array( 'vid' => $to ), array( 'vid' => $from ) ); return; } $u = array( 'views' => (int) $a['views'] + (int) $b['views'], 'product_views' => (int) $a['product_views'] + (int) $b['product_views'], 'first_seen' => min( $a['first_seen'], $b['first_seen'] ), 'last_seen' => max( $a['last_seen'], $b['last_seen'] ), ); foreach ( array( 'user_id', 'email', 'name', 'phone', 'landing', 'referrer' ) as $k ) { if ( empty( $b[ $k ] ) && ! empty( $a[ $k ] ) ) { $u[ $k ] = $a[ $k ]; } } if ( brai_rank( $a['status'] ) >= brai_rank( $b['status'] ) || $a['last_seen'] > $b['last_seen'] ) { foreach ( array( 'status', 'cart', 'cart_value', 'order_id', 'basket_at', 'ordered_at' ) as $k ) { $u[ $k ] = $a[ $k ]; } } $wpdb->update( $v, $u, array( 'vid' => $to ) ); $wpdb->delete( $v, array( 'vid' => $from ) ); } /** * Creates or updates a visitor record. * * @param string $vid Visitor. * @param array $set Fields to set. * @param array $inc Counters to add to. * @param array $first Fields only used for a new record. */ function brai_touch( $vid, $set = array(), $inc = array(), $first = array() ) { global $wpdb; $v = brai_table( 'visitors' ); $now = current_time( 'mysql', true ); $row = $wpdb->get_row( $wpdb->prepare( 'SELECT vid, email FROM %i WHERE vid = %s', $v, $vid ), ARRAY_A ); $uid = get_current_user_id(); if ( $uid ) { $set += array( 'user_id' => $uid ); if ( ! $row || '' === $row['email'] ) { $u = wp_get_current_user(); $name = trim( $u->first_name . ' ' . $u->last_name ); $set += array( 'email' => $u->user_email, 'name' => $name ? $name : $u->display_name, ); } } if ( ! $row ) { $data = array_merge( array( 'vid' => $vid, 'first_seen' => $now, 'last_seen' => $now, 'status' => 'browsing', ), $first, $set ); foreach ( $inc as $k => $n ) { $data[ $k ] = (int) $n; } $wpdb->insert( $v, $data ); return; } $set['last_seen'] = $now; $wpdb->update( $v, $set, array( 'vid' => $vid ) ); foreach ( $inc as $k => $n ) { if ( in_array( $k, array( 'views', 'product_views', 'visits', 'secs' ), true ) ) { $wpdb->query( $wpdb->prepare( 'UPDATE %i SET %i = %i + %d WHERE vid = %s', $v, $k, $k, (int) $n, $vid ) ); } } } /** * Records one event in a visitor's timeline. * * @param string $vid Visitor. * @param string $type Event type. * @param int $object_id Product, category, order or coupon ID. * @param int $qty Quantity or count. * @param float $value Money value. * @param string $url Page, search term or code. */ function brai_event( $vid, $type, $object_id = 0, $qty = 0, $value = 0, $url = '' ) { global $wpdb; $wpdb->insert( brai_table( 'events' ), array( 'vid' => $vid, 'ts' => current_time( 'mysql', true ), 'type' => $type, 'object_id' => (int) $object_id, 'qty' => (int) $qty, 'value' => round( (float) $value, 2 ), 'url' => substr( (string) $url, 0, 250 ), ) ); } /** * Was the same thing logged for this visitor in the last few minutes? * * @param string $vid Visitor. * @param string $type Event type. * @param int $object_id Object. * @param int $mins Minutes. * @return bool */ function brai_recent( $vid, $type, $object_id, $mins ) { global $wpdb; return (bool) $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE vid = %s AND type = %s AND object_id = %d AND ts > %s LIMIT 1', brai_table( 'events' ), $vid, $type, (int) $object_id, gmdate( 'Y-m-d H:i:s', time() - $mins * 60 ) ) ); } /** Device type, browser and operating system from the user agent. */ function brai_device() { $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; if ( preg_match( '/iPad|Tablet|Kindle|Silk|SM-T\d/i', $ua ) || ( preg_match( '/Android/i', $ua ) && ! preg_match( '/Mobile/i', $ua ) ) ) { $d = 'Tablet'; } elseif ( preg_match( '/Mobi|iPhone|iPod|Windows Phone/i', $ua ) ) { $d = 'Mobile'; } else { $d = 'Desktop'; } $b = 'Other'; foreach ( array( 'Edge' => '/Edg(e|A|iOS)?\//', 'Opera' => '/OPR\/|Opera/', 'Samsung Internet' => '/SamsungBrowser/', 'Firefox' => '/Firefox|FxiOS/', 'Chrome' => '/Chrome|CriOS/', 'Safari' => '/Safari/', ) as $name => $re ) { if ( preg_match( $re, $ua ) ) { $b = $name; break; } } $o = 'Other'; foreach ( array( 'iOS' => '/iPhone|iPad|iPod/', 'Android' => '/Android/', 'Windows' => '/Windows/', 'ChromeOS' => '/CrOS/', 'macOS' => '/Mac OS X|Macintosh/', 'Linux' => '/Linux/', ) as $name => $re ) { if ( preg_match( $re, $ua ) ) { $o = $name; break; } } return array( 'device' => $d, 'browser' => $b, 'os' => $o, ); } /** * Rough location (town, region, country) from the visitor's IP address, looked up once per visitor when * location is switched on in Settings. The IP address is sent to the lookup service but never stored. * '-' means it could not be worked out. * * @return array */ function brai_geo() { $ip = class_exists( 'WC_Geolocation' ) ? WC_Geolocation::get_ip_address() : ( isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '' ); $out = array( 'country' => '-' ); if ( ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) { return $out; } $names = function ( $c ) { $c = html_entity_decode( (string) $c ); if ( 2 === strlen( $c ) && function_exists( 'WC' ) ) { $cs = WC()->countries->get_countries(); $c = html_entity_decode( (string) ( $cs[ strtoupper( $c ) ] ?? $c ) ); } return substr( trim( preg_replace( '/\s*\(.*\)$/', '', $c ) ), 0, 60 ); }; $try = apply_filters( 'brai_geo_services', array( 'https://ipapi.co/%s/json/' => array( 'country_name', 'region', 'city' ), 'https://ipinfo.io/%s/json' => array( 'country', 'region', 'city' ), 'https://freeipapi.com/api/json/%s' => array( 'countryName', 'regionName', 'cityName' ), ) ); foreach ( $try as $url => $k ) { $res = wp_remote_get( sprintf( $url, rawurlencode( $ip ) ), array( 'timeout' => 2, 'user-agent' => 'Basketwise', ) ); if ( is_wp_error( $res ) || 200 !== (int) wp_remote_retrieve_response_code( $res ) ) { continue; } $j = json_decode( (string) wp_remote_retrieve_body( $res ), true ); if ( ! is_array( $j ) || ! empty( $j['error'] ) || empty( $j[ $k[0] ] ) || empty( $j[ $k[2] ] ) ) { continue; } return array( 'country' => $names( $j[ $k[0] ] ), 'region' => substr( (string) ( $j[ $k[1] ] ?? '' ), 0, 80 ), 'city' => substr( (string) $j[ $k[2] ], 0, 80 ), ); } if ( class_exists( 'WC_Geolocation' ) ) { $g = WC_Geolocation::geolocate_ip( $ip, true, true ); if ( ! empty( $g['country'] ) ) { $out['country'] = $names( $g['country'] ); } } return $out; } /* ---------- Page views (sent from the browser, so cached pages still count) ---------- */ /** * Answer for a request whose nonce is missing or out of date, which is normal on a page served from a cache. * The tracker script then asks for a fresh nonce (below) and sends the request once more. */ function brai_ajax_stale() { nocache_headers(); status_header( 403 ); exit; } /** Gives the tracker script a fresh nonce. Takes no input and changes nothing. */ function brai_ajax_nonce() { nocache_headers(); wp_send_json( array( 'n' => wp_create_nonce( 'brai_track' ) ) ); } add_action( 'wp_ajax_brai_nonce', 'brai_ajax_nonce' ); add_action( 'wp_ajax_nopriv_brai_nonce', 'brai_ajax_nonce' ); /** Records a page view, visit, site search, device, location and campaign details. */ function brai_ajax_hit() { if ( ! check_ajax_referer( 'brai_track', 'n', false ) ) { brai_ajax_stale(); } $vid = brai_vid(); if ( brai_skip() || ! brai_cookie_vid() || ! $vid ) { status_header( 204 ); exit; } global $wpdb; $pid = absint( $_POST['p'] ?? 0 ); $cid = absint( $_POST['c'] ?? 0 ); $url = substr( sanitize_text_field( wp_unslash( $_POST['url'] ?? '' ) ), 0, 250 ); $ref = esc_url_raw( wp_unslash( $_POST['ref'] ?? '' ) ); $home = wp_parse_url( home_url(), PHP_URL_HOST ); if ( $ref && wp_parse_url( $ref, PHP_URL_HOST ) === $home ) { $ref = ''; // Only remember where they came from outside the site. } $inc = array( 'views' => 1 ); if ( ! empty( $_POST['ns'] ) ) { $inc['visits'] = 1; brai_event( $vid, 'visit', 0, 0, 0, $url ); } if ( $pid && 'product' === get_post_type( $pid ) ) { $inc['product_views'] = 1; if ( ! brai_recent( $vid, 'view', $pid, 10 ) ) { brai_event( $vid, 'view', $pid, 0, 0, $url ); } } elseif ( $cid && term_exists( $cid, 'product_cat' ) && ! brai_recent( $vid, 'cat', $cid, 10 ) ) { brai_event( $vid, 'cat', $cid, 0, 0, $url ); } if ( brai_setting( 'track_search' ) ) { $sq = trim( substr( sanitize_text_field( wp_unslash( $_POST['q'] ?? '' ) ), 0, 100 ) ); if ( '' !== $sq ) { $dup = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM %i WHERE vid = %s AND type = 'search' AND url = %s AND ts > %s LIMIT 1", brai_table( 'events' ), $vid, $sq, gmdate( 'Y-m-d H:i:s', time() - 600 ) ) ); if ( ! $dup ) { $found = new WP_Query( array( 'post_type' => 'product', 'post_status' => 'publish', 's' => $sq, 'posts_per_page' => 1, 'fields' => 'ids', ) ); brai_event( $vid, 'search', 0, (int) $found->found_posts, 0, $sq ); } } } $set = array(); $prof = $wpdb->get_row( $wpdb->prepare( 'SELECT device, country FROM %i WHERE vid = %s', brai_table( 'visitors' ), $vid ), ARRAY_A ); if ( brai_setting( 'track_device' ) && ( ! $prof || '' === (string) $prof['device'] ) ) { $set = brai_device(); $sw = preg_replace( '/[^0-9x]/', '', sanitize_text_field( wp_unslash( $_POST['sw'] ?? '' ) ) ); if ( $sw ) { $set['screen'] = substr( $sw, 0, 15 ); } } if ( brai_setting( 'geo' ) && ( ! $prof || '' === (string) $prof['country'] ) ) { $set += brai_geo(); } $src = json_decode( sanitize_text_field( wp_unslash( $_POST['src'] ?? '' ) ), true ); if ( is_array( $src ) ) { $keep = array(); foreach ( array( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gad_campaignid', 'gad_source', 'gclid', 'gbraid', 'wbraid', 'srsltid', 'msclkid', 'fbclid' ) as $k ) { if ( isset( $src[ $k ] ) && '' !== (string) $src[ $k ] ) { $keep[ $k ] = substr( sanitize_text_field( (string) $src[ $k ] ), 0, 100 ); } } if ( $keep ) { $set['src'] = wp_json_encode( $keep ); } } brai_touch( $vid, $set, $inc, array( 'landing' => $url, 'referrer' => substr( $ref, 0, 250 ), ) ); status_header( 204 ); exit; } add_action( 'wp_ajax_brai_hit', 'brai_ajax_hit' ); add_action( 'wp_ajax_nopriv_brai_hit', 'brai_ajax_hit' ); /** Time spent on the site, sent when a page is hidden or closed. */ function brai_ajax_time() { if ( ! check_ajax_referer( 'brai_track', 'n', false ) ) { brai_ajax_stale(); } $vid = brai_cookie_vid(); $s = isset( $_POST['s'] ) ? min( 1800, absint( $_POST['s'] ) ) : 0; if ( $vid && $s && ! brai_skip() ) { global $wpdb; $wpdb->query( $wpdb->prepare( 'UPDATE %i SET secs = secs + %d WHERE vid = %s', brai_table( 'visitors' ), $s, $vid ) ); } status_header( 204 ); exit; } add_action( 'wp_ajax_brai_time', 'brai_ajax_time' ); add_action( 'wp_ajax_nopriv_brai_time', 'brai_ajax_time' ); // ---------- Optional: block "add to basket" links followed by crawlers ---------- // Shop and category pages carry plain ?add-to-cart=ID links. Shoppers' clicks go through JavaScript or come // from a page on this site, so a plain link visit from elsewhere is usually a crawler following every link. add_action( 'wp_loaded', function () { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- WooCommerce's own front-end link, which has no nonce. It is only read to decide whether to ignore it: nothing is saved and nothing is added to the basket. if ( ! brai_setting( 'block_links' ) || empty( $_GET['add-to-cart'] ) || 'GET' !== ( isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : '' ) || wp_doing_ajax() || is_admin() ) { return; } $ref = isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; $host = preg_replace( '/^www\./', '', strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) ); $from = preg_replace( '/^www\./', '', strtolower( (string) wp_parse_url( $ref, PHP_URL_HOST ) ) ); if ( $from && $from === $host ) { return; } unset( $_GET['add-to-cart'], $_REQUEST['add-to-cart'] ); // phpcs:enable WordPress.Security.NonceVerification.Recommended nocache_headers(); header( 'X-Robots-Tag: noindex, nofollow' ); wp_safe_redirect( remove_query_arg( array( 'add-to-cart', 'quantity', 'variation_id' ) ), 302, 'Basketwise' ); exit; }, 1 ); /* ---------- Basket changes (saved at the end of the request) ---------- */ /** * Queues a basket change to save at the end of the request. * * @param string $type add, remove or qty. * @param int $product_id Product. * @param int $qty Quantity. * @param float $value Value inc VAT. */ function brai_queue( $type, $product_id, $qty, $value = 0 ) { if ( brai_skip() || ! brai_human() ) { return; } $GLOBALS['brai_dirty'] = true; $GLOBALS['brai_ev'][] = array( $type, (int) $product_id, (int) $qty, (float) $value ); } add_action( 'woocommerce_add_to_cart', function ( $key, $pid, $qty, $var_id = 0 ) { $p = wc_get_product( $var_id ? $var_id : $pid ); brai_queue( 'add', $var_id ? $var_id : $pid, $qty, $p ? wc_get_price_including_tax( $p, array( 'qty' => $qty ) ) : 0 ); }, 20, 4 ); add_action( 'woocommerce_cart_item_removed', function ( $key, $cart ) { $it = $cart->removed_cart_contents[ $key ] ?? null; if ( $it ) { brai_queue( 'remove', ! empty( $it['variation_id'] ) ? $it['variation_id'] : $it['product_id'], $it['quantity'] ); } }, 20, 2 ); add_action( 'woocommerce_after_cart_item_quantity_update', function ( $key, $qty, $old, $cart ) { $it = $cart->cart_contents[ $key ] ?? null; if ( $it && (int) $qty !== (int) $old ) { brai_queue( 'qty', ! empty( $it['variation_id'] ) ? $it['variation_id'] : $it['product_id'], $qty ); } }, 20, 4 ); add_action( 'woocommerce_cart_emptied', function () { if ( ! brai_skip() && brai_human() ) { $GLOBALS['brai_dirty'] = true; } } ); /* Reaching the checkout page with something in the basket. */ add_action( 'template_redirect', function () { if ( brai_skip() || ! brai_human() || ! function_exists( 'is_checkout' ) || ! is_checkout() || is_wc_endpoint_url( 'order-received' ) || is_wc_endpoint_url( 'order-pay' ) ) { return; } if ( WC()->cart && ! WC()->cart->is_empty() ) { $GLOBALS['brai_dirty'] = true; $GLOBALS['brai_checkout'] = true; } } ); add_action( 'shutdown', function () { if ( empty( $GLOBALS['brai_dirty'] ) || brai_skip() || ! brai_human() || ! function_exists( 'WC' ) || ! WC()->cart || ! WC()->session ) { return; } $vid = brai_vid( true ); if ( ! $vid ) { return; } global $wpdb; $cur = $wpdb->get_row( $wpdb->prepare( 'SELECT status, ordered_at FROM %i WHERE vid = %s', brai_table( 'visitors' ), $vid ), ARRAY_A ); $status = $cur ? $cur['status'] : 'browsing'; $just_ordered = $cur && 'ordered' === $status && $cur['ordered_at'] && strtotime( $cur['ordered_at'] . ' UTC' ) > time() - HOUR_IN_SECONDS; $items = array(); $total = 0.0; foreach ( WC()->cart->get_cart() as $it ) { $p = $it['data'] ?? null; if ( ! $p ) { continue; } $line = (float) wc_get_price_including_tax( $p, array( 'qty' => (int) $it['quantity'] ) ); $items[] = array( 'id' => $p->get_id(), 'name' => wp_strip_all_tags( $p->get_name() ), 'qty' => (int) $it['quantity'], 'total' => round( $line, 2 ), ); $total += $line; } $set = array(); if ( $items && ! $just_ordered ) { $set = array( 'status' => ( ! empty( $GLOBALS['brai_checkout'] ) || 'checkout' === $status ) ? 'checkout' : 'basket', 'cart' => wp_json_encode( $items ), 'cart_value' => round( $total, 2 ), ); if ( ! in_array( $status, array( 'basket', 'checkout' ), true ) ) { $set['basket_at'] = current_time( 'mysql', true ); $set['reminders'] = 0; // A new basket gets its own reminders. $set['contacted_at'] = null; // And its own follow-up. $set['contacted_by'] = 0; } } elseif ( ! $items && in_array( $status, array( 'basket', 'checkout' ), true ) ) { $set = array( 'status' => 'browsing', 'cart' => null, 'cart_value' => 0, ); } brai_touch( $vid, $set ); foreach ( (array) ( $GLOBALS['brai_ev'] ?? array() ) as $e ) { brai_event( $vid, $e[0], $e[1], $e[2], $e[3] ); } if ( ! empty( $GLOBALS['brai_checkout'] ) && ! $just_ordered && ! brai_recent( $vid, 'checkout', 0, 30 ) ) { brai_event( $vid, 'checkout', 0, count( $items ), $total ); } }, 5 ); /* ---------- Contact details typed at checkout (before the order is placed) ---------- */ /** Saves the email, name and phone a customer types at checkout. */ function brai_ajax_contact() { if ( ! check_ajax_referer( 'brai_track', 'n', false ) ) { brai_ajax_stale(); } $vid = brai_vid(); $email = sanitize_email( wp_unslash( $_POST['email'] ?? '' ) ); if ( brai_skip() || ! $vid || ! is_email( $email ) ) { status_header( 204 ); exit; } global $wpdb; $old = $wpdb->get_row( $wpdb->prepare( 'SELECT email, status FROM %i WHERE vid = %s', brai_table( 'visitors' ), $vid ), ARRAY_A ); $name = trim( sanitize_text_field( wp_unslash( $_POST['first'] ?? '' ) ) . ' ' . sanitize_text_field( wp_unslash( $_POST['last'] ?? '' ) ) ); $set = array( 'email' => $email ); if ( $name ) { $set['name'] = substr( $name, 0, 190 ); } $phone = substr( sanitize_text_field( wp_unslash( $_POST['phone'] ?? '' ) ), 0, 50 ); if ( $phone ) { $set['phone'] = $phone; } if ( $old && 'basket' === $old['status'] ) { $set['status'] = 'checkout'; } brai_touch( $vid, $set ); if ( ! $old || strtolower( $old['email'] ) !== strtolower( $email ) ) { brai_event( $vid, 'email' ); } status_header( 204 ); exit; } add_action( 'wp_ajax_brai_contact', 'brai_ajax_contact' ); add_action( 'wp_ajax_nopriv_brai_contact', 'brai_ajax_contact' ); /* ---------- Orders ---------- */ /** * Marks the visitor as ordered. * * @param int|WC_Order $order Order. */ function brai_order( $order ) { if ( is_numeric( $order ) ) { $order = wc_get_order( $order ); } if ( ! $order || brai_skip() ) { return; } $vid = brai_vid( true ); if ( ! $vid ) { return; } $order->update_meta_data( '_brai_vid', $vid ); $order->save_meta_data(); brai_touch( $vid, array( 'status' => 'ordered', 'order_id' => $order->get_id(), 'ordered_at' => current_time( 'mysql', true ), 'email' => $order->get_billing_email(), 'name' => trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ), 'phone' => (string) $order->get_billing_phone(), ) ); brai_event( $vid, 'order', $order->get_id(), $order->get_item_count(), (float) $order->get_total() ); } add_action( 'woocommerce_checkout_order_processed', function ( $id ) { brai_order( $id ); }, 20 ); add_action( 'woocommerce_store_api_checkout_order_processed', 'brai_order', 20 ); /* ---------- The browser side ---------- */ add_action( 'wp_enqueue_scripts', function () { $s = brai_settings(); $co = function_exists( 'is_checkout' ) && is_checkout() && ! is_wc_endpoint_url( 'order-received' ); wp_enqueue_script( 'brai-tracker', BRAI_URL . 'assets/tracker.js', array(), BRAI_VERSION, array( 'in_footer' => true, 'strategy' => 'defer', ) ); wp_localize_script( 'brai-tracker', 'BRAI', array( 'ajax' => admin_url( 'admin-ajax.php' ), 'n' => wp_create_nonce( 'brai_track' ), 'p' => is_singular( 'product' ) ? (int) get_queried_object_id() : 0, 'c' => ( function_exists( 'is_product_category' ) && is_product_category() ) ? (int) get_queried_object_id() : 0, 'checkout' => $co ? 1 : 0, 'note' => ( $co && brai_rec_live() ) ? __( 'If you do not finish your order, we may email you a reminder about your basket. Every reminder has an unsubscribe link.', 'basketwise' ) : '', 'consent' => $s['consent'], 'cookie' => $s['consent_cookie'], 'cvalue' => $s['consent_value'], 'time' => $s['track_time'] ? 1 : 0, ) ); } );