Make WordPress Core

Ticket #50628: 50628.diff

File 50628.diff, 8.6 KB (added by azaozz, 6 years ago)
  • src/wp-includes/media.php

     
    15221522                // Check if the relative image path from the image meta is at the end of $image_location.
    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'] );
    15281528
     
    15301530                                $dirname = trailingslashit( $dirname );
    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;
    15391538                                }
    15401539                        }
     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                                        }
     1549                                }
     1550                        }
    15411551                }
    15421552        }
    15431553
  • tests/phpunit/tests/media.php

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: tests/phpunit/data/images/test-image-large.jpg
    ___________________________________________________________________
    Added: svn:mime-type
    ## -0,0 +1 ##
    +application/octet-stream
    \ No newline at end of property
    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: tests/phpunit/data/images/test-image-large.png
    ___________________________________________________________________
    Deleted: svn:mime-type
    ## -1 +0,0 ##
    -application/octet-stream
    \ No newline at end of property
     
    77class Tests_Media extends WP_UnitTestCase {
    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 ) {
    1213                self::$_sizes                          = wp_get_additional_image_sizes();
    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        }
    1819
     
    14791480                        }
    14801481                }
    14811482
    1482                 // Add the full size width at the end.
    1483                 $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
     1483                $expected = trim( $expected, ' ,' );
    14841484
    14851485                foreach ( $intermediates as $int ) {
    14861486                        $image_url       = wp_get_attachment_image_url( self::$large_id, $int );
    14871487                        $size_array      = $this->_get_image_size_array_from_meta( $image_meta, $int );
     1488
     1489                        if ( 'full' === $int ) {
     1490                                // Add the full size image.
     1491                                $expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected;
     1492                        }
     1493
    14881494                        $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
    14891495                        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    14901496                }
     
    15001506                add_filter( 'upload_dir', '_upload_dir_no_subdir' );
    15011507
    15021508                // Make an image.
    1503                 $filename = DIR_TESTDATA . '/images/test-image-large.png';
     1509                $filename = DIR_TESTDATA . '/images/' . self::$large_filename;
    15041510                $id       = self::factory()->attachment->create_upload_object( $filename );
    15051511
    15061512                $image_meta      = wp_get_attachment_metadata( $id );
     
    15241530                        }
    15251531                }
    15261532
    1527                 // Add the full size width at the end.
    1528                 $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
     1533                $expected = trim( $expected, ' ,' );
    15291534
    15301535                foreach ( $intermediates as $int ) {
    15311536                        $size_array      = $this->_get_image_size_array_from_meta( $image_meta, $int );
    15321537                        $image_url       = wp_get_attachment_image_url( $id, $int );
     1538
     1539                        if ( 'full' === $int ) {
     1540                                // Add the full size image.
     1541                                $expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected;
     1542                        }
     1543
    15331544                        $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
    15341545                        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    15351546                }
     
    15521563                // Copy hash generation method used in wp_save_image().
    15531564                $hash = 'e' . time() . rand( 100, 999 );
    15541565
    1555                 $filename_base = wp_basename( $image_meta['file'], '.png' );
     1566                $filename_base = wp_basename( $image_meta['file'], '-scaled.jpg' );
    15561567
    15571568                // Add the hash to the image URL.
    15581569                $image_url = str_replace( $filename_base, $filename_base . '-' . $hash, $image_url );
    15591570
    15601571                // 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'] );
    15621572                $image_meta['sizes']['medium']['file']       = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] );
    15631573                $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] );
    15641574                $image_meta['sizes']['large']['file']        = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] );
     
    16011611                        }
    16021612                }
    16031613
    1604                 // Add the full size width at the end.
    1605                 $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
     1614                $expected       = trim( $expected, ' ,' );
     1615                $full_size_file = $image_meta['file'];
    16061616
    16071617                // Prepend an absolute path to simulate a pre-2.7 upload.
    16081618                $image_meta['file'] = 'H:\home\wordpress\trunk/wp-content/uploads/' . $image_meta['file'];
     
    16101620                foreach ( $intermediates as $int ) {
    16111621                        $image_url       = wp_get_attachment_image_url( self::$large_id, $int );
    16121622                        $size_array      = $this->_get_image_size_array_from_meta( $image_meta, $int );
     1623
     1624                        if ( 'full' === $int ) {
     1625                                // Add the full size image.
     1626                                $expected = $uploads_dir_url . $full_size_file . ' ' . $image_meta['width'] . 'w, ' . $expected;
     1627                        }
     1628
    16131629                        $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
    16141630                        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    16151631                }
     
    18551871                $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    18561872
    18571873                $image_meta = wp_get_attachment_metadata( self::$large_id );
    1858                 $size_array = array( 1600, 1200 ); // Full size.
     1874                $size_array = array( $image_meta['width'], $image_meta['height'] ); // Full size.
    18591875
    18601876                $srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta );
    18611877
     
    22722288                $expected = '<img width="999" height="999" src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png"' .
    22732289                        ' class="attachment-testsize size-testsize" alt="" loading="lazy"' .
    22742290                        ' 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"' .
     2291                                ' http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-large-150x150.jpg 150w"' .
    22762292                                ' sizes="(max-width: 999px) 100vw, 999px" />';
    22772293
    22782294                remove_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ) );
     
    28522868
    28532869                $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
    28542870        }
     2871
     2872        /**
     2873         * @ticket 50543
     2874         */
     2875        function test_wp_image_file_matches_image_meta_original_image() {
     2876                $image_meta = wp_get_attachment_metadata( self::$large_id );
     2877                $image_src  = wp_get_original_image_url( self::$large_id );
     2878
     2879                $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
     2880        }
    28552881}
    28562882
    28572883/**