Skip to content
Closed
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
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 fallback text for attachments that have no author.
*
* @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">' . __( '(no author)' ) . '</span>';
}
}

/**
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 62913
* @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 62913
* @covers WP_Media_List_Table::column_author
*/
public function test_column_author_with_no_author() {
wp_set_current_user( self::$admin );

// Create an attachment post with no author (post_author set to 0).
$post_id = self::factory()->attachment->create( array( 'post_author' => 0 ) );
$post = get_post( $post_id );

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

$this->assertStringContainsString( '<span aria-hidden="true">&#8212;</span>', $output );
$this->assertStringContainsString( '<span class="screen-reader-text">(no author)</span>', $output );
$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
Loading