Make WordPress Core

Ticket #49956: 49956.3.diff

File 49956.3.diff, 5.0 KB (added by audrasjb, 5 years ago)

Comments: Remove unapproved comments preview after 1 minute to avoid public access through the moderation hash

  • src/wp-includes/class-walker-comment.php

    diff --git a/src/wp-includes/class-walker-comment.php b/src/wp-includes/class-walker-comment.php
    index 7325fd0a6a..646a4c0a97 100644
    a b class Walker_Comment extends Walker { 
    181181                        return;
    182182                }
    183183
     184                if ( 'comment' === $comment->comment_type ) {
     185                        add_filter( 'comment_text', array( $this, 'comment_text' ), 40, 2 );
     186                }
     187
    184188                if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
    185189                        ob_start();
    186190                        $this->ping( $comment, $depth, $args );
    class Walker_Comment extends Walker { 
    194198                        $this->comment( $comment, $depth, $args );
    195199                        $output .= ob_get_clean();
    196200                }
     201
     202                if ( 'comment' === $comment->comment_type ) {
     203                        remove_filter( 'comment_text', array( $this, 'comment_text' ), 40, 2 );
     204                }
    197205        }
    198206
    199207        /**
    class Walker_Comment extends Walker { 
    244252                <?php
    245253        }
    246254
     255        /**
     256         * Remove links from the pending comment's text if the commenter has not consent to the comment cookie.
     257         *
     258         * @since 5.4.2
     259         *
     260         * @param string          $comment_text Text of the current comment.
     261         * @param WP_Comment|null $comment      The comment object.
     262         * @return string                       Text of the current comment.
     263         */
     264        function comment_text( $comment_text, $comment ) {
     265                $commenter          = wp_get_current_commenter();
     266                $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author'];
     267
     268                if ( '0' == $comment->comment_approved && ! $show_pending_links ) {
     269                        return wp_kses( $comment_text, array() );
     270                }
     271
     272                return $comment_text;
     273        }
     274
    247275        /**
    248276         * Outputs a single comment.
    249277         *
    class Walker_Comment extends Walker { 
    264292                        $add_below = 'div-comment';
    265293                }
    266294
    267                 $commenter = wp_get_current_commenter();
     295                $commenter          = wp_get_current_commenter();
     296                $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author'];
    268297                if ( $commenter['comment_author_email'] ) {
    269298                        $moderation_note = __( 'Your comment is awaiting moderation.' );
    270299                } else {
    class Walker_Comment extends Walker { 
    279308                <div class="comment-author vcard">
    280309                        <?php
    281310                        if ( 0 != $args['avatar_size'] ) {
    282                                 echo get_avatar( $comment, $args['avatar_size'] );}
     311                                echo get_avatar( $comment, $args['avatar_size'] );
     312                        }
    283313                        ?>
    284314                        <?php
    285                                 printf(
    286                                         /* translators: %s: Comment author link. */
    287                                         __( '%s <span class="says">says:</span>' ),
    288                                         sprintf( '<cite class="fn">%s</cite>', get_comment_author_link( $comment ) )
    289                                 );
     315                        $comment_author = get_comment_author_link( $comment );
     316                        if ( '0' == $comment->comment_approved && ! $show_pending_links ) {
     317                                $comment_author = get_comment_author( $comment );
     318                        }
     319                        printf(
     320                                /* translators: %s: Comment author link. */
     321                                __( '%s <span class="says">says:</span>' ),
     322                                sprintf( '<cite class="fn">%s</cite>', $comment_author )
     323                        );
    290324                        ?>
    291325                </div>
    292326                <?php if ( '0' == $comment->comment_approved ) : ?>
    class Walker_Comment extends Walker { 
    354388        protected function html5_comment( $comment, $depth, $args ) {
    355389                $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
    356390
    357                 $commenter = wp_get_current_commenter();
     391                $commenter          = wp_get_current_commenter();
     392                $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author'];
    358393                if ( $commenter['comment_author_email'] ) {
    359394                        $moderation_note = __( 'Your comment is awaiting moderation.' );
    360395                } else {
    class Walker_Comment extends Walker { 
    372407                                                }
    373408                                                ?>
    374409                                                <?php
    375                                                         printf(
    376                                                                 /* translators: %s: Comment author link. */
    377                                                                 __( '%s <span class="says">says:</span>' ),
    378                                                                 sprintf( '<b class="fn">%s</b>', get_comment_author_link( $comment ) )
    379                                                         );
     410                                                $comment_author = get_comment_author_link( $comment );
     411                                                if ( '0' == $comment->comment_approved && ! $show_pending_links ) {
     412                                                        $comment_author = get_comment_author( $comment );
     413                                                }
     414                                                printf(
     415                                                        /* translators: %s: Comment author link. */
     416                                                        __( '%s <span class="says">says:</span>' ),
     417                                                        sprintf( '<b class="fn">%s</b>', $comment_author )
     418                                                );
    380419                                                ?>
    381420                                        </div><!-- .comment-author -->
    382421
  • src/wp-includes/comment.php

    diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php
    index 8648a89a0f..9139bb1b7c 100644
    a b function wp_get_unapproved_comment_author_email() { 
    18471847                $comment    = get_comment( $comment_id );
    18481848
    18491849                if ( $comment && hash_equals( $_GET['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) {
    1850                         $commenter_email = $comment->comment_author_email;
     1850                        // The comment will only be viewable by the comment author for 1 minute.
     1851                        $comment_timestamp = strtotime( $comment->comment_date_gmt . '+1 minute' );
     1852                        $current_timestamp = strtotime( current_time( 'mysql', 1 ) );
     1853
     1854                        if ( $current_timestamp < $comment_timestamp ) {
     1855                                $commenter_email = $comment->comment_author_email;
     1856                        }
    18511857                }
    18521858        }
    18531859