WordPress.org

Make WordPress Core

Changeset 35405


Ignore:
Timestamp:
10/26/2015 06:41:31 PM (6 years ago)
Author:
SergeyBiryukov
Message:

After [35402], don't unnecessary run wp_get_attachment_metadata(), wp_get_attachment_image_srcset(), and wp_get_attachment_image_sizes() in wp_img_add_srcset_and_sizes().

See #34379.

File:
1 edited

Legend:

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

    r35404 r35405  
    11461146    $width  = preg_match( '/ width="([0-9]+)"/',  $image, $match_width  ) ? (int) $match_width[1]  : false;
    11471147
    1148     if ( $id && false === $size ) {
     1148    if ( $id && ! $size ) {
    11491149        $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false;
    11501150
     
    11531153        }
    11541154    }
    1155 
    1156     $meta = wp_get_attachment_metadata( $id );
    11571155
    11581156    /*
     
    11601158     * metadata to match the 'src' against the available sizes for an attachment.
    11611159     */
    1162     if ( ! $size && ! empty( $id ) && is_array( $meta ) ) {
     1160    if ( $id && ! $size ) {
     1161        $meta = wp_get_attachment_metadata( $id );
     1162
    11631163        // Parse the image src value from the img element.
    11641164        $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false;
    11651165
    1166         // Return early if the src value is empty.
    1167         if ( ! $src ) {
     1166        // Return early if the metadata does not exist or the src value is empty.
     1167        if ( ! $meta || ! $src ) {
    11681168            return $image;
    11691169        }
     
    11881188    }
    11891189
    1190     $srcset = wp_get_attachment_image_srcset( $id, $size );
    1191     $sizes  = wp_get_attachment_image_sizes( $id, $size, $width );
    1192 
    11931190    // If ID and size exist, try for 'srcset' and 'sizes' and update the markup.
    1194     if ( $id && $size && $srcset && $sizes ) {
    1195         // Format the srcset and sizes string and escape attributes.
    1196         $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) );
    1197 
    1198         // Add srcset and sizes attributes to the image markup.
    1199         $image = preg_replace( '/<img ([^>]+)[\s?][\/?]>/', '<img $1' . $srcset_and_sizes . ' />', $image );
     1191    if ( $id && $size ) {
     1192        $srcset = wp_get_attachment_image_srcset( $id, $size );
     1193        $sizes  = wp_get_attachment_image_sizes( $id, $size, $width );
     1194
     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        }
    12001202    }
    12011203
Note: See TracChangeset for help on using the changeset viewer.