$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 ) . '
' . esc_html__( 'No data yet for this period.', 'basketwise' ) . '
'; } else { 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' ) . ' |
' . esc_html( $note ) . '
'; } echo '