$contact_id, 'k' => self::sig( $contact_id ), ), home_url( '/' ) ); } private static function sig( $contact_id ) { return substr( hash_hmac( 'sha256', 'unsub|' . (int) $contact_id, wp_salt( 'auth' ) ), 0, 20 ); } /** Handle an unsubscribe link: confirm with a button (mail scanners follow links, people press buttons). */ public static function maybe_unsubscribe() { // phpcs:disable WordPress.Security.NonceVerification -- signed link; the HMAC is the check. if ( empty( $_GET['nb_unsub'] ) ) { return; } $id = absint( $_GET['nb_unsub'] ); $key = isset( $_GET['k'] ) ? sanitize_text_field( wp_unslash( $_GET['k'] ) ) : ''; // phpcs:enable WordPress.Security.NonceVerification $site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); if ( ! $id || ! hash_equals( self::sig( $id ), $key ) || ! Nodebook_Contacts::get( $id ) ) { wp_die( esc_html__( 'This unsubscribe link is not valid.', 'nodebook' ), esc_html( $site ), array( 'response' => 400 ) ); } $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET'; if ( 'POST' === $method ) { Nodebook_Subscription::set( $id, 'unsubscribed', array( 'source' => __( 'Unsubscribe link', 'nodebook' ) ) ); /* translators: %s: site name */ wp_die( '

' . esc_html__( 'You are unsubscribed', 'nodebook' ) . '

' . esc_html( sprintf( __( 'You will not get news or offers from %s by email any more.', 'nodebook' ), $site ) ) . '

', esc_html( $site ), array( 'response' => 200 ) ); } /* translators: %s: site name */ $text = sprintf( __( 'Stop getting news and offers from %s by email?', 'nodebook' ), $site ); wp_die( '

' . esc_html__( 'Unsubscribe', 'nodebook' ) . '

' . esc_html( $text ) . '

', esc_html( $site ), array( 'response' => 200 ) ); } /** * Send an email to a contact and log it. * * @param int $contact_id Contact. * @param string $subject Subject. * @param string $body Plain text; line breaks are kept. * @param int $user_id Who sends it (replies go to them; 0: the site's own address). * @param array $args auto: name of the automation sending it (then it is not counted as a follow-up); * unsubscribe: add a one-click unsubscribe link (newsletter-type email). * @return true|WP_Error */ public static function send( $contact_id, $subject, $body, $user_id, array $args = array() ) { $c = Nodebook_Contacts::get( (int) $contact_id ); if ( ! $c || ! is_email( (string) $c['email'] ) ) { return new WP_Error( 'nodebook_no_email', __( 'This contact has no email address.', 'nodebook' ) ); } if ( 'complained' === (string) ( $c['email_status'] ?? '' ) ) { return new WP_Error( 'nodebook_complained', self::warning( $c ) ); } $subject = trim( wp_strip_all_tags( (string) $subject ) ); $body = trim( (string) $body ); if ( '' === $subject || '' === $body ) { return new WP_Error( 'nodebook_empty', __( 'Write a subject and a message.', 'nodebook' ) ); } $user = get_userdata( (int) $user_id ); $headers = array( 'Content-Type: text/html; charset=UTF-8' ); if ( $user && is_email( $user->user_email ) ) { $headers[] = 'Reply-To: ' . self::header_name( $user->display_name ) . ' <' . $user->user_email . '>'; } /** * Headers of a one-to-one email (add a From line here, or Bcc yourself). * * @param string[] $headers Headers. * @param array $c Contact. * @param WP_User $user Sender. */ $headers = (array) apply_filters( 'nodebook_email_headers', $headers, $c, $user ); $foot = ''; if ( ! empty( $args['unsubscribe'] ) ) { $url = self::unsubscribe_url( (int) $contact_id ); $foot = '

' . esc_html__( 'Unsubscribe', 'nodebook' ) . '

'; $headers[] = 'List-Unsubscribe: <' . esc_url_raw( $url ) . '>'; } $html = '
' . wpautop( esc_html( $body ) ) . $foot . '
'; if ( ! wp_mail( (string) $c['email'], $subject, $html, $headers ) ) { return new WP_Error( 'nodebook_mail_failed', __( 'WordPress could not send the email. Install an SMTP plugin so mail from your site arrives.', 'nodebook' ) ); } if ( empty( $args['auto'] ) ) { Nodebook_Followup::log_touch( (int) $contact_id, 'email', $subject . "\n\n" . $body, (int) $user_id ); } /** * A one-to-one email was sent. * * @param int $contact_id Contact. * @param string $subject Subject. * @param int $user_id Sender. */ do_action( 'nodebook_email_sent', (int) $contact_id, $subject, (int) $user_id ); return true; } /** A word of caution before writing to someone, from their email status ('' when there is none). */ public static function warning( array $c ) { switch ( (string) ( $c['email_status'] ?? '' ) ) { case 'unsubscribed': return __( 'They unsubscribed from your newsletter. A personal reply is fine; no marketing.', 'nodebook' ); case 'bounced': return __( 'Email to this address bounced before. Check the address first.', 'nodebook' ); case 'complained': return __( 'They marked your email as spam, so Nodebook will not send to them.', 'nodebook' ); } return ''; } /** A display name safe for a mail header. */ private static function header_name( $name ) { $name = trim( preg_replace( '/[\r\n"<>]+/', ' ', (string) $name ) ); return '' === $name ? '' : '"' . $name . '"'; } /** admin-ajax: send from the compose sheet. */ public static function ajax() { check_ajax_referer( 'nodebook_ajax', 'nonce' ); Nodebook_Admin::require_cap(); $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; if ( ! $id || ! Nodebook_Team::can_see( $id ) ) { wp_send_json_error( array( 'message' => __( 'Contact not found.', 'nodebook' ) ), 404 ); } $subject = isset( $_POST['subject'] ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : ''; $body = isset( $_POST['body'] ) ? sanitize_textarea_field( wp_unslash( $_POST['body'] ) ) : ''; $sent = self::send( $id, $subject, $body, get_current_user_id() ); if ( is_wp_error( $sent ) ) { wp_send_json_error( array( 'message' => $sent->get_error_message() ), 400 ); } if ( isset( $_POST['kind'] ) && 'review' === sanitize_key( wp_unslash( $_POST['kind'] ) ) ) { Nodebook_Reviews::mark( $id, 'email', get_current_user_id() ); } $c = Nodebook_Contacts::get( $id ); wp_send_json_success( array( /* translators: %s: contact name */ 'message' => sprintf( __( 'Sent to %s. It is on their timeline.', 'nodebook' ), Nodebook_Contacts::display_name( $c ) ), ) ); } }