Fix `comment_notification_recipients` filter behavior so that it is still respected even on comments left by the post author

The code was bailing on this-is-a-comment-on-your-own-post detection, ignoring additional recipients. Now:

* Logic check is done within `wp_notify_postauthor()`
* Logic check is overridable via `comment_notification_notify_author` filter (default still false)
* The code doesn't bail on comment-on-own-post detection, but just removes the author from the array
* The code instead now bails if the recipients list is empty, so `comment_notification_recipients` works properly

props ethitter.
fixes #25699


git-svn-id: https://develop.svn.wordpress.org/trunk@26367 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Mark Jaquith 2013-11-25 01:46:49 +00:00
parent 89d0be9d3d
commit 9cbffc9222
2 changed files with 58 additions and 30 deletions

View File

@ -1377,13 +1377,16 @@ function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment)
* See {@link http://core.trac.wordpress.org/ticket/9235}
*
* @since 1.5.0
* @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing
* @uses do_action() Calls 'comment_post' hook on $comment_ID returned from adding the comment and if the comment was approved.
* @uses wp_filter_comment() Used to filter comment before adding comment.
* @uses wp_allow_comment() checks to see if comment is approved.
* @uses wp_insert_comment() Does the actual comment insertion to the database.
*
* @param array $commentdata Contains information on the comment.
* @uses apply_filters()
* @uses wp_get_comment_status()
* @uses wp_filter_comment()
* @uses wp_allow_comment()
* @uses wp_insert_comment()
* @uses do_action()
* @uses wp_notify_moderator()
* @uses get_option()
* @uses wp_notify_postauthor()
* @return int The ID of the comment after adding.
*/
function wp_new_comment( $commentdata ) {
@ -1418,12 +1421,10 @@ function wp_new_comment( $commentdata ) {
wp_notify_moderator( $comment_ID );
}
if ( get_option('comments_notify') && $commentdata['comment_approved'] ) {
$post = get_post( $commentdata['comment_post_ID'] );
// Don't notify if it's your own comment
if ( ! isset( $commentdata['user_id'] ) || $post->post_author != $commentdata['user_id'] ) {
wp_notify_postauthor( $comment_ID );
}
// wp_notify_postauthor() checks if notifying the author of their own comment.
// By default, it won't, but filters can override this.
if ( get_option( 'comments_notify' ) && $commentdata['comment_approved'] ) {
wp_notify_postauthor( $comment_ID );
}
}
@ -1457,7 +1458,7 @@ function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
case '1':
$status = '1';
if ( get_option('comments_notify') ) {
wp_notify_postauthor( $comment_id );
wp_notify_postauthor( $comment_id );
}
break;
case 'spam':

View File

@ -1000,13 +1000,23 @@ endif;
if ( ! function_exists('wp_notify_postauthor') ) :
/**
* Notify an author of a comment/trackback/pingback to one of their posts.
* Notify an author (and/or others) of a comment/trackback/pingback on a post.
*
* @since 1.0.0
*
* @param int $comment_id Comment ID
* @param string $deprecated Not used
* @return bool False if user email does not exist. True on completion.
* @uses get_comment()
* @uses get_post()
* @uses get_userdata()
* @uses apply_filters()
* @uses wp_specialchars_decode()
* @uses get_option()
* @uses __()
* @uses get_permalink()
* @uses admin_url()
* @uses wp_mail()
* @return bool True on completion. False if no email addresses were specified.
*/
function wp_notify_postauthor( $comment_id, $deprecated = null ) {
if ( null !== $deprecated ) {
@ -1020,21 +1030,43 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
$post = get_post( $comment->comment_post_ID );
$author = get_userdata( $post->post_author );
// The comment was left by the author
if ( $comment->user_id == $post->post_author )
return false;
// Who to notify? By default, just the post author, but others can be added.
$emails = array( $author->user_email );
$emails = apply_filters( 'comment_notification_recipients', $emails, $comment_id );
$emails = array_filter( $emails );
// The author moderated a comment on his own post
if ( $post->post_author == get_current_user_id() )
// If there are no addresses to send the comment to, bail.
if ( ! count( $emails ) ) {
return false;
}
// Facilitate unsetting below without knowing the keys.
$emails = array_flip( $emails );
// Post author may want to receive notifications for their own comments
$notify_author = apply_filters( 'comment_notification_notify_author', false, $comment_id );
// The comment was left by the author
if ( ! $notify_author && $comment->user_id == $post->post_author ) {
unset( $emails[ $author->user_email ] );
}
// The author moderated a comment on their own post
if ( ! $notify_author && $post->post_author == get_current_user_id() ) {
unset( $emails[ $author->user_email ] );
}
// The post author is no longer a member of the blog
if ( ! user_can( $post->post_author, 'read_post', $post->ID ) )
return false;
if ( ! $notify_author && ! user_can( $post->post_author, 'read_post', $post->ID ) ) {
unset( $emails[ $author->user_email ] );
}
// If there's no email to send the comment to
if ( '' == $author->user_email )
// If there's no email to send the comment to, bail, otherwise flip array back around for use below
if ( ! count( $emails ) ) {
return false;
} else {
$emails = array_flip( $emails );
}
$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
@ -1042,9 +1074,7 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$comment_type = $comment->comment_type ? $comment->comment_type : 'comment';
switch ( $comment_type ) {
switch ( $comment->comment_type ) {
case 'trackback':
$notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
/* translators: 1: website name, 2: author IP, 3: author domain */
@ -1107,9 +1137,6 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
if ( isset($reply_to) )
$message_headers .= $reply_to . "\n";
$emails = array( $author->user_email );
$emails = apply_filters( 'comment_notification_recipients', $emails, $comment_id );
$notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment_id );
$subject = apply_filters( 'comment_notification_subject', $subject, $comment_id );
$message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment_id );