From c2cca0dcfcb36ea888b1bc278ece707eb7a6163d Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Wed, 21 Feb 2024 19:25:18 +0000 Subject: [PATCH] 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 --- .../class-wp-customize-media-control.php | 2 +- src/wp-includes/deprecated.php | 2 +- src/wp-includes/media.php | 18 +++++++++++++----- src/wp-includes/post.php | 9 ++++++--- tests/phpunit/tests/media.php | 2 +- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/customize/class-wp-customize-media-control.php b/src/wp-includes/customize/class-wp-customize-media-control.php index 3bdc4e2dc1..ba8d8e9d31 100644 --- a/src/wp-includes/customize/class-wp-customize-media-control.php +++ b/src/wp-includes/customize/class-wp-customize-media-control.php @@ -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 ), ); diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 45b4f89a9e..4de4a93039 100644 --- a/src/wp-includes/deprecated.php +++ b/src/wp-includes/deprecated.php @@ -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 */ diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index b5e477e7f7..5fb5a27230 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -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' ); diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 04fb143a1d..d035acb045 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -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 ); } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 8d91517b1f..7cc9f5470f 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -399,7 +399,7 @@ https://w.org', $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';