Index: /trunk/src/bp-core/bp-core-filters.php
===================================================================
--- /trunk/src/bp-core/bp-core-filters.php (revision 12911)
+++ /trunk/src/bp-core/bp-core-filters.php (revision 12912)
@@ -1068,10 +1068,19 @@
// Add 'List-Unsubscribe' header if applicable.
if ( ! empty( $tokens['unsubscribe'] ) && $tokens['unsubscribe'] !== wp_login_url() ) {
- $user = get_user_by( 'email', $tokens['recipient.email'] );
-
- $link = bp_email_get_unsubscribe_link( array(
- 'user_id' => $user->ID,
+ $user = get_user_by( 'email', $tokens['recipient.email'] );
+ $user_id = isset( $user->ID ) ? $user->ID : 0;
+
+ $args = array(
+ 'user_id' => $user_id,
'notification_type' => $email->get( 'type' ),
- ) );
+ );
+
+ // If this email is not to a current member, include the nonmember's email address and the Inviter ID.
+ if ( ! $user_id ) {
+ $args['email_address'] = $tokens['recipient.email'];
+ $args['member_id'] = bp_loggedin_user_id();
+ }
+
+ $link = bp_email_get_unsubscribe_link( $args );
if ( ! empty( $link ) ) {
Index: /trunk/src/bp-core/bp-core-functions.php
===================================================================
--- /trunk/src/bp-core/bp-core-functions.php (revision 12911)
+++ /trunk/src/bp-core/bp-core-functions.php (revision 12912)
@@ -4050,8 +4050,17 @@
$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() );
+ $raw_user_email = ! empty( $_GET['uem'] ) ? $_GET['uem'] : '';
+ $raw_member_id = ! empty( $_GET['mid'] ) ? absint( $_GET['mid'] ) : 0;
+ $redirect_to = '';
+
+ $new_hash = '';
+ if ( ! empty( $raw_user_id ) ) {
+ $new_hash = hash_hmac( 'sha1', "{$raw_email_type}:{$raw_user_id}", bp_email_get_salt() );
+ } else if ( ! empty( $raw_user_email ) ) {
+ $new_hash = hash_hmac( 'sha1', "{$raw_email_type}:{$raw_user_email}", bp_email_get_salt() );
+ }
// Check required values.
- if ( ! $raw_user_id || ! $raw_email_type || ! $raw_hash || ! array_key_exists( $raw_email_type, $emails ) ) {
+ if ( ( ! $raw_user_id && ! $raw_user_email ) || ! $raw_email_type || ! $raw_hash || ! array_key_exists( $raw_email_type, $emails ) ) {
$redirect_to = wp_login_url();
$result_msg = __( 'Something has gone wrong.', 'buddypress' );
@@ -4079,4 +4088,23 @@
}
+ // This is an unsubscribe request from a nonmember.
+ } else if ( $raw_user_email ) {
+ // Unsubscribe.
+ if ( bp_user_has_opted_out() ) {
+ $result_msg = $emails[ $raw_email_type ]['unsubscribe']['message'];
+ $unsub_msg = __( 'You have already unsubscribed from all communication from this site.', 'buddypress' );
+ } else {
+ $optout_args = array(
+ 'email_address' => $raw_user_email,
+ 'user_id' => $raw_member_id,
+ 'email_type' => $raw_email_type,
+ 'date_modified' => bp_core_current_time(),
+ );
+ bp_add_optout( $optout_args );
+ $result_msg = $emails[ $raw_email_type ]['unsubscribe']['message'];
+ $unsub_msg = __( 'You have been unsubscribed.', 'buddypress' );
+ }
+
+ // This is an unsubscribe request from a current member.
} else {
if ( bp_is_active( 'settings' ) ) {
@@ -4098,15 +4126,27 @@
}
- $message = sprintf(
- '%1$s %3$s',
- $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;
+ if ( $raw_user_id && $redirect_to ) {
+ $message = sprintf(
+ '%1$s %3$s',
+ $result_msg,
+ esc_url( $redirect_to ),
+ esc_html( $unsub_msg )
+ );
+
+ // Template notices are only displayed on BP pages.
+ bp_core_add_message( $message );
+ bp_core_redirect( bp_core_get_user_domain( $raw_user_id ) );
+
+ exit;
+ } else {
+ wp_die(
+ sprintf( '%1$s %2$s', esc_html( $unsub_msg ), esc_html( $result_msg ) ),
+ esc_html( $unsub_msg ),
+ array(
+ 'link_url' => home_url(),
+ 'link_text' => __( 'Go to website\'s home page.', 'buddypress' ),
+ )
+ );
+ }
}
@@ -4142,13 +4182,32 @@
}
- $link = add_query_arg(
- array(
- 'action' => 'unsubscribe',
- 'nh' => hash_hmac( 'sha1', "{$email_type}:{$user_id}", bp_email_get_salt() ),
- 'nt' => $args['notification_type'],
- 'uid' => $user_id,
- ),
- $redirect_to
- );
+ $link = '';
+ // Case where the recipient is a member of the site.
+ if ( ! empty( $user_id ) ) {
+ $link = add_query_arg(
+ array(
+ 'action' => 'unsubscribe',
+ 'nh' => hash_hmac( 'sha1', "{$email_type}:{$user_id}", bp_email_get_salt() ),
+ 'nt' => $args['notification_type'],
+ 'uid' => $user_id,
+ ),
+ $redirect_to
+ );
+
+ // Case where the recipient is not a member of the site.
+ } else if ( ! empty( $args['email_address'] ) ) {
+ $email_address = $args['email_address'];
+ $member_id = (int) $args['member_id'];
+ $link = add_query_arg(
+ array(
+ 'action' => 'unsubscribe',
+ 'nh' => hash_hmac( 'sha1', "{$email_type}:{$email_address}", bp_email_get_salt() ),
+ 'nt' => $args['notification_type'],
+ 'mid' => $member_id,
+ 'uem' => $email_address,
+ ),
+ $redirect_to
+ );
+ }
/**
@@ -4362,4 +4421,22 @@
/**
+ * Check an email address to see if that individual has opted out.
+ *
+ * @since 8.0.0
+ *
+ * @param string $email_address Email address to check.
+ * @return bool True if the user has opted out, false otherwise.
+ */
+function bp_user_has_opted_out( $email_address = '' ) {
+ $optout_class = new BP_Optout();
+ $optout_id = $optout_class->optout_exists(
+ array(
+ 'email_address' => $email_address,
+ )
+ );
+ return (bool) $optout_id;
+}
+
+/**
* Delete a BP_Optout by ID.
*