Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/wp-admin/includes/class-wp-comments-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,9 @@ public function column_date( $comment ) {

echo '<div class="submitted-on">';

if ( 'approved' === wp_get_comment_status( $comment ) && ! empty( $comment->comment_post_ID ) ) {
$post_type = ! empty( $comment->comment_post_ID ) ? get_post_type( $comment->comment_post_ID ) : false;

if ( 'approved' === wp_get_comment_status( $comment ) && $post_type && post_type_exists( $post_type ) ) {
printf(
'<a href="%s">%s</a>',
esc_url( get_comment_link( $comment ) ),
Expand Down Expand Up @@ -1121,7 +1123,10 @@ public function column_response( $comment ) {
$this->pending_count[ $post->ID ] = $pending_comments;
}

if ( current_user_can( 'edit_post', $post->ID ) ) {
// Posts of an unregistered post type have no edit screen or permalink to link to.
$post_type_object = get_post_type_object( $post->post_type );

if ( $post_type_object && current_user_can( 'edit_post', $post->ID ) ) {
$post_link = "<a href='" . get_edit_post_link( $post->ID ) . "' class='comments-edit-item-link'>";
$post_link .= esc_html( get_the_title( $post->ID ) ) . '</a>';
} else {
Expand All @@ -1139,8 +1144,9 @@ public function column_response( $comment ) {

echo $post_link;

$post_type_object = get_post_type_object( $post->post_type );
echo "<a href='" . get_permalink( $post->ID ) . "' class='comments-view-item-link'>" . $post_type_object->labels->view_item . '</a>';
if ( $post_type_object ) {
echo "<a href='" . get_permalink( $post->ID ) . "' class='comments-view-item-link'>" . $post_type_object->labels->view_item . '</a>';
}

echo '<span class="post-com-count-wrapper post-com-count-', $post->ID, '">';
$this->comments_bubble( $post->ID, $pending_comments );
Expand Down
58 changes: 58 additions & 0 deletions tests/phpunit/tests/admin/wpCommentsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,62 @@ public static function data_comment_type(): array {
'all type requested' => array( 'all' ),
);
}

/**
* Tests that the response column of a comment on a post of an unregistered post type
* shows the post title as plain text, rather than reading labels of a null object.
*
* @ticket 36058
*
* @covers WP_Comments_List_Table::column_response
*/
public function test_column_response_should_not_link_post_of_unregistered_post_type() {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );

$post_id = self::factory()->post->create(
array(
'post_type' => 'wptests_unregistered',
'post_title' => 'Post of an unregistered type',
)
);
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) );

$GLOBALS['post'] = get_post( $post_id );

// The capability check for the comment count bubble reports the unregistered post type.
$this->setExpectedIncorrectUsage( 'map_meta_cap' );

ob_start();
$this->table->column_response( get_comment( $comment_id ) );
$output = ob_get_clean();

$this->assertStringContainsString( 'Post of an unregistered type', $output, 'The post title should be shown.' );
$this->assertStringNotContainsString( 'comments-edit-item-link', $output, 'The edit link should be omitted.' );
$this->assertStringNotContainsString( 'comments-view-item-link', $output, 'The view link should be omitted.' );
}

/**
* Tests that the date of an approved comment on a post of an unregistered post type
* is not linked to a comment permalink that cannot work.
*
* @ticket 36058
*
* @covers WP_Comments_List_Table::column_date
*/
public function test_column_date_should_not_link_comment_on_unregistered_post_type() {
$post_id = self::factory()->post->create( array( 'post_type' => 'wptests_unregistered' ) );
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => '1',
)
);

ob_start();
$this->table->column_date( get_comment( $comment_id ) );
$output = ob_get_clean();

$this->assertStringContainsString( 'submitted-on', $output, 'The date should be shown.' );
$this->assertStringNotContainsString( '<a ', $output, 'The date should not be linked.' );
}
}
Loading