0 && in_array( $r['status'], array( 'basket', 'checkout' ), true ); /* translators: %s: shop name. */ $subject = $basket ? sprintf( __( 'Your basket at %s', 'basketwise' ), $site ) : sprintf( __( 'A message from %s', 'basketwise' ), $site ); /* translators: %s: customer's first name. */ $hello = '' !== $first ? sprintf( __( 'Hi %s,', 'basketwise' ), $first ) : __( 'Hello,', 'basketwise' ); echo '
' . esc_html__( 'Email this customer', 'basketwise' ) . ''; if ( brai_unsubscribed( $r['email'] ) ) { echo '

' . esc_html__( 'This customer unsubscribed from basket reminders. Only email them if they have asked you to, or about something other than their basket.', 'basketwise' ) . '

'; } echo '
'; wp_nonce_field( 'brai_send_message' ); echo ''; echo '
' . esc_html__( 'To', 'basketwise' ) . '' . esc_html( ( $r['name'] ? $r['name'] . ' ' : '' ) . '<' . $r['email'] . '>' ) . '
'; echo '
' . esc_html__( 'From', 'basketwise' ) . ''; $labels = array( 'shop' => __( 'The shop', 'basketwise' ), 'me' => __( 'Me', 'basketwise' ), ); foreach ( $senders as $k => $from ) { echo ''; } echo '
'; echo '
'; echo '
'; echo '
'; if ( $basket ) { echo ''; } echo '
'; echo '
  ' . esc_html__( 'or write it in your own email app', 'basketwise' ) . '
'; echo '

' . esc_html__( 'The email uses your shop\'s email design, and replies go to the sender you choose. It is kept in this customer\'s timeline and in the Sent emails log. If the sender\'s address is at a different domain from this website, some inboxes may treat the email as spam.', 'basketwise' ) . '

