Skip to:
Content

BuddyPress.org

Ticket #6932: 6932.diff

File 6932.diff, 19.8 KB (added by tharsheblows, 9 years ago)

adds in unsubscribe tokens to unsubscribable emails

  • src/bp-activity/bp-activity-notifications.php

     
    4040 * @param int $receiver_user_id The ID of the user who is receiving the update.
    4141 */
    4242function bp_activity_at_message_notification( $activity_id, $receiver_user_id ) {
     43
    4344        $notifications = BP_Core_Notification::get_all_for_user( $receiver_user_id, 'all' );
    4445
    4546        // Don't leave multiple notifications for the same activity item.
     
    7374                        $group_name = bp_get_current_group_name();
    7475                }
    7576
     77                $activity_link = bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/';
     78                $unsubscribe_args = array(
     79                                                'uid' => (int)$receiver_user_id,
     80                                                'nt' => 'notification_activity_new_mention'
     81                                        );
     82                $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $activity_link, $unsubscribe_args ) );
     83
    7684                $args = array(
    7785                        'tokens' => array(
    7886                                'activity'         => $activity,
     
    8189                                'mentioned.url'    => $message_link,
    8290                                'poster.name'      => $poster_name,
    8391                                'receiver-user.id' => $receiver_user_id,
     92                                'unsubscribe'      => $unsubscribe_link
    8493                        ),
    8594                );
    8695
     
    152161
    153162                // Send an email if the user hasn't opted-out.
    154163                if ( 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
     164
     165                        $activity_link = bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/';
     166                        $unsubscribe_args = array(
     167                                'uid' => (int)$original_activity->user_id,
     168                                'nt' => 'notification_activity_new_reply'
     169                        );
     170
     171                        $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $activity_link, $unsubscribe_args ) );
    155172                        $args = array(
    156173                                'tokens' => array(
    157174                                        'comment.id'                => $comment_id,
     
    160177                                        'original_activity.user_id' => $original_activity->user_id,
    161178                                        'poster.name'               => $poster_name,
    162179                                        'thread.url'                => esc_url( $thread_link ),
     180                                        'unsubscribe'                           => $unsubscribe_link
    163181                                ),
    164182                        );
    165183
  • src/bp-core/bp-core-actions.php

     
    108108
    109109// Activation redirect.
    110110add_action( 'bp_activation', 'bp_add_activation_redirect' );
     111
     112// Email unsubscribe.
     113add_action( 'bp_get_request_unsubscribe', 'bp_emails_unsubscribe' );
     114
  • src/bp-core/bp-core-filters.php

     
    10241024        $tokens['recipient.email']     = '';
    10251025        $tokens['recipient.name']      = '';
    10261026        $tokens['recipient.username']  = '';
    1027         $tokens['unsubscribe']         = site_url( 'wp-login.php' );
    10281027
    10291028
    10301029        // Who is the email going to?
     
    10411040                }
    10421041
    10431042                if ( $user_obj ) {
    1044                         // Unsubscribe link.
    1045                         $tokens['unsubscribe'] = esc_url( sprintf(
    1046                                 '%s%s/notifications/',
    1047                                 bp_core_get_user_domain( $user_obj->ID ),
    1048                                 function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings'
    1049                         ) );
    10501043                        $tokens['recipient.username'] = $user_obj->user_login;
    10511044                }
    10521045        }
  • src/bp-core/bp-core-functions.php

     
    28982898 *                       on the email delivery class you are using.
    28992899 */
    29002900function bp_send_email( $email_type, $to, $args = array() ) {
     2901
    29012902        static $is_default_wpmail = null;
    29022903        static $wp_html_emails    = null;
    29032904
     
    29542955
    29552956        if ( $must_use_wpmail ) {
    29562957                $to = $email->get( 'to' );
    2957 
    29582958                return wp_mail(
    29592959                        array_shift( $to )->get_address(),
    29602960                        $email->get( 'subject', 'replace-tokens' ),
     
    29892989        }
    29902990
    29912991        $delivery = new $delivery_class();
     2992
    29922993        $status   = $delivery->bp_email( $email );
    29932994
    29942995        if ( is_wp_error( $status ) ) {
     
    32923293                'groups-membership-request-rejected' => __( 'Recipient had requested to join a group, which was rejected.', 'buddypress' ),
    32933294        );
    32943295}
     3296
     3297/**
     3298 * Handles unsubscribe action to unsubscribe user from notification emails.
     3299 *
     3300 * @since
     3301 *
     3302 */
     3303function bp_emails_unsubscribe(){
     3304
     3305        $request = $_GET;
     3306        $admin_email   = get_bloginfo( 'admin_email' );
     3307        $notifications_with_unsubscribe = array(
     3308                'notification_activity_new_mention'             => __( 'You will no longer receive emails when someone mentions you in an update.', 'buddypress' ),
     3309                'notification_activity_new_reply'               => __( 'You will no longer receive emails when someone replies to an update or comment you posted.', 'buddypress' ),
     3310                'notification_messages_new_message'             => __( 'You will no longer receive emails when someone sends you a message.', 'buddypress' ),
     3311                'notification_friends_friendship_request'       => __( 'You will no longer receive emails when someone sends you a friend request.', 'buddypress' ),
     3312                'notification_friends_friendship_accepted'      => __( 'You will no longer receive emails when someone accepts your friendship request.', 'buddypress' ),
     3313                'notification_groups_invite'                    => __( 'You will no longer receive emails when you are invited to join a group.', 'buddypress' ),
     3314                'notification_groups_group_updated'             => __( 'You will no longer receive emails when one of your groups is updated.', 'buddypress' ),
     3315                'notification_groups_membership_request'        => __( 'You will no longer receive emails when someone requests to be a member of your group.', 'buddypress' ),
     3316                'notification_membership_request_completed'     => __( 'You will no longer receive emails when your request to join a group has been accepted or denied.', 'buddypress' ),
     3317                'notification_groups_admin_promotion'           => __( 'You will no longer receive emails when you have been promoted in a group.', 'buddypress' )
     3318        );
     3319
     3320        $user_id = ( isset( $request['uid'] ) && !empty( $request['uid'] ) ) ? $request['uid'] : '';
     3321        $notification_type = ( isset( $request['nt'] ) && !empty( $request['nt'] ) ) ? $request['nt'] : '';
     3322        $to_check = ( isset( $request['nn'] ) && !empty( $request['nn'] ) ) ? $request['nn'] : '';
     3323
     3324        $check_args = array(
     3325                'uid' => $user_id,
     3326                'nt'  => $notification_type
     3327        );
     3328
     3329        $check = bp_check( $check_args );
     3330
     3331        $current_user = is_user_logged_in() ? wp_get_current_user() : false;
     3332
     3333        if( empty( $user_id ) || empty( $notification_type ) || empty( $to_check ) || ! array_key_exists( $notification_type, $notifications_with_unsubscribe ) ){
     3334
     3335                $settings_link = site_url( 'wp-login.php' );
     3336                $result_message = __( 'Sorry, something has gone wrong.', 'buddypress' );
     3337                $how_to_unsubscribe_message = __( 'Please log in and go to your settings to unsubscribe from notifications.', 'buddypress' );
     3338        }
     3339        elseif(  ! hash_equals( $check, $to_check ) ){
     3340
     3341                $settings_link = site_url( 'wp-login.php' );
     3342                $result_message = __( 'Sorry the security check failed.', 'buddypress' );
     3343                $how_to_unsubscribe_message = __( 'Please log in and go to your settings to unsubscribe from notifications.', 'buddypress' );
     3344
     3345        }
     3346        elseif( is_user_logged_in() && (int)$current_user->ID !== (int)$user_id ){
     3347               
     3348                $settings_link = esc_url( sprintf(
     3349                        '%s%s/notifications/',
     3350                        bp_core_get_user_domain( (int)$current_user->ID ),
     3351                        function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings'
     3352                ) );
     3353
     3354                $result_message = __( 'Something has gone wrong.', 'buddypress' );
     3355                $how_to_unsubscribe_message = __( 'Please go to your settings to unsubscribe from notifications.', 'buddypress' );
     3356        }
     3357        else{
     3358               
     3359                $settings_link = esc_url( sprintf(
     3360                        '%s%s/notifications/',
     3361                        bp_core_get_user_domain( $user_id ),
     3362                        function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings'
     3363                ) );
     3364
     3365                // unsubscribe them by setting the usermeta to 'no'
     3366                $is_unsubscribed = bp_update_user_meta( $user_id, $notification_type, 'no' );
     3367                $result_message = ( empty( $is_unsubscribed ) && bp_get_user_meta( $user_id, $notification_type, true ) !== 'no' ) ? __( 'Sorry, something has gone wrong with unsubscribing you from these emails.', 'buddypress' ) : $notifications_with_unsubscribe[ $notification_type ];
     3368                $how_to_unsubscribe_message = __( 'You can change this or any other email notification preferences in your email settings.', 'buddypress' );
     3369        }
     3370
     3371        $message = sprintf(
     3372                '%1$s <a href="%2$s">%3$s</a>',
     3373                $result_message,
     3374                $settings_link,
     3375                $how_to_unsubscribe_message
     3376        );
     3377
     3378        bp_core_add_message( $message );
     3379        bp_core_redirect( esc_url( remove_query_arg( array_keys( $request ) ) ) );
     3380}
     3381
     3382/**
     3383 * Creates unsubscribe link for notification emails.
     3384 *
     3385 * @since
     3386 *
     3387 * @param string        $link   URL to which the unsubscribe query string is appended.
     3388 * @param array         $args   Used to build unsubscribe query string.
     3389 *
     3390 * @return string The unsubscribe link.
     3391 */
     3392function bp_email_get_unsubscribe_link( $link = null, $args = null ){
     3393
     3394        if( ! is_array( $args ) ){
     3395                return site_url( 'wp-login.php' );     
     3396        }
     3397
     3398        $check = bp_check( $args );
     3399
     3400        $args['nn'] = $check;
     3401        $args['action'] = 'unsubscribe';
     3402
     3403        $unsubscribe_link = esc_url( add_query_arg( $args, $link ) );
     3404
     3405        $args['link'] = esc_url( $link );
     3406
     3407        /**
     3408         * Filters the unsubscribe link.
     3409         *
     3410         * @since
     3411         *
     3412         * @param string        $link   URL to which the unsubscribe query string is appended.
     3413         * @param array         $args   Used to build unsubscribe query string.
     3414         */
     3415        return apply_filters( 'bp_email_get_unsubscribe_link', $unsubscribe_link, $args );
     3416}
     3417
  • src/bp-core/bp-core-options.php

     
    893893         */
    894894        return apply_filters( 'bp_get_theme_package_id', bp_get_option( '_bp_theme_package_id', $default ) );
    895895}
     896
     897/**
     898 * Get a persistent salt not dependent on salts in wp-config.php.
     899 *
     900 * @return string
     901 *
     902 */
     903function bp_get_salt(){
     904
     905        $salt = bp_get_option( 'bp_persistent_salt' );
     906        if( ! $salt ){
     907                $salt = base64_encode( mcrypt_create_iv(12, MCRYPT_DEV_URANDOM) );
     908                bp_add_option( 'bp_persistent_salt', $salt );
     909        }
     910
     911        return $salt;
     912}
     913
     914/**
     915 * Get a function that makes a check for the given args.
     916 *
     917 * @param  array        $args   Any array.
     918 *
     919 * @return string
     920 *
     921 */
     922function bp_check( $args ){
     923
     924        $salt = bp_get_salt();
     925        $string = ( is_array( $args) ) ? implode( ':', $args ) : (string)$args;
     926
     927        // should this return a substring like the nonce does? It's quite long.
     928        $check = hash_hmac( 'md5', $string, $salt );
     929
     930        return $check;
     931}
  • src/bp-friends/bp-friends-notifications.php

     
    3232                return;
    3333        }
    3434
     35        $friends_link = trailingslashit( esc_url( bp_core_get_user_domain( (int)$friend_id ) .  bp_get_friends_slug() ) );
     36        $unsubscribe_args = array(
     37                                        'uid' => (int)$friend_id,
     38                                        'nt' => 'notification_friends_friendship_request'
     39                                );
     40        $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $friends_link, $unsubscribe_args ) );
     41
    3542        $args = array(
    3643                'tokens' => array(
    3744                        'friend-requests.url' => esc_url( bp_core_get_user_domain( $friend_id ) . bp_get_friends_slug() . '/requests/' ),
     
    4047                        'initiator.id'        => $initiator_id,
    4148                        'initiator.url'       => esc_url( bp_core_get_user_domain( $initiator_id ) ),
    4249                        'initiator.name'      => bp_core_get_user_displayname( $initiator_id ),
     50                        'unsubscribe'             => $unsubscribe_link
    4351                ),
    4452        );
    4553        bp_send_email( 'friends-request', $friend_id, $args );
     
    6371                return;
    6472        }
    6573
     74        $friends_link = trailingslashit( esc_url( bp_core_get_user_domain( (int)$initiator_id ) .  bp_get_friends_slug() ) );
     75        $unsubscribe_args = array(
     76                                        'uid' => (int)$initiator_id,
     77                                        'nt' => 'notification_friends_friendship_accepted'
     78                                );
     79        $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $friends_link, $unsubscribe_args ) );
     80
    6681        $args = array(
    6782                'tokens' => array(
    6883                        'friend.id'      => $friend_id,
     
    7085                        'friend.name'    => bp_core_get_user_displayname( $friend_id ),
    7186                        'friendship.id'  => $friendship_id,
    7287                        'initiator.id'   => $initiator_id,
     88                        'unsubscribe'    => $unsubscribe_link
    7389                ),
    7490        );
    7591        bp_send_email( 'friends-request-accepted', $initiator_id, $args );
  • src/bp-groups/bp-groups-notifications.php

     
    6868                        continue;
    6969                }
    7070
     71                $user_groups_link = bp_core_get_user_domain( (int)$user_id ) . bp_get_groups_slug();
     72                $unsubscribe_args = array(
     73                                                'uid' => (int)$user_id,
     74                                                'nt' => 'notification_groups_group_updated'
     75                                        );
     76                $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $user_groups_link, $unsubscribe_args ) );
     77
    7178                $args = array(
    7279                        'tokens' => array(
    7380                                'changed_text' => $changed_text,
     
    7582                                'group.id'     => $group_id,
    7683                                'group.url'    => esc_url( bp_get_group_permalink( $group ) ),
    7784                                'group.name'   => $group->name,
     85                                'unsubscribe'  => $unsubscribe_link
    7886                        ),
    7987                );
    8088                bp_send_email( 'groups-details-updated', (int) $user_id, $args );
     
    124132                return;
    125133        }
    126134
     135        $user_groups_link = bp_core_get_user_domain( (int)$admin_id ) . bp_get_groups_slug();
     136
     137        $unsubscribe_args = array(
     138                                        'uid' => (int)$admin_id,
     139                                        'nt' => 'notification_groups_membership_request'
     140                                );
     141        $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $user_groups_link, $unsubscribe_args ) );
     142
    127143        $group = groups_get_group( array( 'group_id' => $group_id ) );
    128144        $args  = array(
    129145                'tokens' => array(
     
    136152                        'profile.url'          => esc_url( bp_core_get_user_domain( $requesting_user_id ) ),
    137153                        'requesting-user.id'   => $requesting_user_id,
    138154                        'requesting-user.name' => bp_core_get_user_displayname( $requesting_user_id ),
     155                        'unsubscribe'              => $unsubscribe_link
    139156                ),
    140157        );
    141158        bp_send_email( 'groups-membership-request', (int) $admin_id, $args );
     
    172189                return;
    173190        }
    174191
     192        $user_groups_link = bp_core_get_user_domain( (int)$requesting_user_id ) . bp_get_groups_slug();
     193
     194        $unsubscribe_args = array(
     195                                        'uid' => (int)$requesting_user_id,
     196                                        'nt' => 'notification_membership_request_completed'
     197                                );
     198        $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $user_groups_link, $unsubscribe_args ) );
     199
    175200        $group = groups_get_group( array( 'group_id' => $group_id ) );
    176201        $args  = array(
    177202                'tokens' => array(
     
    180205                        'group.name'         => $group->name,
    181206                        'group.url'          => esc_url( bp_get_group_permalink( $group ) ),
    182207                        'requesting-user.id' => $requesting_user_id,
     208                        'unsubscribe'            => $unsubscribe_link
    183209                ),
    184210        );
    185211
     
    226252                return;
    227253        }
    228254
     255        $user_groups_link = bp_core_get_user_domain( (int)$user_id ) . bp_get_groups_slug();
     256
     257        $unsubscribe_args = array(
     258                                        'uid' => (int)$user_id,
     259                                        'nt' => 'notification_groups_admin_promotion'
     260                                );
     261        $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $user_groups_link, $unsubscribe_args ) );
     262
    229263        $group = groups_get_group( array( 'group_id' => $group_id ) );
    230264        $args  = array(
    231265                'tokens' => array(
     
    235269                        'group.name'  => $group->name,
    236270                        'promoted_to' => $promoted_to,
    237271                        'user.id'     => $user_id,
     272                        'unsubscribe' => $unsubscribe_link
    238273                ),
    239274        );
    240275        bp_send_email( 'groups-member-promoted', (int) $user_id, $args );
     
    276311                return;
    277312        }
    278313
    279         $invited_link = bp_core_get_user_domain( $invited_user_id ) . bp_get_groups_slug();
     314        $invited_link = bp_core_get_user_domain( (int)$invited_user_id ) . bp_get_groups_slug();
     315        $unsubscribe_args = array(
     316                                        'uid' => (int)$invited_user_id,
     317                                        'nt' => 'notification_groups_invite'
     318                                );
     319        $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $invited_link, $unsubscribe_args ) );
     320
    280321        $args         = array(
    281322                'tokens' => array(
    282323                        'group'        => $group,
     
    286327                        'inviter.url'  => bp_core_get_user_domain( $inviter_user_id ),
    287328                        'inviter.id'   => $inviter_user_id,
    288329                        'invites.url'  => esc_url( $invited_link . '/invites/' ),
     330                        'unsubscribe'  => $unsubscribe_link
    289331                ),
    290332        );
    291333        bp_send_email( 'groups-invitation', (int) $invited_user_id, $args );
  • src/bp-groups/bp-groups-screens.php

     
    14231423                $group_promo   = 'yes';
    14241424
    14251425        if ( !$group_request = bp_get_user_meta( bp_displayed_user_id(), 'notification_groups_membership_request', true ) )
    1426                 $group_request = 'yes'; ?>
     1426                $group_request = 'yes';
    14271427
     1428        if ( !$group_request_completed = bp_get_user_meta( bp_displayed_user_id(), 'notification_membership_request_completed', true ) )
     1429                $group_request_completed = 'yes'; ?>
     1430
    14281431        <table class="notification-settings" id="groups-notification-settings">
    14291432                <thead>
    14301433                        <tr>
     
    14601463                                <td class="yes"><input type="radio" name="notifications[notification_groups_membership_request]" id="notification-groups-membership-request-yes" value="yes" <?php checked( $group_request, 'yes', true ) ?>/><label for="notification-groups-membership-request-yes" class="bp-screen-reader-text"><?php _e( 'Yes, send email', 'buddypress' ); ?></label></td>
    14611464                                <td class="no"><input type="radio" name="notifications[notification_groups_membership_request]" id="notification-groups-membership-request-no" value="no" <?php checked( $group_request, 'no', true ) ?>/><label for="notification-groups-membership-request-no" class="bp-screen-reader-text"><?php _e( 'No, do not send email', 'buddypress' ); ?></label></td>
    14621465                        </tr>
     1466                        <tr id="groups-notification-settings-request-completed">
     1467                                <td></td>
     1468                                <td><?php _ex( 'Your request to join a group has been approved or denied', 'group settings on notification settings page', 'buddypress' ) ?></td>
     1469                                <td class="yes"><input type="radio" name="notifications[notification_membership_request_completed]" id="notification-groups-membership-request-completed-yes" value="yes" <?php checked( $group_request_completed, 'yes', true ) ?>/><label for="notification-groups-membership-request-completed-yes" class="bp-screen-reader-text"><?php _e( 'Yes, send email', 'buddypress' ); ?></label></td>
     1470                                <td class="no"><input type="radio" name="notifications[notification_membership_request_completed]" id="notification-groups-membership-request-completed-no" value="no" <?php checked( $group_request_completed, 'no', true ) ?>/><label for="notification-groups-membership-request-completed-no" class="bp-screen-reader-text"><?php _e( 'No, do not send email', 'buddypress' ); ?></label></td>
     1471                        </tr>
    14631472
    14641473                        <?php
    14651474
  • src/bp-messages/bp-messages-notifications.php

     
    5858                        continue;
    5959                }
    6060
     61                $messages_link = bp_get_root_domain() . '/' .  bp_get_activity_root_slug() . '/';
     62                $unsubscribe_args = array(
     63                                                'uid' => (int)$recipient->user_id,
     64                                                'nt' => 'notification_messages_new_message'
     65                                        );
     66                $unsubscribe_link = esc_url( bp_email_get_unsubscribe_link( $messages_link, $unsubscribe_args ) );
     67
    6168                $args = array(
    6269                        'tokens' => array(
    6370                                'usermessage' => wp_strip_all_tags( stripslashes( $message ) ),
     
    6471                                'message.url' => esc_url( bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() . '/view/' . $thread_id . '/' ),
    6572                                'sender.name' => $sender_name,
    6673                                'usersubject' => sanitize_text_field( stripslashes( $subject ) ),
     74                                'unsubscribe' => $unsubscribe_link
    6775                        ),
    6876                );
    6977                bp_send_email( 'messages-unread', $ud, $args );