Make WordPress Core

Ticket #35045: 35045.8.diff

File 35045.8.diff, 6.1 KB (added by azaozz, 9 years ago)
  • src/wp-includes/media.php

     
    971971 * @return string|bool          The 'srcset' attribute value. False on error or when only one source exists.
    972972 */
    973973function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
     974        /**
     975         * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
     976         *
     977         * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
     978         * @param array  $size_array    Array of width and height values in pixels (in that order).
     979         * @param string $image_src     The 'src' of the image.
     980         * @param int    $attachment_id The image attachment ID or 0 if not supplied.
     981         */
     982        $image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
     983
    974984        if ( empty( $image_meta['sizes'] ) ) {
    975985                return false;
    976986        }
     
    987997        }
    988998
    989999        $image_basename = wp_basename( $image_meta['file'] );
    990         $image_baseurl = _wp_upload_dir_baseurl();
    9911000
    9921001        /*
    9931002         * WordPress flattens animated GIFs into one frame when generating intermediate sizes.
     
    10041013                return false;
    10051014        }
    10061015
    1007         // Uploads are (or have been) in year/month sub-directories.
    1008         if ( $image_basename !== $image_meta['file'] ) {
    1009                 $dirname = dirname( $image_meta['file'] );
     1016        // Retrieve the uploads sub-directory from the full size image.
     1017        $dirname = dirname( $image_meta['file'] );
     1018        $dirname = $dirname === '.' ? '' : trailingslashit( $dirname );
    10101019
    1011                 if ( $dirname !== '.' ) {
    1012                         $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    1013                 }
    1014         }
     1020        $image_baseurl = _wp_upload_dir_baseurl();
     1021        $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    10151022
    1016         $image_baseurl = trailingslashit( $image_baseurl );
    1017 
    10181023        /*
    10191024         * Images that have been edited in WordPress after being uploaded will
    10201025         * contain a unique hash. Look for that hash and use it later to filter
     
    10351040        // Array to hold URL candidates.
    10361041        $sources = array();
    10371042
     1043        /**
     1044         * To make sure the ID matches our image src, we will check to see if any sizes in our attachment
     1045         * meta match our $image_src. If no mathces are found we don't return a srcset to avoid serving
     1046         * an incorrect image. See #35045.
     1047         */
     1048        $src_matched = false;
     1049
    10381050        /*
    10391051         * Loop through available images. Only use images that are resized
    10401052         * versions of the same edit.
     
    10411053         */
    10421054        foreach ( $image_sizes as $image ) {
    10431055
     1056                // If the file name is part of the `src`, we've confirmed a match.
     1057                if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
     1058                        $src_matched = true;
     1059                }
     1060
    10441061                // Filter out images that are from previous edits.
    10451062                if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
    10461063                        continue;
     
    10991116        $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
    11001117
    11011118        // Only return a 'srcset' value if there is more than one source.
    1102         if ( count( $sources ) < 2 ) {
     1119        if ( ! $src_matched || count( $sources ) < 2 ) {
    11031120                return false;
    11041121        }
    11051122
     
    12811298                return $image;
    12821299        }
    12831300
    1284         $base_url = trailingslashit( _wp_upload_dir_baseurl() );
    1285         $image_base_url = $base_url;
    1286 
    1287         $dirname = dirname( $image_meta['file'] );
    1288         if ( $dirname !== '.' ) {
    1289                 $image_base_url .= trailingslashit( $dirname );
    1290         }
    1291 
    1292         $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
    1293 
    1294         foreach ( $all_sizes as $key => $file ) {
    1295                 $all_sizes[ $key ] = $image_base_url . $file;
    1296         }
    1297 
    1298         // Add the original image.
    1299         $all_sizes[] = $base_url . $image_meta['file'];
    1300 
    1301         // Bail early if the image src doesn't match any of the known image sizes.
    1302         if ( ! in_array( $image_src, $all_sizes ) ) {
    1303                 return $image;
    1304         }
    1305 
    13061301        $width  = preg_match( '/ width="([0-9]+)"/',  $image, $match_width  ) ? (int) $match_width[1]  : 0;
    13071302        $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
    13081303
  • tests/phpunit/tests/media.php

     
    11771177                // Intermediate sized GIFs should not include the full size in the srcset.
    11781178                $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
    11791179        }
     1180
     1181        /**
     1182         * @ticket 35045
     1183         * @ticket 33641
     1184         */
     1185        function test_wp_make_content_images_responsive_schemes() {
     1186                $image_meta = wp_get_attachment_metadata( self::$large_id );
     1187                $size_array = $this->_get_image_size_array_from_name( 'medium' );
     1188
     1189                $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
     1190                $sizes  = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
     1191
     1192                // Build HTML for the editor.
     1193                $img          = get_image_tag( self::$large_id, '', '', '', 'medium' );
     1194                $img_https    = str_replace( 'http://', 'https://', $img );
     1195                $img_relative = str_replace( 'http://', '//', $img );
     1196
     1197                // Manually add srcset and sizes to the markup from get_image_tag().
     1198                $respimg          = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
     1199                $respimg_https    = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https );
     1200                $respimg_relative = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative );
     1201
     1202                $content = '
     1203                        <p>Image, http: protocol. Should have srcset and sizes.</p>
     1204                        %1$s
     1205
     1206                        <p>Image, http: protocol. Should have srcset and sizes.</p>
     1207                        %2$s
     1208
     1209                        <p>Image, protocol-relative. Should have srcset and sizes.</p>
     1210                        %3$s';
     1211
     1212                $unfiltered = sprintf( $content, $img, $img_https, $img_relative );
     1213                $expected   = sprintf( $content, $respimg, $respimg_https, $respimg_relative );
     1214                $actual     = wp_make_content_images_responsive( $unfiltered );
     1215
     1216                $this->assertSame( $expected, $actual );
     1217        }
    11801218}