From 69900349cac57a298680725176b1660da3544bfd Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 1 Dec 2015 20:49:13 +0000 Subject: [PATCH] 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 --- src/wp-includes/media.php | 11 ++++++----- tests/phpunit/tests/media.php | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index c4aac252e4..06c27cbc7f 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -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( '/]+>/', $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 ) ) { diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index dc5b990bd1..55a53a1eb4 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -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_html5 = str_replace( ' />', '>', $img ); // Manually add srcset and sizes to the markup from get_image_tag(); $respimg = preg_replace( '|]+) />|', '', $img ); $respimg_no_size_in_class = preg_replace( '|]+) />|', '', $img_no_size_in_class ); $respimg_no_width_height = preg_replace( '|]+) />|', '', $img_no_width_height ); $respimg_with_sizes_attr = preg_replace('|]+) />|', '', $img_with_sizes_attr ); + $respimg_xhtml = preg_replace( '|]+)/>|', '', $img_xhtml ); + $respimg_html5 = preg_replace( '|]+)>|', '', $img_html5 ); $content = '

Image, standard. Should have srcset and sizes.

@@ -1040,10 +1044,16 @@ EOF; %4$s

Image, with sizes attribute. Should NOT have two sizes attributes.

- %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 ); +

Image, XHTML 1.0 style (no space before the closing slash). Should have srcset and sizes.

+ %6$s + +

Image, HTML 5.0 style. Should have srcset and sizes.

+ %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 ) ); }