'; echo '
'; } add_action( 'admin_post_brai_send_message', function () { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Not allowed', 'basketwise' ) ); } check_admin_referer( 'brai_send_message' ); 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 = 'emailfail'; if ( $r ) { $sender = isset( $_POST['sender'] ) && 'me' === sanitize_key( wp_unslash( $_POST['sender'] ) ) ? 'me' : 'shop'; $subject = isset( $_POST['subject'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['subject'] ) ) ) : ''; $body = isset( $_POST['body'] ) ? trim( sanitize_textarea_field( wp_unslash( $_POST['body'] ) ) ) : ''; $subject = function_exists( 'mb_substr' ) ? mb_substr( $subject, 0, 200 ) : substr( $subject, 0, 200 ); $body = function_exists( 'mb_substr' ) ? mb_substr( $body, 0, 5000 ) : substr( $body, 0, 5000 ); if ( true === brai_send_message( $r, $sender, $subject, $body, ! empty( $_POST['button'] ) ) ) { $msg = 'emailed'; if ( ! empty( $_POST['contacted'] ) && empty( $r['contacted_at'] ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table. $wpdb->update( brai_table( 'visitors' ), array( 'contacted_at' => current_time( 'mysql', true ), 'contacted_by' => get_current_user_id(), ), array( 'vid' => $vid ) ); } } } wp_safe_redirect( add_query_arg( array( 'vid' => $vid, 'msg' => $msg, ), brai_base_url() ) ); exit; } ); /** * What to call a log entry that has no subject line (reminders sent before the log existed). * * @param array $m Log entry. * @return string */ function brai_mail_subject( $m ) { if ( '' !== (string) $m['subject'] ) { return (string) $m['subject']; } /* translators: %d: reminder number. */ return sprintf( __( 'Basket reminder %d', 'basketwise' ), max( 1, (int) $m['step'] ) ); } /** * Who sent a log entry, in words. * * @param array $m Log entry. * @return string */ function brai_mail_sender( $m ) { if ( 'reminder' === $m['kind'] ) { /* translators: %d: reminder number. */ return sprintf( __( 'Automatic reminder %d', 'basketwise' ), max( 1, (int) $m['step'] ) ); } $u = $m['user_id'] ? get_userdata( (int) $m['user_id'] ) : null; /* translators: %s: staff member's name. */ return $u ? sprintf( __( 'Written by %s', 'basketwise' ), $u->display_name ) : __( 'Written by hand', 'basketwise' ); } /** * The emails sent to one customer, on their page. * * @param array $r Visitor row. */ function brai_messages_box( $r ) { $rows = brai_mail_log( 'all', '', $r['vid'], 30, 0 ); if ( ! $rows ) { return; } echo '

' . esc_html__( 'Emails sent', 'basketwise' ) . ' ' . esc_html( number_format_i18n( count( $rows ) ) ) . '

'; foreach ( $rows as $m ) { $head = '' . esc_html( brai_mail_subject( $m ) ) . '' . esc_html( get_date_from_gmt( $m['ts'], 'j M, H:i' ) . ' · ' . brai_mail_sender( $m ) ) . ''; if ( '' !== trim( (string) $m['body'] ) ) { echo '
' . wp_kses_post( $head ) . '
'; if ( $m['from_email'] ) { /* translators: %s: sender name and address. */ echo '

' . esc_html( sprintf( __( 'From %s', 'basketwise' ), trim( $m['from_name'] . ' <' . $m['from_email'] . '>' ) ) ) . '

'; } echo wp_kses_post( wpautop( esc_html( $m['body'] ) ) ) . '
'; } else { echo '
' . wp_kses_post( $head ) . '
'; } } echo '
'; } /** The Sent emails tab: every email sent to customers, written by hand or automatic. */ function brai_mail_log_page() { global $wpdb; $base = add_query_arg( 'tab', 'sent', brai_base_url() ); $kind = sanitize_key( brai_get( 'kind' ) ); $kind = in_array( $kind, array( 'manual', 'reminder' ), true ) ? $kind : 'all'; $q = trim( brai_get( 'q' ) ); $like = '' !== $q ? '%' . $wpdb->esc_like( $q ) . '%' : ''; $per = 50; $total = brai_mail_log_count( $kind, $like ); $pages = max( 1, (int) ceil( $total / $per ) ); $paged = min( $pages, max( 1, (int) brai_get( 'paged' ) ) ); $rows = brai_mail_log( $kind, $like, '', $per, ( $paged - 1 ) * $per ); $kinds = array( 'all' => __( 'All', 'basketwise' ), 'manual' => __( 'Written by you', 'basketwise' ), 'reminder' => __( 'Automatic reminders', 'basketwise' ), ); echo '
'; brai_nav( 'sent' ); echo '

'; /* translators: %d: days. */ echo esc_html( sprintf( __( 'Every email sent to customers from Basketwise: the ones you write and the automatic basket reminders. Kept for %d days, like the rest of your records.', 'basketwise' ), brai_keep_days() ) ); echo '

'; foreach ( $kinds as $k => $label ) { printf( '%s%s', $k === $kind ? 'on' : '', esc_url( add_query_arg( array_filter( array( 'kind' => 'all' === $k ? '' : $k, 'q' => rawurlencode( $q ) ) ), $base ) ), esc_html( $label ), esc_html( number_format_i18n( brai_mail_log_count( $k, $like ) ) ) ); } echo '
'; if ( 'all' !== $kind ) { echo ''; } echo '
'; /* translators: %s: number of emails. */ echo esc_html( sprintf( _n( '%s email', '%s emails', $total, 'basketwise' ), number_format_i18n( $total ) ) ); echo '
'; if ( ! $rows ) { echo '

' . esc_html__( 'No emails here yet. Open a visitor from the Baskets tab and press Email to write one.', 'basketwise' ) . '

'; return; } echo ''; foreach ( $rows as $m ) { $to = '' !== $m['to_email'] ? $m['to_email'] : __( '(record deleted)', 'basketwise' ); echo ''; echo ''; } echo '
' . esc_html__( 'Sent', 'basketwise' ) . '' . esc_html__( 'To', 'basketwise' ) . '' . esc_html__( 'Email', 'basketwise' ) . '' . esc_html__( 'Sent by', 'basketwise' ) . '' . esc_html__( 'From', 'basketwise' ) . '
' . esc_html( get_date_from_gmt( $m['ts'], 'j M Y, H:i' ) ) . '' . esc_html( $to ) . ''; if ( '' !== trim( (string) $m['body'] ) ) { echo '
' . esc_html( brai_mail_subject( $m ) ) . '
' . wp_kses_post( wpautop( esc_html( $m['body'] ) ) ) . '
'; } else { echo '' . esc_html( brai_mail_subject( $m ) ) . ''; } echo '
' . esc_html( brai_mail_sender( $m ) ) . '' . esc_html( $m['from_email'] ? $m['from_email'] : '–' ) . '
'; if ( $pages > 1 ) { echo '
' . wp_kses_post( (string) paginate_links( array( 'base' => add_query_arg( array_filter( array( 'kind' => 'all' === $kind ? '' : $kind, 'q' => rawurlencode( $q ), 'paged' => '%#%' ) ), $base ), 'format' => '', 'current' => $paged, 'total' => $pages, 'prev_text' => '‹', 'next_text' => '›', ) ) ) . '
'; } echo ''; }