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-includes/class-walker-comment.php

    r49160 r50109  
    4040        'id'     => 'comment_ID',
    4141    );
     42
     43    /**
     44     * Whether the comment approval notification opt-in form or message needs to be
     45     * output automatically.
     46     *
     47     * @since 5.7.0
     48     *
     49     * @var bool
     50     */
     51    protected $needs_comment_approval_notification_output = true;
    4252
    4353    /**
     
    256266     * Filters the comment text.
    257267     *
    258      * Removes links from the pending comment's text if the commenter did not consent
    259      * to the comment cookies.
     268     *   - Removes links from the pending comment's text if the commenter did not consent
     269     *     to the comment cookies
     270     *   - Prepends the approval notification opt-in form or message to pending comments
    260271     *
    261272     * @since 5.4.2
     273     * @since 5.7.0 Comment approval notification opt-in form is now automatically
     274     *              appended if necessary.
    262275     *
    263276     * @param string          $comment_text Text of the current comment.
     
    273286        }
    274287
     288        /*
     289         * Checks if we need to output the comment approval notification opt-in form.
     290         */
     291        if ( $this->needs_comment_approval_notification_output ) {
     292            $comment_text = $this->comment_approval_notification_form( $comment ) . "\n" . $comment_text;
     293        }
     294
     295        $this->needs_comment_approval_notification_output = true;
     296
    275297        return $comment_text;
     298    }
     299
     300    /**
     301     * Outputs the awaiting moderation text.
     302     *
     303     * @since 5.7.0
     304     *
     305     * @param WP_Comment $comment Comment to display.
     306     */
     307    protected function awaiting_moderation_text( $comment ) {
     308        if ( '0' !== $comment->comment_approved ) {
     309            return;
     310        }
     311
     312        $commenter = wp_get_current_commenter();
     313
     314        if ( $commenter['comment_author_email'] ) {
     315            $moderation_note = __( 'Your comment is awaiting moderation.' );
     316        } else {
     317            $moderation_note = __( 'Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.' );
     318        }
     319
     320        printf(
     321            '<em class="comment-awaiting-moderation">%s</em>',
     322            esc_html( $moderation_note )
     323        );
     324    }
     325
     326    /**
     327     * Gets the comment approval notification opt-in form or message for a pending comment.
     328     *
     329     * @since 5.7.0
     330     *
     331     * @param WP_Comment $comment Comment to display.
     332     * @return string HTML output.
     333     */
     334    protected function comment_approval_notification_form( $comment ) {
     335        $comment_approval_output = '';
     336
     337        if ( '0' === $comment->comment_approved && has_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' ) ) {
     338            if ( get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) {
     339                $comment_approval_output = sprintf(
     340                    '<p><em class="wp-comment-approved-notification-optedin">%s</em></p>',
     341                    esc_html__( 'You will receive an email when your comment is approved.' )
     342                );
     343            } else {
     344                $comment_approval_output = sprintf(
     345                    '<form action="%1$s" method="post">
     346                        <p>
     347                            <input type="checkbox" id="wp-comment-approved-notification-optin" name="wp-comment-approved-notification-optin">
     348                            <label for="wp-comment-approved-notification-optin">
     349                                %2$s
     350                            </label>
     351                        </p>
     352                        <input type="hidden" name="comment_ID" value="%3$s">
     353                        <input type="hidden" name="moderation-hash" value="%4$s">
     354                        <input type="submit" class="button" value="%5$s">
     355                    </form>',
     356                    esc_url( site_url( '/wp-comments-post.php' ) ),
     357                    esc_html__( 'I want to be notified by email when my comment is approved.' ),
     358                    absint( $comment->comment_ID ),
     359                    wp_hash( $comment->comment_date_gmt ),
     360                    esc_html_x( 'Save', 'comment approval notification form' )
     361                );
     362            }
     363        }
     364
     365        // Disable the backcompat output.
     366        $this->needs_comment_approval_notification_output = false;
     367
     368        // Return the approval notification opt-in form.
     369        return $comment_approval_output;
    276370    }
    277371
     
    298392        $commenter          = wp_get_current_commenter();
    299393        $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         }
    306394        ?>
    307395        <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
     
    329417            ?>
    330418        </div>
    331         <?php if ( '0' == $comment->comment_approved ) : ?>
    332         <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
    333         <br />
    334         <?php endif; ?>
     419
     420        <?php
     421        // Output the comment moderation feedback if needed.
     422        $this->awaiting_moderation_text( $comment );
     423
     424        // Output the comment approval notification opt-in form if needed.
     425        echo $this->comment_approval_notification_form( $comment );
     426        ?>
    335427
    336428        <div class="comment-meta commentmetadata">
     
    402494        $commenter          = wp_get_current_commenter();
    403495        $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         }
    410496        ?>
    411497        <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
     
    451537                    </div><!-- .comment-metadata -->
    452538
    453                     <?php if ( '0' == $comment->comment_approved ) : ?>
    454                     <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
    455                     <?php endif; ?>
     539                    <?php
     540                    // Output the comment moderation feedback if needed.
     541                    $this->awaiting_moderation_text( $comment );
     542
     543                    // Output the comment approval notification opt-in form if needed.
     544                    echo $this->comment_approval_notification_form( $comment );
     545                    ?>
    456546                </footer><!-- .comment-meta -->
    457547
Note: See TracChangeset for help on using the changeset viewer.