namespace, '/unsubscribe(?:/(?P[A-Za-z0-9]+))?', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'unsubscribe_page' ), // Public on purpose: confirm, unsubscribe and preference links are opened from an email by the subscriber. Each acts only with the token in the link, which belongs to that one subscriber. 'permission_callback' => '__return_true', ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'unsubscribe_post' ), // Public on purpose: confirm, unsubscribe and preference links are opened from an email by the subscriber. Each acts only with the token in the link, which belongs to that one subscriber. 'permission_callback' => '__return_true', ), ) ); register_rest_route( $this->namespace, '/confirm(?:/(?P[A-Za-z0-9]+))?', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'confirm_page' ), // Public on purpose: confirm, unsubscribe and preference links are opened from an email by the subscriber. Each acts only with the token in the link, which belongs to that one subscriber. 'permission_callback' => '__return_true', ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'confirm_post' ), // Public on purpose: confirm, unsubscribe and preference links are opened from an email by the subscriber. Each acts only with the token in the link, which belongs to that one subscriber. 'permission_callback' => '__return_true', ), ) ); register_rest_route( $this->namespace, '/preferences(?:/(?P[A-Za-z0-9]+))?', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'preferences_page' ), // Public on purpose: confirm, unsubscribe and preference links are opened from an email by the subscriber. Each acts only with the token in the link, which belongs to that one subscriber. 'permission_callback' => '__return_true', ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'preferences_post' ), // Public on purpose: confirm, unsubscribe and preference links are opened from an email by the subscriber. Each acts only with the token in the link, which belongs to that one subscriber. 'permission_callback' => '__return_true', ), ) ); } /* Unsubscribe --------------------------------------------------------- */ /** * GET /unsubscribe/:token. A page with a button. Acts on nothing. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function unsubscribe_page( $request ) { $token = (string) $request->get_param( 'token' ); if ( 'preview' === $token ) { return Mailpuff_Public_Page::response( __( 'Test email', 'mailpuff' ), '

' . esc_html__( 'This test email has nothing to unsubscribe from. In a campaign email, this link lets the recipient unsubscribe.', 'mailpuff' ) . '

' ); } if ( ! Mailpuff_Subscription::contact_for_token( $token ) ) { return $this->invalid_unsubscribe(); } return Mailpuff_Public_Page::response( __( 'Unsubscribe', 'mailpuff' ), '

' . esc_html__( 'Do you want to stop getting emails from us?', 'mailpuff' ) . '

' . $this->form( rest_url( $this->namespace . '/unsubscribe/' . $token ), '' ) ); } /** * POST /unsubscribe/:token. Unsubscribes. * * A mail client's one-click POST (RFC 8058) gets a small JSON reply. The * page's own form marks itself and gets a page. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function unsubscribe_post( $request ) { $token = (string) $request->get_param( 'token' ); // The optional "why" after an unsubscribe. It changes no one's status. if ( $this->from_page( $request ) && 'reason' === (string) $request->get_param( 'step' ) ) { return $this->reason_post( $request, $token ); } $ok = 'preview' !== $token && Mailpuff_Subscription::unsubscribe( $token ); if ( $this->from_page( $request ) ) { if ( ! $ok ) { return $this->invalid_unsubscribe(); } return $this->unsubscribed_page( $token ); } // A test's one-click (its List-Unsubscribe goes to the test page) // succeeds and unsubscribes nobody. if ( 'preview' === $token ) { return $this->success( array( 'unsubscribed' => false, 'test' => true, ) ); } if ( ! $ok ) { return $this->error( 'invalid_token', __( 'Invalid unsubscribe token.', 'mailpuff' ), 404 ); } return $this->success( array( 'unsubscribed' => true ) ); } /** * The page after an unsubscribe: it is done, and an optional question why. * * @param string $token Unsubscribe token. * @return Mailpuff_Page_Response */ private function unsubscribed_page( $token ) { $choices = ''; foreach ( Mailpuff_Subscription::reasons() as $key => $label ) { $choices .= ''; } $inner = '' . '
' . esc_html__( 'If you have a moment: why did you leave?', 'mailpuff' ) . '' . $choices . '
' . '' . ''; return Mailpuff_Public_Page::response( __( 'Unsubscribed', 'mailpuff' ), '

