Make WordPress Core

Ticket #36982: media-srcset-sizes.patch

File media-srcset-sizes.patch, 6.7 KB (added by kylereicks, 8 years ago)

Adds image attributes and additional context to wp_calculate_image_srcset and wp_calculate_image_sizes filters

  • src/wp-includes/media.php

     
    826826
    827827                        if ( is_array( $image_meta ) ) {
    828828                                $size_array = array( absint( $width ), absint( $height ) );
    829                                 $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
    830                                 $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );
     829                                $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id, $attr );
     830                                $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id, $attr );
    831831
    832832                                if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
    833833                                        $attr['srcset'] = $srcset;
     
    972972 * @param string $image_src     The 'src' of the image.
    973973 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    974974 * @param int    $attachment_id Optional. The image attachment ID to pass to the filter. Default 0.
     975 * @param array  $attr          Optional. Image attributes that are passed to the wp_calculate_image_srcset filter.
     976 * @param string $context       Optional. Context for the image that is passed to the wp_calculate_image_srcset filter.
    975977 * @return string|bool          The 'srcset' attribute value. False on error or when only one source exists.
    976978 */
    977 function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
     979function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0, $attr = array(), $context = '' ) {
    978980        /**
    979981         * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
    980982         *
     
    11421144         * @param array  $size_array    Array of width and height values in pixels (in that order).
    11431145         * @param string $image_src     The 'src' of the image.
    11441146         * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
     1147         * @param array  $attr          Image attributes.
     1148         * @param string $context       Context for the image.
    11451149         * @param int    $attachment_id Image attachment ID or 0.
    11461150         */
    1147         $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
     1151        $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id, $attr, $context );
    11481152
    11491153        // Only return a 'srcset' value if there is more than one source.
    11501154        if ( ! $src_matched || count( $sources ) < 2 ) {
     
    12041208 *                                    Default null.
    12051209 * @param int          $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id`
    12061210 *                                    is needed when using the image size name as argument for `$size`. Default 0.
     1211 * @param array        $attr          Optional. Image attributes that are passed to the wp_calculate_image_sizes filter.
     1212 * @param string       $context       Optional. Context for the image that is passed to the wp_calculate_image_sizes filter.
    12071213 * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
    12081214 */
    1209 function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) {
     1215function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0, $attr = array(), $context = '' ) {
    12101216        $width = 0;
    12111217
    12121218        if ( is_array( $size ) ) {
     
    12411247         *                                    in pixels (in that order).
    12421248         * @param string|null  $image_src     The URL to the image file or null.
    12431249         * @param array|null   $image_meta    The image meta data as returned by wp_get_attachment_metadata() or null.
     1250         * @param array        $attr          Image attributes.
     1251         * @param string       $context       Context for the image.
    12441252         * @param int          $attachment_id Image attachment ID of the original image or 0.
    12451253         */
    1246         return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );
     1254        return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id, $attr, $context );
    12471255}
    12481256
    12491257/**
     
    12541262 * @see wp_image_add_srcset_and_sizes()
    12551263 *
    12561264 * @param string $content The raw post content to be filtered.
     1265 * @param string $context Optional. Context for the image that is passed to wp_image_add_srcset_and_sizes.
    12571266 * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
    12581267 */
    1259 function wp_make_content_images_responsive( $content ) {
     1268function wp_make_content_images_responsive( $content, $context = 'the_content' ) {
    12601269        if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
    12611270                return $content;
    12621271        }
     
    12891298
    12901299        foreach ( $selected_images as $image => $attachment_id ) {
    12911300                $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
    1292                 $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );
     1301                $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id, $context ), $content );
    12931302        }
    12941303
    12951304        return $content;
     
    13061315 * @param string $image         An HTML 'img' element to be filtered.
    13071316 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    13081317 * @param int    $attachment_id Image attachment ID.
     1318 * @param string $context       Optional. Context for the image that is passed to wp_calculate_image_srcset and wp_calculate_image_sizes.
    13091319 * @return string Converted 'img' element with 'srcset' and 'sizes' attributes added.
    13101320 */
    1311 function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {
     1321function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id, $context = '' ) {
    13121322        // Ensure the image meta exists.
    13131323        if ( empty( $image_meta['sizes'] ) ) {
    13141324                return $image;
     
    13581368        }
    13591369
    13601370        $size_array = array( $width, $height );
    1361         $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
     1371        $image_attr = preg_match_all( '/(\S+)=\"([^\"]+)\"/', $image, $match_attr ) ? array_combine( array_map( 'esc_attr', $match_attr[1] ), array_map( 'esc_attr', $match_attr[2] ) ) : array();
     1372        $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id, $image_attr, $context );
    13621373
    13631374        if ( $srcset ) {
    13641375                // Check if there is already a 'sizes' attribute.
     
    13651376                $sizes = strpos( $image, ' sizes=' );
    13661377
    13671378                if ( ! $sizes ) {
    1368                         $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
     1379                        $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id, $image_attr, $context );
    13691380                }
    13701381        }
    13711382