Skip to content

Commit e220135

Browse files
committed
Media: guard attachment icon size lookup
1 parent 5a87725 commit e220135

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

src/wp-includes/media.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,9 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
953953
// Get a thumbnail or intermediate image if there is one.
954954
$image = image_downsize( $attachment_id, $size );
955955
if ( ! $image ) {
956-
$src = false;
956+
$src = false;
957+
$width = 0;
958+
$height = 0;
957959

958960
if ( $icon ) {
959961
$src = wp_mime_type_icon( $attachment_id );
@@ -962,8 +964,13 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
962964
/** This filter is documented in wp-includes/post.php */
963965
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
964966

965-
$src_file = $icon_dir . '/' . wp_basename( $src );
966-
list( $width, $height ) = wp_getimagesize( $src_file );
967+
$src_file = $icon_dir . '/' . wp_basename( $src );
968+
$image_size = wp_getimagesize( $src_file );
969+
970+
if ( is_array( $image_size ) ) {
971+
$width = $image_size[0];
972+
$height = $image_size[1];
973+
}
967974
}
968975
}
969976

tests/phpunit/tests/media.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,31 @@ public function test_wp_get_attachment_image_url() {
15471547
$this->assertSame( $image[0], wp_get_attachment_image_url( $attachment_id ) );
15481548
}
15491549

1550+
/**
1551+
* @ticket 64742
1552+
*/
1553+
public function test_wp_get_attachment_image_src_with_icon_when_icon_file_size_cannot_be_read() {
1554+
$post_id = self::factory()->post->create();
1555+
$attachment_id = self::factory()->attachment->create_object(
1556+
'test.pdf',
1557+
$post_id,
1558+
array(
1559+
'post_mime_type' => 'application/pdf',
1560+
'post_type' => 'attachment',
1561+
)
1562+
);
1563+
1564+
$filter = static function () {
1565+
return 'https://example.org/wp-includes/images/media/missing-icon.png';
1566+
};
1567+
1568+
add_filter( 'wp_mime_type_icon', $filter );
1569+
1570+
$this->assertFalse( wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) );
1571+
1572+
remove_filter( 'wp_mime_type_icon', $filter );
1573+
}
1574+
15501575
/**
15511576
* @ticket 12235
15521577
*/

0 commit comments

Comments
 (0)