Basketwise (Overview, Baskets, Insights) and each visitor's page. * * @package Basketwise */ defined( 'ABSPATH' ) || exit; // phpcs:disable WordPress.DB.DirectDatabaseQuery -- the plugin's own tables; every query goes through $wpdb->prepare(). add_action( 'admin_menu', function () { add_submenu_page( 'woocommerce', __( 'Basketwise', 'basketwise' ), __( 'Basketwise', 'basketwise' ), 'manage_woocommerce', 'basket-recovery', 'brai_admin_page' ); }, 60 ); add_action( 'admin_enqueue_scripts', function ( $hook ) { if ( 'woocommerce_page_basket-recovery' !== $hook ) { return; } wp_enqueue_style( 'brai-admin', BRAI_URL . 'assets/admin.css', array(), BRAI_VERSION ); wp_enqueue_script( 'brai-admin', BRAI_URL . 'assets/admin.js', array(), BRAI_VERSION, true ); if ( 'emails' === brai_get( 'tab' ) ) { wp_enqueue_media(); wp_enqueue_script( 'brai-emails', BRAI_URL . 'assets/admin-emails.js', array(), BRAI_VERSION, true ); wp_localize_script( 'brai-emails', 'braiMail', array( /* translators: %s: number of hours. */ 'hour' => __( '%s hour', 'basketwise' ), /* translators: %s: number of hours. */ 'hours' => __( '%s hours', 'basketwise' ), /* translators: %s: number of days. */ 'day' => __( '%s day', 'basketwise' ), /* translators: %s: number of days. */ 'days' => __( '%s days', 'basketwise' ), /* translators: %s: a length of time, such as "1 hour". */ 'after' => __( 'after %s', 'basketwise' ), /* translators: %s: a length of time, such as "1 hour". */ 'afterEnd' => __( '%s after', 'basketwise' ), /* translators: %s: a number, such as 10. */ 'pctOff' => __( '%s% off', 'basketwise' ), /* translators: %s: an amount, such as 5. */ 'amtOff' => __( '%s off', 'basketwise' ), 'notSent' => __( 'Not sent', 'basketwise' ), 'pickBox' => __( 'Click one of the colour boxes above first, then a website colour.', 'basketwise' ), 'matched' => __( 'Matched to the website colours. Save to keep them.', 'basketwise' ), 'noLogo' => __( 'No logo', 'basketwise' ), 'pickLogo' => __( 'Choose a logo', 'basketwise' ), 'useLogo' => __( 'Use this logo', 'basketwise' ), 'sending' => __( 'Sending…', 'basketwise' ), 'wentWrong' => __( 'Something went wrong.', 'basketwise' ), ) ); } } ); /** * Header with tabs, shared by every Basketwise screen. * * @param string $cur Current tab. */ function brai_nav( $cur ) { $base = brai_base_url(); $d = (int) brai_get( 'days' ); $keep = $d ? array( 'days' => $d ) : array(); $tabs = array( 'activity' => array( __( 'Overview', 'basketwise' ), add_query_arg( $keep, $base ), 'dashboard' ), 'baskets' => array( __( 'Baskets', 'basketwise' ), add_query_arg( array_merge( $keep, array( 'tab' => 'baskets' ) ), $base ), 'cart' ), 'insights' => array( __( 'Insights', 'basketwise' ), add_query_arg( array_merge( $keep, array( 'tab' => 'insights' ) ), $base ), 'chart-bar' ), 'sent' => array( __( 'Sent emails', 'basketwise' ), add_query_arg( 'tab', 'sent', $base ), 'email' ), 'emails' => array( __( 'Email settings', 'basketwise' ), add_query_arg( 'tab', 'emails', $base ), 'email-alt' ), 'settings' => array( __( 'Settings', 'basketwise' ), add_query_arg( 'tab', 'settings', $base ), 'admin-generic' ), ); echo '

' . esc_html__( 'Basketwise', 'basketwise' ) . '' . esc_html( get_bloginfo( 'name' ) ) . '

'; } /** Chooses which screen to show. */ function brai_admin_page() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $tab = sanitize_key( brai_get( 'tab' ) ); $tab = '' !== $tab ? $tab : 'activity'; if ( 'emails' === $tab ) { brai_mail_admin(); return; } if ( 'settings' === $tab ) { brai_settings_admin(); return; } if ( 'sent' === $tab ) { brai_mail_log_page(); return; } $vid = brai_requested_vid(); if ( '' !== $vid ) { brai_admin_detail( $vid ); return; } if ( 'weekly' === brai_get( 'preview' ) ) { brai_weekly_preview(); return; } if ( '' !== brai_get( 'preview' ) ) { brai_digest_preview(); return; } brai_activity_page( in_array( $tab, array( 'baskets', 'insights' ), true ) ? $tab : 'activity' ); } /** Morning summary preview. */ function brai_digest_preview() { list( $from, $to ) = brai_digest_window(); $rows = brai_digest_rows( $from, $to ); $s = brai_mail_settings(); echo '
'; brai_nav( 'emails' ); echo '

' . esc_html__( 'Morning summary preview', 'basketwise' ) . '

'; /* translators: 1: recipients, 2: next send time. */ echo esc_html( sprintf( __( 'Sent to %1$s. Next one: %2$s.', 'basketwise' ), $s['digest_to'] ? $s['digest_to'] : __( 'nobody (switched off)', 'basketwise' ), wp_date( 'D j M, H:i', (int) wp_next_scheduled( 'brai_digest' ) ) ) ) . ' '; echo $rows ? esc_html__( 'If it went now, this is what it would contain:', 'basketwise' ) : esc_html__( 'Nothing to send so far (no abandoned baskets since the last summary), so no email would go out.', 'basketwise' ); echo '

'; if ( $rows ) { echo '
' . wp_kses_post( brai_digest_html( $rows, $from, $to ) ) . '
'; } echo '
'; } /** * How many visitors are in one of the Baskets lists, and what their baskets add up to. * * The SQL is fixed. The list and the search are chosen by the placeholders: * abandoned - a basket or checkout with something in it, quiet for longer than the "abandoned after" setting * live - a basket or checkout still active * ordered - placed an order * browsing - looked at products, nothing in the basket * all - everyone * * @param string $since Only visitors seen since (Y-m-d H:i:s, UTC). * @param string $cut When a quiet basket counts as abandoned (Y-m-d H:i:s, UTC). * @param string $view abandoned, live, ordered, browsing or all. * @param string $like Search as a LIKE pattern, or '' for no search. * @return object n (visitors), v (basket value). */ function brai_visitor_totals( $since, $cut, $view, $like = '' ) { global $wpdb; return $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) n, COALESCE(SUM(cart_value),0) v FROM %i WHERE last_seen >= %s AND ( ( %s = 'abandoned' AND status IN ('basket','checkout') AND cart_value > 0 AND last_seen < %s ) OR ( %s = 'live' AND status IN ('basket','checkout') AND last_seen >= %s ) OR ( %s = 'ordered' AND status = 'ordered' ) OR ( %s = 'browsing' AND status = 'browsing' AND product_views > 0 ) OR %s = 'all' ) AND ( %s = '' OR name LIKE %s OR email LIKE %s OR phone LIKE %s OR cart LIKE %s OR city LIKE %s )", brai_table( 'visitors' ), $since, $view, $cut, $view, $cut, $view, $view, $view, $like, $like, $like, $like, $like, $like ) ); } /** * The visitors in one of the Baskets lists. See brai_visitor_totals() for the lists. * * @param string $since Only visitors seen since. * @param string $cut When a quiet basket counts as abandoned. * @param string $view abandoned, live, ordered, browsing or all. * @param string $like Search as a LIKE pattern, or ''. * @param bool $contact_first Put visitors who left an email or phone number first. * @param int $limit Rows. * @param int $offset Offset. * @return array */ function brai_visitor_rows( $since, $cut, $view, $like, $contact_first, $limit, $offset ) { global $wpdb; return (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE last_seen >= %s AND ( ( %s = 'abandoned' AND status IN ('basket','checkout') AND cart_value > 0 AND last_seen < %s ) OR ( %s = 'live' AND status IN ('basket','checkout') AND last_seen >= %s ) OR ( %s = 'ordered' AND status = 'ordered' ) OR ( %s = 'browsing' AND status = 'browsing' AND product_views > 0 ) OR %s = 'all' ) AND ( %s = '' OR name LIKE %s OR email LIKE %s OR phone LIKE %s OR cart LIKE %s OR city LIKE %s ) ORDER BY ( %d = 1 AND ( email <> '' OR phone <> '' ) ) DESC, last_seen DESC LIMIT %d OFFSET %d", brai_table( 'visitors' ), $since, $view, $cut, $view, $cut, $view, $view, $view, $like, $like, $like, $like, $like, $like, $contact_first ? 1 : 0, $limit, $offset ), ARRAY_A ); } /** * Overview, Baskets and Insights. * * @param string $tab activity, baskets or insights. */ function brai_activity_page( $tab ) { global $wpdb; $v = brai_table( 'visitors' ); $e = brai_table( 'events' ); $days = (int) brai_get( 'days' ); $days = in_array( $days, array( 1, 7, 30, 90 ), true ) ? $days : 7; $since = gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ); $cut = gmdate( 'Y-m-d H:i:s', time() - brai_abandon_mins() * 60 ); /* The lists on the Baskets tab. What each one contains is set out in brai_visitor_totals(). */ $views = array( 'abandoned' => __( 'Abandoned', 'basketwise' ), 'live' => __( 'Baskets now', 'basketwise' ), 'ordered' => __( 'Ordered', 'basketwise' ), 'browsing' => __( 'Browsing products', 'basketwise' ), 'all' => __( 'Everyone', 'basketwise' ), ); $view = sanitize_key( brai_get( 'view' ) ); $view = isset( $views[ $view ] ) ? $view : 'abandoned'; $paged = max( 1, (int) brai_get( 'paged' ) ); $per = 30; $base = brai_base_url(); $q = trim( brai_get( 'q' ) ); $like = '' !== $q ? '%' . $wpdb->esc_like( $q ) . '%' : ''; $link = function ( $args ) use ( $base, $days, $view, $q, $tab ) { return add_query_arg( array_merge( array( 'tab' => 'activity' !== $tab ? $tab : false, 'days' => $days, 'view' => 'baskets' === $tab ? $view : false, 'q' => ( 'baskets' === $tab && '' !== $q ) ? $q : false, ), $args ), $base ); }; echo '
'; brai_nav( $tab ); ?>
__( '24 hours', 'basketwise' ), 7 => __( '7 days', 'basketwise' ), 30 => __( '30 days', 'basketwise' ), 90 => __( '90 days', 'basketwise' ), ) as $d => $l ) { printf( '%s', $d === $days ? 'on' : '', esc_url( $link( array( 'days' => $d, 'paged' => false, ) ) ), esc_html( $l ) ); } ?>

