WordPress.org

Make WordPress Core

Changeset 35820


Ignore:
Timestamp:
12/07/2015 08:07:37 PM (5 years ago)
Author:
mikeschroder
Message:

Media: Don't generate responsive image attributes if src does not match ID in wp-image- class.

We rely on the wp-image- class to quickly find an attachment ID to add responsive image attributes.
To avoid incorrect images being displayed, do not add these attributes if the src does not match the
meta from the attachment ID in the class.

Props azaozz, kovshenin, joemcgill.
Fixes: #34898.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r35811 r35820  
    12761276        strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
    12771277
     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 ) ) {
    12781300        return $image;
    12791301    }
  • trunk/tests/phpunit/tests/media.php

    r35753 r35820  
    10571057
    10581058        $this->assertSame( $content_filtered, wp_make_content_images_responsive( $content_unfiltered ) );
     1059    }
     1060
     1061    /**
     1062     * When rendering attributes for responsive images,
     1063     * we rely on the 'wp-image-*' class to find the image by ID.
     1064     * The class name may not be consistent with attachment IDs in DB when
     1065     * working with imported content or when a user has edited
     1066     * the 'src' attribute manually. To avoid incorrect images
     1067     * being displayed, ensure we don't add attributes in this case.
     1068     *
     1069     * @ticket 34898
     1070     * @ticket 33641
     1071     */
     1072    function test_wp_make_content_images_responsive_wrong() {
     1073        $image = get_image_tag( self::$large_id, '', '', '', 'medium' );
     1074
     1075        // Replace the src URL
     1076        $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $image );
     1077
     1078        $this->assertSame( $image_wrong_src, wp_make_content_images_responsive( $image_wrong_src ) );
    10591079    }
    10601080
Note: See TracChangeset for help on using the changeset viewer.