Make WordPress Core

Ticket #34898: 34898.2.diff

File 34898.2.diff, 2.3 KB (added by kirasong, 9 years ago)
  • src/wp-includes/media.php

     
    12781278                return $image;
    12791279        }
    12801280
     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 ) ) {
     1300                return $image;
     1301        }
     1302
    12811303        $width  = preg_match( '/ width="([0-9]+)"/',  $image, $match_width  ) ? (int) $match_width[1]  : 0;
    12821304        $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
    12831305
  • tests/phpunit/tests/media.php

     
    10591059        }
    10601060
    10611061        /**
     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
    10621070         * @ticket 33641
    10631071         */
     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 ) );
     1079        }
     1080
     1081        /**
     1082         * @ticket 33641
     1083         */
    10641084        function test_wp_make_content_images_responsive_with_preexisting_srcset() {
    10651085                // Generate HTML and add a dummy srcset attribute.
    10661086                $image_html = get_image_tag( self::$large_id, '', '', '', 'medium' );