Index: src/wp-includes/media.php =================================================================== --- src/wp-includes/media.php (revision 35953) +++ src/wp-includes/media.php (working copy) @@ -1278,26 +1278,34 @@ return $image; } - $base_url = trailingslashit( _wp_upload_dir_baseurl() ); - $image_base_url = $base_url; + /** + * To make sure that our ID and image src match, we loop through all the sizes + * in our attachment metadata and bail early if our src file isn't included. + * + * Checks only for the presence of 'filename.ext' or 'year/month/filename.ext' (when upload sub-directories are used) + * to accomodate for CDN enabling plugins that diredtly replace image sources in post_content. + */ + if ( strpos( $image_src, $image_meta['file'] ) === false ) { + $sub_sizes = wp_list_pluck( $image_meta['sizes'], 'file' ); - $dirname = dirname( $image_meta['file'] ); - if ( $dirname !== '.' ) { - $image_base_url .= trailingslashit( $dirname ); - } + $dirname = dirname( $image_meta['file'] ); + $dirname = $dirname === '.' ? '' : trailingslashit( $dirname ); - $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' ); + $matched = false; - foreach ( $all_sizes as $key => $file ) { - $all_sizes[ $key ] = $image_base_url . $file; - } + foreach( $sub_sizes as $size ) { + $size = $dirname . $size; - // Add the original image. - $all_sizes[] = $base_url . $image_meta['file']; + if ( strpos( $image_src, $size ) !== false ) { + $matched = true; + break; + } + } - // Bail early if the image src doesn't match any of the known image sizes. - if ( ! in_array( $image_src, $all_sizes ) ) { - return $image; + // Bail early if the image src doesn't match any of the known image sizes. + if ( ! $matched ) { + return $image; + } } $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; Index: tests/phpunit/tests/media.php =================================================================== --- tests/phpunit/tests/media.php (revision 35953) +++ tests/phpunit/tests/media.php (working copy) @@ -1133,4 +1133,42 @@ // Intermediate sized GIFs should not include the full size in the srcset. $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) ); } + + /** + * @ticket 35045 + * @ticket 33641 + */ + function test_wp_make_content_images_responsive_schemes() { + $image_meta = wp_get_attachment_metadata( self::$large_id ); + $size_array = $this->_get_image_size_array_from_name( 'medium' ); + + $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) ); + $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) ); + + // Build HTML for the editor. + $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); + $img_https = str_replace( 'http://', 'https://', $img ); + $img_relative = str_replace( 'http://', '//', $img ); + + // Manually add srcset and sizes to the markup from get_image_tag(). + $respimg = preg_replace( '|]+) />|', '', $img ); + $respimg_https = preg_replace( '|]+) />|', '', $img_https ); + $respimg_relative = preg_replace( '|]+) />|', '', $img_relative ); + + $content = ' +
Image, http: protocol. Should have srcset and sizes.
+ %1$s + +Image, http: protocol. Should have srcset and sizes.
+ %2$s + +Image, protocol-relative. Should have srcset and sizes.
+ %3$s'; + + $unfiltered = sprintf( $content, $img, $img_https, $img_relative ); + $expected = sprintf( $content, $respimg, $respimg_https, $respimg_relative ); + $actual = wp_make_content_images_responsive( $unfiltered ); + + $this->assertSame( $expected, $actual ); + } }