Make WordPress Core

Changeset 47986


Ignore:
Timestamp:
06/10/2020 07:25:25 PM (3 years ago)
Author:
whyisjake
Message:

Editor: Ensure latest comments can only be viewed from public posts.
This brings the changes from [47984] to the 5.2 branch.
Props: poena, xknown.

Location:
branches/5.2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.2

  • branches/5.2/src/wp-includes/comment-template.php

    r47917 r47986  
    592592 */
    593593function get_comment_excerpt( $comment_ID = 0 ) {
    594     $comment      = get_comment( $comment_ID );
    595     $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
    596     $words        = explode( ' ', $comment_text );
    597 
    598     /**
    599      * Filters the amount of words used in the comment excerpt.
     594    $comment = get_comment( $comment_ID );
     595
     596    if ( ! post_password_required( $comment->comment_post_ID ) ) {
     597        $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
     598    } else {
     599        $comment_text = __( 'Password protected' );
     600    }
     601
     602    /* translators: Maximum number of words used in a comment excerpt. */
     603    $comment_excerpt_length = intval( _x( '20', 'comment_excerpt_length' ) );
     604
     605    /**
     606     * Filters the maximum number of words used in the comment excerpt.
    600607     *
    601608     * @since 4.4.0
     
    603610     * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
    604611     */
    605     $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );
    606 
    607     $use_ellipsis = count( $words ) > $comment_excerpt_length;
    608     if ( $use_ellipsis ) {
    609         $words = array_slice( $words, 0, $comment_excerpt_length );
    610     }
    611 
    612     $excerpt = trim( join( ' ', $words ) );
    613     if ( $use_ellipsis ) {
    614         $excerpt .= '…';
    615     }
     612    $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
     613
     614    $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' );
     615
    616616    /**
    617617     * Filters the retrieved comment excerpt.
     
    23122312        'must_log_in'          => '<p class="must-log-in">' . sprintf(
    23132313            /* translators: %s: login URL */
    2314                                     __( 'You must be <a href="%s">logged in</a> to post a comment.' ),
     2314            __( 'You must be <a href="%s">logged in</a> to post a comment.' ),
    23152315            wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
    23162316        ) . '</p>',
     
    23182318        'logged_in_as'         => '<p class="logged-in-as">' . sprintf(
    23192319            /* translators: 1: edit user link, 2: accessibility text, 3: user name, 4: logout URL */
    2320                                     __( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ),
     2320            __( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ),
    23212321            get_edit_user_link(),
    23222322            /* translators: %s: user name */
  • branches/5.2/tests/phpunit/tests/blocks/render.php

    r45139 r47986  
    290290    }
    291291
     292    public function test_render_latest_comments_on_password_protected_post() {
     293        $post_id      = self::factory()->post->create(
     294            array(
     295                'post_password' => 'password',
     296            )
     297        );
     298        $comment_text = wp_generate_password( 10, false );
     299        self::factory()->comment->create(
     300            array(
     301                'comment_post_ID' => $post_id,
     302                'comment_content' => $comment_text,
     303            )
     304        );
     305        $comments = do_blocks( '<!-- wp:latest-comments {"commentsToShow":1,"displayExcerpt":true} /-->' );
     306
     307        $this->assertNotContains( $comment_text, $comments );
     308    }
     309
    292310    /**
    293311     * @ticket 45109
Note: See TracChangeset for help on using the changeset viewer.