Skip to content

Commit

Permalink
Editor: Ensure latest comments can only be viewed from public posts.
Browse files Browse the repository at this point in the history
This brings the changes from [47984] to the 5.2 branch.
Props: poena, xknown.


git-svn-id: https://develop.svn.wordpress.org/branches/5.2@47986 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
whyisjake committed Jun 10, 2020
1 parent c20e760 commit e5e5fa1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/wp-includes/comment-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,28 +591,28 @@ function comment_date( $d = '', $comment_ID = 0 ) {
* @return string The maybe truncated comment with 20 words or less.
*/
function get_comment_excerpt( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID );
$comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
$words = explode( ' ', $comment_text );
$comment = get_comment( $comment_ID );

if ( ! post_password_required( $comment->comment_post_ID ) ) {
$comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
} else {
$comment_text = __( 'Password protected' );
}

/* translators: Maximum number of words used in a comment excerpt. */
$comment_excerpt_length = intval( _x( '20', 'comment_excerpt_length' ) );

/**
* Filters the amount of words used in the comment excerpt.
* Filters the maximum number of words used in the comment excerpt.
*
* @since 4.4.0
*
* @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
*/
$comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );
$comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );

$use_ellipsis = count( $words ) > $comment_excerpt_length;
if ( $use_ellipsis ) {
$words = array_slice( $words, 0, $comment_excerpt_length );
}
$excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' );

$excerpt = trim( join( ' ', $words ) );
if ( $use_ellipsis ) {
$excerpt .= '…';
}
/**
* Filters the retrieved comment excerpt.
*
Expand Down Expand Up @@ -2311,13 +2311,13 @@ function comment_form( $args = array(), $post_id = null ) {
/** This filter is documented in wp-includes/link-template.php */
'must_log_in' => '<p class="must-log-in">' . sprintf(
/* translators: %s: login URL */
__( 'You must be <a href="%s">logged in</a> to post a comment.' ),
__( 'You must be <a href="%s">logged in</a> to post a comment.' ),
wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
) . '</p>',
/** This filter is documented in wp-includes/link-template.php */
'logged_in_as' => '<p class="logged-in-as">' . sprintf(
/* translators: 1: edit user link, 2: accessibility text, 3: user name, 4: logout URL */
__( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ),
__( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ),
get_edit_user_link(),
/* translators: %s: user name */
esc_attr( sprintf( __( 'Logged in as %s. Edit your profile.' ), $user_identity ) ),
Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/tests/blocks/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,24 @@ public function test_global_post_persistence() {
$this->assertEquals( $global_post, $post );
}

public function test_render_latest_comments_on_password_protected_post() {
$post_id = self::factory()->post->create(
array(
'post_password' => 'password',
)
);
$comment_text = wp_generate_password( 10, false );
self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_content' => $comment_text,
)
);
$comments = do_blocks( '<!-- wp:latest-comments {"commentsToShow":1,"displayExcerpt":true} /-->' );

$this->assertNotContains( $comment_text, $comments );
}

/**
* @ticket 45109
*/
Expand Down

0 comments on commit e5e5fa1

Please sign in to comment.