' . esc_html__( 'Visitor data deleted.', 'basketwise' ) . '

'; } if ( 'activity' === $tab ) { $visitors = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE last_seen >= %s AND views > 0', $v, $since ) ); $visits = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE type = 'visit' AND ts >= %s", $e, $since ) ); $stage = $wpdb->get_results( $wpdb->prepare( "SELECT type, COUNT(DISTINCT vid) n FROM %i WHERE ts >= %s AND type IN ('view','add','checkout','order') GROUP BY type", $e, $since ), OBJECT_K ); $st = function ( $k ) use ( $stage ) { return isset( $stage[ $k ] ) ? (int) $stage[ $k ]->n : 0; }; $rev = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) n, COALESCE(SUM(value),0) v FROM %i WHERE type = 'order' AND ts >= %s", $e, $since ) ); $aband = brai_visitor_totals( $since, $cut, 'abandoned' ); $rem_sent = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE type = 'reminder' AND ts >= %s", $e, $since ) ); $recov = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) n, COALESCE(SUM(o.value),0) v FROM %i o WHERE o.type = 'order' AND o.ts >= %s AND EXISTS ( SELECT 1 FROM %i r WHERE r.vid = o.vid AND r.type = 'reminder' AND r.ts < o.ts )", $e, $since, $e ) ); $base_n = max( $visitors, $st( 'add' ) ); $hourly = 1 === $days; $buckets = array(); $labels = array(); for ( $i = $hourly ? 23 : $days - 1; $i >= 0; $i-- ) { $t = time() - $i * ( $hourly ? HOUR_IN_SECONDS : DAY_IN_SECONDS ); $k = gmdate( $hourly ? 'Y-m-d H' : 'Y-m-d', $t ); $buckets[ $k ] = array( 0, 0, 0 ); $labels[ $k ] = wp_date( $hourly ? 'H:00' : 'j M', $t ); } $fmt = $hourly ? '%Y-%m-%d %H' : '%Y-%m-%d'; foreach ( $wpdb->get_results( $wpdb->prepare( "SELECT DATE_FORMAT(ts, %s) b, COUNT(DISTINCT vid) n FROM %i WHERE ts >= %s AND type IN ('visit','view','cat') GROUP BY b", $fmt, $e, $since ) ) as $x ) { if ( isset( $buckets[ $x->b ] ) ) { $buckets[ $x->b ][0] = (int) $x->n; } } foreach ( $wpdb->get_results( $wpdb->prepare( "SELECT DATE_FORMAT(ts, %s) b, type, COUNT(DISTINCT vid) n FROM %i WHERE ts >= %s AND type IN ('add','order') GROUP BY b, type", $fmt, $e, $since ) ) as $x ) { if ( isset( $buckets[ $x->b ] ) ) { $buckets[ $x->b ][ 'add' === $x->type ? 1 : 2 ] = (int) $x->n; } } /* Each figure beside the same length of time before it, when the records go back far enough. */ $cmp = null; if ( 2 * $days <= brai_keep_days() ) { $cmp = array( brai_period_stats( time() - $days * DAY_IN_SECONDS, time() ), brai_period_stats( time() - 2 * $days * DAY_IN_SECONDS, time() - $days * DAY_IN_SECONDS ), ); } $delta = function ( $key, $up_good = true ) use ( $cmp, $days ) { if ( ! $cmp ) { return ''; } /* translators: %d: number of days. */ $title = 1 === $days ? __( 'Compared with the 24 hours before', 'basketwise' ) : sprintf( __( 'Compared with the %d days before', 'basketwise' ), $days ); return brai_delta_html( $cmp[0][ $key ], $cmp[1][ $key ], $up_good, $title ); }; $kpi = function ( $label, $value, $sub, $style = '', $change = '' ) { echo '
' . esc_html( $label ) . '
' . esc_html( $value ) . wp_kses_post( $change ) . '
' . esc_html( $sub ) . '
'; }; echo '
'; /* translators: %s: number of visits. */ $kpi( __( 'Visitors', 'basketwise' ), number_format_i18n( $visitors ), $visits ? sprintf( _n( '%s visit', '%s visits', $visits, 'basketwise' ), number_format_i18n( $visits ) ) : __( 'who accepted analytics cookies', 'basketwise' ), '', $delta( 'visitors' ) ); /* translators: %s: percentage. */ $kpi( __( 'Added to basket', 'basketwise' ), number_format_i18n( $st( 'add' ) ), $visitors ? sprintf( __( '%s%% of visitors', 'basketwise' ), round( 100 * $st( 'add' ) / $visitors, 1 ) ) : '', '', $delta( 'baskets' ) ); /* translators: %s: percentage. */ $kpi( __( 'Reached checkout', 'basketwise' ), number_format_i18n( $st( 'checkout' ) ), $st( 'add' ) ? sprintf( __( '%s%% of baskets', 'basketwise' ), round( 100 * $st( 'checkout' ) / $st( 'add' ) ) ) : '', '', $delta( 'checkouts' ) ); /* translators: %s: percentage. */ $kpi( __( 'Orders', 'basketwise' ), number_format_i18n( (int) $rev->n ), $base_n ? sprintf( __( '%s%% conversion', 'basketwise' ), round( 100 * $st( 'order' ) / $base_n, 2 ) ) : '', 'color:#067647', $delta( 'orders' ) ); /* translators: %s: amount. */ $kpi( __( 'Order value', 'basketwise' ), brai_money( $rev->v ), $rev->n ? sprintf( __( 'Avg %s per order', 'basketwise' ), brai_money( $rev->v / $rev->n ) ) : '', '', $delta( 'revenue' ) ); /* translators: %s: amount. */ $kpi( __( 'Abandoned', 'basketwise' ), number_format_i18n( (int) $aband->n ), sprintf( __( '%s left in baskets', 'basketwise' ), brai_money( $aband->v ) ), 'color:#b42318', $delta( 'abandoned', false ) ); /* translators: 1: amount, 2: number of emails. */ $kpi( __( 'Recovered by reminders', 'basketwise' ), number_format_i18n( (int) $recov->n ), sprintf( __( '%1$s from %2$d emails', 'basketwise' ), brai_money( $recov->v ), $rem_sent ), '', $delta( 'recovered' ) ); echo '
'; ?>

$visitors, __( 'Viewed a product', 'basketwise' ) => $st( 'view' ), __( 'Added to basket', 'basketwise' ) => $st( 'add' ), __( 'Reached checkout', 'basketwise' ) => $st( 'checkout' ), __( 'Ordered', 'basketwise' ) => $st( 'order' ), ); $cols = array( '#b2ddff', '#53b1fd', '#f79009', '#dc6803', '#12b76a' ); $prev = null; $i = 0; foreach ( $steps as $l => $n ) { $drop = ( null !== $prev && $prev > 0 && $n < $prev ) ? round( 100 * ( $prev - $n ) / $prev ) : null; echo '
' . esc_html( $l ) . ( null !== $drop ? ' −' . (int) $drop . '%' : '' ) . '' . esc_html( number_format_i18n( $n ) ) . '
' . wp_kses_post( brai_bar( $n, $base_n, $cols[ $i ] ) ) . '
'; $prev = $n; ++$i; } ?>

→
'; } foreach ( $recent as $r ) { echo ''; echo ''; echo ''; echo ''; echo ''; } ?>
' . esc_html__( 'No abandoned baskets waiting for a follow-up in this period.', 'basketwise' ) . '
' . wp_kses_post( brai_avatar( $r ) ) . '
' . esc_html( brai_name( $r ) ) . '' . ( ( $r['name'] && $r['email'] ) ? '
' . esc_html( $r['email'] ) . '
' : '' ) . ( $r['phone'] ? '
' . esc_html( $r['phone'] ) . '
' : '' ) . '
' . wp_kses_post( brai_basket_html( $r, 2 ) ) . '' . esc_html( brai_source( $r['landing'], $r['referrer'], $r['src'] ) ) . '' . esc_html( brai_when( $r['last_seen'] ) ) . '
n; $pages = max( 1, (int) ceil( $total / $per ) ); $paged = min( $paged, $pages ); $rows = brai_visitor_rows( $since, $cut, $view, $like, false, $per, ( $paged - 1 ) * $per ); $seen = array(); if ( $rows ) { $vids = wp_list_pluck( $rows, 'vid' ); $in = implode( ',', array_fill( 0, count( $vids ), '%s' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $in is only a list of %s placeholders, one for each visitor. foreach ( $wpdb->get_results( $wpdb->prepare( "SELECT vid, object_id FROM %i WHERE type = 'view' AND vid IN ($in) ORDER BY id DESC LIMIT 3000", array_merge( array( $e ), $vids ) ) ) as $ev ) { $seen[ $ev->vid ] = $seen[ $ev->vid ] ?? array(); if ( count( $seen[ $ev->vid ] ) < 2 && ! in_array( (int) $ev->object_id, $seen[ $ev->vid ], true ) ) { $seen[ $ev->vid ][] = (int) $ev->object_id; } } } ?>
$vw ) { $n = (int) brai_visitor_totals( $since, $cut, $k, $like )->n; printf( '%s%s', $k === $view ? 'on' : '', esc_url( $link( array( 'view' => $k, 'paged' => false, ) ) ), esc_html( $vw ), esc_html( number_format_i18n( $n ) ) ); } ?>
'; } foreach ( $rows as $r ) { $camp = brai_campaign( $r['landing'], $r['src'] ); $place = brai_place( $r ); $dev = brai_device_html( $r ); $bits = array(); if ( (int) $r['visits'] > 1 ) { /* translators: %d: number of visits. */ $bits[] = sprintf( __( '%d visits', 'basketwise' ), (int) $r['visits'] ); } /* translators: %d: number of pages. */ $bits[] = sprintf( _n( '%d page', '%d pages', (int) $r['views'], 'basketwise' ), (int) $r['views'] ); if ( (int) $r['secs'] ) { /* translators: %s: time. */ $bits[] = sprintf( __( '%s on site', 'basketwise' ), brai_dur( $r['secs'] ) ); } echo ''; echo ''; echo ''; echo ''; } ?>
' . esc_html__( 'Nothing here for this period.', 'basketwise' ) . '
' . wp_kses_post( brai_avatar( $r ) ) . '
' . esc_html( brai_name( $r ) ) . ''; if ( $r['user_id'] ) { echo ' '; } if ( $r['name'] && $r['email'] ) { echo ''; } if ( $r['phone'] ) { echo ''; } if ( $dev || $place ) { echo '
' . wp_kses_post( $dev ) . ( $dev && $place ? ' · ' : '' ) . ( $place ? '' . esc_html( $place ) : '' ) . '
'; } echo '
' . wp_kses_post( brai_badge( $r ) ) . wp_kses_post( brai_contacted_html( $r ) ); if ( 'ordered' === $r['status'] && $r['order_id'] ) { $o = wc_get_order( $r['order_id'] ); if ( $o ) { echo '
#' . esc_html( $o->get_order_number() ) . ' ' . esc_html( wc_get_order_status_name( $o->get_status() ) ) . '
'; } } if ( (int) $r['reminders'] ) { /* translators: %d: number of reminders. */ echo '
' . esc_html( sprintf( _n( '%d reminder sent', '%d reminders sent', (int) $r['reminders'], 'basketwise' ), (int) $r['reminders'] ) ) . '
'; } echo '
' . wp_kses_post( brai_basket_html( $r ) ) . '' . esc_html( brai_source( $r['landing'], $r['referrer'], $r['src'] ) ) . ( $camp ? '
' . esc_html( $camp ) . '
' : '' ) . '
' . esc_html( implode( ' · ', $bits ) ); foreach ( $seen[ $r['vid'] ] ?? array() as $pid ) { echo ''; } /* translators: %s: date. */ echo '' . esc_html( brai_when( $r['last_seen'] ) ) . '
' . esc_html( sprintf( __( 'First seen %s', 'basketwise' ), get_date_from_gmt( $r['first_seen'], 'j M H:i' ) ) ) . '
1 ) : ?>
1 ) : ?> ← →
array(), 'camp' => array(), 'dev' => array(), 'loc' => array(), ); $put = function ( &$bucket, $key, $r ) { if ( ! isset( $bucket[ $key ] ) ) { $bucket[ $key ] = array( 'v' => 0, 'b' => 0, 'o' => 0, ); } ++$bucket[ $key ]['v']; if ( in_array( $r['status'], array( 'basket', 'checkout', 'ordered' ), true ) ) { ++$bucket[ $key ]['b']; } if ( 'ordered' === $r['status'] ) { ++$bucket[ $key ]['o']; } }; foreach ( $wpdb->get_results( $wpdb->prepare( "SELECT landing, referrer, src, device, city, region, country, status FROM %i WHERE last_seen >= %s AND ( views > 0 OR status <> 'browsing' ) ORDER BY last_seen DESC LIMIT 10000", $v, $since ), ARRAY_A ) as $r ) { $put( $agg['src'], brai_source( $r['landing'], $r['referrer'], $r['src'] ), $r ); $c = brai_campaign( $r['landing'], $r['src'] ); if ( $c ) { $put( $agg['camp'], $c, $r ); } if ( $r['device'] ) { $put( $agg['dev'], $r['device'], $r ); } $p = brai_place( $r ); if ( $p ) { $put( $agg['loc'], $p, $r ); } } foreach ( $agg as &$a ) { uasort( $a, function ( $x, $y ) { return $y['v'] <=> $x['v']; } ); } unset( $a ); $searches = $wpdb->get_results( $wpdb->prepare( "SELECT url term, COUNT(*) n, MIN(qty) res FROM %i WHERE type = 'search' AND ts >= %s GROUP BY url ORDER BY n DESC LIMIT 10", $e, $since ) ); $ab_items = array(); foreach ( wp_list_pluck( brai_visitor_rows( $since, $cut, 'abandoned', '', false, 3000, 0 ), 'cart' ) as $cj ) { foreach ( (array) json_decode( (string) $cj, true ) as $it ) { $id = (int) ( $it['id'] ?? 0 ); if ( ! $id ) { continue; } if ( ! isset( $ab_items[ $id ] ) ) { $ab_items[ $id ] = array( 'name' => (string) $it['name'], 'n' => 0, 'v' => 0, ); } ++$ab_items[ $id ]['n']; $ab_items[ $id ]['v'] += (float) ( $it['total'] ?? 0 ); } } uasort( $ab_items, function ( $x, $y ) { return ( $y['n'] <=> $x['n'] ) ? ( $y['n'] <=> $x['n'] ) : ( $y['v'] <=> $x['v'] ); } ); $top_view = $wpdb->get_results( $wpdb->prepare( "SELECT object_id, COUNT(*) n FROM %i WHERE type = 'view' AND ts >= %s GROUP BY object_id ORDER BY n DESC LIMIT 8", $e, $since ) ); $top_add = $wpdb->get_results( $wpdb->prepare( "SELECT object_id, COUNT(DISTINCT vid) n FROM %i WHERE type = 'add' AND ts >= %s GROUP BY object_id ORDER BY n DESC LIMIT 8", $e, $since ) ); echo '
'; brai_seg_box( __( 'Where visitors came from', 'basketwise' ), $agg['src'], __( 'Source', 'basketwise' ) ); brai_seg_box( __( 'Ad campaigns', 'basketwise' ), $agg['camp'], __( 'Campaign', 'basketwise' ), __( 'Google Ads campaigns and tagged (UTM) links. Name your Google Ads campaigns in Settings.', 'basketwise' ) ); brai_seg_box( __( 'Devices', 'basketwise' ), $agg['dev'], __( 'Device', 'basketwise' ) ); brai_seg_box( __( 'Locations', 'basketwise' ), $agg['loc'], __( 'Town', 'basketwise' ), brai_setting( 'geo' ) ? __( 'Approximate, from the visitor\'s internet connection.', 'basketwise' ) : __( 'Location is switched off in Settings.', 'basketwise' ) ); echo '

