WordPress.org

Make WordPress Core

Ticket #33717: 33717.9.diff

File 33717.9.diff, 6.3 KB (added by swissspidy, 6 years ago)
  • src/wp-includes/class-walker-comment.php

    diff --git src/wp-includes/class-walker-comment.php src/wp-includes/class-walker-comment.php
    index e82ec66..c009147 100644
    class Walker_Comment extends Walker { 
    270270                <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
    271271                <br />
    272272                <?php endif; ?>
     273                <?php if ( has_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_commenter' ) ) : ?>
     274                        <em class="comment-awaiting-moderation"><?php _e( 'You will receive an email when it gets approved.' ) ?></em>
     275                        <br/>
     276                <?php endif; ?>
    273277
    274278                <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
    275279                        <?php
    class Walker_Comment extends Walker { 
    334338                                        <?php if ( '0' == $comment->comment_approved ) : ?>
    335339                                        <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
    336340                                        <?php endif; ?>
     341                                        <?php if ( has_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_commenter' ) ) : ?>
     342                                        <p class="comment-awaiting-moderation"><?php _e( 'You will receive an email when it gets approved.' ) ?></p>
     343                                        <?php endif; ?>
    337344                                </footer><!-- .comment-meta -->
    338345
    339346                                <div class="comment-content">
  • src/wp-includes/comment-functions.php

    diff --git src/wp-includes/comment-functions.php src/wp-includes/comment-functions.php
    index 6b055d1..b5af39d 100644
    function wp_new_comment_notify_postauthor( $comment_ID ) { 
    18141814}
    18151815
    18161816/**
     1817 * Notify a comment author when their comment gets approved.
     1818 *
     1819 * This notification is only sent when the comment status
     1820 * changes from unapproved to approved.
     1821 *
     1822 * @since 4.4.0
     1823 *
     1824 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
     1825 * @return bool Whether the email was sent successfully.
     1826 */
     1827function wp_new_comment_notify_commenter( $comment_id ) {
     1828        $comment = get_comment( $comment_id );
     1829        if ( ! $comment ) {
     1830                return false;
     1831        }
     1832
     1833        $post           = get_post( $comment->comment_post_ID );
     1834        $comment_author = get_user_by( 'email', $comment->comment_author_email );
     1835
     1836        if ( ! $post ) {
     1837                return false;
     1838        }
     1839
     1840        // The comment was left by the post author.
     1841        if ( $comment->user_id === $post->post_author || $comment_author === get_userdata( $post->post_author ) ) {
     1842                return false;
     1843        }
     1844
     1845        if ( 1 === intval( get_comment_meta( $comment->comment_ID, '_wp_commenter_notification_sent', true ) ) ) {
     1846                return false;
     1847        }
     1848
     1849        /*
     1850         * The blogname option is escaped with esc_html
     1851         * on the way into the database in sanitize_option.
     1852         * We want to reverse this for the plain text arena of emails.
     1853         */
     1854        $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     1855
     1856        /* translators: 1: blog name, 2: post title */
     1857        $subject = sprintf( __( '[%1$s] Your comment on "%2$s" has been approved' ), $blogname, $post->post_title );
     1858
     1859        /* translators: 1: comment author's name */
     1860        $notify_message = sprintf( __( 'Howdy %s,' ), $comment->comment_author ) . "\r\n\r\n";
     1861        /* translators: 1: post title */
     1862        $notify_message .= sprintf( __( 'Your comment on the post "%1$s" has been approved.' ), $post->post_title ) . "\r\n\r\n";
     1863        /* translators: 1: comment permalink */
     1864        $notify_message .= sprintf( __( 'View comment: %s' ), get_comment_link( $comment ) ) . "\r\n";
     1865
     1866        /**
     1867         * Filter the comment approval notification email text.
     1868         *
     1869         * @since 4.4.0
     1870         *
     1871         * @param string     $notify_message The comment notification email text.
     1872         * @param WP_Comment $comment        Comment object.
     1873         */
     1874        $notify_message = apply_filters( 'comment_approval_notification_text', $notify_message, $comment );
     1875
     1876        /**
     1877         * Filter the comment approval notification email subject.
     1878         *
     1879         * @since 4.4.0
     1880         *
     1881         * @param string     $subject The comment notification email subject.
     1882         * @param WP_Comment $comment Comment object.
     1883         */
     1884        $subject = apply_filters( 'comment_approval_notification_subject', $subject, $comment );
     1885
     1886        $sent = wp_mail( $comment->comment_author_email, wp_specialchars_decode( $subject ), $notify_message );
     1887
     1888        update_comment_meta( $comment->comment_ID, '_wp_commenter_notification_sent', intval( $sent ) );
     1889
     1890        return $sent;
     1891}
     1892
     1893/**
    18171894 * Sets the status of a comment.
    18181895 *
    18191896 * The 'wp_set_comment_status' action is called after the comment is handled.
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index bbe9f24..cdc4cdc 100644
    add_action( 'comment_post', 'wp_new_comment_notify_postauthor' ); 
    356356add_action( 'after_password_reset', 'wp_password_change_notification' );
    357357add_action( 'register_new_user',      'wp_send_new_user_notifications' );
    358358add_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
     359add_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_commenter' );
    359360
    360361// REST API actions.
    361362add_action( 'init',          'rest_api_init' );
  • tests/phpunit/tests/comment.php

    diff --git tests/phpunit/tests/comment.php tests/phpunit/tests/comment.php
    index 8faa0b7..d5165c2 100644
    class Tests_Comment extends WP_UnitTestCase { 
    392392                ) );
    393393
    394394                $comment = $this->factory->comment->create( array(
    395                         'comment_post_ID' => $post,
     395                        'comment_post_ID'      => $post,
     396                        'comment_author'       => rand_str(),
     397                        'comment_author_email' => rand_str() . '@example.com',
    396398                ) );
    397399
    398400                return array(
    class Tests_Comment extends WP_UnitTestCase { 
    484486        }
    485487
    486488        /**
     489         * @ticket 33717
     490         */
     491        public function test_wp_new_comment_notify_commenter() {
     492                $comment_data = $this->setup_notify_comment();
     493                $comment = get_comment(  $comment_data['comment'] );
     494
     495                $notified = wp_new_comment_notify_commenter( $comment->comment_ID );
     496
     497                if ( isset( $GLOBALS['phpmailer']->mock_sent )
     498                     && ! empty( $GLOBALS['phpmailer']->mock_sent )
     499                     && $comment->comment_author_email == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
     500                ) {
     501                        $email_sent = true;
     502                } else {
     503                        $email_sent = false;
     504                }
     505                unset( $GLOBALS['phpmailer']->mock_sent );
     506
     507                $this->assertTrue( $notified  );
     508                $this->assertTrue( $email_sent  );
     509        }
     510
     511        /**
    487512         * Helper function to test moderator notifications.
    488513         *
    489514         * @since 4.4.0