Make WordPress Core

Ticket #34678: 34678.2.diff

File 34678.2.diff, 3.2 KB (added by azaozz, 11 years ago)
  • src/wp-includes/media.php

     
    12111211        $selected_images = $attachment_ids = array();
    12121212
    12131213        foreach( $images as $image ) {
    1214                 if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
    1215                         ( $attachment_id = absint( $class_id[1] ) ) ) {
     1214                if ( false === strpos( $image, ' srcset=' ) && false === strpos( $image, ' sizes=' ) &&
     1215                        preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && ( $attachment_id = absint( $class_id[1] ) ) ) {
    12161216
    12171217                        /*
    12181218                         * If exactly the same image tag is used more than once, overwrite it.
    … …  
    13081308        $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
    13091309
    13101310        if ( $srcset ) {
    1311                 // Check if there is already a 'sizes' attribute.
    1312                 $sizes = strpos( $image, ' sizes=' );
    1313 
    1314                 if ( ! $sizes ) {
    1315                         $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
    1316                 }
     1311                $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
    13171312        }
    13181313
    13191314        if ( $srcset && $sizes ) {
    13201315                // Format the 'srcset' and 'sizes' string and escape attributes.
    1321                 $attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );
     1316                $attr = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes ) );
    13221317
    1323                 if ( is_string( $sizes ) ) {
    1324                         $attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) );
    1325                 }
    1326 
    13271318                // Add 'srcset' and 'sizes' attributes to the image markup.
    13281319                $image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
    13291320        }
  • tests/phpunit/tests/media.php

     
    967967                $respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
    968968                $respimg_no_size_in_class = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class );
    969969                $respimg_no_width_height = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_width_height );
    970                 $respimg_with_sizes_attr = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes_attr );
    971970
    972971                $content = '
    973972                        <p>Image, standard. Should have srcset and sizes.</p>
    … …  
    982981                        <p>Image, no attachment ID class. Should NOT have srcset and sizes.</p>
    983982                        %4$s
    984983
    985                         <p>Image, with sizes attribute. Should NOT have two sizes attributes.</p>
     984                        <p>Image, with sizes attribute. Should NOT have srcset and sizes.</p>
    986985                        %5$s';
    987986
    988987                $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 );
     988                $content_filtered = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $img_with_sizes_attr );
    990989
    991990                $this->assertSame( $content_filtered, wp_make_content_images_responsive( $content_unfiltered ) );
    992991        }