' . esc_html__( 'Site searches', 'basketwise' ) . '

'; if ( ! $searches ) { echo '

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

'; } else { echo ''; foreach ( $searches as $s ) { echo ''; } echo '
' . esc_html__( 'Searched for', 'basketwise' ) . '' . esc_html__( 'Times', 'basketwise' ) . '' . esc_html__( 'Products found', 'basketwise' ) . '
' . esc_html( $s->term ) . '' . (int) $s->n . '' . ( (int) $s->res ? (int) $s->res : '' . esc_html__( 'None', 'basketwise' ) . '' ) . '
'; } echo '

' . esc_html__( 'Searches that found nothing show what people want but can\'t find on the site.', 'basketwise' ) . '

'; echo '

' . esc_html__( 'Most abandoned products', 'basketwise' ) . '

'; if ( ! $ab_items ) { echo '

' . esc_html__( 'No abandoned baskets for this period.', 'basketwise' ) . '

'; } else { echo ''; foreach ( array_slice( $ab_items, 0, 8, true ) as $id => $it ) { echo ''; } echo '
' . esc_html__( 'Product', 'basketwise' ) . '' . esc_html__( 'Baskets', 'basketwise' ) . '' . esc_html__( 'Value', 'basketwise' ) . '
' . esc_html( wp_trim_words( $it['name'], 9 ) ) . '' . (int) $it['n'] . '' . esc_html( brai_money( $it['v'] ) ) . '
'; } echo '
'; foreach ( array( array( __( 'Most viewed products', 'basketwise' ), $top_view, __( 'Views', 'basketwise' ) ), array( __( 'Most added to basket', 'basketwise' ), $top_add, __( 'Visitors', 'basketwise' ) ), ) as $set ) { echo '

