prepare(). /** * Writes an email to the log. * * @param array $m vid, kind (manual or reminder), step, user_id, from_name, from_email, to_email, subject, body. * @return int Log entry ID. */ function brai_log_mail( $m ) { global $wpdb; $wpdb->insert( brai_table( 'messages' ), array( 'vid' => (string) ( $m['vid'] ?? '' ), 'ts' => current_time( 'mysql', true ), 'kind' => 'reminder' === ( $m['kind'] ?? '' ) ? 'reminder' : 'manual', 'step' => (int) ( $m['step'] ?? 0 ), 'user_id' => (int) ( $m['user_id'] ?? 0 ), 'from_name' => substr( (string) ( $m['from_name'] ?? '' ), 0, 190 ), 'from_email' => substr( (string) ( $m['from_email'] ?? '' ), 0, 190 ), 'to_email' => substr( (string) ( $m['to_email'] ?? '' ), 0, 190 ), 'subject' => substr( (string) ( $m['subject'] ?? '' ), 0, 255 ), 'body' => (string) ( $m['body'] ?? '' ), ) ); return (int) $wpdb->insert_id; } /** * Who a hand-written email can come from. * * @return array shop and me, each with name and email. "me" is left out when the logged-in person has no email address. */ function brai_senders() { $s = brai_mail_settings(); $out = array( 'shop' => array( 'name' => (string) $s['from_name'], 'email' => sanitize_email( (string) get_option( 'admin_email' ) ), ), ); $u = wp_get_current_user(); if ( $u && $u->exists() && is_email( $u->user_email ) ) { $out['me'] = array( 'name' => '' !== trim( (string) $u->display_name ) ? (string) $u->display_name : (string) $s['from_name'], 'email' => sanitize_email( $u->user_email ), ); } return $out; } /** * The body of a hand-written email, in the shop's email design. * * @param array $r Visitor row. * @param string $subject Subject. * @param string $text The message as typed (plain text). * @param bool $button Add a "Return to your basket" button. * @return string HTML. */ function brai_message_html( $r, $subject, $text, $button ) { $s = brai_mail_settings(); $inner = wpautop( esc_html( $text ) ); $items = $button && ! empty( $r['cart'] ) ? (array) json_decode( (string) $r['cart'], true ) : array(); if ( $items ) { $color = $s['button_text'] ? $s['button_text'] : ( brai_is_light( $s['button_bg'] ) ? '#1d2327' : '#ffffff' ); $inner .= '
'; } return brai_mail_frame( $inner, $s, $subject ); } /** * Sends a hand-written email to a customer and logs it. * * @param array $r Visitor row. * @param string $sender shop or me. * @param string $subject Subject. * @param string $text Message (plain text). * @param bool $button Add a "Return to your basket" button. * @return bool|string True, or the reason it was not sent. */ function brai_send_message( $r, $sender, $subject, $text, $button = false ) { $senders = brai_senders(); $from = isset( $senders[ $sender ] ) ? $senders[ $sender ] : $senders['shop']; if ( empty( $r['email'] ) || ! is_email( $r['email'] ) ) { return __( 'This visitor has not left an email address.', 'basketwise' ); } if ( '' === trim( $subject ) || '' === trim( $text ) ) { return __( 'Add a subject and a message first.', 'basketwise' ); } if ( ! is_email( $from['email'] ) ) { return __( 'The sender has no email address.', 'basketwise' ); } $name = trim( str_replace( array( '"', '<', '>', "\r", "\n" ), '', $from['name'] ) ); $headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: "' . $name . '" <' . $from['email'] . '>', 'Reply-To: "' . $name . '" <' . $from['email'] . '>', ); if ( ! wp_mail( $r['email'], $subject, brai_message_html( $r, $subject, $text, $button ), $headers ) ) { return __( 'The email could not be sent. Check that this site can send email.', 'basketwise' ); } $id = brai_log_mail( array( 'vid' => $r['vid'], 'kind' => 'manual', 'user_id' => get_current_user_id(), 'from_name' => $name, 'from_email' => $from['email'], 'to_email' => $r['email'], 'subject' => $subject, 'body' => $text, ) ); brai_event( $r['vid'], 'message', $id, 0, 0, $subject ); return true; } /** * Log entries, newest first. * * @param string $kind all, manual or reminder. * @param string $like Search (recipient or subject) as a LIKE pattern, or ''. * @param string $vid Only this visitor, or ''. * @param int $limit Rows. * @param int $offset Offset. * @return array */ function brai_mail_log( $kind, $like, $vid, $limit, $offset ) { global $wpdb; return (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE ( %s = 'all' OR kind = %s ) AND ( %s = '' OR to_email LIKE %s OR subject LIKE %s OR body LIKE %s ) AND ( %s = '' OR vid = %s ) ORDER BY id DESC LIMIT %d OFFSET %d", brai_table( 'messages' ), $kind, $kind, $like, $like, $like, $like, $vid, $vid, $limit, $offset ), ARRAY_A ); } /** * How many log entries match. * * @param string $kind all, manual or reminder. * @param string $like Search as a LIKE pattern, or ''. * @param string $vid Only this visitor, or ''. * @return int */ function brai_mail_log_count( $kind, $like = '', $vid = '' ) { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE ( %s = 'all' OR kind = %s ) AND ( %s = '' OR to_email LIKE %s OR subject LIKE %s OR body LIKE %s ) AND ( %s = '' OR vid = %s )", brai_table( 'messages' ), $kind, $kind, $like, $like, $like, $like, $vid, $vid ) ); } /** * Reminders sent before the log existed (before 1.3.0) are copied into it once, so the log starts complete. * Their subject lines were not kept, so they show as "Basket reminder 1", 2 or 3. */ function brai_backfill_mail_log() { global $wpdb; if ( get_option( 'brai_log_filled' ) ) { return; } update_option( 'brai_log_filled', 1, false ); if ( (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', brai_table( 'messages' ) ) ) ) { return; } $wpdb->query( $wpdb->prepare( "INSERT INTO %i (vid, ts, kind, step, user_id, from_name, from_email, to_email, subject, body) SELECT e.vid, e.ts, 'reminder', e.qty, 0, '', '', COALESCE(v.email, ''), '', '' FROM %i e LEFT JOIN %i v ON v.vid = e.vid WHERE e.type = 'reminder' ORDER BY e.id ASC", brai_table( 'messages' ), brai_table( 'events' ), brai_table( 'visitors' ) ) ); }