Comments: Introduce two new filters, `notify_moderator` and `notify_post_author`, both of which make it possible to selectively override site notification email settings for new comments.

The `notify_moderator` filter makes it possible to override the value for the `moderation_notify` option, which controls whether to send new comment emails to "site moderators", that is to say, the owner of the admin email for the site and the post author if they have the ability to modify the comment.

The `notify_post_author` filter likewise makes it possible to override the value for the `comments_notify` option, which controls whether to send new comment emails to the post author. If the post author is the comment author, default behavior is not to send the notification. Note: enabling or disabling notifications via this hook could also affect other recipients added via the 'comment_notification_recipients' filter in `wp_notify_postauthor()`, if hooked.

Passing a falsey value to either of the new filters will prevent notifications from being sent, regardless of their corresponding option values.

Adds tests.

Props coffee2code, adamsilverstein, DrewAPicture.
Fixes #761.

Built from https://develop.svn.wordpress.org/trunk@35339


git-svn-id: http://core.svn.wordpress.org/trunk@35305 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Drew Jaynes 2015-10-21 18:35:31 +00:00
parent 223afd7e04
commit f954194a1c
3 changed files with 48 additions and 11 deletions

View File

@ -1758,7 +1758,12 @@ function wp_new_comment_notify_moderator( $comment_ID ) {
$comment = get_comment( $comment_ID ); $comment = get_comment( $comment_ID );
// Only send notifications for pending comments. // Only send notifications for pending comments.
if ( '0' != $comment->comment_approved ) { $maybe_notify = ( '0' == $comment->comment_approved );
/** This filter is documented in wp-includes/comment-functions.php */
$maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_ID );
if ( ! $maybe_notify ) {
return false; return false;
} }
@ -1770,17 +1775,33 @@ function wp_new_comment_notify_moderator( $comment_ID ) {
* *
* @since 4.4.0 * @since 4.4.0
* *
* @param int $comment_ID ID of the comment. * Uses the {@see 'notify_post_author'} filter to determine whether the post author
* should be notified when a new comment is added, overriding site setting.
*
* @param int $comment_ID Comment ID.
* @return bool True on success, false on failure. * @return bool True on success, false on failure.
*/ */
function wp_new_comment_notify_postauthor( $comment_ID ) { function wp_new_comment_notify_postauthor( $comment_ID ) {
$comment = get_comment( $comment_ID ); $comment = get_comment( $comment_ID );
$maybe_notify = get_option( 'comments_notify' );
/**
* Filter whether to send the post author new comment notification emails,
* overriding the site setting.
*
* @since 4.4.0
*
* @param bool $maybe_notify Whether to notify the post author about the new comment.
* @param int $comment_ID The ID of the comment for the notification.
*/
$maybe_notify = apply_filters( 'notify_post_author', $maybe_notify, $comment_ID );
/* /*
* `wp_notify_postauthor()` checks if notifying the author of their own comment. * wp_notify_postauthor() checks if notifying the author of their own comment.
* By default, it won't, but filters can override this. * By default, it won't, but filters can override this.
*/ */
if ( ! get_option( 'comments_notify' ) ) { if ( ! $maybe_notify ) {
return false; return false;
} }
@ -1800,7 +1821,7 @@ function wp_new_comment_notify_postauthor( $comment_ID ) {
* *
* @since 1.0.0 * @since 1.0.0
* *
* global wpdb $wpdb * @global wpdb $wpdb WordPress database abstraction object.
* *
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
* @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'. * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.

View File

@ -1561,20 +1561,36 @@ endif;
if ( !function_exists('wp_notify_moderator') ) : if ( !function_exists('wp_notify_moderator') ) :
/** /**
* Notifies the moderator of the blog about a new comment that is awaiting approval. * Notifies the moderator of the site about a new comment that is awaiting approval.
* *
* @since 1.0.0 * @since 1.0.0
* *
* @global wpdb $wpdb WordPress database abstraction object. * @global wpdb $wpdb WordPress database abstraction object.
* *
* @param int $comment_id Comment ID * Uses the {@see 'notify_moderator'} filter to determine whether the site moderator
* @return true Always returns true * should be notified, overriding the site setting.
*
* @param int $comment_id Comment ID.
* @return true Always returns true.
*/ */
function wp_notify_moderator($comment_id) { function wp_notify_moderator($comment_id) {
global $wpdb; global $wpdb;
if ( 0 == get_option( 'moderation_notify' ) ) $maybe_notify = get_option( 'moderation_notify' );
/**
* Filter whether to send the site moderator email notifications, overriding the site setting.
*
* @since 4.4.0
*
* @param bool $maybe_notify Whether to notify blog moderator.
* @param int $comment_ID The id of the comment for the notification.
*/
$maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id );
if ( ! $maybe_notify ) {
return true; return true;
}
$comment = get_comment($comment_id); $comment = get_comment($comment_id);
$post = get_post($comment->comment_post_ID); $post = get_post($comment->comment_post_ID);

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.4-alpha-35338'; $wp_version = '4.4-alpha-35339';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.