Skip to content

Commit e3356d8

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/branches/5.0@47988 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 20545be commit e3356d8

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/wp-includes/comment-template.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -569,42 +569,40 @@ function comment_date( $d = '', $comment_ID = 0 ) {
569569
}
570570

571571
/**
572-
* Retrieve the excerpt of the current comment.
572+
* Retrieves the excerpt of the given comment.
573573
*
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.
574+
* Returns a maximum of 20 words with an ellipsis appended if necessary.
577575
*
578576
* @since 1.5.0
579577
* @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
580578
*
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
*/
585583
function 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 );
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' ) );
589594

590595
/**
591-
* Filters the amount of words used in the comment excerpt.
596+
* Filters the maximum number of words used in the comment excerpt.
592597
*
593598
* @since 4.4.0
594599
*
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 );
602+
$comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
598603

599-
$use_ellipsis = count( $words ) > $comment_excerpt_length;
600-
if ( $use_ellipsis ) {
601-
$words = array_slice( $words, 0, $comment_excerpt_length );
602-
}
604+
$excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' );
603605

604-
$excerpt = trim( join( ' ', $words ) );
605-
if ( $use_ellipsis ) {
606-
$excerpt .= '…';
607-
}
608606
/**
609607
* Filters the retrieved comment excerpt.
610608
*

tests/phpunit/tests/blocks/render.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ public function test_global_post_persistence() {
263263
$this->assertEquals( $global_post, $post );
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
268286
*/

0 commit comments

Comments
 (0)