WordPress.org

Make WordPress Core

Ticket #33717: 33717.15.patch

File 33717.15.patch, 19.9 KB (added by imath, 9 months ago)
  • src/wp-admin/comment.php

    diff --git src/wp-admin/comment.php src/wp-admin/comment.php
    index ad11f3e05b..18249c7bfa 100644
    switch ( $action ) { 
    165165                ?>
    166166<div id="message" class="notice notice-warning"><p><strong><?php _e( 'Caution:' ); ?></strong> <?php echo $caution_msg; ?></p></div>
    167167
     168<?php
     169if ( 0 === (int) $comment->comment_approved && true === (bool) get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) :
     170        ?>
     171        <div id="message" class="notice notice-warning">
     172                <p>
     173                        <?php
     174                        /* translators: %s is the comment author name. */
     175                        printf( esc_html__( '%s has opted in to receive a notification on comment’s approval.' ), get_comment_author( $comment ) );
     176                        ?>
     177                </p>
     178        </div>
     179<?php endif; ?>
     180
    168181<table class="form-table comment-ays">
    169182<tr>
    170183<th scope="row"><?php _e( 'Author' ); ?></th>
  • src/wp-admin/css/dashboard.css

    diff --git src/wp-admin/css/dashboard.css src/wp-admin/css/dashboard.css
    index 2411c79900..e76acdeaa5 100644
    body #dashboard-widgets .postbox form .submit { 
    821821        position: relative;
    822822}
    823823
     824#activity-widget #the-comment-list .comment-item .attention {
     825        display: none;
     826        color: #d54e21;
     827        font-size: 90%;
     828}
     829
    824830#activity-widget #the-comment-list .avatar {
    825831        position: absolute;
    826832        top: 12px;
    body #dashboard-widgets .postbox form .submit { 
    857863        width: 4px;
    858864}
    859865
     866#activity-widget #the-comment-list .unapproved .attention {
     867        display: block;
     868}
     869
    860870#activity-widget #the-comment-list .spam-undo-inside .avatar,
    861871#activity-widget #the-comment-list .trash-undo-inside .avatar {
    862872        position: relative;
  • src/wp-admin/css/list-tables.css

    diff --git src/wp-admin/css/list-tables.css src/wp-admin/css/list-tables.css
    index 1febe4e837..56334925a7 100644
     
    217217        background-color: #f5f5f5;
    218218}
    219219
     220#the-comment-list .comment .attention {
     221        display: none;
     222        color: #d54e21;
     223        font-size: 90%;
     224}
     225
    220226#the-comment-list .unapproved th,
    221227#the-comment-list .unapproved td {
    222228        background-color: #fef7f1;
     
    230236        margin-left: 4px;
    231237}
    232238
     239#the-comment-list .comment.unapproved .attention {
     240        display: block;
     241}
     242
    233243#the-comment-list .approve a {
    234244        color: #006505;
    235245}
  • src/wp-admin/edit-form-comment.php

    diff --git src/wp-admin/edit-form-comment.php src/wp-admin/edit-form-comment.php
    index ef0818c510..81614200db 100644
    if ( ! defined( 'ABSPATH' ) ) { 
    1616<div class="wrap">
    1717<h1><?php _e( 'Edit Comment' ); ?></h1>
    1818
     19<?php
     20if ( 0 === (int) $comment->comment_approved && true === (bool) get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) :
     21        ?>
     22        <div id="message" class="notice notice-warning">
     23                <p>
     24                        <?php
     25                        /* translators: %s is the comment author name. */
     26                        printf( esc_html__( '%s has opted in to receive a notification on comment’s approval.' ), get_comment_author( $comment ) );
     27                        ?>
     28                </p>
     29        </div>
     30<?php endif; ?>
     31
    1932<div id="poststuff">
    2033<input type="hidden" name="action" value="editedcomment" />
    2134<input type="hidden" name="comment_ID" value="<?php echo esc_attr( $comment->comment_ID ); ?>" />
  • src/wp-admin/includes/class-wp-comments-list-table.php

    diff --git src/wp-admin/includes/class-wp-comments-list-table.php src/wp-admin/includes/class-wp-comments-list-table.php
    index 563187ffe4..daf837aaa6 100644
    class WP_Comments_List_Table extends WP_List_Table { 
    882882                        }
    883883                }
    884884
     885                if ( 0 === (int) $comment->comment_approved && true === (bool) get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) {
     886                        /* translators: %s is the comment author name. */
     887                        echo '<p class="attention"><span class="dashicons dashicons-info"></span> ' . sprintf( esc_html__( '%s has opted in to receive a notification on comment’s approval.' ), get_comment_author( $comment ) ) . '</p>';
     888                }
     889
    885890                comment_text( $comment );
    886891
    887892                if ( $this->user_can ) {
  • src/wp-admin/includes/dashboard.php

    diff --git src/wp-admin/includes/dashboard.php src/wp-admin/includes/dashboard.php
    index 2f438067da..79e7dcfa73 100644
    function _wp_dashboard_recent_comments_row( &$comment, $show_date = true ) { 
    827827                                }
    828828                                ?>
    829829                        </p>
    830 
    831830                                <?php
     831                                if ( 0 === (int) $comment->comment_approved && true === (bool) get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) :
     832                                        ?>
     833                                                <p class="attention">
     834                                                        <span class="dashicons dashicons-info"></span>
     835                                                        <?php
     836                                                        /* translators: %s is the comment author name. */
     837                                                        printf( esc_html__( '%s has opted in to receive a notification on comment’s approval.' ), get_comment_author( $comment ) );
     838                                                        ?>
     839                                                </p>
     840                                        <?php
     841                                        endif;
     842
    832843                        else :
    833844                                switch ( $comment->comment_type ) {
    834845                                        case 'pingback':
  • src/wp-comments-post.php

    diff --git src/wp-comments-post.php src/wp-comments-post.php
    index 06cbd460f4..3b1a8b44ca 100644
    require __DIR__ . '/wp-load.php'; 
    2222
    2323nocache_headers();
    2424
    25 $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
    26 if ( is_wp_error( $comment ) ) {
    27         $data = (int) $comment->get_error_data();
    28         if ( ! empty( $data ) ) {
     25if ( isset( $_POST['wp-comment-approved-notification-optin'], $_POST['comment_ID'], $_POST['moderation-hash'] ) ) {
     26        $comment = get_comment( $_POST['comment_ID'] );
     27
     28        if ( ! is_null( $comment->comment_ID ) && hash_equals( $_POST['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) {
     29                update_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true );
     30        } else {
    2931                wp_die(
    30                         '<p>' . $comment->get_error_message() . '</p>',
    31                         __( 'Comment Submission Failure' ),
     32                        '<p>' . __( 'Sorry we haven’t found your comment.' ) . '</p>',
     33                        __( 'Comment Notification opt-in failure' ),
    3234                        array(
    33                                 'response'  => $data,
    3435                                'back_link' => true,
    3536                        )
    3637                );
    37         } else {
    38                 exit;
    3938        }
    40 }
     39} else {
     40        $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
     41        if ( is_wp_error( $comment ) ) {
     42                $data = (int) $comment->get_error_data();
     43                if ( ! empty( $data ) ) {
     44                        wp_die(
     45                                '<p>' . $comment->get_error_message() . '</p>',
     46                                __( 'Comment Submission Failure' ),
     47                                array(
     48                                        'response'  => $data,
     49                                        'back_link' => true,
     50                                )
     51                        );
     52                } else {
     53                        exit;
     54                }
     55        }
    4156
    42 $user            = wp_get_current_user();
    43 $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );
     57        $user            = wp_get_current_user();
     58        $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );
    4459
    45 /**
    46  * Perform other actions when comment cookies are set.
    47  *
    48  * @since 3.4.0
    49  * @since 4.9.6 The `$cookies_consent` parameter was added.
    50  *
    51  * @param WP_Comment $comment         Comment object.
    52  * @param WP_User    $user            Comment author's user object. The user may not exist.
    53  * @param bool       $cookies_consent Comment author's consent to store cookies.
    54  */
    55 do_action( 'set_comment_cookies', $comment, $user, $cookies_consent );
     60        /**
     61         * Perform other actions when comment cookies are set.
     62         *
     63         * @since 3.4.0
     64         * @since 4.9.6 The `$cookies_consent` parameter was added.
     65         *
     66         * @param WP_Comment $comment         Comment object.
     67         * @param WP_User    $user            Comment author's user object. The user may not exist.
     68         * @param bool       $cookies_consent Comment author's consent to store cookies.
     69         */
     70        do_action( 'set_comment_cookies', $comment, $user, $cookies_consent );
     71}
    5672
    5773$location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment->comment_ID;
    5874
  • src/wp-includes/class-walker-comment.php

    diff --git src/wp-includes/class-walker-comment.php src/wp-includes/class-walker-comment.php
    index 9df2d20b3b..9c5add1c49 100644
    class Walker_Comment extends Walker { 
    4040                'id'     => 'comment_ID',
    4141        );
    4242
     43        /**
     44         * Backward compability checks.
     45         *
     46         * @since 5.7.0
     47         * @var array
     48         */
     49        public $backcompat = array(
     50                'needs_comment_approval_notification_output' => true,
     51        );
     52
    4353        /**
    4454         * Starts the list before the elements are added.
    4555         *
    class Walker_Comment extends Walker { 
    259269         * to the comment cookies.
    260270         *
    261271         * @since 5.4.2
     272         * @since 5.7.0 Adds backcompatibilty output if needed.
    262273         *
    263274         * @param string          $comment_text Text of the current comment.
    264275         * @param WP_Comment|null $comment      The comment object. Null if not found.
    class Walker_Comment extends Walker { 
    272283                        $comment_text = wp_kses( $comment_text, array() );
    273284                }
    274285
     286                /*
     287                 * Checks if we need to output the comment approval notification/form.
     288                 */
     289                if ( $this->backcompat['needs_comment_approval_notification_output'] ) {
     290                        $comment_text = $this->comment_approval_notification_form( $comment, true ) . "\n" . $comment_text;
     291                }
     292
    275293                return $comment_text;
    276294        }
    277295
     296        /**
     297         * Outputs the awaiting moderation text.
     298         *
     299         * @since 5.7.0
     300         *
     301         * @param WP_Comment $comment Comment to display.
     302         */
     303        protected function awaiting_moderation_text( $comment ) {
     304
     305                if ( "0" === $comment->comment_approved ) {
     306                        $commenter = wp_get_current_commenter();
     307                        if ( $commenter['comment_author_email'] ) {
     308                                $moderation_note = __( 'Your comment is awaiting moderation.' );
     309                        } else {
     310                                $moderation_note = __( 'Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.' );
     311                        }
     312
     313                        printf( '<em class="comment-awaiting-moderation">%s</em>', esc_html( $moderation_note ) );
     314                }
     315        }
     316
     317        /**
     318         * Gets the comment's approval notification/form.
     319         *
     320         * @since 5.7.0
     321         *
     322         * @param WP_Comment $comment Comment to display.
     323         * @param bool $skip_backcompat True to skip the backward compability output.
     324         *                              False otherwise.
     325         * @return string HTML Output.
     326         */
     327        protected function comment_approval_notification_form( $comment, $skip_backcompat = false ) {
     328                $comment_approval_output = '';
     329
     330                if ( "0" === $comment->comment_approved && has_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' ) ) {
     331                        if ( true === (bool) get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) {
     332                                $comment_approval_output = sprintf(
     333                                        '<p><em class="wp-comment-approved-notification-optedin">%s</em></p>',
     334                                        esc_html__( 'You will receive an email when your comment has been approved.' )
     335                                );
     336                        } else {
     337                                $comment_approval_output = sprintf(
     338                                        '<form action="%1$s" method="post">
     339                                                <p>
     340                                                        <label for="wp-comment-approved-notification-optin">
     341                                                                <input type="checkbox" id="wp-comment-approved-notification-optin" name="wp-comment-approved-notification-optin">
     342                                                                %2$s
     343                                                        </label>
     344                                                </p>
     345                                                <input type="hidden" name="comment_ID" value="%3$s">
     346                                                <input type="hidden" name="moderation-hash" value="%4$s">
     347                                                <input type="submit" class="button" value="%5$s">
     348                                        </form>',
     349                                        esc_url( site_url( '/wp-comments-post.php' ) ),
     350                                        esc_html__( 'I want to be notified by email when my comment is approved.' ),
     351                                        absint( $comment->comment_ID ),
     352                                        wp_hash( $comment->comment_date_gmt ),
     353                                        esc_html_x( 'Save', 'comment approved notification form' )
     354                                );
     355                        }
     356                }
     357
     358                // Skip the backcompat output.
     359                if ( $skip_backcompat ) {
     360                        $this->backcompat['needs_comment_approval_notification_output'] = false;
     361                }
     362
     363                // Return the approval notification/form.
     364                return $comment_approval_output;
     365        }
     366
    278367        /**
    279368         * Outputs a single comment.
    280369         *
    class Walker_Comment extends Walker { 
    297386
    298387                $commenter          = wp_get_current_commenter();
    299388                $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author'];
    300 
    301                 if ( $commenter['comment_author_email'] ) {
    302                         $moderation_note = __( 'Your comment is awaiting moderation.' );
    303                 } else {
    304                         $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
    305                 }
    306389                ?>
    307390                <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
    308391                <?php if ( 'div' !== $args['style'] ) : ?>
    class Walker_Comment extends Walker { 
    328411                        );
    329412                        ?>
    330413                </div>
    331                 <?php if ( '0' == $comment->comment_approved ) : ?>
    332                 <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
    333                 <br />
    334                 <?php endif; ?>
     414
     415                <?php
     416                // Output the comment moderation feedback if needed.
     417                $this->awaiting_moderation_text( $comment );
     418
     419                // Output the comment approval notification form if needed.
     420                echo $this->comment_approval_notification_form( $comment, true );
     421                ?>
    335422
    336423                <div class="comment-meta commentmetadata">
    337424                        <?php
    class Walker_Comment extends Walker { 
    401488
    402489                $commenter          = wp_get_current_commenter();
    403490                $show_pending_links = ! empty( $commenter['comment_author'] );
    404 
    405                 if ( $commenter['comment_author_email'] ) {
    406                         $moderation_note = __( 'Your comment is awaiting moderation.' );
    407                 } else {
    408                         $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
    409                 }
    410491                ?>
    411492                <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
    412493                        <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
    class Walker_Comment extends Walker { 
    450531                                                ?>
    451532                                        </div><!-- .comment-metadata -->
    452533
    453                                         <?php if ( '0' == $comment->comment_approved ) : ?>
    454                                         <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
    455                                         <?php endif; ?>
     534                                        <?php
     535                                        // Output the comment moderation feedback if needed.
     536                                        $this->awaiting_moderation_text( $comment );
     537
     538                                        // Output the comment approval notification form if needed.
     539                                        echo $this->comment_approval_notification_form( $comment, true );
     540                                        ?>
    456541                                </footer><!-- .comment-meta -->
    457542
    458543                                <div class="comment-content">
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 116e66a57c..3d5cbee07c 100644
    function wp_new_comment_notify_postauthor( $comment_ID ) { 
    23482348        return wp_notify_postauthor( $comment_ID );
    23492349}
    23502350
     2351/**
     2352 * Notify a comment author when their comment gets approved.
     2353 *
     2354 * This notification is only sent once when the comment status
     2355 * changes from unapproved to approved.
     2356 *
     2357 * @since 5.7.0
     2358 *
     2359 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
     2360 * @return bool Whether the email was sent successfully.
     2361 */
     2362function wp_new_comment_notify_comment_author( $comment_id ) {
     2363        $comment = get_comment( $comment_id );
     2364
     2365        if ( ! $comment ) {
     2366                return false;
     2367        }
     2368
     2369        $post           = get_post( $comment->comment_post_ID );
     2370        $comment_author = get_user_by( 'email', $comment->comment_author_email );
     2371
     2372        if ( ! $post ) {
     2373                return false;
     2374        }
     2375
     2376        // Make sure the comment author can be notified by email.
     2377        if ( empty( $comment->comment_author_email ) ) {
     2378                return false;
     2379        }
     2380
     2381        if ( true !== (bool) get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) {
     2382                return false;
     2383        }
     2384
     2385        /**
     2386         * The blogname option is escaped with esc_html when
     2387         * saved into the database, we need to reverse this for
     2388         * the plain text area of the email.
     2389         */
     2390        $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     2391
     2392        /* translators: 1: blog name, 2: post title */
     2393        $subject = sprintf( __( '[%1$s] Your comment on "%2$s" has been approved' ), $blogname, $post->post_title );
     2394
     2395        if ( ! empty( $comment->comment_author ) ) {
     2396                /* translators: 1: comment author's name */
     2397                $notify_message = sprintf( __( 'Howdy %s,' ), $comment->comment_author ) . "\r\n\r\n";
     2398        } else {
     2399                $notify_message = __( 'Howdy,' ) . "\r\n\r\n";
     2400        }
     2401
     2402        /* translators: 1: post title */
     2403        $notify_message .= sprintf( __( 'Your comment on the post "%s" has been approved.' ), $post->post_title ) . "\r\n\r\n";
     2404        /* translators: 1: comment permalink */
     2405        $notify_message .= sprintf( __( 'View comment: %s' ), get_comment_link( $comment ) ) . "\r\n";
     2406
     2407        $comment_approval_notification = array(
     2408                'subject' => $subject,
     2409                'message' => $notify_message,
     2410        );
     2411
     2412        /**
     2413         * Filters the content of the email sent to notify a comment author their comment has been approved.
     2414         *
     2415         * Content should be formatted for transmission via wp_mail().
     2416         *
     2417         * @since 5.7.0
     2418         *
     2419         * @param array $comment_approval_notification {
     2420         *     Used to build wp_mail().
     2421         *
     2422         *     @type string $subject The subject of the email.
     2423         *     @type string $message The content of the email.
     2424         * }
     2425         * @param WP_Comment $comment Comment object.
     2426         */
     2427        $comment_approval_notification = apply_filters( 'comment_approval_notification', $comment_approval_notification, $comment );
     2428
     2429        $sent = wp_mail(
     2430                $comment->comment_author_email,
     2431                wp_specialchars_decode( $comment_approval_notification['subject'] ),
     2432                $comment_approval_notification['message']
     2433        );
     2434
     2435        // Delete the opt-in now the notification has been sent.
     2436        delete_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin' );
     2437
     2438        return $sent;
     2439}
     2440
    23512441/**
    23522442 * Sets the status of a comment.
    23532443 *
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index 334a12a929..6a92a2be07 100644
    add_action( 'comment_post', 'wp_new_comment_notify_postauthor' ); 
    463463add_action( 'after_password_reset', 'wp_password_change_notification' );
    464464add_action( 'register_new_user', 'wp_send_new_user_notifications' );
    465465add_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10, 2 );
     466add_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' );
    466467
    467468// REST API actions.
    468469add_action( 'init', 'rest_api_init' );
  • tests/phpunit/tests/comment.php

    diff --git tests/phpunit/tests/comment.php tests/phpunit/tests/comment.php
    index c3013ad2c6..c244823989 100644
    class Tests_Comment extends WP_UnitTestCase { 
    554554                return $notify_message;
    555555        }
    556556
     557        /**
     558         * @ticket 33717
     559         */
     560        public function test_wp_new_comment_notify_comment_author_once_only() {
     561                $c = self::factory()->comment->create(
     562                        array(
     563                                'comment_post_ID'      => self::$post_id,
     564                                'comment_approved'     => '0',
     565                                'comment_author_email' => 'foo@bar.mail',
     566                        )
     567                );
     568
     569                // The user subscribed to receive an email once comment is approved.
     570                update_comment_meta( $c, '_wp_comment_author_notification_optin', true );
     571
     572                // For the purpose of the test we are removing this hook to directly use the function to notify the comment author.
     573                remove_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' );
     574
     575                // Approve the comment.
     576                wp_set_comment_status( $c, 'approve' );
     577
     578                $sent = wp_new_comment_notify_comment_author( $c );
     579                $this->assertTrue( $sent );
     580
     581                $resent = wp_new_comment_notify_comment_author( $c );
     582                $this->assertFalse( $resent );
     583        }
     584
     585        /**
     586         * @ticket 33717
     587         */
     588        public function test_wp_new_comment_notify_comment_author_has_not_optin() {
     589                $c = self::factory()->comment->create(
     590                        array(
     591                                'comment_post_ID'      => self::$post_id,
     592                                'comment_approved'     => '0',
     593                                'comment_author_email' => 'bat@man.mail',
     594                        )
     595                );
     596
     597                // For the purpose of the test we are removing this hook to directly use the function to notify the comment author.
     598                remove_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' );
     599
     600                // Approve the comment.
     601                wp_set_comment_status( $c, 'approve' );
     602
     603                $sent = wp_new_comment_notify_comment_author( $c );
     604
     605                // The user hasn't subscribed to receive an email, no email should be sent.
     606                $this->assertFalse( $sent );
     607        }
     608
    557609        /**
    558610         * @ticket 12431
    559611         */