' . esc_html( $label ) . '' . esc_html( $value ) . '' . ( '' !== $sub ? '' . esc_html( $sub ) . '' : '' ) . ''; }; echo '

' . esc_html__( 'Last 24 hours', 'basketwise' ) . '

'; $tile( __( 'Visitors', 'basketwise' ), number_format_i18n( $day['visitors'] ) ); $tile( __( 'Added to basket', 'basketwise' ), number_format_i18n( $day['baskets'] ) ); $tile( __( 'Orders', 'basketwise' ), number_format_i18n( $day['orders'] ), $day['orders'] ? brai_money( $day['revenue'] ) : '', 'brai-good' ); echo '

' . esc_html__( 'Last 7 days', 'basketwise' ) . '

'; /* translators: %s: value. */ $tile( __( 'Abandoned', 'basketwise' ), number_format_i18n( $week['abandoned'] ), sprintf( __( '%s left in baskets', 'basketwise' ), brai_money( $week['abandoned_value'] ) ), 'brai-bad' ); $tile( __( 'Reminders sent', 'basketwise' ), number_format_i18n( $week['reminders'] ), brai_rec_live() ? '' : __( 'Reminders are off', 'basketwise' ) ); $tile( __( 'Recovered', 'basketwise' ), number_format_i18n( $week['recovered'] ), $week['recovered'] ? brai_money( $week['recovered_value'] ) : '', 'brai-good' ); echo '
'; $rows = array_slice( brai_not_contacted( brai_visitor_rows( gmdate( 'Y-m-d H:i:s', $now - WEEK_IN_SECONDS ), gmdate( 'Y-m-d H:i:s', $now - brai_abandon_mins() * 60 ), 'abandoned', '', true, 40, 0 ) ), 0, 4 ); if ( $rows ) { echo '

' . esc_html__( 'Abandoned baskets to follow up', 'basketwise' ) . '

'; } echo '

' . esc_html__( 'Open Basketwise', 'basketwise' ) . '

