Make WordPress Core

Ticket #35045: 35045.6.diff

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

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    987987        }
    988988
    989989        $image_basename = wp_basename( $image_meta['file'] );
    990         $image_baseurl = _wp_upload_dir_baseurl();
    991990
    992991        /*
    993992         * WordPress flattens animated GIFs into one frame when generating intermediate sizes.
     
    10041003                return false;
    10051004        }
    10061005
    1007         // Uploads are (or have been) in year/month sub-directories.
    1008         if ( $image_basename !== $image_meta['file'] ) {
    1009                 $dirname = dirname( $image_meta['file'] );
     1006        // Retrieve the uploads sub-directory from the full size image.
     1007        $file_path = $image_meta['file'];
     1008        $dirname = dirname( $file_path );
     1009        $dirname = $dirname === '.' ? '' : trailingslashit( $dirname );
    10101010
    1011                 if ( $dirname !== '.' ) {
    1012                         $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    1013                 }
    1014         }
     1011        /**
     1012         * Filter the image dirname used to locate images in image sizes.
     1013         *
     1014         * @since 4.4.1
     1015         *
     1016         * @param string    $dirname        The directory name of file.
     1017         * @param int       $attachment_id  ID of attachment.
     1018     * @param string    $file_path      Path to file including filename.
     1019         */
     1020        $dirname = apply_filters( 'srcset_image_dirname', $dirname, $attachment_id, $file_path );
     1021
     1022        $image_baseurl = _wp_upload_dir_baseurl();
     1023        $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    10151024
    1016         $image_baseurl = trailingslashit( $image_baseurl );
     1025        /**
     1026         * Filter the image base URL used in constructing srcset image size URLs.
     1027         *
     1028         * @since 4.4.1
     1029         *
     1030         * @param string    $image_baseurl  The base URL of the image src.
     1031         * @param int       $attachment_id  ID of attachment.
     1032     * @param string    $image_src      Image src URL.
     1033     * @param string    $dirname        The directory name of file.
     1034         */
     1035        $image_baseurl = apply_filters( 'srcset_image_baseurl', $image_baseurl, $attachment_id, $image_src, $dirname );
    10171036
    10181037        // Calculate the image aspect ratio.
    10191038        $image_ratio = $image_height / $image_width;
     
    10381057        // Array to hold URL candidates.
    10391058        $sources = array();
    10401059
     1060        /**
     1061         * To make sure the ID matches our image src, we will check to see if any sizes in our attachment
     1062         * meta match our $image_src. If no mathces are found we don't return a srcset to avoid serving
     1063         * an incorrect image. See #35045.
     1064         */
     1065        $src_matched = false;
     1066
    10411067        /*
    10421068         * Loop through available images. Only use images that are resized
    10431069         * versions of the same edit.
    10441070         */
    10451071        foreach ( $image_sizes as $image ) {
    10461072
     1073                // If the file name is part of the `src`, we've confirmed a match.
     1074                if ( false !== strpos( $image_src, $dirname . $image['file'] ) ) {
     1075                        $src_matched = true;
     1076                }
     1077
    10471078                // Filter out images that are from previous edits.
    10481079                if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
    10491080                        continue;
     
    10961127        $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
    10971128
    10981129        // Only return a 'srcset' value if there is more than one source.
    1099         if ( count( $sources ) < 2 ) {
     1130        if ( ! $src_matched || count( $sources ) < 2 ) {
    11001131                return false;
    11011132        }
    11021133
     
    12751306        if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) &&
    12761307                strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
    12771308
    1278                 return $image;
    1279         }
    1280 
    1281         $base_url = trailingslashit( _wp_upload_dir_baseurl() );
    1282         $image_base_url = $base_url;
    1283 
    1284         $dirname = dirname( $image_meta['file'] );
    1285         if ( $dirname !== '.' ) {
    1286                 $image_base_url .= trailingslashit( $dirname );
    1287         }
    1288 
    1289         $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
    1290 
    1291         foreach ( $all_sizes as $key => $file ) {
    1292                 $all_sizes[ $key ] = $image_base_url . $file;
    1293         }
    1294 
    1295         // Add the original image.
    1296         $all_sizes[] = $base_url . $image_meta['file'];
    1297 
    1298         // Bail early if the image src doesn't match any of the known image sizes.
    1299         if ( ! in_array( $image_src, $all_sizes ) ) {
    13001309                return $image;
    13011310        }
    13021311
  • tests/phpunit/tests/media.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    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}