Make WordPress Core


Ignore:
Timestamp:
01/31/2021 12:48:24 PM (4 years ago)
Author:
johnbillion
Message:

Comments: Introduce a method for commenters to opt-in to receiving an email notification when their moderated comment gets approved.

The opt-in form is shown after the comment is submitted and held for moderation.

Sorry this took five years.

Props jeffr0, swissspidy, mrahmadawais, wonderboymusic, jdgrimes, obenland, Monika, imath, garrett-eclipse, johnbillion

Fixes #33717

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-comments-post.php

    r49108 r50109  
    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 ( $comment && 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>' . __( 'Invalid comment ID.' ) . '</p>',
     33            __( 'Comment Notification Opt-in Failure' ),
    3234            array(
    33                 'response'  => $data,
     35                'response'  => 404,
    3436                'back_link' => true,
    3537            )
    3638        );
    37     } else {
    38         exit;
    3939    }
     40} else {
     41    $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
     42    if ( is_wp_error( $comment ) ) {
     43        $data = (int) $comment->get_error_data();
     44        if ( ! empty( $data ) ) {
     45            wp_die(
     46                '<p>' . $comment->get_error_message() . '</p>',
     47                __( 'Comment Submission Failure' ),
     48                array(
     49                    'response'  => $data,
     50                    'back_link' => true,
     51                )
     52            );
     53        } else {
     54            exit;
     55        }
     56    }
     57
     58    $user            = wp_get_current_user();
     59    $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );
     60
     61    /**
     62     * Perform other actions when comment cookies are set.
     63     *
     64     * @since 3.4.0
     65     * @since 4.9.6 The `$cookies_consent` parameter was added.
     66     *
     67     * @param WP_Comment $comment         Comment object.
     68     * @param WP_User    $user            Comment author's user object. The user may not exist.
     69     * @param bool       $cookies_consent Comment author's consent to store cookies.
     70     */
     71    do_action( 'set_comment_cookies', $comment, $user, $cookies_consent );
    4072}
    41 
    42 $user            = wp_get_current_user();
    43 $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );
    44 
    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 );
    5673
    5774$location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment->comment_ID;
Note: See TracChangeset for help on using the changeset viewer.