Skip to content

Commit

Permalink
Add "Message-ID" email header to outgoing reply-by-emails.
Browse files Browse the repository at this point in the history
This will allow proper email threading to occur in email clients that
support it.

To prepare the "Message-ID" header, a new listener property - "reply_to_id"
- is added to determine the uniqueness of the outgoing email.  The
"reply_to_id" is generally the post ID of whatever is creating the email.

See #49.
  • Loading branch information
r-a-y committed Jun 12, 2014
1 parent 6b5a1b9 commit bce67d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
33 changes: 27 additions & 6 deletions bp-rbe-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ public function wp_mail_filter( $args ) {
// Don't like this? there's a filter for that!
$querystring = apply_filters( 'bp_rbe_encode_querystring', bp_rbe_encode( array( 'string' => $querystring ) ), $querystring );

// Set the 'Message-Id' header
// This is used to enable message threading in email clients
if ( ! empty( $listener->reply_to_id ) ) {
$reply_to_id = $listener->reply_to_id;
} elseif ( ! empty( $listener->secondary_item_id ) ) {
$reply_to_id = $listener->secondary_item_id;
} else {
$reply_to_id = $listener->item_id;
}
$args['headers'] .= 'Message-ID: <' . md5( $querystring . $reply_to_id ) . '@' . esc_url( $_SERVER['SERVER_NAME'] ) . '>' . PHP_EOL;

// Inject the querystring into the email address
$args['headers'] .= 'Reply-To: ' . bp_rbe_inject_qs_in_email( $querystring ) . PHP_EOL;

Expand Down Expand Up @@ -349,6 +360,11 @@ public function activity_listener( $item ) {
break;
}

// reply-to id fallback
if ( empty( $this->listener->reply_to_id ) ) {
$this->listener->reply_to_id = ! empty( $this->listener->secondary_item_id ) ? $this->listener->secondary_item_id : $this->listener->item_id;
}

}

/**
Expand Down Expand Up @@ -389,6 +405,9 @@ public function group_forum_listener( $post_id ) {
// @see BP_Reply_By_Email::get_temporary_variables()
// @see BP_Reply_By_Email::set_group_id()
$this->listener->secondary_item_id = bp_get_current_group_id();

// reply-to ID
$this->listener->reply_to_id = $post_id;
}

/**
Expand All @@ -403,9 +422,10 @@ public function message_listener( $item ) {

$this->listener = new stdClass;

$this->listener->component = $bp->messages->id;
$this->listener->item_id = $item->thread_id;
$this->listener->user_id = $item->sender_id;
$this->listener->component = $bp->messages->id;
$this->listener->item_id = $item->thread_id;
$this->listener->user_id = $item->sender_id;
$this->listener->reply_to_id = $item->id;
}

/**
Expand All @@ -427,8 +447,9 @@ public function message_listener( $item ) {
public function bbp_listener( $reply_id, $topic_id ) {
$this->listener = new stdClass;

$this->listener->component = 'bbpress';
$this->listener->item_id = $topic_id;
$this->listener->component = 'bbpress';
$this->listener->item_id = $topic_id;
$this->listener->reply_to_id = $reply_id;
}

/**
Expand Down Expand Up @@ -687,4 +708,4 @@ public function disable_non_rbe_notice( $retval, $args ) {

return $retval;
}
}
}
3 changes: 3 additions & 0 deletions includes/bp-rbe-extend-bbpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public function extend_activity_listener( $listener, $item ) {
// setup our secondary item ID, which is the topic ID
$listener->secondary_item_id = $item->secondary_item_id;

// setup reply-to ID, which is the post ID
$listener->reply_to_id = $item->secondary_item_id;

// if activity type is a bbPress reply, we need to grab the topic ID manually
if ( $item->type == $this->activity_type ) {
global $bp;
Expand Down

0 comments on commit bce67d5

Please sign in to comment.