' . esc_html__( 'You are unsubscribed. You will get no more emails from us.', 'mailpuff' ) . '

' . $this->form( rest_url( $this->namespace . '/unsubscribe/' . $token ), $inner ), 'ok' ); } /** * POST /unsubscribe/:token with step=reason: keep why they left. * * @param WP_REST_Request $request Request. * @param string $token Unsubscribe token. * @return Mailpuff_Page_Response */ private function reason_post( $request, $token ) { $contact = Mailpuff_Subscription::contact_for_token( $token ); if ( ! $contact ) { return $this->invalid_unsubscribe(); } if ( ! $this->within_rate_limit( 'reasons' ) ) { return Mailpuff_Public_Page::response( __( 'Try again later', 'mailpuff' ), '

' . esc_html__( 'Too many requests. Try again in a minute.', 'mailpuff' ) . '

', 'error', 429 ); } Mailpuff_Subscription::record_reason( $contact, (string) $request->get_param( 'reason' ), (string) $request->get_param( 'note' ) ); return Mailpuff_Public_Page::response( __( 'Thanks', 'mailpuff' ), '

' . esc_html__( 'Thanks for telling us. You are unsubscribed.', 'mailpuff' ) . '

', 'ok' ); } /** * @return Mailpuff_Page_Response */ private function invalid_unsubscribe() { return Mailpuff_Public_Page::response( __( 'Invalid link', 'mailpuff' ), '

' . esc_html__( 'This unsubscribe link is invalid or has expired.', 'mailpuff' ) . '

', 'error', 404 ); } /* Confirm ------------------------------------------------------------- */ /** * GET /confirm/:token. A page with a button. Acts on nothing. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function confirm_page( $request ) { $token = (string) $request->get_param( 'token' ); if ( 'preview' === $token ) { return Mailpuff_Public_Page::response( __( 'Test email', 'mailpuff' ), '

' . esc_html__( 'This is a test of the confirmation email. In the real email, this link confirms a subscription.', 'mailpuff' ) . '

' ); } if ( ! Mailpuff_Subscription::contact_for_confirm_token( $token ) ) { return $this->expired(); } return Mailpuff_Public_Page::response( __( 'Confirm your subscription', 'mailpuff' ), '

' . esc_html__( 'Confirm that you want to get emails from us.', 'mailpuff' ) . '

' . $this->form( rest_url( $this->namespace . '/confirm/' . $token ), '' ) ); } /** * POST /confirm/:token. Confirms. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function confirm_post( $request ) { if ( ! Mailpuff_Subscription::confirm( (string) $request->get_param( 'token' ) ) ) { return $this->expired(); } return Mailpuff_Public_Page::response( __( 'You are subscribed', 'mailpuff' ), '

' . esc_html__( 'Thanks for confirming. You are on the list.', 'mailpuff' ) . '

', 'ok' ); } /** * @return Mailpuff_Page_Response */ private function expired() { return Mailpuff_Public_Page::response( __( 'Link expired', 'mailpuff' ), '

' . esc_html__( 'This link has expired or was already used.', 'mailpuff' ) . '

', 'error', 404 ); } /* Preferences --------------------------------------------------------- */ /** * GET /preferences/:token. The form. Acts on nothing. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function preferences_page( $request ) { $token = (string) $request->get_param( 'token' ); if ( 'preview' === $token ) { return Mailpuff_Public_Page::response( __( 'Test email', 'mailpuff' ), '

' . esc_html__( 'This test email has no preferences to change. In a campaign email, this link opens the recipient\'s own settings.', 'mailpuff' ) . '

' ); } $contact = Mailpuff_Subscription::contact_for_token( $token ); if ( ! $contact ) { return Mailpuff_Public_Page::response( __( 'Invalid link', 'mailpuff' ), '

' . esc_html__( 'This link is invalid or has expired.', 'mailpuff' ) . '

', 'error', 404 ); } return $this->preferences_form( $contact, $token ); } /** * POST /preferences/:token. Saves, or unsubscribes from everything. * Joining a group never changes consent, and leaving every group does not * unsubscribe. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function preferences_post( $request ) { $token = (string) $request->get_param( 'token' ); $contact = Mailpuff_Subscription::contact_for_token( $token ); if ( ! $contact ) { return Mailpuff_Public_Page::response( __( 'Invalid link', 'mailpuff' ), '

