$vid, 'k' => brai_sign( $vid, 'timeline' ), ), brai_admin_url() ); } /** * The visitor whose timeline was asked for: from a link on the admin screens (nonce) or from a staff email (signature). * * @return string Visitor, or '' if the link is not genuine. */ function brai_requested_vid() { $vid = sanitize_key( brai_get( 'vid' ) ); if ( '' !== $vid ) { return $vid; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- a link from a staff email: checked against its signature on the next line, and the screen itself needs the manage_woocommerce capability. $vid = isset( $_GET['vid'] ) ? sanitize_key( wp_unslash( $_GET['vid'] ) ) : ''; $key = isset( $_GET['k'] ) ? sanitize_text_field( wp_unslash( $_GET['k'] ) ) : ''; // phpcs:enable WordPress.Security.NonceVerification.Recommended return ( '' !== $vid && '' !== $key && hash_equals( brai_sign( $vid, 'timeline' ), $key ) ) ? $vid : ''; } /** * Money as plain text. * * @param float $v Amount. * @return string */ function brai_money( $v ) { return wp_strip_all_tags( wc_price( $v ) ); } /** * Landing-page query values merged with the campaign details the tracker sent. * * @param string $landing Landing URL. * @param string $src JSON from the tracker. * @return array */ function brai_params( $landing, $src = '' ) { $q = array(); parse_str( (string) wp_parse_url( (string) $landing, PHP_URL_QUERY ), $q ); $s = $src ? json_decode( (string) $src, true ) : array(); return array_merge( $q, is_array( $s ) ? $s : array() ); } /** * Where a visitor came from. * * @param string $landing Landing URL. * @param string $ref Referrer. * @param string $src Campaign JSON. * @return string */ function brai_source( $landing, $ref, $src = '' ) { $q = brai_params( $landing, $src ); if ( ! empty( $q['utm_source'] ) ) { return sanitize_text_field( $q['utm_source'] ) . ( ! empty( $q['utm_medium'] ) ? ' / ' . sanitize_text_field( $q['utm_medium'] ) : '' ); } if ( ! empty( $q['gclid'] ) || ! empty( $q['gad_source'] ) || ! empty( $q['gad_campaignid'] ) || ! empty( $q['gbraid'] ) || ! empty( $q['wbraid'] ) ) { return 'Google Ads'; } if ( ! empty( $q['msclkid'] ) ) { return 'Microsoft Ads'; } if ( ! empty( $q['srsltid'] ) ) { return 'Google (Shopping / search)'; } if ( ! empty( $q['fbclid'] ) ) { return 'Facebook'; } if ( $ref ) { $h = preg_replace( '/^www\./', '', (string) wp_parse_url( $ref, PHP_URL_HOST ) ); if ( preg_match( '/(^|\.)google\./', $h ) ) { return 'Google search'; } if ( preg_match( '/(^|\.)bing\./', $h ) ) { return 'Bing'; } return $h; } return 'Direct'; } /** * Google Ads campaign (from gad_campaignid) or UTM campaign, plus the UTM keyword. * * @param string $landing Landing URL. * @param string $src Campaign JSON. * @return string */ function brai_campaign( $landing, $src = '' ) { $q = brai_params( $landing, $src ); $out = ''; if ( ! empty( $q['gad_campaignid'] ) ) { $id = preg_replace( '/\D/', '', (string) $q['gad_campaignid'] ); $map = brai_campaign_names(); /* translators: %s: Google Ads campaign ID. */ $out = isset( $map[ $id ] ) ? $map[ $id ] : sprintf( __( 'Google Ads campaign %s', 'basketwise' ), $id ); } elseif ( ! empty( $q['utm_campaign'] ) ) { $out = sanitize_text_field( $q['utm_campaign'] ); } if ( ! empty( $q['utm_term'] ) ) { $out .= ( $out ? ' ยท ' : '' ) . '"' . sanitize_text_field( $q['utm_term'] ) . '"'; } return $out; } /** * Status label and colours for a visitor. * * @param array $r Visitor row. * @return array label, text colour, background. */ function brai_label( $r ) { $idle = strtotime( $r['last_seen'] . ' UTC' ) < time() - brai_abandon_mins() * 60; switch ( $r['status'] ) { case 'ordered': return array( __( 'Ordered', 'basketwise' ), '#067647', '#dcfae6' ); case 'checkout': return $idle ? array( __( 'Abandoned checkout', 'basketwise' ), '#b42318', '#fee4e2' ) : array( __( 'At checkout now', 'basketwise' ), '#b54708', '#fef0c7' ); case 'basket': return $idle ? array( __( 'Abandoned basket', 'basketwise' ), '#c4320a', '#ffead5' ) : array( __( 'Basket in progress', 'basketwise' ), '#b54708', '#fef0c7' ); } return array( __( 'Browsing', 'basketwise' ), '#344054', '#f2f4f7' ); } /** * Status badge HTML. * * @param array $r Visitor row. * @return string */ function brai_badge( $r ) { $l = brai_label( $r ); return '' . esc_html( $l[0] ) . ''; } /** * "5 minutes ago". * * @param string $gmt Date in UTC. * @return string */ function brai_when( $gmt ) { /* translators: %s: time, e.g. 5 minutes. */ return sprintf( __( '%s ago', 'basketwise' ), human_time_diff( strtotime( $gmt . ' UTC' ), time() ) ); } /** * Seconds as "4m 10s". * * @param int $s Seconds. * @return string */ function brai_dur( $s ) { $s = (int) $s; if ( $s < 60 ) { return $s . 's'; } if ( $s < 3600 ) { return floor( $s / 60 ) . 'm ' . ( $s % 60 ) . 's'; } return floor( $s / 3600 ) . 'h ' . floor( ( $s % 3600 ) / 60 ) . 'm'; } /** * Town, region and country. * * @param array $r Visitor row. * @return string */ function brai_place( $r ) { $c = trim( (string) ( $r['country'] ?? '' ) ); if ( '' === $c || '-' === $c ) { return ''; } $city = trim( (string) ( $r['city'] ?? '' ) ); $reg = trim( (string) ( $r['region'] ?? '' ) ); $base = WC()->countries ? html_entity_decode( (string) ( WC()->countries->get_countries()[ WC()->countries->get_base_country() ] ?? '' ) ) : ''; $base = trim( preg_replace( '/\s*\(.*\)$/', '', $base ) ); $parts = array_filter( array( $city, $reg !== $city ? $reg : '', $base === $c ? '' : $c ) ); return $parts ? implode( ', ', $parts ) : $c; } /** * Name, email or "Guest 1A2B3C". * * @param array $r Visitor row. * @return string */ function brai_name( $r ) { if ( $r['name'] ) { return $r['name']; } /* translators: %s: short visitor code. */ return $r['email'] ? $r['email'] : sprintf( __( 'Guest %s', 'basketwise' ), strtoupper( substr( $r['vid'], 1, 6 ) ) ); } /** * Coloured initials circle. * * @param array $r Visitor row. * @param int $size Pixels. * @return string */ function brai_avatar( $r, $size = 36 ) { $pal = array( '#2e90fa', '#7a5af8', '#ee46bc', '#f79009', '#12b76a', '#0ba5ec', '#f04438', '#6172f3' ); $col = $pal[ hexdec( substr( md5( (string) $r['vid'] ), 0, 2 ) ) % count( $pal ) ]; $ini = '?'; if ( $r['name'] || $r['email'] ) { $w = preg_split( '/[\s@._-]+/', (string) ( $r['name'] ? $r['name'] : $r['email'] ), -1, PREG_SPLIT_NO_EMPTY ); $ini = strtoupper( substr( $w[0], 0, 1 ) . ( $r['name'] && isset( $w[1] ) ? substr( $w[1], 0, 1 ) : '' ) ); } return '' . esc_html( $ini ) . ''; } /** * Device icon and name. * * @param array $r Visitor row. * @return string */ function brai_device_html( $r ) { if ( empty( $r['device'] ) ) { return ''; } $ic = array( 'Mobile' => 'smartphone', 'Tablet' => 'tablet', 'Desktop' => 'desktop', ); return '' . esc_html( $r['device'] . ( ! empty( $r['browser'] ) ? ', ' . $r['browser'] : '' ) ); } /** * Basket contents. * * @param array $r Visitor row. * @param int $max Lines to show. * @return string */ function brai_basket_html( $r, $max = 3 ) { $items = $r['cart'] ? json_decode( $r['cart'], true ) : array(); if ( ! $items ) { return '–'; } $out = array(); foreach ( array_slice( $items, 0, $max ) as $i ) { $out[] = (int) $i['qty'] . ' x ' . esc_html( $i['name'] ); } if ( count( $items ) > $max ) { $out[] = '+' . ( count( $items ) - $max ) . ' ' . esc_html__( 'more', 'basketwise' ) . ''; } return implode( '
', $out ) . '
' . wc_price( $r['cart_value'] ) . ' ' . esc_html__( 'inc VAT', 'basketwise' ) . '
'; } /** * The page a visitor first landed on, as a link. * * @param string $landing Landing URL. * @return string */ function brai_landing_html( $landing ) { if ( ! $landing ) { return '–'; } $path = (string) wp_parse_url( $landing, PHP_URL_PATH ); $pid = url_to_postid( home_url( $path ) ); $t = $pid ? get_the_title( $pid ) : ( '' === trim( $path, '/' ) ? __( 'Home page', 'basketwise' ) : $path ); return '' . esc_html( $t ) . ''; } /** * A small horizontal bar. * * @param int $n Value. * @param int $max Maximum. * @param string $color Colour. * @return string */ function brai_bar( $n, $max, $color = '#53b1fd' ) { $w = $max ? max( 2, round( 100 * $n / $max ) ) : 0; return ''; } /** * Grouped bar chart as inline SVG. * * @param array $buckets key => array( visitors, baskets, orders ). * @param array $labels key => label. * @return string */ function brai_chart( $buckets, $labels ) { $max = 1; foreach ( $buckets as $b ) { $max = max( $max, $b[0], $b[1], $b[2] ); } $n = max( 1, count( $buckets ) ); $w = 1000; $h = 190; $gw = $w / $n; $bw = max( 1, ( $gw - 3 ) / 3 ); $cols = array( '#b2ddff', '#f79009', '#12b76a' ); $nm = array( __( 'visitors', 'basketwise' ), __( 'baskets', 'basketwise' ), __( 'orders', 'basketwise' ) ); $svg = ''; foreach ( array( 0, 0.5, 1 ) as $f ) { $y = round( $h - $h * $f, 1 ); $svg .= '' . (int) round( $max * $f ) . ''; } $every = max( 1, (int) ceil( $n / 12 ) ); $i = 0; foreach ( $buckets as $k => $b ) { $x0 = $i * $gw + 1.5; for ( $s = 0; $s < 3; $s++ ) { $bh = $b[ $s ] ? max( 2, round( $h * $b[ $s ] / $max, 1 ) ) : 0; $svg .= '' . esc_html( $labels[ $k ] . ': ' . $b[ $s ] . ' ' . $nm[ $s ] ) . ''; } if ( 0 === $i % $every ) { $svg .= '' . esc_html( $labels[ $k ] ) . ''; } ++$i; } return $svg . ''; } /** Allowed HTML for the chart. */ function brai_kses_svg() { return array( 'svg' => array( 'viewbox' => true, 'class' => true, 'role' => true, 'aria-label' => true, ), 'line' => array( 'x1' => true, 'x2' => true, 'y1' => true, 'y2' => true, 'stroke' => true, ), 'text' => array( 'x' => true, 'y' => true, 'text-anchor' => true, 'font-size' => true, 'fill' => true, ), 'rect' => array( 'x' => true, 'y' => true, 'width' => true, 'height' => true, 'fill' => true, 'rx' => true, ), 'title' => array(), ); } /** Allowed HTML for the settings forms (post HTML plus form fields). */ function brai_form_kses() { static $allowed = null; if ( null === $allowed ) { $common = array( 'class' => true, 'id' => true, 'style' => true, 'title' => true, 'aria-label' => true, 'data-*' => true, ); $allowed = wp_kses_allowed_html( 'post' ); $allowed['input'] = $common + array( 'type' => true, 'name' => true, 'value' => true, 'min' => true, 'max' => true, 'step' => true, 'checked' => true, 'maxlength' => true, 'spellcheck' => true, 'placeholder' => true, ); $allowed['select'] = $common + array( 'name' => true ); $allowed['option'] = array( 'value' => true, 'selected' => true, 'style' => true, ); $allowed['textarea'] = $common + array( 'name' => true, 'rows' => true, ); $allowed['button'] = $common + array( 'type' => true, 'name' => true, 'value' => true, ); $allowed['label'] = $common + array( 'for' => true ); $allowed['span'] = $common; $allowed['div'] = $common; } return $allowed; } /** * Echoes a breakdown box: name, visitors (with bar), baskets, orders. * * @param string $title Box title. * @param array $data key => array( v, b, o ). * @param string $head First column heading. * @param string $note Small print. */ function brai_seg_box( $title, $data, $head, $note = '' ) { $max = 0; foreach ( $data as $d ) { $max = max( $max, $d['v'] ); } echo '

' . esc_html( $title ) . '

'; if ( ! $data ) { echo '

' . esc_html__( 'No data yet for this period.', 'basketwise' ) . '

'; } else { echo ''; foreach ( array_slice( $data, 0, 8, true ) as $k => $d ) { echo ''; } echo '
' . esc_html( $head ) . '' . esc_html__( 'Visitors', 'basketwise' ) . '' . esc_html__( 'Baskets', 'basketwise' ) . '' . esc_html__( 'Orders', 'basketwise' ) . '
' . esc_html( (string) $k ) . wp_kses_post( brai_bar( $d['v'], $max ) ) . '' . (int) $d['v'] . '' . (int) $d['b'] . '' . ( $d['o'] ? '' . (int) $d['o'] . '' : '0' ) . '
'; } if ( $note ) { echo '

' . esc_html( $note ) . '

'; } echo '
'; } /** * Icon, colour and text for one timeline event. * * @param object $e Event row. * @return array */ function brai_event_html( $e ) { $obj = (int) $e->object_id; $plink = $obj ? '' . esc_html( get_the_title( $obj ) ) . '' : ''; switch ( $e->type ) { case 'visit': return array( 'admin-home', '#475467', esc_html__( 'Started a visit', 'basketwise' ) . ( $e->url ? ' ' . esc_html__( 'landing on', 'basketwise' ) . ' ' . esc_html( strtok( (string) $e->url, '?' ) ) . '' : '' ) ); case 'view': return array( 'visibility', '#2e90fa', esc_html__( 'Viewed', 'basketwise' ) . ' ' . $plink ); case 'cat': $t = get_term( $obj, 'product_cat' ); return array( 'category', '#6172f3', esc_html__( 'Viewed category', 'basketwise' ) . ' ' . ( $t && ! is_wp_error( $t ) ? '' . esc_html( $t->name ) . '' : '#' . $obj ) ); case 'search': return array( 'search', '#7a5af8', esc_html__( 'Searched for', 'basketwise' ) . ' “' . esc_html( $e->url ) . '” (' . ( (int) $e->qty ? (int) $e->qty . ' ' . esc_html__( 'products found', 'basketwise' ) : esc_html__( 'nothing found', 'basketwise' ) ) . ')' ); case 'add': return array( 'cart', '#f79009', '' . esc_html__( 'Added', 'basketwise' ) . ' ' . (int) $e->qty . ' x ' . $plink . ( (float) $e->value ? ' (' . wc_price( $e->value ) . ')' : '' ) ); case 'remove': return array( 'minus', '#98a2b3', esc_html__( 'Removed', 'basketwise' ) . ' ' . $plink ); case 'qty': /* translators: %d: quantity. */ return array( 'update', '#98a2b3', sprintf( esc_html__( 'Changed quantity to %d for', 'basketwise' ), (int) $e->qty ) . ' ' . $plink ); case 'checkout': return array( 'money-alt', '#dc6803', '' . esc_html__( 'Went to checkout', 'basketwise' ) . ' (' . (int) $e->qty . ' ' . esc_html__( 'items', 'basketwise' ) . ', ' . wc_price( $e->value ) . ')' ); case 'email': return array( 'id', '#0ba5ec', esc_html__( 'Entered their contact details at checkout', 'basketwise' ) ); case 'reminder': /* translators: %d: reminder number. */ return array( 'email-alt', '#ee46bc', '' . sprintf( esc_html__( 'Reminder email %d sent', 'basketwise' ), (int) $e->qty ) . '' ); case 'message': return array( 'email', '#2271b1', '' . esc_html__( 'Email sent', 'basketwise' ) . '' . ( '' !== (string) $e->url ? ': ' . esc_html( (string) $e->url ) : '' ) ); case 'restore': return array( 'undo', '#ee46bc', '' . esc_html__( 'Clicked "Return to your basket" in a reminder email', 'basketwise' ) . '' ); case 'unsub': return array( 'dismiss', '#f04438', esc_html__( 'Unsubscribed from basket reminders', 'basketwise' ) ); case 'coupon': return array( 'tickets-alt', '#7a5af8', esc_html__( 'Sent discount code', 'basketwise' ) . ' ' . esc_html( strtoupper( (string) $e->url ) ) . '' ); case 'order': $o = wc_get_order( $obj ); return array( 'yes-alt', '#12b76a', '' . esc_html__( 'Placed order', 'basketwise' ) . ' ' . ( $o ? '#' . esc_html( $o->get_order_number() ) . '' : '#' . $obj ) . ' (' . wc_price( $e->value ) . ')' ); } return array( 'marker', '#98a2b3', esc_html( $e->type ) ); }