Make WordPress Core

Changeset 35678


Ignore:
Timestamp:
11/18/2015 07:47:11 PM (9 years ago)
Author:
wonderboymusic
Message:

Media: when making images responsive, check if they already have a sizes attribute.

Adds unit test.

Props jaspermdegroot.
Fixes #34678.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r35672 r35678  
    12061206
    12071207    foreach( $images as $image ) {
    1208         if ( false === strpos( $image, ' srcset="' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
     1208        if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
    12091209            ( $attachment_id = absint( $class_id[1] ) ) ) {
    12101210
     
    13031303
    13041304    if ( $srcset ) {
    1305         $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
     1305        // Check if there is already a 'sizes' attribute.
     1306        $sizes = strpos( $image, ' sizes=' );
     1307
     1308        if ( ! $sizes ) {
     1309            $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
     1310        }
    13061311    }
    13071312
    13081313    if ( $srcset && $sizes ) {
    13091314        // Format the 'srcset' and 'sizes' string and escape attributes.
    1310         $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes ) );
     1315        $attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );
     1316
     1317        if ( is_string( $sizes ) ) {
     1318            $attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) );
     1319        }
    13111320
    13121321        // Add 'srcset' and 'sizes' attributes to the image markup.
    1313         $image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $srcset_and_sizes . ' />', $image );
     1322        $image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
    13141323    }
    13151324
  • trunk/tests/phpunit/tests/media.php

    r35569 r35678  
    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 );
     964        $img_with_sizes_attr = str_replace( '<img ', '<img sizes="99vw" ', $img );
    964965
    965966        // Manually add srcset and sizes to the markup from get_image_tag();
     
    967968        $respimg_no_size_in_class = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class );
    968969        $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 );
    969971
    970972        $content = '
     
    979981
    980982            <p>Image, no attachment ID class. Should NOT have srcset and sizes.</p>
    981             %4$s';
    982 
    983         $content_unfiltered = sprintf( $content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id );
    984         $content_filtered = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id );
     983            %4$s
     984
     985            <p>Image, with sizes attribute. Should NOT have two sizes attributes.</p>
     986            %5$s';
     987
     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 );
    985990
    986991        $this->assertSame( $content_filtered, wp_make_content_images_responsive( $content_unfiltered ) );
Note: See TracChangeset for help on using the changeset viewer.