' . esc_html__( 'This link is invalid or has expired.', 'mailpuff' ) . '

', 'error', 404 ); } if ( 'unsubscribe' === (string) $request->get_param( 'saddle_action' ) ) { Mailpuff_Subscription::unsubscribe( $token ); return $this->unsubscribed_page( $token ); } $groups = $request->get_param( 'groups' ); Mailpuff_Subscription::save_preferences( $contact, (string) $request->get_param( 'first_name' ), (string) $request->get_param( 'last_name' ), is_array( $groups ) ? $groups : array() ); if ( in_array( (string) $request->get_param( 'post_delivery' ), Mailpuff_Post_Emails::offered(), true ) ) { Mailpuff_Post_Emails::set_choice( (int) $contact->id, (string) $request->get_param( 'post_delivery' ) ); } // The box is on the page only while the owner asks: ticked gives // consent, unticked withdraws it. if ( Mailpuff_Tracking_Consent::asks() ) { if ( '1' === (string) $request->get_param( 'tracking_consent' ) ) { Mailpuff_Tracking_Consent::give( (int) $contact->id, 'preferences' ); } else { Mailpuff_Tracking_Consent::withdraw( (int) $contact->id ); } } return Mailpuff_Public_Page::response( __( 'Preferences saved', 'mailpuff' ), '

' . esc_html__( 'Your choices are saved.', 'mailpuff' ) . '

', 'ok' ); } /** * The preferences form. * * @param object $contact Contact row. * @param string $token Unsubscribe token. * @return Mailpuff_Page_Response */ private function preferences_form( $contact, $token ) { if ( ! in_array( $contact->status, array( Mailpuff_Schema::SUBSCRIBER_SUBSCRIBED, Mailpuff_Schema::SUBSCRIBER_PENDING, Mailpuff_Schema::SUBSCRIBER_NOT_SUBSCRIBED ), true ) ) { return Mailpuff_Public_Page::response( __( 'Your preferences', 'mailpuff' ), '

' . esc_html__( 'You are not subscribed, so there is nothing to change.', 'mailpuff' ) . '

' ); } $fields = '' . ''; $groups = Mailpuff_Subscription::public_groups( (int) $contact->id ); if ( $groups ) { $fields .= '
' . esc_html__( 'What you get', 'mailpuff' ) . ''; foreach ( $groups as $group ) { $fields .= ''; } $fields .= '
'; } $fields .= $this->post_choice( (int) $contact->id ); if ( Mailpuff_Tracking_Consent::asks() ) { $fields .= ''; } $buttons = '' . ''; return Mailpuff_Public_Page::response( __( 'Your preferences', 'mailpuff' ), $this->form( rest_url( $this->namespace . '/preferences/' . $token ), $fields . $buttons, false ) ); } /** * "New posts": each post, the digest or none, while either email is on * and new posts reach this person. * * @param int $contact_id Contact ID. * @return string HTML, escaped; '' when there is nothing to pick. */ private function post_choice( $contact_id ) { $offered = Mailpuff_Post_Emails::offered(); if ( count( $offered ) < 2 || ! Mailpuff_Post_Emails::reaches( $contact_id ) ) { return ''; } $labels = Mailpuff_Post_Emails::choice_labels(); $picked = Mailpuff_Post_Emails::choice_of( $contact_id ); $html = '
' . esc_html__( 'New posts', 'mailpuff' ) . ''; foreach ( $offered as $choice ) { $html .= ''; } return $html . '
'; } /* Helpers ------------------------------------------------------------- */ /** * A form that POSTs back to a page route. * * @param string $action Absolute URL. * @param string $inner Inner HTML, escaped. * @param bool $mark Add the marker that tells the route a person submitted it. * @return string */ private function form( $action, $inner, $mark = true ) { return '
' . ( $mark ? '' : '' ) . $inner . '
'; } /** * Whether a POST came from one of these pages (and not a mail client's * one-click request). * * @param WP_REST_Request $request Request. * @return bool */ private function from_page( $request ) { return '1' === (string) $request->get_param( 'saddle_page' ); } }