Make WordPress Core


Ignore:
Timestamp:
10/21/2015 06:34:06 PM (8 years ago)
Author:
DrewAPicture
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-functions.php

    r35331 r35339  
    17591759
    17601760    // Only send notifications for pending comments.
    1761     if ( '0' != $comment->comment_approved ) {
     1761    $maybe_notify = ( '0' == $comment->comment_approved );
     1762
     1763    /** This filter is documented in wp-includes/comment-functions.php */
     1764    $maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_ID );
     1765
     1766    if ( ! $maybe_notify ) {
    17621767        return false;
    17631768    }
     
    17711776 * @since 4.4.0
    17721777 *
    1773  * @param int $comment_ID ID of the comment.
     1778 * Uses the {@see 'notify_post_author'} filter to determine whether the post author
     1779 * should be notified when a new comment is added, overriding site setting.
     1780 *
     1781 * @param int $comment_ID Comment ID.
    17741782 * @return bool True on success, false on failure.
    17751783 */
     
    17771785    $comment = get_comment( $comment_ID );
    17781786
     1787    $maybe_notify = get_option( 'comments_notify' );
     1788
     1789    /**
     1790     * Filter whether to send the post author new comment notification emails,
     1791     * overriding the site setting.
     1792     *
     1793     * @since 4.4.0
     1794     *
     1795     * @param bool $maybe_notify Whether to notify the post author about the new comment.
     1796     * @param int  $comment_ID   The ID of the comment for the notification.
     1797     */
     1798    $maybe_notify = apply_filters( 'notify_post_author', $maybe_notify, $comment_ID );
     1799
    17791800    /*
    1780      * `wp_notify_postauthor()` checks if notifying the author of their own comment.
     1801     * wp_notify_postauthor() checks if notifying the author of their own comment.
    17811802     * By default, it won't, but filters can override this.
    17821803     */
    1783     if ( ! get_option( 'comments_notify' ) ) {
     1804    if ( ! $maybe_notify ) {
    17841805        return false;
    17851806    }
     
    18011822 * @since 1.0.0
    18021823 *
    1803  * global wpdb $wpdb
     1824 * @global wpdb $wpdb WordPress database abstraction object.
    18041825 *
    18051826 * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
Note: See TracChangeset for help on using the changeset viewer.