WordPress.org

Make WordPress Core

Changeset 48454


Ignore:
Timestamp:
07/12/2020 09:18:38 PM (6 months ago)
Author:
azaozz
Message:

Media: Fix wp_image_file_matches_image_meta() to also match when the original image URL is used.

Also refreshes and fixes some of the unit tests for media, and replaces test-image-large.png with test-image-large.jpg which is larger to allow testing of original_image in the image meta.

Fixes #50628.

Location:
trunk
Files:
1 added
1 deleted
3 edited

Legend:

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

    r48430 r48454  
    15231523        if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) {
    15241524            $match = true;
    1525         } elseif ( ! empty( $image_meta['sizes'] ) ) {
     1525        } else {
    15261526            // Retrieve the uploads sub-directory from the full size image.
    15271527            $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
     
    15311531            }
    15321532
    1533             foreach ( $image_meta['sizes'] as $image_size_data ) {
    1534                 $relative_path = $dirname . $image_size_data['file'];
     1533            if ( ! empty( $image_meta['original_image'] ) ) {
     1534                $relative_path = $dirname . $image_meta['original_image'];
    15351535
    15361536                if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
    15371537                    $match = true;
    1538                     break;
     1538                }
     1539            }
     1540
     1541            if ( ! $match && ! empty( $image_meta['sizes'] ) ) {
     1542                foreach ( $image_meta['sizes'] as $image_size_data ) {
     1543                    $relative_path = $dirname . $image_size_data['file'];
     1544
     1545                    if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
     1546                        $match = true;
     1547                        break;
     1548                    }
    15391549                }
    15401550            }
  • trunk/tests/phpunit/tests/image/functions.php

    r48341 r48454  
    346346            DIR_TESTDATA . '/images/' . __FUNCTION__ . '.jpg'
    347347        );
     348
     349        if ( is_wp_error( $file ) && $file->get_error_code() === 'invalid_image' ) {
     350            $this->markTestSkipped( 'Tests_Image_Functions::test_wp_crop_image_url() cannot access remote image.' );
     351        }
     352
    348353        $this->assertNotWPError( $file );
    349354        $this->assertFileExists( $file );
  • trunk/tests/phpunit/tests/media.php

    r48331 r48454  
    88    protected static $large_id;
    99    protected static $_sizes;
     10    protected static $large_filename = 'test-image-large.jpg';
    1011
    1112    public static function wpSetUpBeforeClass( $factory ) {
     
    1314        $GLOBALS['_wp_additional_image_sizes'] = array();
    1415
    15         $filename       = DIR_TESTDATA . '/images/test-image-large.png';
     16        $filename       = DIR_TESTDATA . '/images/' . self::$large_filename;
    1617        self::$large_id = $factory->attachment->create_upload_object( $filename );
    1718    }
     
    13331334    function test_wp_get_attachment_image_defaults() {
    13341335        $image    = image_downsize( self::$large_id, 'thumbnail' );
    1335         $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />', $image[1], $image[2], $image[0] );
     1336        $expected = sprintf(
     1337            '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />',
     1338            $image[1],
     1339            $image[2],
     1340            $image[0]
     1341        );
    13361342
    13371343        $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
     
    13481354
    13491355        $image    = image_downsize( self::$large_id, 'thumbnail' );
    1350         $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />', $image[1], $image[2], $image[0] );
     1356        $expected = sprintf(
     1357            '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />',
     1358            $image[1],
     1359            $image[2],
     1360            $image[0]
     1361        );
    13511362
    13521363        $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
     
    14801491        }
    14811492
    1482         // Add the full size width at the end.
    1483         $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
     1493        $expected = trim( $expected, ' ,' );
    14841494
    14851495        foreach ( $intermediates as $int ) {
    1486             $image_url       = wp_get_attachment_image_url( self::$large_id, $int );
    1487             $size_array      = $this->_get_image_size_array_from_meta( $image_meta, $int );
    1488             $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
     1496            $image_url  = wp_get_attachment_image_url( self::$large_id, $int );
     1497            $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
     1498
     1499            if ( 'full' === $int ) {
     1500                // Add the full size image. Expected to be in the srcset when the full size image is used as src.
     1501                $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected;
     1502            } else {
     1503                $_expected = $expected;
     1504            }
     1505
     1506            $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] );
    14891507            $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    14901508        }
     
    15011519
    15021520        // Make an image.
    1503         $filename = DIR_TESTDATA . '/images/test-image-large.png';
     1521        $filename = DIR_TESTDATA . '/images/' . self::$large_filename;
    15041522        $id       = self::factory()->attachment->create_upload_object( $filename );
    15051523
     
    15251543        }
    15261544
    1527         // Add the full size width at the end.
    1528         $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
     1545        $expected = trim( $expected, ' ,' );
    15291546
    15301547        foreach ( $intermediates as $int ) {
    1531             $size_array      = $this->_get_image_size_array_from_meta( $image_meta, $int );
    1532             $image_url       = wp_get_attachment_image_url( $id, $int );
    1533             $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
     1548            $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
     1549            $image_url  = wp_get_attachment_image_url( $id, $int );
     1550
     1551            if ( 'full' === $int ) {
     1552                // Add the full size image. Expected to be in the srcset when the full size image is used as src.
     1553                $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected;
     1554            } else {
     1555                $_expected = $expected;
     1556            }
     1557
     1558            $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] );
    15341559            $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    15351560        }
     
    15531578        $hash = 'e' . time() . rand( 100, 999 );
    15541579
    1555         $filename_base = wp_basename( $image_meta['file'], '.png' );
     1580        $filename_base = wp_basename( self::$large_filename, '.jpg' );
     1581        $filename_hash = "{$filename_base}-{$hash}";
    15561582
    15571583        // Add the hash to the image URL.
    1558         $image_url = str_replace( $filename_base, $filename_base . '-' . $hash, $image_url );
     1584        $image_url = str_replace( $filename_base, $filename_hash, $image_url );
    15591585
    15601586        // Replace file paths for full and medium sizes with hashed versions.
    1561         $image_meta['file']                          = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] );
    1562         $image_meta['sizes']['medium']['file']       = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] );
    1563         $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] );
    1564         $image_meta['sizes']['large']['file']        = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] );
     1587        $image_meta['sizes']['medium']['file']       = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium']['file'] );
     1588        $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium_large']['file'] );
     1589        $image_meta['sizes']['large']['file']        = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['large']['file'] );
    15651590
    15661591        // Calculate a srcset array.
     
    16021627        }
    16031628
    1604         // Add the full size width at the end.
    1605         $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
     1629        $expected       = trim( $expected, ' ,' );
     1630        $full_size_file = $image_meta['file'];
    16061631
    16071632        // Prepend an absolute path to simulate a pre-2.7 upload.
     
    16091634
    16101635        foreach ( $intermediates as $int ) {
    1611             $image_url       = wp_get_attachment_image_url( self::$large_id, $int );
    1612             $size_array      = $this->_get_image_size_array_from_meta( $image_meta, $int );
    1613             $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
     1636            $image_url  = wp_get_attachment_image_url( self::$large_id, $int );
     1637            $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
     1638
     1639            if ( 'full' === $int ) {
     1640                // Add the full size image. Expected to be in the srcset when the full size image is used as src.
     1641                $_expected = $uploads_dir_url . $full_size_file . ' ' . $image_meta['width'] . 'w, ' . $expected;
     1642            } else {
     1643                $_expected = $expected;
     1644            }
     1645
     1646            $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] );
    16141647            $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    16151648        }
     
    16821715        );
    16831716
    1684         $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png 218w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-600x824.png 600w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055.png 768w';
     1717        $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
     1718
     1719        $expected_srcset = $uploads_url . 'test-768x1055-218x300.png 218w, ' .
     1720            $uploads_url . 'test-768x1055-600x824.png 600w, ' .
     1721            $uploads_url . 'test-768x1055.png 768w';
    16851722
    16861723        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
     
    17271764        );
    17281765
    1729         $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 2000w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w';
     1766        $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
     1767
     1768        $expected_srcset = $uploads_url . 'test.png 2000w, ' .
     1769            $uploads_url . 'test-300x150.png 300w, ' .
     1770            $uploads_url . 'test-768x384.png 768w, ' .
     1771            $uploads_url . 'test-1024x512.png 1024w';
    17301772
    17311773        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
     
    18441886        );
    18451887
    1846         $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test%20image-300x150.png 300w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test%20image-768x384.png 768w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test%20image-1024x512.png 1024w';
     1888        $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
     1889
     1890        $expected_srcset = $uploads_url . 'test%20image-300x150.png 300w, ' .
     1891            $uploads_url . 'test%20image-768x384.png 768w, ' .
     1892            $uploads_url . 'test%20image-1024x512.png 1024w';
    18471893
    18481894        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( array( 300, 150 ), $image_src, $image_meta ) );
     
    18561902
    18571903        $image_meta = wp_get_attachment_metadata( self::$large_id );
    1858         $size_array = array( 1600, 1200 ); // Full size.
     1904        $size_array = array( $image_meta['width'], $image_meta['height'] ); // Full size.
    18591905
    18601906        $srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta );
     
    20122058            %7$s';
    20132059
    2014         $content_unfiltered = sprintf( $content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id, $img_with_sizes_attr, $img_xhtml, $img_html5 );
    2015         $content_filtered   = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $respimg_with_sizes_attr, $respimg_xhtml, $respimg_html5 );
     2060        $content_unfiltered = sprintf(
     2061            $content,
     2062            $img,
     2063            $img_no_size_in_class,
     2064            $img_no_width_height,
     2065            $img_no_size_id,
     2066            $img_with_sizes_attr,
     2067            $img_xhtml,
     2068            $img_html5
     2069        );
     2070
     2071        $content_filtered = sprintf(
     2072            $content,
     2073            $respimg,
     2074            $respimg_no_size_in_class,
     2075            $respimg_no_width_height,
     2076            $img_no_size_id,
     2077            $respimg_with_sizes_attr,
     2078            $respimg_xhtml,
     2079            $respimg_html5
     2080        );
    20162081
    20172082        // Do not add width, height, and loading.
     
    21352200            %3$s';
    21362201
    2137         $unfiltered = sprintf( $content, $img, $img_https, $img_relative );
    2138         $expected   = sprintf( $content, $respimg, $respimg_https, $respimg_relative );
    2139         $actual     = wp_filter_content_tags( $unfiltered );
     2202        $unfiltered = sprintf(
     2203            $content,
     2204            $img,
     2205            $img_https,
     2206            $img_relative
     2207        );
     2208
     2209        $expected = sprintf(
     2210            $content,
     2211            $respimg,
     2212            $respimg_https,
     2213            $respimg_relative
     2214        );
     2215
     2216        $actual = wp_filter_content_tags( $unfiltered );
    21402217
    21412218        $this->assertSame( $expected, $actual );
     
    21772254        $_SERVER['HTTPS'] = 'on';
    21782255
    2179         $expected = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-1024x512.jpg 1024w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-300x150.jpg 300w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg 1200w';
    2180         $actual   = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
     2256        $uploads_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
     2257
     2258        $expected = $uploads_url . 'test-1024x512.jpg 1024w, ' .
     2259            $uploads_url . 'test-300x150.jpg 300w, ' .
     2260            $uploads_url . 'test.jpg 1200w';
     2261
     2262        $actual = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
    21812263
    21822264        $this->assertSame( $expected, $actual );
     
    22662348        remove_all_filters( 'wp_calculate_image_sizes' );
    22672349
     2350        $basename    = wp_basename( self::$large_filename, '.jpg' );
     2351        $year_month  = gmdate( 'Y/m' );
     2352        $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/';
     2353
     2354        $expected = '<img width="999" height="999" ' .
     2355            'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' .
     2356            'class="attachment-testsize size-testsize" alt="" loading="lazy" ' .
     2357            'srcset="' . $uploads_url . 'test-image-testsize-999x999.jpg 999w, ' . $uploads_url . $basename . '-150x150.jpg 150w" ' .
     2358            'sizes="(max-width: 999px) 100vw, 999px" />';
     2359
    22682360        $actual = wp_get_attachment_image( self::$large_id, 'testsize' );
    2269         $year   = gmdate( 'Y' );
    2270         $month  = gmdate( 'm' );
    2271 
    2272         $expected = '<img width="999" height="999" src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png"' .
    2273             ' class="attachment-testsize size-testsize" alt="" loading="lazy"' .
    2274             ' srcset="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png 999w,' .
    2275                 ' http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-large-150x150.png 150w"' .
    2276                 ' sizes="(max-width: 999px) 100vw, 999px" />';
    22772361
    22782362        remove_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ) );
     
    22832367    function _filter_36246( $data, $attachment_id ) {
    22842368        $data['sizes']['testsize'] = array(
    2285             'file'      => 'test-image-testsize-999x999.png',
     2369            'file'      => 'test-image-testsize-999x999.jpg',
    22862370            'width'     => 999,
    22872371            'height'    => 999,
    2288             'mime-type' => 'image/png',
     2372            'mime-type' => 'image/jpg',
    22892373        );
    22902374        return $data;
     
    28532937        $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
    28542938    }
     2939
     2940    /**
     2941     * @ticket 50543
     2942     */
     2943    function test_wp_image_file_matches_image_meta_original_image() {
     2944        $image_meta = wp_get_attachment_metadata( self::$large_id );
     2945        $image_src  = wp_get_original_image_url( self::$large_id );
     2946
     2947        $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
     2948    }
    28552949}
    28562950
Note: See TracChangeset for help on using the changeset viewer.