From 05d4233956fe343cd582ce15a0b5d0b20bb9004f Mon Sep 17 00:00:00 2001 From: boonebgorges Date: Mon, 14 Sep 2015 02:16:02 +0000 Subject: [PATCH] Send comment notification emails via a hooked function. Previously, `wp_notify_postauthor()` and `wp_notify_moderator()` were called directly from `wp_new_comment()`, making it difficult to modify or suppress default notification emails. Props dshanske, thomaswm. See #33587. git-svn-id: https://develop.svn.wordpress.org/trunk@34106 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-functions.php | 45 ++++++++++++++++++++------- src/wp-includes/default-filters.php | 4 +++ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/comment-functions.php b/src/wp-includes/comment-functions.php index f953a9e102..8a65a46d4c 100644 --- a/src/wp-includes/comment-functions.php +++ b/src/wp-includes/comment-functions.php @@ -1629,21 +1629,42 @@ function wp_new_comment( $commentdata ) { */ do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] ); - if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching - if ( '0' == $commentdata['comment_approved'] ) { - wp_notify_moderator( $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 ); - } - } - return $comment_ID; } +/** + * Send a comment moderation notification to the comment moderator. + * + * @since 4.4.0 + * + * @param int $comment_ID ID of the comment. + * @param int $comment_approved Whether the comment is approved. + */ +function wp_new_comment_notify_moderator( $comment_ID, $comment_approved ) { + if ( '0' == $comment_approved ) { + wp_notify_moderator( $comment_ID ); + } +} + +/** + * Send a notification of a new comment to the post author. + * + * @since 4.4.0 + * + * @param int $comment_ID ID of the comment. + */ +function wp_new_comment_notify_postauthor( $comment_ID ) { + $comment = get_comment( $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' ) && $comment->comment_approved ) { + wp_notify_postauthor( $comment_ID ); + } +} + /** * Sets the status of a comment. * diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index b6f0ce303e..8ce3660f43 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -334,6 +334,10 @@ add_action( 'split_shared_term', '_wp_check_split_terms_in_menus', 10, 4 ); add_action( 'split_shared_term', '_wp_check_split_nav_menu_terms', 10, 4 ); add_action( 'wp_split_shared_term_batch', '_wp_batch_split_terms' ); +// Email notifications. +add_action( 'comment_post', 'wp_new_comment_notify_moderator', 10, 2 ); +add_action( 'comment_post', 'wp_new_comment_notify_postauthor' ); + /** * Filters formerly mixed into wp-includes */