WordPress.org

Make WordPress Core

Ticket #35030: 35030.1.patch

File 35030.1.patch, 9.4 KB (added by jaspermdegroot, 6 years ago)
  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 1c8387b..8eb5268 100644
    function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 
    10521052         */
    10531053        $max_srcset_image_width = apply_filters( 'max_srcset_image_width', 1600, $size_array );
    10541054
    1055         // Array to hold URL candidates.
    1056         $sources = array();
     1055        // Arrays to hold URL candidates.
     1056        $sources = $src = $alternatives = array();
    10571057
    10581058        /**
    10591059         * To make sure the ID matches our image src, we will check to see if any sizes in our attachment
    function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 
    10671067         * versions of the same edit.
    10681068         */
    10691069        foreach ( $image_sizes as $image ) {
     1070                $is_src = false;
    10701071
    10711072                // Check if image meta isn't corrupted.
    10721073                if ( ! is_array( $image ) ) {
    function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 
    10761077                // If the file name is part of the `src`, we've confirmed a match.
    10771078                if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
    10781079                        $src_matched = true;
     1080                        $is_src      = true;
    10791081                }
    10801082
    10811083                // Filter out images that are from previous edits.
    function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 
    10871089                 * Filter out images that are wider than '$max_srcset_image_width' unless
    10881090                 * that file is in the 'src' attribute.
    10891091                 */
    1090                 if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width &&
    1091                         false === strpos( $image_src, $image['file'] ) ) {
    1092 
     1092                if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && ! $is_src ) {
    10931093                        continue;
    10941094                }
    10951095
    function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 
    11091109                // If the image dimensions are within 1px of the expected size, use it.
    11101110                if ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ) {
    11111111                        // Add the URL, descriptor, and value to the sources array to be returned.
    1112                         $sources[ $image['width'] ] = array(
     1112                        $source = array(
    11131113                                'url'        => $image_baseurl . $image['file'],
    11141114                                'descriptor' => 'w',
    11151115                                'value'      => $image['width'],
    11161116                        );
     1117
     1118                        /*
     1119                         * Use two arrays to make sure the 'src' image becomes first in the 'srcset', because
     1120                         * of a bug in iOS8 that makes it always select the first source from the set. See #35030.
     1121                         */
     1122                        if ( $is_src ) {
     1123                                $src[ $image['width'] ] = $source;
     1124                        } else {
     1125                                $alternatives[ $image['width'] ] = $source;
     1126                        }
    11171127                }
    11181128        }
    11191129
     1130        $sources = $src + $alternatives;
     1131
    11201132        /**
    11211133         * Filter an image's 'srcset' sources.
    11221134         *
  • tests/phpunit/tests/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index a58360a..31b0b9f 100644
    EOF; 
    845845                                return array( 1024, 768 );
    846846                        case 'full':
    847847                                return array( 1600, 1200 ); // actual size of ../data/images/test-image-large.png
     848                        case 'twentyfifteen-logo':
     849                                return array( 248, 186 );
    848850                        default:
    849851                                return array( 800, 600 ); // soft-resized image
    850852                }
    851853        }
    852854
    853855        /**
     856         * Helper function to move the src image to the first position in the expected srcset string.
     857         */
     858        function _src_first( $srcset, $src_url, $src_width ) {
     859                $src_string = $src_url . ' ' . $src_width . 'w';
     860                $src_not_first = ', ' . $src_string;
     861
     862                if ( strpos( $srcset, $src_not_first ) ) {
     863                        $srcset = str_replace( $src_not_first, '', $srcset );
     864                        $srcset = $src_string . ', ' . $srcset;
     865                }
     866
     867                return $srcset;
     868        }
     869
     870        /**
    854871         * @ticket 33641
    855872         */
    856873        function test_wp_calculate_image_srcset() {
    EOF; 
    885902                foreach ( $intermediates as $int ) {
    886903                        $image_url = wp_get_attachment_image_url( self::$large_id, $int );
    887904                        $size_array = $this->_get_image_size_array_from_name( $int );
    888                         $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
     905                        $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
     906                        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    889907                }
    890908        }
    891909
    EOF; 
    929947                foreach ( $intermediates as $int ) {
    930948                        $size_array = $this->_get_image_size_array_from_name( $int );
    931949                        $image_url = wp_get_attachment_image_url( $id, $int );
    932                         $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
     950                        $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
     951                        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    933952                }
    934953
    935954                // Remove the attachment
    EOF; 
    10081027                foreach ( $intermediates as $int ) {
    10091028                        $image_url = wp_get_attachment_image_url( self::$large_id, $int );
    10101029                        $size_array = $this->_get_image_size_array_from_name( $int );
    1011                         $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
     1030                        $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
     1031                        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    10121032                }
    10131033        }
    10141034
    EOF; 
    11231143                        ),
    11241144                );
    11251145
    1126                 $expected_srcset = '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, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 2000w';
     1146                $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';
    11271147
    11281148                $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
    11291149        }
    EOF; 
    12021222        }
    12031223
    12041224        /**
     1225         * Helper filter function to shuffle the image meta sizes array.
     1226         */
     1227        function _shuffle_sizes( $image_meta ) {
     1228                $sizes = $image_meta['sizes'];
     1229                $keys = array_keys( $sizes );
     1230                $image_meta_sizes = array();
     1231
     1232                shuffle( $keys );
     1233
     1234                foreach( $keys as $key ) {
     1235                        $image_meta_sizes[ $key ] = $sizes[ $key ];
     1236                }
     1237
     1238                $image_meta['sizes'] = $image_meta_sizes;
     1239
     1240                return $image_meta;
     1241        }
     1242
     1243        /**
     1244         * @ticket 35030
     1245         */
     1246        function test_wp_calculate_image_srcset_src_first() {
     1247                global $_wp_additional_image_sizes;
     1248
     1249                $year_month = date('Y/m');
     1250                $image_meta = wp_get_attachment_metadata( self::$large_id );
     1251                $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
     1252
     1253                // Set up test cases for all expected size names.
     1254                $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
     1255
     1256                // Add any soft crop intermediate sizes.
     1257                foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
     1258                        if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
     1259                                $intermediates[] = $name;
     1260                        }
     1261                }
     1262
     1263                $expected = "";
     1264
     1265                foreach( $image_meta['sizes'] as $name => $size ) {
     1266                        // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
     1267                        if ( in_array( $name, $intermediates ) ) {
     1268                                $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
     1269                        }
     1270                }
     1271
     1272                // Add the full size width at the end.
     1273                $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
     1274
     1275                foreach ( $intermediates as $int ) {
     1276                        $image_url = wp_get_attachment_image_url( self::$large_id, $int );
     1277                        $size_array = $this->_get_image_size_array_from_name( $int );
     1278
     1279                        // The expected first source in the srcset is the src image.
     1280                        $expected_first = $image_url . ' ' . $size_array[0] . 'w';
     1281
     1282                        // Shuffle the sizes array used by 'wp_calculate_image_srcset()' to create the srcset.
     1283                        add_filter( 'wp_calculate_image_srcset_meta', array( $this, '_shuffle_sizes' ) );
     1284
     1285                        // Get the first source from the calculated srcset and compare with the expected first source.
     1286                        preg_match( '/[^,]+/', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ), $srcset_first );
     1287                        $this->assertSame( $expected_first, $srcset_first[0] );
     1288
     1289                        remove_filter( 'wp_calculate_image_srcset_meta', array( $this, '_shuffle_sizes' ) );
     1290                }
     1291        }
     1292
     1293        /**
    12051294         * @ticket 33641
    12061295         */
    12071296        function test_wp_get_attachment_image_srcset() {
    EOF; 
    12351324
    12361325                $expected .= $uploads_dir . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
    12371326
    1238                 $this->assertSame( $expected, $srcset );
     1327                $expected_srcset = $this->_src_first( $expected, $uploads_dir . $image_meta['file'], $size_array[0] );
     1328
     1329                $this->assertSame( $expected_srcset, $srcset );
    12391330        }
    12401331
    12411332        /**
    EOF; 
    15171608
    15181609                $_SERVER['HTTPS'] = 'on';
    15191610
    1520                 $expected = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-300x150.jpg 300w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-1024x512.jpg 1024w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg 1200w';
     1611                $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';
    15211612                $actual   = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
    15221613
    15231614                $this->assertSame( $expected, $actual );