Make WordPress Core

Ticket #761: 761.9.diff

File 761.9.diff, 9.5 KB (added by DrewAPicture, 9 years ago)

Docs fixes

  • src/wp-includes/comment-functions.php

     
    17641764 *
    17651765 * @since 4.4.0
    17661766 *
    1767  * @param int $comment_ID ID of the comment.
     1767 * Uses the {@see 'notify_post_author'} filter to determine whether the post author
     1768 * should be notified when a new comment is added, overriding site setting.
     1769 *
     1770 * @param int $comment_ID Comment ID.
    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        /**
     1779         * Filter whether to send the post author new comment notification emails,
     1780         * overriding the site setting.
     1781         *
     1782         * @since 4.4.0
     1783         *
     1784         * @param bool $maybe_notify Whether to notify the post author about the new comment.
     1785         * @param int  $comment_ID   The id of the comment for the notification.
     1786         */
     1787        $maybe_notify = apply_filters( 'notify_post_author', $maybe_notify, $comment_ID );
     1788
    17731789        /*
    1774          * `wp_notify_postauthor()` checks if notifying the author of their own comment.
     1790         * wp_notify_postauthor() checks if notifying the author of their own comment.
    17751791         * By default, it won't, but filters can override this.
    17761792         */
    1777         if ( ! get_option( 'comments_notify' ) ) {
     1793        if ( ! $maybe_notify ) {
    17781794                return false;
    17791795        }
    17801796
     
    17941810 *
    17951811 * @since 1.0.0
    17961812 *
    1797  * global wpdb $wpdb
     1813 * @global wpdb $wpdb WordPress database abstraction object.
    17981814 *
    17991815 * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
    18001816 * @param string         $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
  • src/wp-includes/pluggable.php

     
    15611561
    15621562if ( !function_exists('wp_notify_moderator') ) :
    15631563/**
    1564  * Notifies the moderator of the blog about a new comment that is awaiting approval.
     1564 * Notifies the moderator of the site about a new comment that is awaiting approval.
    15651565 *
    15661566 * @since 1.0.0
    15671567 *
    15681568 * @global wpdb $wpdb WordPress database abstraction object.
    15691569 *
    1570  * @param int $comment_id Comment ID
    1571  * @return true Always returns true
     1570 * Uses the {@see 'notify_moderator'} filter to determine whether the site moderator
     1571 * should be notified, overriding the site setting.
     1572 *
     1573 * @param int $comment_id Comment ID.
     1574 * @return true Always returns true.
    15721575 */
    15731576function wp_notify_moderator($comment_id) {
    15741577        global $wpdb;
    15751578
    1576         if ( 0 == get_option( 'moderation_notify' ) )
     1579        $maybe_notify = get_option( 'moderation_notify' );
     1580
     1581        /**
     1582         * Filter whether to send the site moderator email notifications, overriding the site setting.
     1583         *
     1584         * @since 4.4.0
     1585         *
     1586         * @param bool $maybe_notify Whether to notify blog moderator.
     1587         * @param int  $comment_ID   The id of the comment for the notification.
     1588         */
     1589        $maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id );
     1590
     1591        if ( $maybe_notify ) {
    15771592                return true;
     1593        }
    15781594
    15791595        $comment = get_comment($comment_id);
    15801596        $post = get_post($comment->comment_post_ID);
  • tests/phpunit/tests/comment.php

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