Media: Ensure wp_mine_type_icon() returns expected file type.

Add an argument to `wp_mime_type_icon()` to control the file type returned. Following [57638], there are two file formats in the media icons directory. Different systems would pull up different files by default dependent on the order loaded into the cached array, causing intermittent test failures and unpredictable behavior.

Function update allows core usages to always return the `.svg` while maintaining backwards compatibility for any extended usage that expects a `.png`. Follow up to [57638].

Also handles a missed case in media list view.

Props SergeyBiryukov, sabernhardt, joedolson, antpb.
Fixes #31352.

git-svn-id: https://develop.svn.wordpress.org/trunk@57687 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Dolson 2024-02-21 19:25:18 +00:00
parent 8ced706f7f
commit c2cca0dcfc
5 changed files with 22 additions and 11 deletions

View File

@ -99,7 +99,7 @@ class WP_Customize_Media_Control extends WP_Customize_Control {
'id' => 1,
'url' => $this->setting->default,
'type' => $type,
'icon' => wp_mime_type_icon( $type ),
'icon' => wp_mime_type_icon( $type, '.svg' ),
'title' => wp_basename( $this->setting->default ),
);

View File

@ -1910,7 +1910,7 @@ function get_attachment_icon_src( $id = 0, $fullsize = false ) {
$src = wp_get_attachment_url( $post->ID );
$src_file = & $file;
} elseif ( $src = wp_mime_type_icon( $post->ID ) ) {
} elseif ( $src = wp_mime_type_icon( $post->ID, '.svg' ) ) {
// No thumb, no image. We'll look for a mime-related icon instead.
/** This filter is documented in wp-includes/post.php */

View File

@ -972,14 +972,22 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
$src = false;
if ( $icon ) {
$src = wp_mime_type_icon( $attachment_id );
$src = wp_mime_type_icon( $attachment_id, '.svg' );
if ( $src ) {
/** This filter is documented in wp-includes/post.php */
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
$src_file = $icon_dir . '/' . wp_basename( $src );
$src_file = $icon_dir . '/' . wp_basename( $src );
list( $width, $height ) = wp_getimagesize( $src_file );
$ext = strtolower( substr( $src_file, -4 ) );
if ( '.svg' === $ext ) {
// SVG does not have true dimensions, so this assigns width and height directly.
$width = 48;
$height = 64;
} else {
list( $width, $height ) = wp_getimagesize( $src_file );
}
}
}
@ -3067,7 +3075,7 @@ function wp_playlist_shortcode( $attr ) {
list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'thumbnail' );
$track['thumb'] = compact( 'src', 'width', 'height' );
} else {
$src = wp_mime_type_icon( $attachment->ID );
$src = wp_mime_type_icon( $attachment->ID, '.svg' );
$width = 48;
$height = 64;
$track['image'] = compact( 'src', 'width', 'height' );
@ -4339,7 +4347,7 @@ function wp_prepare_attachment_for_js( $attachment ) {
'mime' => $attachment->post_mime_type,
'type' => $type,
'subtype' => $subtype,
'icon' => wp_mime_type_icon( $attachment->ID ),
'icon' => wp_mime_type_icon( $attachment->ID, '.svg' ),
'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ),
'nonces' => array(
'update' => false,
@ -4510,7 +4518,7 @@ function wp_prepare_attachment_for_js( $attachment ) {
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' );
$response['thumb'] = compact( 'src', 'width', 'height' );
} else {
$src = wp_mime_type_icon( $attachment->ID );
$src = wp_mime_type_icon( $attachment->ID, '.svg' );
$width = 48;
$height = 64;
$response['image'] = compact( 'src', 'width', 'height' );

View File

@ -6803,10 +6803,11 @@ function wp_attachment_is_image( $post = null ) {
*
* @since 2.1.0
*
* @param string|int $mime MIME type or attachment ID.
* @param string|int $mime MIME type or attachment ID.
* @param string $preferred_ext File format to prefer in return. Default .png.
* @return string|false Icon, false otherwise.
*/
function wp_mime_type_icon( $mime = 0 ) {
function wp_mime_type_icon( $mime = 0, $preferred_ext = '.png' ) {
if ( ! is_numeric( $mime ) ) {
$icon = wp_cache_get( "mime_type_icon_$mime" );
}
@ -6885,7 +6886,9 @@ function wp_mime_type_icon( $mime = 0 ) {
}
continue;
}
$icon_files[ "$dir/$file" ] = "$uri/$file";
if ( $ext === $preferred_ext ) {
$icon_files[ "$dir/$file" ] = "$uri/$file";
}
}
closedir( $dh );
}

View File

@ -399,7 +399,7 @@ https://w.org</a>',
$this->assertSame( '', $prepped['subtype'] );
// #21963, there will be a GUID always, so there will be a URL.
$this->assertNotEquals( '', $prepped['url'] );
$this->assertSame( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
$this->assertSame( site_url( 'wp-includes/images/media/default.svg' ), $prepped['icon'] );
// Fake a mime.
$post->post_mime_type = 'image/jpeg';