'; } /* ---------- CSV export of a Baskets list ---------- */ /** * One line of a CSV file. * * @param array $cells Values. * @return string */ function brai_csv_line( $cells ) { $out = array(); foreach ( $cells as $c ) { $c = (string) $c; /* A cell that starts with one of these would be run as a formula by a spreadsheet. */ if ( '' !== $c && in_array( $c[0], array( '=', '+', '-', '@', "\t", "\r" ), true ) ) { $c = "'" . $c; } $out[] = '"' . str_replace( '"', '""', $c ) . '"'; } return implode( ',', $out ) . "\r\n"; } /** * Link that downloads the list now on screen. * * @param string $view Which list. * @param int $days Period. * @param string $q Search. * @return string */ function brai_export_url( $view, $days, $q ) { return wp_nonce_url( add_query_arg( array_filter( array( 'action' => 'brai_export', 'view' => $view, 'days' => (int) $days, 'q' => rawurlencode( $q ), ) ), admin_url( 'admin-post.php' ) ), 'brai_export' ); } add_action( 'admin_post_brai_export', function () { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Not allowed', 'basketwise' ) ); } check_admin_referer( 'brai_export' ); global $wpdb; $view = isset( $_GET['view'] ) ? sanitize_key( wp_unslash( $_GET['view'] ) ) : 'abandoned'; $view = in_array( $view, array( 'abandoned', 'live', 'ordered', 'browsing', 'all' ), true ) ? $view : 'abandoned'; $days = isset( $_GET['days'] ) ? absint( $_GET['days'] ) : 7; $days = in_array( $days, array( 1, 7, 30, 90 ), true ) ? $days : 7; $q = isset( $_GET['q'] ) ? trim( sanitize_text_field( wp_unslash( $_GET['q'] ) ) ) : ''; $like = '' !== $q ? '%' . $wpdb->esc_like( $q ) . '%' : ''; $rows = brai_visitor_rows( gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ), gmdate( 'Y-m-d H:i:s', time() - brai_abandon_mins() * 60 ), $view, $like, false, 5000, 0 ); $csv = "\xEF\xBB\xBF" . brai_csv_line( array( __( 'Name', 'basketwise' ), __( 'Email', 'basketwise' ), __( 'Phone', 'basketwise' ), __( 'Status', 'basketwise' ), __( 'Basket', 'basketwise' ), __( 'Basket value', 'basketwise' ), __( 'Came from', 'basketwise' ), __( 'Campaign', 'basketwise' ), __( 'Device', 'basketwise' ), __( 'Town', 'basketwise' ), __( 'Region', 'basketwise' ), __( 'Country', 'basketwise' ), __( 'Visits', 'basketwise' ), __( 'Pages viewed', 'basketwise' ), __( 'Reminders sent', 'basketwise' ), __( 'Unsubscribed', 'basketwise' ), __( 'Contacted', 'basketwise' ), __( 'Note', 'basketwise' ), __( 'First seen', 'basketwise' ), __( 'Last active', 'basketwise' ), ) ); foreach ( $rows as $r ) { $items = array(); foreach ( (array) ( $r['cart'] ? json_decode( $r['cart'], true ) : array() ) as $i ) { $items[] = (int) ( $i['qty'] ?? 1 ) . ' x ' . (string) ( $i['name'] ?? '' ); } $csv .= brai_csv_line( array( $r['name'], $r['email'], $r['phone'], brai_label( $r )[0], implode( '; ', $items ), (float) $r['cart_value'] ? wc_format_decimal( $r['cart_value'], wc_get_price_decimals() ) : '', brai_source( $r['landing'], $r['referrer'], $r['src'] ), brai_campaign( $r['landing'], $r['src'] ), $r['device'], $r['city'], $r['region'], '-' === $r['country'] ? '' : $r['country'], (int) $r['visits'], (int) $r['views'], (int) $r['reminders'], $r['email'] && brai_unsubscribed( $r['email'] ) ? __( 'Yes', 'basketwise' ) : '', ! empty( $r['contacted_at'] ) ? get_date_from_gmt( $r['contacted_at'], 'Y-m-d H:i' ) : '', (string) ( $r['note'] ?? '' ), get_date_from_gmt( $r['first_seen'], 'Y-m-d H:i' ), get_date_from_gmt( $r['last_seen'], 'Y-m-d H:i' ), ) ); } nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="basketwise-' . $view . '-' . wp_date( 'Y-m-d' ) . '.csv"' ); header( 'X-Content-Type-Options: nosniff' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- a CSV file download, not HTML; every cell is quoted in brai_csv_line(). echo $csv; exit; } ); /* ---------- Send the next reminder to one customer now ---------- */ add_action( 'admin_post_brai_send_now', function () { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Not allowed', 'basketwise' ) ); } check_admin_referer( 'brai_send_now' ); global $wpdb; $vid = isset( $_POST['vid'] ) ? sanitize_key( wp_unslash( $_POST['vid'] ) ) : ''; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table. $r = '' !== $vid ? $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE vid = %s', brai_table( 'visitors' ), $vid ), ARRAY_A ) : null; $msg = 'notsent'; if ( $r ) { $step = brai_next_reminder( $r ); if ( is_int( $step ) && brai_send_reminder( $r, $step ) ) { // phpcs:disable WordPress.DB.DirectDatabaseQuery -- the plugin's own table. $wpdb->update( brai_table( 'visitors' ), array( 'reminders' => $step, 'reminded_at' => current_time( 'mysql', true ), ), array( 'vid' => $vid ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery brai_event( $vid, 'reminder', 0, $step, $r['cart_value'] ); $msg = 'sent'; } } wp_safe_redirect( add_query_arg( array( 'vid' => $vid, 'msg' => $msg, ), brai_base_url() ) ); exit; } ); /** * The "Send reminder now" button on a visitor's page, or nothing if a reminder cannot be sent. * * @param array $r Visitor row. */ function brai_send_now_button( $r ) { $step = brai_next_reminder( $r ); if ( ! is_int( $step ) ) { return; } /* translators: 1: email number, 2: email address. */ $ask = sprintf( __( 'Send reminder email %1$d to %2$s now?', 'basketwise' ), $step, $r['email'] ); echo '
'; wp_nonce_field( 'brai_send_now' ); echo ''; echo '
'; } /** The notice after "Send reminder now". */ function brai_send_now_notice() { $msg = sanitize_key( brai_get( 'msg' ) ); if ( 'sent' === $msg ) { echo '

' . esc_html__( 'Reminder sent.', 'basketwise' ) . '

'; } elseif ( 'emailed' === $msg ) { echo '

' . esc_html__( 'Email sent. It is in the timeline below and in the Sent emails log.', 'basketwise' ) . '

'; } elseif ( 'emailfail' === $msg ) { echo '

' . esc_html__( 'The email was not sent. Check the subject and message are filled in, and that this site can send email.', 'basketwise' ) . '

'; } elseif ( 'followup' === $msg ) { echo '

' . esc_html__( 'Follow-up saved.', 'basketwise' ) . '

'; } elseif ( 'notsent' === $msg ) { echo '

' . esc_html__( 'The reminder could not be sent. Check that this site can send email.', 'basketwise' ) . '

'; } } /* ---------- How each reminder email is doing ---------- */ /** A card for the Email settings tab. */ function brai_reminder_stats_card() { $days = brai_keep_days(); $stats = brai_reminder_stats( time() - $days * DAY_IN_SECONDS ); $s = brai_mail_settings(); $any = 0; foreach ( $stats as $st ) { $any += $st['sent']; } echo '

' . esc_html__( 'How each email is doing', 'basketwise' ) . ' '; /* translators: %d: days. */ echo esc_html( sprintf( __( 'last %d days', 'basketwise' ), $days ) ); echo '

'; if ( ! $any ) { echo '

' . esc_html__( 'Once reminders have been sent, you will see here how many were clicked and how many led to an order.', 'basketwise' ) . '

'; return; } echo ''; foreach ( $stats as $n => $st ) { if ( ! $st['sent'] && $n > (int) $s['count'] ) { continue; } $pc = function ( $x ) use ( $st ) { return $st['sent'] ? ' ' . esc_html( round( 100 * $x / $st['sent'] ) . '%' ) . '' : ''; }; /* translators: %d: email number. */ echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html__( 'Email', 'basketwise' ) . '' . esc_html__( 'Sent', 'basketwise' ) . '' . esc_html__( 'Clicked', 'basketwise' ) . '' . esc_html__( 'Orders', 'basketwise' ) . '' . esc_html__( 'Order value', 'basketwise' ) . '
' . esc_html( sprintf( __( 'Email %d', 'basketwise' ), $n ) ) . '' . esc_html( number_format_i18n( $st['sent'] ) ) . '' . esc_html( number_format_i18n( $st['clicked'] ) ) . wp_kses_post( $pc( $st['clicked'] ) ) . '' . ( $st['orders'] ? '' . esc_html( number_format_i18n( $st['orders'] ) ) . '' : '0' ) . wp_kses_post( $pc( $st['orders'] ) ) . '' . esc_html( brai_money( $st['value'] ) ) . '
'; echo '

' . esc_html__( 'A click or an order is credited to the last reminder that customer was sent. "Clicked" counts the "Return to your basket" button.', 'basketwise' ) . '

'; $codes = brai_coupon_stats( time() - $days * DAY_IN_SECONDS ); if ( $codes['sent'] ) { echo '

' . esc_html__( 'Discount codes', 'basketwise' ) . '   '; echo esc_html( sprintf( /* translators: 1: codes sent, 2: codes used, 3: percentage used, 4: order value. */ __( '%1$s sent, %2$s used (%3$s%%), %4$s in orders.', 'basketwise' ), number_format_i18n( $codes['sent'] ), number_format_i18n( $codes['used'] ), round( 100 * $codes['used'] / $codes['sent'] ), brai_money( $codes['value'] ) ) ); echo '

'; } echo ''; } /* ---------- Weekly summary preview ---------- */ /** Shows what the weekly summary email would say today. */ function brai_weekly_preview() { $s = brai_mail_settings(); $next = (int) wp_next_scheduled( 'brai_weekly' ); echo '

← ' . esc_html__( 'Email settings', 'basketwise' ) . '

'; echo '

' . esc_html__( 'Weekly summary', 'basketwise' ) . '

'; if ( ! empty( $s['weekly'] ) && $s['digest_to'] && $next ) { /* translators: 1: email addresses, 2: date and time. */ echo esc_html( sprintf( __( 'Sent to %1$s. Next one: %2$s.', 'basketwise' ), $s['digest_to'], wp_date( 'D j M, H:i', $next ) ) ); } else { esc_html_e( 'The weekly summary is switched off. This is what it would say today.', 'basketwise' ); } echo '

' . wp_kses_post( brai_weekly_html( time() ) ) . '
'; } /* ---------- Follow-up: mark a basket as contacted and keep a note ---------- */ /** * Leaves out baskets that have already been followed up. * * @param array $rows Visitor rows. * @return array */ function brai_not_contacted( $rows ) { return array_values( array_filter( (array) $rows, function ( $r ) { return empty( $r['contacted_at'] ); } ) ); } /** * A small "Contacted" label for the lists, with the note beneath it if there is one. * * @param array $r Visitor row. * @return string HTML. */ function brai_contacted_html( $r ) { $h = ''; if ( ! empty( $r['contacted_at'] ) ) { /* translators: %s: date. */ $h .= '
' . esc_html( sprintf( __( 'Contacted %s', 'basketwise' ), get_date_from_gmt( $r['contacted_at'], 'j M' ) ) ) . '
'; } if ( isset( $r['note'] ) && '' !== trim( (string) $r['note'] ) ) { $h .= '
' . esc_html( wp_html_excerpt( (string) $r['note'], 60, '…' ) ) . '
'; } return $h; } /** * The Follow-up box on a visitor's page. * * @param array $r Visitor row. */ function brai_followup_box( $r ) { $done = ! empty( $r['contacted_at'] ); echo '

' . esc_html__( 'Follow-up', 'basketwise' ) . '

'; echo '
'; wp_nonce_field( 'brai_followup' ); echo ''; echo '

'; if ( $done ) { $by = ! empty( $r['contacted_by'] ) ? get_userdata( (int) $r['contacted_by'] ) : null; echo '
'; if ( $by ) { /* translators: 1: date and time, 2: staff member's name. */ echo esc_html( sprintf( __( 'Marked on %1$s by %2$s', 'basketwise' ), get_date_from_gmt( $r['contacted_at'], 'j M, H:i' ), $by->display_name ) ); } else { /* translators: %s: date and time. */ echo esc_html( sprintf( __( 'Marked on %s', 'basketwise' ), get_date_from_gmt( $r['contacted_at'], 'j M, H:i' ) ) ); } echo ''; } echo '

