close
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Prevent empty author links in media list tables
  • Loading branch information
snehapatil2001 committed Feb 6, 2025
commit aefac31995a3f29ea19fceace446958e53504678
17 changes: 12 additions & 5 deletions src/wp-admin/includes/class-wp-media-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,22 @@ public function column_title( $post ) {
* Handles the author column output.
*
* @since 4.3.0
* @since 6.8.0 Added check for empty author and fallback display
Comment thread
snehapatil2001 marked this conversation as resolved.
Outdated
*
* @param WP_Post $post The current WP_Post object.
*/
public function column_author( $post ) {
printf(
'<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'author' => get_the_author_meta( 'ID' ) ), 'upload.php' ) ),
get_the_author()
);
$author = get_the_author();

if ( ! empty( $author ) ) {
printf(
'<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'author' => get_the_author_meta( 'ID' ) ), 'upload.php' ) ),
esc_html( $author )
);
} else {
echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . esc_html__( '(no author)', 'default' ) . '</span>';
Comment thread
snehapatil2001 marked this conversation as resolved.
Outdated
}
}

/**
Expand Down
45 changes: 45 additions & 0 deletions tests/phpunit/tests/admin/wpMediaListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,51 @@ public function tear_down() {
parent::tear_down();
}

/**
* Tests that column_author() displays the correct author link when an author exists.
*
* @ticket 12345
Comment thread
snehapatil2001 marked this conversation as resolved.
Outdated
* @covers WP_Media_List_Table::column_author
*/
public function test_column_author_with_valid_author() {
wp_set_current_user( self::$admin );

$author_id = self::factory()->user->create( array( 'role' => 'author' ) );
$post = self::factory()->post->create_and_get( array( 'post_author' => $author_id ) );

$author_name = get_the_author_meta( 'display_name', $author_id );
$expected_url = esc_url( add_query_arg( array( 'author' => $author_id ), 'upload.php' ) );

ob_start();
self::$list_table->column_author( $post );
$output = ob_get_clean();

$this->assertStringContainsString( '<a href="' . $expected_url . '">', $output );
$this->assertStringContainsString( '>' . esc_html( $author_name ) . '</a>', $output );
}

/**
* Tests that column_author() displays the correct fallback when no author exists.
*
* @ticket 12345
Comment thread
snehapatil2001 marked this conversation as resolved.
Outdated
* @covers WP_Media_List_Table::column_author
*/
public function test_column_author_with_no_author() {
wp_set_current_user( self::$admin );

add_filter( 'the_author', '__return_empty_string' );
Comment thread
snehapatil2001 marked this conversation as resolved.
Outdated

ob_start();
self::$list_table->column_author( self::$post );
$output = ob_get_clean();

remove_filter( 'the_author', '__return_empty_string' );

$this->assertStringContainsString( '<span aria-hidden="true">&#8212;</span>', $output );
$this->assertStringContainsString( '<span class="screen-reader-text">' . esc_html__( '(no author)', 'default' ) . '</span>', $output );
Comment thread
snehapatil2001 marked this conversation as resolved.
Outdated
$this->assertStringNotContainsString( '<a href="', $output );
}

/**
* Tests that a call to WP_Media_List_Table::prepare_items() on a site without any scheduled events
* does not result in a PHP warning.
Expand Down