<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[35339] trunk: 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.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/35339">35339</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/35339","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>DrewAPicture</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-10-21 18:34:06 +0000 (Wed, 21 Oct 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/761">#761</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescommentfunctionsphp">trunk/src/wp-includes/comment-functions.php</a></li>
<li><a href="#trunksrcwpincludespluggablephp">trunk/src/wp-includes/pluggable.php</a></li>
<li><a href="#trunktestsphpunittestscommentphp">trunk/tests/phpunit/tests/comment.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescommentfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/comment-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/comment-functions.php       2015-10-21 18:33:46 UTC (rev 35338)
+++ trunk/src/wp-includes/comment-functions.php 2015-10-21 18:34:06 UTC (rev 35339)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1758,7 +1758,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $comment = get_comment( $comment_ID );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // Only send notifications for pending comments.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( '0' != $comment->comment_approved ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1770,17 +1775,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.4.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param int $comment_ID ID of the comment.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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.
</ins><span class="cx" style="display: block; padding: 0 10px">  * @return bool True on success, false on failure.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_new_comment_notify_postauthor( $comment_ID ) {
</span><span class="cx" style="display: block; padding: 0 10px">        $comment = get_comment( $comment_ID );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $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 );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /*
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * `wp_notify_postauthor()` checks if notifying the author of their own comment.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * wp_notify_postauthor() checks if notifying the author of their own comment.
</ins><span class="cx" style="display: block; padding: 0 10px">          * By default, it won't, but filters can override this.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! get_option( 'comments_notify' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $maybe_notify ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1800,7 +1821,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 1.0.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * global wpdb $wpdb
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @global wpdb $wpdb WordPress database abstraction object.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string         $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
</span></span></pre></div>
<a id="trunksrcwpincludespluggablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/pluggable.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/pluggable.php       2015-10-21 18:33:46 UTC (rev 35338)
+++ trunk/src/wp-includes/pluggable.php 2015-10-21 18:34:06 UTC (rev 35339)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1561,20 +1561,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> if ( !function_exists('wp_notify_moderator') ) :
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Notifies the moderator of the blog about a new comment that is awaiting approval.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Notifies the moderator of the site about a new comment that is awaiting approval.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 1.0.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param int $comment_id Comment ID
- * @return true Always returns true
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_notify_moderator($comment_id) {
</span><span class="cx" style="display: block; padding: 0 10px">        global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( 0 == get_option( 'moderation_notify' ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return true;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $comment = get_comment($comment_id);
</span><span class="cx" style="display: block; padding: 0 10px">        $post = get_post($comment->comment_post_ID);
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2172,7 +2188,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                $use_random_int_functionality = false;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                } catch ( Throwable $t ) { 
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         } catch ( Throwable $t ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $use_random_int_functionality = false;
</span><span class="cx" style="display: block; padding: 0 10px">                } catch ( Exception $e ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $use_random_int_functionality = false;
</span></span></pre></div>
<a id="trunktestsphpunittestscommentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/comment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment.php     2015-10-21 18:33:46 UTC (rev 35338)
+++ trunk/tests/phpunit/tests/comment.php       2015-10-21 18:34:06 UTC (rev 35339)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7,8 +7,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected static $user_id;
</span><span class="cx" style="display: block; padding: 0 10px">        protected static $post_id;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        public function setUp() {
+               parent::setUp();
+               unset( $GLOBALS['phpmailer']->mock_sent );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public static function wpSetUpBeforeClass( $factory ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                self::$user_id = $factory->user->create();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self::$user_id = $factory->user->create( array(
+                       'role'       => 'author',
+                       'user_login' => 'test_wp_user_get',
+                       'user_pass'  => 'password',
+                       'user_email' => 'test@test.com',
+               ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 self::$post_id = $factory->post->create( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'post_author' => self::$user_id
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -359,4 +370,207 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->assertSame( $post->$pf, $comment->$pf, $pf );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+
+       /**
+        * Helper function to set up comment for 761 tests.
+        *
+        * @since 4.4.0
+        * @access public
+        */
+       public function setup_notify_comment(){
+               /**
+                * Mock some server variables.
+                */
+               $_SERVER['SERVER_NAME'] = 'phpunit.wordpress.dev';
+               $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
+
+               /**
+                * Prevent flood alert from firing.
+                */
+               add_filter( 'comment_flood_filter', '__return_false' );
+
+               /**
+                * Set up a comment for testing.
+                */
+               $post = $this->factory->post->create( array(
+                       'post_author' => self::$user_id,
+               ) );
+
+               $comment = $this->factory->comment->create( array(
+                       'comment_post_ID' => $post,
+               ) );
+
+               return array(
+                       'post'    => $post,
+                       'comment' => $comment,
+               );
+       }
+
+       /**
+        * @ticket 761
+        */
+       public function test_wp_notify_moderator_filter_moderation_notify_option_true_filter_false() {
+               $comment_data = $this->setup_notify_comment();
+
+               /**
+                * Test with moderator notification setting on, filter set to off.
+                * Should not send a notification.
+                */
+               update_option( 'moderation_notify', 1 );
+               add_filter( 'notify_moderator', '__return_false' );
+
+               $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] );
+
+               $this->assertFalse( $notification_sent, 'Moderator notification setting on, filter set to off' );
+
+               remove_filter( 'notify_moderator', '__return_false' );
+
+       }
+
+       /**
+        * @ticket 761
+        */
+       public function test_wp_notify_moderator_filter_moderation_notify_option_false_filter_true() {
+               $comment_data = $this->setup_notify_comment();
+
+               /**
+                * Test with moderator notification setting off, filter set to on.
+                * Should send a notification.
+                */
+               update_option( 'moderation_notify', 0 );
+               add_filter( 'notify_moderator', '__return_true' );
+
+               $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] );
+
+               $this->assertTrue( $notification_sent, 'Moderator notification setting off, filter set to on' );
+
+               remove_filter( 'notify_moderator', '__return_true' );
+       }
+
+       /**
+        * @ticket 761
+        */
+       public function test_wp_notify_post_author_filter_comments_notify_option_true_filter_false() {
+
+               $comment_data = $this->setup_notify_comment();
+
+               /**
+                * Test with author notification setting on, filter set to off.
+                * Should not send a notification.
+                */
+               update_option( 'comments_notify', 1 );
+               add_filter( 'notify_post_author', '__return_false' );
+
+               $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] );
+
+               $this->assertFalse( $notification_sent, 'Test with author notification setting on, filter set to off' );
+
+               remove_filter( 'notify_post_author', '__return_false' );
+       }
+
+       /**
+        * @ticket 761
+        */
+       public function test_wp_notify_post_author_filter_comments_notify_option_false_filter_true() {
+               $comment_data = $this->setup_notify_comment();
+
+               /**
+                * Test with author notification setting off, filter set to on.
+                * Should send a notification.
+                */
+               update_option( 'comments_notify', 0 );
+               add_filter( 'notify_post_author', '__return_true' );
+
+               $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] );
+
+               $this->assertTrue( $notification_sent, 'Test with author notification setting off, filter set to on' );
+
+               remove_filter( 'notify_post_author', '__return_true' );
+       }
+
+       /**
+        * Helper function to test moderator notifications.
+        *
+        * @since 4.4.0
+        * @access public
+        */
+       public function try_sending_moderator_notification( $comment, $post ) {
+
+               // Don't approve comments, triggering notifications.
+               add_filter( 'pre_comment_approved', '__return_false' );
+
+               // Moderators are notified when a new comment is added.
+               $data = array(
+                       'comment_post_ID'      => $post,
+                       'comment_author'       => rand_str(),
+                       'comment_author_url'   => '',
+                       'comment_author_email' => '',
+                       'comment_type'         => '',
+                       'comment_content'      => rand_str(),
+               );
+               wp_new_comment( $data );
+
+               // Check to see if a notification email was sent to the moderator `admin@example.org`.
+               if ( isset( $GLOBALS['phpmailer']->mock_sent )
+                       && ! empty( $GLOBALS['phpmailer']->mock_sent )
+                       && 'admin@example.org' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
+               ) {
+                       $email_sent_when_comment_added = true;
+                       unset( $GLOBALS['phpmailer']->mock_sent );
+               } else {
+                       $email_sent_when_comment_added = false;
+               }
+
+               return $email_sent_when_comment_added;
+       }
+
+       /**
+        * Helper function to test sending author notifications.
+        *
+        * @since 4.4.0
+        * @access public
+        */
+       public function try_sending_author_notification( $comment, $post ) {
+
+               // Approve comments, triggering notifications.
+               add_filter( 'pre_comment_approved', '__return_true' );
+
+               // Post authors possibly notified when a comment is approved on their post.
+               wp_set_comment_status( $comment, 'approve' );
+
+               // Check to see if a notification email was sent to the post author `test@test.com`.
+               if ( isset( $GLOBALS['phpmailer']->mock_sent )
+                       && ! empty( $GLOBALS['phpmailer']->mock_sent )
+                       && 'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
+               ) {
+                       $email_sent_when_comment_approved = true;
+               } else {
+                       $email_sent_when_comment_approved = false;
+               }
+               unset( $GLOBALS['phpmailer']->mock_sent );
+
+               // Post authors are notified when a new comment is added to their post.
+               $data = array(
+                       'comment_post_ID'      => $post,
+                       'comment_author'       => rand_str(),
+                       'comment_author_url'   => '',
+                       'comment_author_email' => '',
+                       'comment_type'         => '',
+                       'comment_content'      => rand_str(),
+               );
+               wp_new_comment( $data );
+
+               // Check to see if a notification email was sent to the post author `test@test.com`.
+               if ( isset( $GLOBALS['phpmailer']->mock_sent ) &&
+                    ! empty( $GLOBALS['phpmailer']->mock_sent ) &&
+                    'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) {
+                       $email_sent_when_comment_added = true;
+                       unset( $GLOBALS['phpmailer']->mock_sent );
+               } else {
+                       $email_sent_when_comment_added = false;
+               }
+
+               return $email_sent_when_comment_approved || $email_sent_when_comment_added;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>