bp_email_unsubscribe_handler()

Handles unsubscribing user from notification emails.

Description

Source

File: bp-core/bp-core-functions.php

4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
function bp_email_unsubscribe_handler() {
    $emails         = bp_email_get_unsubscribe_type_schema();
    $raw_email_type = ! empty( $_GET['nt'] ) ? $_GET['nt'] : '';
    $raw_hash       = ! empty( $_GET['nh'] ) ? $_GET['nh'] : '';
    $raw_user_id    = ! empty( $_GET['uid'] ) ? absint( $_GET['uid'] ) : 0;
    $new_hash       = hash_hmac( 'sha1', "{$raw_email_type}:{$raw_user_id}", bp_email_get_salt() );
 
    // Check required values.
    if ( ! $raw_user_id || ! $raw_email_type || ! $raw_hash || ! array_key_exists( $raw_email_type, $emails ) ) {
        $redirect_to = wp_login_url();
        $result_msg  = __( 'Something has gone wrong.', 'buddyboss' );
        $unsub_msg   = __( 'Please log in and go to your settings to unsubscribe from notification emails.', 'buddyboss' );
 
    // Check valid hash.
    } elseif ( ! hash_equals( $new_hash, $raw_hash ) ) {
        $redirect_to = wp_login_url();
        $result_msg  = __( 'Something has gone wrong.', 'buddyboss' );
        $unsub_msg   = __( 'Please log in and go to your settings to unsubscribe from notification emails.', 'buddyboss' );
 
    // Don't let authenticated users unsubscribe other users' email notifications.
    } elseif ( is_user_logged_in() && get_current_user_id() !== $raw_user_id ) {
        $result_msg  = __( 'Something has gone wrong.', 'buddyboss' );
        $unsub_msg   = __( 'Please go to your notifications settings to unsubscribe from emails.', 'buddyboss' );
 
        if ( bp_is_active( 'settings' ) ) {
            $redirect_to = sprintf(
                '%s%s/notifications/',
                bp_core_get_user_domain( get_current_user_id() ),
                bp_get_settings_slug()
            );
        } else {
            $redirect_to = bp_core_get_user_domain( get_current_user_id() );
        }
 
    } else {
        if ( bp_is_active( 'settings' ) ) {
            $redirect_to = sprintf(
                '%s%s/notifications/',
                bp_core_get_user_domain( $raw_user_id ),
                bp_get_settings_slug()
            );
        } else {
            $redirect_to = bp_core_get_user_domain( $raw_user_id );
        }
 
        // Unsubscribe.
        $meta_key = $emails[ $raw_email_type ]['unsubscribe']['meta_key'];
        bp_update_user_meta( $raw_user_id, $meta_key, 'no' );
 
        $result_msg = $emails[ $raw_email_type ]['unsubscribe']['message'];
        $unsub_msg  = __( 'You can change this or any other email notification preferences in your email settings.', 'buddyboss' );
    }
 
    $message = sprintf(
        '%1$s <a href="%2$s">%3$s</a>',
        $result_msg,
        esc_url( $redirect_to ),
        esc_html( $unsub_msg )
    );
 
    bp_core_add_message( $message );
    bp_core_redirect( bp_core_get_user_domain( $raw_user_id ) );
 
    exit;
}

Changelog

Changelog
Version Description
BuddyPress 2.7.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.