WordPress.org

Make WordPress Core

Changeset 47988


Ignore:
Timestamp:
06/10/2020 07:31:08 PM (7 months ago)
Author:
whyisjake
Message:

Editor: Ensure latest comments can only be viewed from public posts.

This brings the changes from [47984] to the 5.0 branch.

Props: poena, xknown.

Location:
branches/5.0
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0

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

    r43827 r47988  
    570570
    571571/**
    572  * Retrieve the excerpt of the current comment.
    573  *
    574  * Will cut each word and only output the first 20 words with '…' at the end.
    575  * If the word count is less than 20, then no truncating is done and no '…'
    576  * will appear.
     572 * Retrieves the excerpt of the given comment.
     573 *
     574 * Returns a maximum of 20 words with an ellipsis appended if necessary.
    577575 *
    578576 * @since 1.5.0
     
    581579 * @param int|WP_Comment $comment_ID  WP_Comment or ID of the comment for which to get the excerpt.
    582580 *                                    Default current comment.
    583  * @return string The maybe truncated comment with 20 words or less.
     581 * @return string The possibly truncated comment excerpt.
    584582 */
    585583function get_comment_excerpt( $comment_ID = 0 ) {
    586584    $comment = get_comment( $comment_ID );
    587     $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
    588     $words = explode( ' ', $comment_text );
    589 
    590     /**
    591      * Filters the amount of words used in the comment excerpt.
     585
     586    if ( ! post_password_required( $comment->comment_post_ID ) ) {
     587        $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
     588    } else {
     589        $comment_text = __( 'Password protected' );
     590    }
     591
     592    /* translators: Maximum number of words used in a comment excerpt. */
     593    $comment_excerpt_length = intval( _x( '20', 'comment_excerpt_length' ) );
     594
     595    /**
     596     * Filters the maximum number of words used in the comment excerpt.
    592597     *
    593598     * @since 4.4.0
     
    595600     * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
    596601     */
    597     $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );
    598 
    599     $use_ellipsis = count( $words ) > $comment_excerpt_length;
    600     if ( $use_ellipsis ) {
    601         $words = array_slice( $words, 0, $comment_excerpt_length );
    602     }
    603 
    604     $excerpt = trim( join( ' ', $words ) );
    605     if ( $use_ellipsis ) {
    606         $excerpt .= '…';
    607     }
     602    $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
     603
     604    $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' );
     605
    608606    /**
    609607     * Filters the retrieved comment excerpt.
  • branches/5.0/tests/phpunit/tests/blocks/render.php

    r43884 r47988  
    264264    }
    265265
     266    public function test_render_latest_comments_on_password_protected_post() {
     267        $post_id      = self::factory()->post->create(
     268            array(
     269                'post_password' => 'password',
     270            )
     271        );
     272        $comment_text = wp_generate_password( 10, false );
     273        self::factory()->comment->create(
     274            array(
     275                'comment_post_ID' => $post_id,
     276                'comment_content' => $comment_text,
     277            )
     278        );
     279        $comments = do_blocks( '<!-- wp:latest-comments {"commentsToShow":1,"displayExcerpt":true} /-->' );
     280
     281        $this->assertNotContains( $comment_text, $comments );
     282    }
     283
    266284    /**
    267285     * @ticket 45109
Note: See TracChangeset for help on using the changeset viewer.