Make WordPress Core

Ticket #761: 761.8.diff

File 761.8.diff, 9.1 KB (added by adamsilverstein, 9 years ago)
  • src/wp-includes/comment-functions.php

     
    17641764 *
    17651765 * @since 4.4.0
    17661766 *
     1767 * Calls {@see 'notify_post_author'} to determine if the post author should be
     1768 * notified when a new comment is added, overriding site setting.
     1769 *
    17671770 * @param int $comment_ID ID of the comment.
    17681771 * @return bool True on success, false on failure.
    17691772 */
     
    17701773function wp_new_comment_notify_postauthor( $comment_ID ) {
    17711774        $comment = get_comment( $comment_ID );
    17721775
     1776        $maybe_notify = get_option( 'comments_notify' );
     1777        /**
     1778         * Filter whether to send post author new comment notification emails, overriding
     1779         * site setting.
     1780         *
     1781         * @since 4.4.0
     1782         *
     1783         * @param bool $maybe_notify Whether to notify post author.
     1784         * @param int  $comment_ID   The id of the comment for the notification.
     1785         */
     1786        $maybe_notify = apply_filters( 'notify_post_author', $maybe_notify, $comment_ID );
     1787
    17731788        /*
    17741789         * `wp_notify_postauthor()` checks if notifying the author of their own comment.
    17751790         * By default, it won't, but filters can override this.
    17761791         */
    1777         if ( ! get_option( 'comments_notify' ) ) {
     1792        if ( ! $maybe_notify ) {
    17781793                return false;
    17791794        }
    17801795
     
    17941809 *
    17951810 * @since 1.0.0
    17961811 *
     1812 * Calls {@see 'notify_post_author'} to determine if the post author should be
     1813 * notified, overriding site setting.
     1814 *
    17971815 * global wpdb $wpdb
    17981816 *
    17991817 * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
  • src/wp-includes/pluggable.php

     
    15671567 *
    15681568 * @global wpdb $wpdb WordPress database abstraction object.
    15691569 *
     1570 * Calls {@see 'notify_moderator'} to determine if the site moderator
     1571 * should be notified, overriding site setting.
     1572 *
     1573 * @uses apply_filters() Calls 'wp_notify_moderator' to determine if blog moderator
     1574 *                       should be notified.
     1575 *
    15701576 * @param int $comment_id Comment ID
    15711577 * @return true Always returns true
    15721578 */
     
    15731579function wp_notify_moderator($comment_id) {
    15741580        global $wpdb;
    15751581
    1576         if ( 0 == get_option( 'moderation_notify' ) )
     1582        $maybe_notify = get_option( 'moderation_notify' );
     1583
     1584        /**
     1585         * Filter whether to send blog moderator email notifications, overriding
     1586         * site setting.
     1587         *
     1588         * @since 4.4.0
     1589         *
     1590         * @param bool $maybe_notify Whether to notify blog moderator.
     1591         * @param int  $comment_ID   The id of the comment for the notification.
     1592         */
     1593        if ( ! apply_filters( 'notify_moderator', $maybe_notify, $comment_id ) ) {
    15771594                return true;
     1595        }
    15781596
    15791597        $comment = get_comment($comment_id);
    15801598        $post = get_post($comment->comment_post_ID);
  • tests/phpunit/tests/comment.php

     
    358358                        $this->assertSame( $post->$pf, $comment->$pf, $pf );
    359359                }
    360360        }
     361
     362
     363        /**
     364         * Helper function to set up comment for 761 tests
     365         */
     366        public function setup_notify_comment(){
     367                /**
     368                 * Mock some server variables.
     369                 */
     370                $_SERVER['SERVER_NAME'] = 'phpunit.wordpress.dev';
     371                $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
     372
     373                /**
     374                 * Prevent flood alert from firing.
     375                 */
     376                add_filter( 'comment_flood_filter', '__return_false' );
     377
     378                /**
     379                 * Set up the post author to test notifying.
     380                 */
     381                $user = $this->factory->user->create(
     382                                array(
     383                                                'role'       => 'author',
     384                                                'user_login' => 'test_wp_user_get',
     385                                                'user_pass'  => 'password',
     386                                                'user_email' => 'test@test.com',
     387                                )
     388                );
     389
     390                /**
     391                 * Set up a comment for testing.
     392                 */
     393                $post = $this->factory->post->create(
     394                                array(
     395                                                'post_author' => $user,
     396                                )
     397                );
     398
     399                $comment = $this->factory->comment->create(
     400                                array(
     401                                                'comment_post_ID' => $post,
     402                                )
     403                );
     404
     405                return array(
     406                                'post'    => $post,
     407                                'comment' => $comment,
     408                );
     409        }
     410
     411        /**
     412         * @ticket 761
     413         */
     414        public function test_wp_notify_moderator_filter() {
     415
     416                $comment_data = $this->setup_notify_comment();
     417
     418                /**
     419                 * Test with moderator notification setting on, filter set to off.
     420                 * Should not send a notification.
     421                 */
     422                update_option( 'moderation_notify', 1 );
     423                add_filter( 'notify_moderator', '__return_false' );
     424                $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] );
     425                $this->assertFalse( $notification_sent, 'Moderator notification setting on, filter set to off' );
     426                remove_filter( 'notify_moderator', '__return_false' );
     427
     428                /**
     429                 * Test with moderator notification setting off, filter set to on.
     430                 * Should send a notification.
     431                 */
     432                update_option( 'moderation_notify', 0 );
     433                add_filter( 'notify_moderator', '__return_true' );
     434                $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] );
     435                $this->assertTrue( $notification_sent, 'Moderator notification setting off, filter set to on' );
     436                remove_filter( 'notify_moderator', '__return_true' );
     437
     438        }
     439
     440        /**
     441         * @ticket 761
     442         */
     443        public function test_wp_notify_post_author_filter() {
     444
     445                $comment_data = $this->setup_notify_comment();
     446
     447                /**
     448                 * Test with author notification setting on, filter set to off.
     449                 * Should not send a notification.
     450                 */
     451                update_option( 'comments_notify', 1 );
     452                add_filter( 'notify_post_author', '__return_false' );
     453                $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] );
     454                $this->assertFalse( $notification_sent, 'Test with author notification setting on, filter set to off' );
     455                remove_filter( 'notify_post_author', '__return_false' );
     456
     457                /**
     458                 * Test with author notification setting off, filter set to on.
     459                 * Should send a notification.
     460                 */
     461                update_option( 'comments_notify', 0 );
     462                add_filter( 'notify_post_author', '__return_true' );
     463                $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] );
     464                $this->assertTrue( $notification_sent, 'Test with author notification setting off, filter set to on' );
     465                remove_filter( 'notify_post_author', '__return_true' );
     466
     467        }
     468
     469        /**
     470         * Helper function to test moderator notifications.
     471         */
     472        public function try_sending_moderator_notification( $comment, $post ) {
     473
     474                /**
     475                 * Don't approve comments, triggering notifications.
     476                 */
     477                add_filter( 'pre_comment_approved', '__return_false' );
     478
     479                /**
     480                 * Moderators are notified when a new comment is added.
     481                 */
     482                $data = array(
     483                                'comment_post_ID'      => $post,
     484                                'comment_author'       => rand_str(),
     485                                'comment_author_url'   => '',
     486                                'comment_author_email' => '',
     487                                'comment_type'         => '',
     488                                'comment_content'      => rand_str(),
     489                );
     490                wp_new_comment( $data );
     491
     492                /**
     493                 * Check to see if a notification email was sent to the moderator `admin@example.org`.
     494                 */
     495                if ( isset( $GLOBALS['phpmailer']->mock_sent ) &&
     496                        ! empty( $GLOBALS['phpmailer']->mock_sent ) &&
     497                        'admin@example.org' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) {
     498                        $email_sent_when_comment_added = true;
     499                        unset( $GLOBALS['phpmailer']->mock_sent );
     500                } else {
     501                        $email_sent_when_comment_added = false;
     502                }
     503
     504                return $email_sent_when_comment_added;
     505        }
     506
     507        /**
     508         * Helper function to test sending author notifications.
     509         */
     510        public function try_sending_author_notification( $comment, $post ) {
     511
     512                /**
     513                 * Approve comments, triggering notifications.
     514                 */
     515                add_filter( 'pre_comment_approved', '__return_true' );
     516
     517                /**
     518                 * Post authors possibly notified when a comment is approved on their post.
     519                 */
     520                wp_set_comment_status( $comment, 'approve' );
     521
     522                /**
     523                 * Check to see if a notification email was sent to the post author `test@test.com`.
     524                 */
     525                if ( isset( $GLOBALS['phpmailer']->mock_sent ) &&
     526                        ! empty( $GLOBALS['phpmailer']->mock_sent ) &&
     527                        'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) {
     528                        $email_sent_when_comment_approved = true;
     529                } else {
     530                        $email_sent_when_comment_approved = false;
     531                }
     532                unset( $GLOBALS['phpmailer']->mock_sent );
     533
     534                /**
     535                 * Post authors are notified when a new comment is added to their post.
     536                 */
     537                $data = array(
     538                                'comment_post_ID'      => $post,
     539                                'comment_author'       => rand_str(),
     540                                'comment_author_url'   => '',
     541                                'comment_author_email' => '',
     542                                'comment_type'         => '',
     543                                'comment_content'      => rand_str(),
     544                );
     545                wp_new_comment( $data );
     546
     547                /**
     548                 * Check to see if a notification email was sent to the post author `test@test.com`.
     549                 */
     550                if ( isset( $GLOBALS['phpmailer']->mock_sent ) &&
     551                     ! empty( $GLOBALS['phpmailer']->mock_sent ) &&
     552                     'test@test.com' == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) {
     553                        $email_sent_when_comment_added = true;
     554                        unset( $GLOBALS['phpmailer']->mock_sent );
     555                } else {
     556                        $email_sent_when_comment_added = false;
     557                }
     558
     559                return $email_sent_when_comment_approved && $email_sent_when_comment_added;
     560        }
    361561}