'; echo '

'; echo '

'; echo '

' . esc_html__( 'Contacted baskets drop off the follow-up lists. A new basket from the same customer starts afresh.', 'basketwise' ) . '

'; echo '
'; } add_action( 'admin_post_brai_followup', function () { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Not allowed', 'basketwise' ) ); } check_admin_referer( 'brai_followup' ); global $wpdb; $vid = isset( $_POST['vid'] ) ? sanitize_key( wp_unslash( $_POST['vid'] ) ) : ''; // phpcs:disable WordPress.DB.DirectDatabaseQuery -- the plugin's own table. $r = '' !== $vid ? $wpdb->get_row( $wpdb->prepare( 'SELECT vid, contacted_at FROM %i WHERE vid = %s', brai_table( 'visitors' ), $vid ), ARRAY_A ) : null; if ( $r ) { $note = isset( $_POST['note'] ) ? sanitize_textarea_field( wp_unslash( $_POST['note'] ) ) : ''; $set = array( 'note' => function_exists( 'mb_substr' ) ? mb_substr( $note, 0, 1000 ) : substr( $note, 0, 1000 ) ); if ( ! empty( $_POST['contacted'] ) ) { if ( empty( $r['contacted_at'] ) ) { $set['contacted_at'] = current_time( 'mysql', true ); $set['contacted_by'] = get_current_user_id(); } } else { $set['contacted_at'] = null; $set['contacted_by'] = 0; } $wpdb->update( brai_table( 'visitors' ), $set, array( 'vid' => $vid ) ); } // phpcs:enable WordPress.DB.DirectDatabaseQuery wp_safe_redirect( add_query_arg( array( 'vid' => $vid, 'msg' => 'followup', ), brai_base_url() ) ); exit; } ); /* ---------- Rules for automatic reminders (Email settings > Schedule) ---------- */ /** * The Rules card: a minimum basket value and quiet hours. * * @param array $s Email settings. */ function brai_rules_card( $s ) { $hours = function ( $name, $value ) { $h = ''; }; echo '

' . esc_html__( 'Rules', 'basketwise' ) . ' ' . esc_html__( 'for automatic reminders', 'basketwise' ) . '

'; echo '
'; echo '' . esc_html( get_woocommerce_currency_symbol() ) . ' '; echo '

' . esc_html__( 'Baskets worth less than this are not sent reminders. 0 means every basket.', 'basketwise' ) . '

'; echo '
'; echo ''; echo '

'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- two