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

@@ -1561,20 +1561,36 @@ endif;
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
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $comment_id Comment ID
* @return true Always returns true
* Uses the {@see 'notify_moderator'} filter to determine whether the site moderator
* should be notified, overriding the site setting.
*
* @param int $comment_id Comment ID.
* @return true Always returns true.
*/
function wp_notify_moderator($comment_id) {
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;
}
$comment = get_comment($comment_id);
$post = get_post($comment->comment_post_ID);
@@ -2172,7 +2188,7 @@ function wp_rand( $min = 0, $max = 0 ) {
} else {
$use_random_int_functionality = false;
}
} catch ( Throwable $t ) {
} catch ( Throwable $t ) {
$use_random_int_functionality = false;
} catch ( Exception $e ) {
$use_random_int_functionality = false;