' . esc_html( $set[0] ) . '

'; if ( ! $set[1] ) { echo '

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

'; } else { $mx = (int) $set[1][0]->n; echo ''; foreach ( $set[1] as $t ) { echo ''; } echo '
' . esc_html__( 'Product', 'basketwise' ) . '' . esc_html( $set[2] ) . '
' . esc_html( wp_trim_words( get_the_title( $t->object_id ), 9 ) ) . '' . wp_kses_post( brai_bar( (int) $t->n, $mx, '#f79009' ) ) . '' . (int) $t->n . '
'; } echo '
'; } echo '
'; } echo ''; } /** * One visitor's page. * * @param string $vid Visitor. */ function brai_admin_detail( $vid ) { global $wpdb; $base = brai_base_url(); $r = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE vid = %s', brai_table( 'visitors' ), $vid ), ARRAY_A ); echo '

← ' . esc_html__( 'Baskets', 'basketwise' ) . '

'; if ( ! $r ) { /* translators: %d: days. */ echo '

' . esc_html( sprintf( __( 'Visitor not found. Their data may have been deleted or passed the %d-day limit.', 'basketwise' ), brai_keep_days() ) ) . '

'; return; } $ev = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE vid = %s ORDER BY id DESC LIMIT 500', brai_table( 'events' ), $vid ) ); $items = $r['cart'] ? (array) json_decode( $r['cart'], true ) : array(); $orders = $r['email'] ? wc_get_orders( array( 'billing_email' => $r['email'], 'limit' => 10, 'orderby' => 'date', 'order' => 'DESC', ) ) : array(); $camp = brai_campaign( $r['landing'], $r['src'] ); $params = brai_params( $r['landing'], $r['src'] ); $order = ( 'ordered' === $r['status'] && $r['order_id'] ) ? wc_get_order( $r['order_id'] ) : null; $unsub = $r['email'] && brai_unsubscribed( $r['email'] ); $counts = array(); foreach ( $ev as $x ) { $counts[ $x->type ] = ( $counts[ $x->type ] ?? 0 ) + 1; } $tel = preg_replace( '/[^0-9+]/', '', $r['phone'] ); $meta = array(); if ( $r['name'] && $r['email'] ) { $meta[] = '' . esc_html( $r['email'] ) . ''; } if ( $r['phone'] ) { $meta[] = '' . esc_html( $r['phone'] ) . ''; } if ( $r['user_id'] ) { $meta[] = '' . esc_html__( 'Customer account', 'basketwise' ) . ''; } if ( ! $meta ) { $meta[] = esc_html__( 'No contact details yet (they appear once someone types them at checkout or logs in)', 'basketwise' ); } $kpi = function ( $label, $value, $sub = '', $small = false ) { echo '
' . esc_html( $label ) . '
' . esc_html( $value ) . '
' . ( $sub ? '
' . esc_html( $sub ) . '
' : '' ) . '
'; }; brai_send_now_notice(); ?>

get_order_number() ) ); ?>

