Make WordPress Core

Changeset 57687


Ignore:
Timestamp:
02/21/2024 07:25:18 PM (4 months ago)
Author:
joedolson
Message:

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.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/customize/class-wp-customize-media-control.php

    r57524 r57687  
    100100                    'url'   => $this->setting->default,
    101101                    'type'  => $type,
    102                     'icon'  => wp_mime_type_icon( $type ),
     102                    'icon'  => wp_mime_type_icon( $type, '.svg' ),
    103103                    'title' => wp_basename( $this->setting->default ),
    104104                );
  • trunk/src/wp-includes/deprecated.php

    r57524 r57687  
    19111911        $src = wp_get_attachment_url( $post->ID );
    19121912        $src_file = & $file;
    1913     } elseif ( $src = wp_mime_type_icon( $post->ID ) ) {
     1913    } elseif ( $src = wp_mime_type_icon( $post->ID, '.svg' ) ) {
    19141914        // No thumb, no image. We'll look for a mime-related icon instead.
    19151915
  • trunk/src/wp-includes/media.php

    r57597 r57687  
    973973
    974974        if ( $icon ) {
    975             $src = wp_mime_type_icon( $attachment_id );
     975            $src = wp_mime_type_icon( $attachment_id, '.svg' );
    976976
    977977            if ( $src ) {
     
    979979                $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
    980980
    981                 $src_file               = $icon_dir . '/' . wp_basename( $src );
     981                $src_file = $icon_dir . '/' . wp_basename( $src );
    982982                list( $width, $height ) = wp_getimagesize( $src_file );
     983                $ext      = strtolower( substr( $src_file, -4 ) );
     984                if ( '.svg' === $ext ) {
     985                    // SVG does not have true dimensions, so this assigns width and height directly.
     986                    $width  = 48;
     987                    $height = 64;
     988                } else {
     989                    list( $width, $height ) = wp_getimagesize( $src_file );
     990                }
    983991            }
    984992        }
     
    30683076                $track['thumb']               = compact( 'src', 'width', 'height' );
    30693077            } else {
    3070                 $src            = wp_mime_type_icon( $attachment->ID );
     3078                $src            = wp_mime_type_icon( $attachment->ID, '.svg' );
    30713079                $width          = 48;
    30723080                $height         = 64;
     
    43404348        'type'          => $type,
    43414349        'subtype'       => $subtype,
    4342         'icon'          => wp_mime_type_icon( $attachment->ID ),
     4350        'icon'          => wp_mime_type_icon( $attachment->ID, '.svg' ),
    43434351        'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ),
    43444352        'nonces'        => array(
     
    45114519            $response['thumb']            = compact( 'src', 'width', 'height' );
    45124520        } else {
    4513             $src               = wp_mime_type_icon( $attachment->ID );
     4521            $src               = wp_mime_type_icon( $attachment->ID, '.svg' );
    45144522            $width             = 48;
    45154523            $height            = 64;
  • trunk/src/wp-includes/post.php

    r57644 r57687  
    68046804 * @since 2.1.0
    68056805 *
    6806  * @param string|int $mime MIME type or attachment ID.
     6806 * @param string|int $mime          MIME type or attachment ID.
     6807 * @param string     $preferred_ext File format to prefer in return. Default .png.
    68076808 * @return string|false Icon, false otherwise.
    68086809 */
    6809 function wp_mime_type_icon( $mime = 0 ) {
     6810function wp_mime_type_icon( $mime = 0, $preferred_ext = '.png' ) {
    68106811    if ( ! is_numeric( $mime ) ) {
    68116812        $icon = wp_cache_get( "mime_type_icon_$mime" );
     
    68866887                            continue;
    68876888                        }
    6888                         $icon_files[ "$dir/$file" ] = "$uri/$file";
     6889                        if ( $ext === $preferred_ext ) {
     6890                            $icon_files[ "$dir/$file" ] = "$uri/$file";
     6891                        }
    68896892                    }
    68906893                    closedir( $dh );
  • trunk/tests/phpunit/tests/media.php

    r57019 r57687  
    400400        // #21963, there will be a GUID always, so there will be a URL.
    401401        $this->assertNotEquals( '', $prepped['url'] );
    402         $this->assertSame( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
     402        $this->assertSame( site_url( 'wp-includes/images/media/default.svg' ), $prepped['icon'] );
    403403
    404404        // Fake a mime.
Note: See TracChangeset for help on using the changeset viewer.