Make WordPress Core

Ticket #34379: 34379.3.patch

File 34379.3.patch, 3.4 KB (added by SergeyBiryukov, 10 years ago)
  • src/wp-includes/media.php

     
    809809                if ( empty($default_attr['alt']) )
    810810                        $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
    811811
    812                 $attr   = wp_parse_args( $attr, $default_attr );
    813                 $srcset = wp_get_attachment_image_srcset( $attachment_id, $size );
    814                 $sizes  = wp_get_attachment_image_sizes( $attachment_id, $size, $width );
     812                $attr = wp_parse_args( $attr, $default_attr );
    815813
    816814                // Generate srcset and sizes if not already present.
    817                 if ( empty( $attr['srcset'] ) && $srcset && $sizes ) {
    818                         $attr['srcset'] = $srcset;
     815                if ( empty( $attr['srcset'] ) ) {
     816                        $srcset = wp_get_attachment_image_srcset( $attachment_id, $size );
     817                        $sizes  = wp_get_attachment_image_sizes( $attachment_id, $size, $width );
    819818
    820                         if ( empty( $attr['sizes'] ) ) {
    821                                 $attr['sizes'] = $sizes;
     819                        if ( $srcset && $sizes ) {
     820                                $attr['srcset'] = $srcset;
     821
     822                                if ( empty( $attr['sizes'] ) ) {
     823                                        $attr['sizes'] = $sizes;
     824                                }
    822825                        }
    823826                }
    824827
     
    11421145        $size   = preg_match( '/size-([^\s|"]+)/i',   $image, $match_size   ) ? $match_size[1]         : false;
    11431146        $width  = preg_match( '/ width="([0-9]+)"/',  $image, $match_width  ) ? (int) $match_width[1]  : false;
    11441147
    1145         if ( $id && false === $size ) {
     1148        if ( $id && ! $size ) {
    11461149                $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false;
    11471150
    11481151                if ( $width && $height ) {
     
    11501153                }
    11511154        }
    11521155
    1153         $meta = wp_get_attachment_metadata( $id );
    1154 
    11551156        /*
    11561157         * If attempts to parse the size value failed, attempt to use the image
    11571158         * metadata to match the 'src' against the available sizes for an attachment.
    11581159         */
    1159         if ( ! $size && ! empty( $id ) && is_array( $meta ) ) {
     1160        if ( $id && ! $size ) {
     1161                $meta = wp_get_attachment_metadata( $id );
     1162
    11601163                // Parse the image src value from the img element.
    11611164                $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false;
    11621165
    1163                 // Return early if the src value is empty.
    1164                 if ( ! $src ) {
     1166                // Return early if the metadata does not exist or the src value is empty.
     1167                if ( ! $meta || ! $src ) {
    11651168                        return $image;
    11661169                }
    11671170
     
    11841187
    11851188        }
    11861189
    1187         $srcset = wp_get_attachment_image_srcset( $id, $size );
    1188         $sizes  = wp_get_attachment_image_sizes( $id, $size, $width );
    1189 
    11901190        // If ID and size exist, try for 'srcset' and 'sizes' and update the markup.
    1191         if ( $id && $size && $srcset && $sizes ) {
    1192                 // Format the srcset and sizes string and escape attributes.
    1193                 $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) );
     1191        if ( $id && $size ) {
     1192                $srcset = wp_get_attachment_image_srcset( $id, $size );
     1193                $sizes  = wp_get_attachment_image_sizes( $id, $size, $width );
    11941194
    1195                 // Add srcset and sizes attributes to the image markup.
    1196                 $image = preg_replace( '/<img ([^>]+)[\s?][\/?]>/', '<img $1' . $srcset_and_sizes . ' />', $image );
     1195                if ( $srcset && $sizes ) {
     1196                        // Format the srcset and sizes string and escape attributes.
     1197                        $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) );
     1198
     1199                        // Add srcset and sizes attributes to the image markup.
     1200                        $image = preg_replace( '/<img ([^>]+)[\s?][\/?]>/', '<img $1' . $srcset_and_sizes . ' />', $image );
     1201                }
    11971202        }
    11981203
    11991204        return $image;