WordPress.org

Make WordPress Core

Ticket #34477: 34477.1.patch

File 34477.1.patch, 3.0 KB (added by azaozz, 5 years ago)
  • src/wp-includes/media.php

     
    814814                // Generate 'srcset' and 'sizes' if not already present.
    815815                if ( empty( $attr['srcset'] ) ) {
    816816                        $image_meta = wp_get_attachment_metadata( $attachment_id );
     817                        $size_name = is_string( $size ) ? $size : '';
    817818
    818819                        if ( is_array( $image_meta ) ) {
    819820                                $size_array = array( absint( $width ), absint( $height ) );
    820821                                $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
    821                                 $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id );
     822                                $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $size_name );
    822823
    823824                                if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
    824825                                        $attr['srcset'] = $srcset;
     
    10821083 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
    10831084 * @param int          $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed
    10841085 *                                    when using the image size name as argument for `$size`.
    1085  *
     1086 * @param string       $size_name     Optional. The image size name (thumbnail, medium, large, etc.).
    10861087 * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
    10871088 */
    1088 function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0 ) {
     1089function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $size_name = '' ) {
    10891090        $width = 0;
    10901091
    10911092        if ( is_numeric( $size ) ) {
     
    10931094        } elseif ( is_array( $size ) ) {
    10941095                $width = absint( $size[0] );
    10951096        } elseif ( is_string( $size ) ) {
     1097                $size_name = $size;
     1098
    10961099                if ( ! $image_meta && $attachment_id ) {
    10971100                        $image_meta = wp_get_attachment_metadata( $attachment_id );
    10981101                }
     
    11231126         * @param array        $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    11241127         * @param int          $attachment_id Image attachment ID of the original image.
    11251128         */
    1126         return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id );
     1129        return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $size_name );
    11271130}
    11281131
    11291132/**
     
    12351238                return $image;
    12361239        }
    12371240
     1241        $size_name = '';
     1242
     1243        if ( strpos( $image, 'size-' ) !== false && preg_match( '/ class="([^"]+)"/', $image, $classes ) ) {
     1244                $size_name = preg_match( '/(size-[^ ]+)/', $classes[1], $match_size_name ) ? $match_size_name[1] : '';
     1245        }
     1246
    12381247        $size_array = array( $width, $height );
    12391248        $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
    12401249
    12411250        if ( $srcset ) {
    1242                 $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id );
     1251                $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $size_name );
    12431252        }
    12441253
    12451254        if ( $srcset && $sizes ) {