Media: don't use get_media_embedded_in_content() in wp_make_content_images_responsive().

Adds unit test.

Props azaozz.
Fixes #34807.


git-svn-id: https://develop.svn.wordpress.org/trunk@35753 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-12-01 20:49:13 +00:00
parent a69f591c55
commit 69900349ca
2 changed files with 19 additions and 8 deletions

View File

@ -1206,11 +1206,13 @@ function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null,
* @return string Converted content with 'srcset' and 'sizes' attributes added to images.
*/
function wp_make_content_images_responsive( $content ) {
$images = get_media_embedded_in_content( $content, 'img' );
if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
return $content;
}
$selected_images = $attachment_ids = array();
foreach( $images as $image ) {
foreach( $matches[0] as $image ) {
if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
( $attachment_id = absint( $class_id[1] ) ) ) {
@ -3506,12 +3508,11 @@ function get_media_embedded_in_content( $content, $types = null ) {
* Filter the embedded media types that are allowed to be returned from the content blob.
*
* @since 4.2.0
* @since 4.4.0 Added 'img' to the allowed types.
*
* @param array $allowed_media_types An array of allowed media types. Default media types are
* 'audio', 'video', 'object', 'embed', 'iframe', and 'img'.
* 'audio', 'video', 'object', 'embed', and 'iframe'.
*/
$allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe', 'img' ) );
$allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe' ) );
if ( ! empty( $types ) ) {
if ( ! is_array( $types ) ) {

View File

@ -1019,12 +1019,16 @@ EOF;
$img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
$img_no_size_id = str_replace( 'wp-image-', 'id-', $img );
$img_with_sizes_attr = str_replace( '<img ', '<img sizes="99vw" ', $img );
$img_xhtml = str_replace( ' />', '/>', $img );
$img_html5 = str_replace( ' />', '>', $img );
// Manually add srcset and sizes to the markup from get_image_tag();
$respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
$respimg_no_size_in_class = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class );
$respimg_no_width_height = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_width_height );
$respimg_with_sizes_attr = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes_attr );
$respimg_xhtml = preg_replace( '|<img ([^>]+)/>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_xhtml );
$respimg_html5 = preg_replace( '|<img ([^>]+)>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_html5 );
$content = '
<p>Image, standard. Should have srcset and sizes.</p>
@ -1040,10 +1044,16 @@ EOF;
%4$s
<p>Image, with sizes attribute. Should NOT have two sizes attributes.</p>
%5$s';
%5$s
$content_unfiltered = sprintf( $content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id, $img_with_sizes_attr );
$content_filtered = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $respimg_with_sizes_attr );
<p>Image, XHTML 1.0 style (no space before the closing slash). Should have srcset and sizes.</p>
%6$s
<p>Image, HTML 5.0 style. Should have srcset and sizes.</p>
%7$s';
$content_unfiltered = sprintf( $content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id, $img_with_sizes_attr, $img_xhtml, $img_html5 );
$content_filtered = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $respimg_with_sizes_attr, $respimg_xhtml, $respimg_html5 );
$this->assertSame( $content_filtered, wp_make_content_images_responsive( $content_unfiltered ) );
}