Skip to:
Content

BuddyPress.org

Changeset 12912


Ignore:
Timestamp:
04/28/2021 11:49:18 PM (3 years ago)
Author:
dcavins
Message:

BP_Optouts: Improve unsubscribe behavior.

Improve unsubscribe link behavior when user is not
a site member, meaning that the unsubscribe is an
opt-out request.

  • Introduce bp_user_has_opted_out() as convenience

function for checking an email address's status

  • Build bp_email_get_unsubscribe_link() for

non-member case.

  • bp_email_unsubscribe_handler() handles

non-member case and uses wp_die() screen
to display message.

  • Fix unsusbscribe headers for opt-out links.

Props imath.

See #8448.

Location:
trunk/src/bp-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-filters.php

    r12757 r12912  
    10681068    // Add 'List-Unsubscribe' header if applicable.
    10691069    if ( ! empty( $tokens['unsubscribe'] ) && $tokens['unsubscribe'] !== wp_login_url() ) {
    1070         $user = get_user_by( 'email', $tokens['recipient.email'] );
    1071 
    1072         $link = bp_email_get_unsubscribe_link( array(
    1073             'user_id'           => $user->ID,
     1070        $user    = get_user_by( 'email', $tokens['recipient.email'] );
     1071        $user_id = isset( $user->ID ) ? $user->ID : 0;
     1072
     1073        $args = array(
     1074            'user_id'           => $user_id,
    10741075            'notification_type' => $email->get( 'type' ),
    1075         ) );
     1076        );
     1077
     1078        // If this email is not to a current member, include the nonmember's email address and the Inviter ID.
     1079        if ( ! $user_id ) {
     1080            $args['email_address'] = $tokens['recipient.email'];
     1081            $args['member_id']     = bp_loggedin_user_id();
     1082        }
     1083
     1084        $link = bp_email_get_unsubscribe_link( $args );
    10761085
    10771086        if ( ! empty( $link ) ) {
  • trunk/src/bp-core/bp-core-functions.php

    r12905 r12912  
    40504050    $raw_hash       = ! empty( $_GET['nh'] ) ? $_GET['nh'] : '';
    40514051    $raw_user_id    = ! empty( $_GET['uid'] ) ? absint( $_GET['uid'] ) : 0;
    4052     $new_hash       = hash_hmac( 'sha1', "{$raw_email_type}:{$raw_user_id}", bp_email_get_salt() );
     4052    $raw_user_email = ! empty( $_GET['uem'] ) ? $_GET['uem'] : '';
     4053    $raw_member_id  = ! empty( $_GET['mid'] ) ? absint( $_GET['mid'] ) : 0;
     4054    $redirect_to    = '';
     4055
     4056    $new_hash = '';
     4057    if ( ! empty( $raw_user_id ) ) {
     4058        $new_hash = hash_hmac( 'sha1', "{$raw_email_type}:{$raw_user_id}", bp_email_get_salt() );
     4059    } else if ( ! empty( $raw_user_email ) ) {
     4060        $new_hash = hash_hmac( 'sha1', "{$raw_email_type}:{$raw_user_email}", bp_email_get_salt() );
     4061    }
    40534062
    40544063    // Check required values.
    4055     if ( ! $raw_user_id || ! $raw_email_type || ! $raw_hash || ! array_key_exists( $raw_email_type, $emails ) ) {
     4064    if ( ( ! $raw_user_id && ! $raw_user_email ) || ! $raw_email_type || ! $raw_hash || ! array_key_exists( $raw_email_type, $emails ) ) {
    40564065        $redirect_to = wp_login_url();
    40574066        $result_msg  = __( 'Something has gone wrong.', 'buddypress' );
     
    40794088        }
    40804089
     4090    // This is an unsubscribe request from a nonmember.
     4091    } else if ( $raw_user_email ) {
     4092        // Unsubscribe.
     4093        if ( bp_user_has_opted_out() ) {
     4094            $result_msg = $emails[ $raw_email_type ]['unsubscribe']['message'];
     4095            $unsub_msg  = __( 'You have already unsubscribed from all communication from this site.', 'buddypress' );
     4096        } else {
     4097            $optout_args = array(
     4098                'email_address' => $raw_user_email,
     4099                'user_id'       => $raw_member_id,
     4100                'email_type'    => $raw_email_type,
     4101                'date_modified' => bp_core_current_time(),
     4102            );
     4103            bp_add_optout( $optout_args );
     4104            $result_msg = $emails[ $raw_email_type ]['unsubscribe']['message'];
     4105            $unsub_msg  = __( 'You have been unsubscribed.', 'buddypress' );
     4106        }
     4107
     4108    // This is an unsubscribe request from a current member.
    40814109    } else {
    40824110        if ( bp_is_active( 'settings' ) ) {
     
    40984126    }
    40994127
    4100     $message = sprintf(
    4101         '%1$s <a href="%2$s">%3$s</a>',
    4102         $result_msg,
    4103         esc_url( $redirect_to ),
    4104         esc_html( $unsub_msg )
    4105     );
    4106 
    4107     bp_core_add_message( $message );
    4108     bp_core_redirect( bp_core_get_user_domain( $raw_user_id ) );
    4109 
    4110     exit;
     4128    if ( $raw_user_id && $redirect_to ) {
     4129        $message = sprintf(
     4130            '%1$s <a href="%2$s">%3$s</a>',
     4131            $result_msg,
     4132            esc_url( $redirect_to ),
     4133            esc_html( $unsub_msg )
     4134        );
     4135
     4136        // Template notices are only displayed on BP pages.
     4137        bp_core_add_message( $message );
     4138        bp_core_redirect( bp_core_get_user_domain( $raw_user_id ) );
     4139
     4140        exit;
     4141    } else {
     4142        wp_die(
     4143            sprintf( '%1$s %2$s', esc_html( $unsub_msg ), esc_html( $result_msg ) ),
     4144            esc_html( $unsub_msg ),
     4145            array(
     4146                'link_url'  => home_url(),
     4147                'link_text' => __( 'Go to website\'s home page.', 'buddypress' ),
     4148            )
     4149        );
     4150    }
    41114151}
    41124152
     
    41424182    }
    41434183
    4144     $link = add_query_arg(
    4145         array(
    4146             'action' => 'unsubscribe',
    4147             'nh'     => hash_hmac( 'sha1', "{$email_type}:{$user_id}", bp_email_get_salt() ),
    4148             'nt'     => $args['notification_type'],
    4149             'uid'    => $user_id,
    4150         ),
    4151         $redirect_to
    4152     );
     4184    $link = '';
     4185    // Case where the recipient is a member of the site.
     4186    if ( ! empty( $user_id ) ) {
     4187        $link = add_query_arg(
     4188            array(
     4189                'action' => 'unsubscribe',
     4190                'nh'     => hash_hmac( 'sha1', "{$email_type}:{$user_id}", bp_email_get_salt() ),
     4191                'nt'     => $args['notification_type'],
     4192                'uid'    => $user_id,
     4193            ),
     4194            $redirect_to
     4195        );
     4196
     4197    // Case where the recipient is not a member of the site.
     4198    } else if ( ! empty( $args['email_address'] ) ) {
     4199        $email_address = $args['email_address'];
     4200        $member_id     = (int) $args['member_id'];
     4201        $link          = add_query_arg(
     4202            array(
     4203                'action' => 'unsubscribe',
     4204                'nh'     => hash_hmac( 'sha1', "{$email_type}:{$email_address}", bp_email_get_salt() ),
     4205                'nt'     => $args['notification_type'],
     4206                'mid'    => $member_id,
     4207                'uem'    => $email_address,
     4208            ),
     4209            $redirect_to
     4210        );
     4211    }
    41534212
    41544213    /**
     
    43624421
    43634422/**
     4423 * Check an email address to see if that individual has opted out.
     4424 *
     4425 * @since 8.0.0
     4426 *
     4427 * @param string $email_address Email address to check.
     4428 * @return bool True if the user has opted out, false otherwise.
     4429 */
     4430function bp_user_has_opted_out( $email_address = '' ) {
     4431    $optout_class = new BP_Optout();
     4432    $optout_id    = $optout_class->optout_exists(
     4433        array(
     4434            'email_address' => $email_address,
     4435        )
     4436    );
     4437    return (bool) $optout_id;
     4438}
     4439
     4440/**
    43644441 * Delete a BP_Optout by ID.
    43654442 *
Note: See TracChangeset for help on using the changeset viewer.