Skip to content

Commit

Permalink
Rework private reply notifications to fix confict with latest version…
Browse files Browse the repository at this point in the history
… of bbPress
  • Loading branch information
pippinsplugins committed Jun 16, 2014
1 parent 86ee168 commit e842d98
Showing 1 changed file with 69 additions and 10 deletions.
79 changes: 69 additions & 10 deletions bbp-private-replies.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function __construct() {
add_filter( 'the_excerpt', array( $this, 'hide_reply' ), 999 );

// prevent private replies from being sent in email subscriptions
add_filter( 'bbp_subscription_mail_message', array( $this, 'prevent_subscription_email' ), 10, 4 );
add_filter( 'bbp_subscription_mail_message', array( $this, 'prevent_subscription_email' ), 10, 3 );

// add a class name indicating the read status
add_filter( 'post_class', array( $this, 'reply_post_class' ) );
Expand Down Expand Up @@ -205,22 +205,81 @@ public function hide_reply( $content = '', $reply_id = 0 ) {
* @param $message string The email message
* @param $reply_id int The ID of the reply
* @param $topic_id int The ID of the reply's topic
* @param $user_id int The ID of the user receiving the notification
*
* @return mixed
*/
public function prevent_subscription_email( $message, $reply_id, $topic_id, $user_id ) {
public function prevent_subscription_email( $message, $reply_id, $topic_id ) {

if( ! $this->is_private( $reply_id ) )
return $message; // reply isn't private so return message unchanged
if( $this->is_private( $reply_id ) ) {
$this->subscription_email( $message, $reply_id, $topic_id );
return false;
}

return $message; // message unchanged
}


/**
* Sends the new reply notification email to moderators on private replies
*
* @since 1.2
*
* @param $message string The email message
* @param $reply_id int The ID of the reply
* @param $topic_id int The ID of the reply's topic
*
* @return void
*/
public function subscription_email( $message, $reply_id, $topic_id ) {

$topic_author = bbp_get_topic_author_id( $topic_id );
$reply_author = bbp_get_reply_author_id( $reply_id );
if( ! $this->is_private( $reply_id ) ) {

if( ( $topic_author != $user_id || $topic_author != $reply_author ) && $reply_author != $user_id && ! user_can( $user_id, 'moderate' ) )
return false; // this prevents the email from getting sent
return false; // reply isn't private so do nothing

return $message; // message unchanged
}

$topic_author = bbp_get_topic_author_id( $topic_id );
$reply_author = bbp_get_reply_author_id( $reply_id );
$reply_author_name = bbp_get_reply_author_display_name( $reply_id );

// Strip tags from text and setup mail data
$topic_title = strip_tags( bbp_get_topic_title( $topic_id ) );
$reply_content = strip_tags( bbp_get_reply_content( $reply_id ) );
$reply_url = bbp_get_reply_url( $reply_id );
$blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
$do_not_reply = '<noreply@' . ltrim( get_home_url(), '^(http|https)://' ) . '>';

$subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id );

// Array to hold BCC's
$headers = array();

// Setup the From header
$headers[] = 'From: ' . get_bloginfo( 'name' ) . ' ' . $do_not_reply;

// Get topic subscribers and bail if empty
$user_ids = bbp_get_topic_subscribers( $topic_id, true );
if ( empty( $user_ids ) ) {
return false;
}

// Loop through users
foreach ( (array) $user_ids as $user_id ) {

// Don't send notifications to the person who made the post
if ( ! empty( $reply_author ) && (int) $user_id === (int) $reply_author ) {
continue;
}

if( user_can( $user_id, 'moderate' ) || (int) $topic_author === (int) $user_id ) {

// Get email address of subscribed user
$headers[] = 'Bcc: ' . get_userdata( $user_id )->user_email;

}
}

wp_mail( $do_not_reply, $subject, $message, $headers );
}


Expand Down

2 comments on commit e842d98

@shazahm1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this breaks private notifications being sent for users of Mandrill since Mandrill does not support the bcc header.
ref: http://bbpress.org/forums/topic/problem-with-the-default-do-not-reply-email-address/#post-147950

Probably a simple solution to address this would be to add an action hook which can be used to override this function (like WP core does quite often) so a user such as myself can override this and revert to the one email per subscriber method like emails used to be sent by bbPress.

Maybe inserting something like this on line 265:

$override = apply_filters( 'bbp_private_override', NULL, $user_ids );
if ( null !== $override )
    return $override;

Actually, now that I've written this out ... I might as well simple unhook this function and hook in my own mandril compatible function...

@frankwarwick
Copy link

@frankwarwick frankwarwick commented on e842d98 Aug 4, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.