get_image_id(), 'thumbnail' ) : ''; echo ''; echo ''; echo ''; } ?>
' . ( $img ? '' : '' ) . '' . esc_html( $it['name'] ) . '' . ( $p && $p->get_sku() ? '
SKU ' . esc_html( $p->get_sku() ) . '
' : '' ) . '
x ' . (int) $it['qty'] . '' . wp_kses_post( wc_price( $it['total'] ) ) . '

' . esc_html__( 'No activity recorded.', 'basketwise' ) . '

'; } $day = ''; foreach ( $ev as $x ) { $d = get_date_from_gmt( $x->ts, 'Y-m-d' ); if ( $d !== $day ) { echo '
' . esc_html( get_date_from_gmt( $x->ts, 'l j F' ) ) . '
'; $day = $d; } $h = brai_event_html( $x ); printf( '
%s%s
', esc_html( get_date_from_gmt( $x->ts, 'H:i' ) ), esc_attr( $h[1] ), esc_attr( $h[0] ), wp_kses_post( $h[2] ) ); } ?>

$r['device'] ) ) ); ?>
()

' . esc_html__( 'with this email', 'basketwise' ) . '' : ''; ?>

'; } ?>
#' . esc_html( $o->get_order_number() ) . '
' . esc_html( wc_format_datetime( $o->get_date_created(), 'j M Y' ) . ', ' . wc_get_order_status_name( $o->get_status() ) ) . '
' . wp_kses_post( $o->get_formatted_order_total() ) . '

' . esc_html__( 'Yes', 'basketwise' ) . '' : esc_html__( 'No', 'basketwise' ); ?>

delete( brai_table( 'events' ), array( 'vid' => $vid ) ); $wpdb->delete( brai_table( 'visitors' ), array( 'vid' => $vid ) ); $wpdb->delete( brai_table( 'messages' ), array( 'vid' => $vid ) ); wp_safe_redirect( add_query_arg( array( 'tab' => 'baskets', 'deleted' => 1, ), brai_base_url() ) ); exit; } ); add_action( 'admin_post_brai_rec_toggle', function () { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Not allowed', 'basketwise' ) ); } check_admin_referer( 'brai_rec' ); if ( brai_rec_live() ) { delete_option( 'brai_recovery_live' ); } else { update_option( 'brai_recovery_live', time(), false ); } wp_safe_redirect( add_query_arg( 'tab', 'emails', brai_base_url() ) ); exit; } );