$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( 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( $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 = '