Make WordPress Core

Ticket #34430: 34430.diff

File 34430.diff, 8.7 KB (added by joemcgill, 9 years ago)
  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 4f43e59..d8ba4fc 100644
    function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon 
    881881 * }
    882882 *
    883883 */
    884 function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' ) {
    885         // Get the intermediate size.
    886         $image = image_get_intermediate_size( $attachment_id, $size );
    887 
     884function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium', $attachment_meta = null, $img_url = null ) {
    888885        // Get the post meta.
    889         $img_meta = wp_get_attachment_metadata( $attachment_id );
    890         if ( ! is_array( $img_meta ) ) {
     886        $img_meta = ( is_array( $attachment_meta ) ) ? $attachment_meta : wp_get_attachment_metadata( $attachment_id );
     887        if ( empty( $img_meta['sizes'] ) ) {
    891888                return false;
    892889        }
    893890
    894         // Extract the height and width from the intermediate or the full size.
    895         $img_width  = ( $image ) ? $image['width']  : $img_meta['width'];
    896         $img_height = ( $image ) ? $image['height'] : $img_meta['height'];
    897 
    898         // Bail early if the width isn't greater than zero.
    899         if ( ! $img_width > 0 ) {
    900                 return false;
     891        // Try using passed in $img_url if we have it.
     892        if ( ! $img_url && $image = wp_get_attachment_image_src( $attachment_id, $size ) ) {
     893                $img_url = $image[0];
    901894        }
    902895
    903         // Use the URL from the intermediate size or build the url from the metadata.
    904         if ( ! empty( $image['url'] ) ) {
    905                 $img_url = $image['url'];
     896        $img_sizes = $img_meta['sizes'];
     897
     898        // Get the intermediate size.
     899        if ( is_array( $size ) ) {
     900                list( $img_width, $img_height ) = $size;
    906901        } else {
    907                 $uploads_dir = wp_upload_dir();
    908                 $img_file = ( $image ) ? path_join( dirname( $img_meta['file'] ) , $image['file'] ) : $img_meta['file'];
    909                 $img_url = $uploads_dir['baseurl'] . '/' . $img_file;
     902                $img_width  = ( ! empty( $img_sizes[$size] ) ) ? $img_sizes[$size]['width']  : $img_meta['width'];
     903                $img_height = ( ! empty( $img_sizes[$size] ) ) ? $img_sizes[$size]['height'] : $img_meta['height'];
    910904        }
    911905
    912         $img_sizes = $img_meta['sizes'];
     906        // Bail early if the width isn't greater than zero.
     907        if ( ! $img_width > 0 ) {
     908                return false;
     909        }
    913910
    914911        // Add full size to the img_sizes array.
    915912        $img_sizes['full'] = array(
    function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' 
    949946         */
    950947        foreach ( $img_sizes as $img ) {
    951948
    952                 // Filter out images that are leftovers from previous renditions.
     949                // Filter out images that are leftovers from previous versions.
    953950                if ( $img_edited && ! strpos( $img['file'], $img_edit_hash[0] ) ) {
    954951                        continue;
    955952                }
    function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' 
    959956                        continue;
    960957                }
    961958
    962                 $candidate_url = path_join( dirname( $img_url ), $img['file'] );
    963 
    964959                // Calculate the new image ratio.
    965960                if ( $img['width'] ) {
    966961                        $img_ratio_compare = $img['height'] / $img['width'];
    function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' 
    969964                }
    970965
    971966                // If the new ratio differs by less than 0.01, use it.
    972                 if ( abs( $img_ratio - $img_ratio_compare ) < 0.01 && ! in_array( $candidate_url, $candidates ) ) {
    973                         // Add the URL to our list of candidates.
    974                         $candidates[] = $candidate_url;
     967                if ( abs( $img_ratio - $img_ratio_compare ) < 0.01 && ! in_array( $img['file'], $candidates ) ) {
     968                        // Add the image file to our list of candidates.
     969                        $candidates[] = $img['file'];
    975970
    976971                        // Add the url, descriptor, and value to the sources array to be returned.
    977972                        $sources[] = array(
    978                                 'url'        => $candidate_url,
     973                                'url'        => path_join( dirname( $img_url ), $img['file'] ),
    979974                                'descriptor' => 'w',
    980975                                'value'      => $img['width'],
    981976                        );
    function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' 
    10051000 *                                    values in pixels (in that order). Default 'medium'.
    10061001 * @return string|bool A 'srcset' value string or false.
    10071002 */
    1008 function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium' ) {
    1009         $srcset_array = wp_get_attachment_image_srcset_array( $attachment_id, $size );
     1003function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $attachment_meta = null, $img_url = null ) {
     1004        $srcset_array = wp_get_attachment_image_srcset_array( $attachment_id, $size, $attachment_meta, $img_url );
    10101005
    10111006        // Only return a srcset value if there is more than one source.
    10121007        if ( count( $srcset_array ) <= 1 ) {
    function wp_make_content_images_responsive( $content ) { 
    10911086        $images = get_media_embedded_in_content( $content, 'img' );
    10921087
    10931088        $attachment_ids = array();
     1089        $match_groups = array();
    10941090
    10951091        foreach( $images as $image ) {
    10961092                if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) {
    1097                         $attachment_id = (int) $class_id[1];
    1098                         if ( $attachment_id ) {
    1099                                 $attachment_ids[] = $attachment_id;
    1100                         }
     1093                        $attachment_ids[] = (int) $class_id[1];
     1094                        $match_groups[] = array(
     1095                                'id' => (int) $class_id[1],
     1096                                'html' => $image,
     1097                        );
    11011098                }
    11021099        }
    11031100
    function wp_make_content_images_responsive( $content ) { 
    11111108                _prime_post_caches( $attachment_ids, false, true );
    11121109        }
    11131110
    1114         foreach( $images as $image ) {
    1115                 $content = str_replace( $image, wp_img_add_srcset_and_sizes( $image ), $content );
     1111        foreach( $match_groups as $match ) {
     1112                $post_meta = get_metadata( 'post', $match['id'], '_wp_attachment_metadata', true );
     1113                $content = str_replace( $match['html'], wp_img_add_srcset_and_sizes( $match['html'], $post_meta ), $content );
    11161114        }
    11171115
    11181116        return $content;
    function wp_make_content_images_responsive( $content ) { 
    11291127 * @param string $image An HTML 'img' element to be filtered.
    11301128 * @return string Converted 'img' element with `srcset` and `sizes` attributes added.
    11311129 */
    1132 function wp_img_add_srcset_and_sizes( $image ) {
     1130function wp_img_add_srcset_and_sizes( $image, $attachment_meta = null ) {
    11331131        // Return early if a 'srcset' attribute already exists.
    11341132        if ( false !== strpos( $image, ' srcset="' ) ) {
    11351133                return $image;
    11361134        }
    11371135
    1138         // Parse id, size, width, and height from the `img` element.
    1139         $id     = preg_match( '/wp-image-([0-9]+)/i', $image, $match_id     ) ? (int) $match_id[1]     : false;
    1140         $size   = preg_match( '/size-([^\s|"]+)/i',   $image, $match_size   ) ? $match_size[1]         : false;
    1141         $width  = preg_match( '/ width="([0-9]+)"/',  $image, $match_width  ) ? (int) $match_width[1]  : false;
     1136
     1137        // Parse src, id, size, width, and height from the `img` element.
     1138        $src    = preg_match( '/ src="([^"]+)"/',     $image, $match_src   ) ? $match_src[1]         : false;
     1139        $id     = preg_match( '/wp-image-([0-9]+)/i', $image, $match_id    ) ? (int) $match_id[1]    : false;
     1140        $size   = preg_match( '/size-([^\s|"]+)/i',   $image, $match_size  ) ? $match_size[1]        : false;
     1141        $width  = preg_match( '/ width="([0-9]+)"/i', $image, $match_width ) ? (int) $match_width[1] : false;
    11421142
    11431143        if ( $id && false === $size ) {
    11441144                $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false;
    function wp_img_add_srcset_and_sizes( $image ) { 
    11481148                }
    11491149        }
    11501150
     1151        if ( ! is_array( $attachment_meta ) ) {
     1152                $attachment_meta = wp_get_attachment_metadata( $id );
     1153        }
    11511154        /*
    11521155         * If attempts to parse the size value failed, attempt to use the image
    11531156         * metadata to match the 'src' against the available sizes for an attachment.
    11541157         */
    1155         if ( ! $size && ! empty( $id ) && is_array( $meta = wp_get_attachment_metadata( $id ) ) ) {
     1158        if ( ! $size && ! empty( $id ) && $attachment_meta ) {
    11561159                // Parse the image src value from the img element.
    11571160                $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false;
    11581161
    function wp_img_add_srcset_and_sizes( $image ) { 
    11671170                 */
    11681171                $image_filename = wp_basename( $src );
    11691172
    1170                 if ( $image_filename === basename( $meta['file'] ) ) {
     1173                if ( $image_filename === basename( $attachment_meta['file'] ) ) {
    11711174                        $size = 'full';
    11721175                } else {
    1173                         foreach( $meta['sizes'] as $image_size => $image_size_data ) {
     1176                        foreach( $attachment_meta['sizes'] as $image_size => $image_size_data ) {
    11741177                                if ( $image_filename === $image_size_data['file'] ) {
    11751178                                        $size = $image_size;
    11761179                                        break;
    function wp_img_add_srcset_and_sizes( $image ) { 
    11811184        }
    11821185
    11831186        // If ID and size exist, try for 'srcset' and 'sizes' and update the markup.
    1184         if ( $id && $size && ( $srcset = wp_get_attachment_image_srcset( $id, $size ) ) && ( $sizes = wp_get_attachment_image_sizes( $id, $size, $width ) ) ) {
     1187        if ( $id && $size && ( $srcset = wp_get_attachment_image_srcset( $id, $size, $attachment_meta, $src ) ) && ( $sizes = wp_get_attachment_image_sizes( $id, $size, $width ) ) ) {
    11851188                // Format the srcset and sizes string and escape attributes.
    11861189                $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) );
    11871190