Make WordPress Core

Ticket #34807: 34807.2.patch

File 34807.2.patch, 4.1 KB (added by azaozz, 8 years ago)
  • src/wp-includes/media.php

     
    12061206 * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
    12071207 */
    12081208function wp_make_content_images_responsive( $content ) {
    1209         $images = get_media_embedded_in_content( $content, 'img' );
     1209        if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
     1210                return $content;
     1211        }
    12101212
    12111213        $selected_images = $attachment_ids = array();
    12121214
    1213         foreach( $images as $image ) {
     1215        foreach( $matches[0] as $image ) {
    12141216                if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
    12151217                        ( $attachment_id = absint( $class_id[1] ) ) ) {
    12161218
     
    35063508         * Filter the embedded media types that are allowed to be returned from the content blob.
    35073509         *
    35083510         * @since 4.2.0
    3509          * @since 4.4.0 Added 'img' to the allowed types.
    35103511         *
    35113512         * @param array $allowed_media_types An array of allowed media types. Default media types are
    3512          *                                   'audio', 'video', 'object', 'embed', 'iframe', and 'img'.
     3513         *                                   'audio', 'video', 'object', 'embed', and 'iframe'.
    35133514         */
    3514         $allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe', 'img' ) );
     3515        $allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe' ) );
    35153516
    35163517        if ( ! empty( $types ) ) {
    35173518                if ( ! is_array( $types ) ) {
  • tests/phpunit/tests/media.php

     
    962962                $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
    963963                $img_no_size_id = str_replace( 'wp-image-', 'id-', $img );
    964964                $img_with_sizes_attr = str_replace( '<img ', '<img sizes="99vw" ', $img );
     965                $img_xhtml = str_replace( ' />', '/>', $img );
     966                $img_html5 = str_replace( ' />', '>', $img );
    965967
    966968                // Manually add srcset and sizes to the markup from get_image_tag();
    967969                $respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
     
    968970                $respimg_no_size_in_class = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class );
    969971                $respimg_no_width_height = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_width_height );
    970972                $respimg_with_sizes_attr = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes_attr );
     973                $respimg_xhtml = preg_replace( '|<img ([^>]+)/>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_xhtml );
     974                $respimg_html5 = preg_replace( '|<img ([^>]+)>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_html5 );
    971975
    972976                $content = '
    973977                        <p>Image, standard. Should have srcset and sizes.</p>
     
    983987                        %4$s
    984988
    985989                        <p>Image, with sizes attribute. Should NOT have two sizes attributes.</p>
    986                         %5$s';
     990                        %5$s
    987991
    988                 $content_unfiltered = sprintf( $content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id, $img_with_sizes_attr );
    989                 $content_filtered = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $respimg_with_sizes_attr );
     992                        <p>Image, XHTML 1.0 style (no space before the closing slash). Should have srcset and sizes.</p>
     993                        %6$s
    990994
     995                        <p>Image, HTML 5.0 style. Should have srcset and sizes.</p>
     996                        %7$s';
     997
     998                $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 );
     999                $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 );
     1000
    9911001                $this->assertSame( $content_filtered, wp_make_content_images_responsive( $content_unfiltered ) );
    9921002        }
    9931003