Make WordPress Core

Ticket #35045: 35045.2.diff

File 35045.2.diff, 3.5 KB (added by joemcgill, 9 years ago)
  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 89b88df..c279bad 100644
    function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 
    12781278                return $image;
    12791279        }
    12801280
    1281         $base_url = trailingslashit( _wp_upload_dir_baseurl() );
    1282         $image_base_url = $base_url;
     1281        /**
     1282         * To make sure that our ID and image src match, we loop through all the sizes
     1283         * in our attachment metadata and bail early if our src file isn't included.
     1284         */
     1285        $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
     1286        $all_sizes[] = $image_meta['file'];
    12831287
    12841288        $dirname = dirname( $image_meta['file'] );
    1285         if ( $dirname !== '.' ) {
    1286                 $image_base_url .= trailingslashit( $dirname );
    1287         }
    12881289
    1289         $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
     1290        $matched = false;
    12901291
    1291         foreach ( $all_sizes as $key => $file ) {
    1292                 $all_sizes[ $key ] = $image_base_url . $file;
    1293         }
     1292        foreach( $all_sizes as $size ) {
     1293                // If uploads use month/day directories, prepend that to the filename of the size.
     1294                if ( $size !== $image_meta['file'] && $dirname !== '.' ) {
     1295                        $size = trailingslashit( $dirname ) . $size;
     1296                }
    12941297
    1295         // Add the original image.
    1296         $all_sizes[] = $base_url . $image_meta['file'];
     1298                if ( false !== strpos( $image_src, $size ) ) {
     1299                        $matched = true;
     1300                        break;
     1301                }
     1302        }
    12971303
    12981304        // Bail early if the image src doesn't match any of the known image sizes.
    1299         if ( ! in_array( $image_src, $all_sizes ) ) {
     1305        if ( ! $matched ) {
    13001306                return $image;
    13011307        }
    13021308
  • tests/phpunit/tests/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index 3da75a0..b9d87c5 100644
    EOF; 
    11331133                // Intermediate sized GIFs should not include the full size in the srcset.
    11341134                $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
    11351135        }
     1136
     1137        /**
     1138         * @ticket 35045
     1139         * @ticket 33641
     1140         */
     1141        function test_wp_make_content_images_responsive_schemes() {
     1142                $image_meta = wp_get_attachment_metadata( self::$large_id );
     1143                $size_array = $this->_get_image_size_array_from_name( 'medium' );
     1144
     1145                $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
     1146                $sizes  = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
     1147
     1148                // Build HTML for the editor.
     1149                $img          = get_image_tag( self::$large_id, '', '', '', 'medium' );
     1150                $img_https    = str_replace( 'http://', 'https://', $img );
     1151                $img_relative = str_replace( 'http://', '//', $img );
     1152
     1153                // Manually add srcset and sizes to the markup from get_image_tag().
     1154                $respimg          = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
     1155                $respimg_https    = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https );
     1156                $respimg_relative = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative );
     1157
     1158                $content = '
     1159                        <p>Image, http: protocol. Should have srcset and sizes.</p>
     1160                        %1$s
     1161
     1162                        <p>Image, http: protocol. Should have srcset and sizes.</p>
     1163                        %2$s
     1164
     1165                        <p>Image, protocol-relative. Should have srcset and sizes.</p>
     1166                        %3$s';
     1167
     1168                $unfiltered = sprintf( $content, $img, $img_https, $img_relative );
     1169                $expected   = sprintf( $content, $respimg, $respimg_https, $respimg_relative );
     1170                $actual     = wp_make_content_images_responsive( $unfiltered );
     1171
     1172                $this->assertSame( $expected, $actual );
     1173        }
    11361174}