Make WordPress Core


Ignore:
Timestamp:
10/22/2015 04:45:37 PM (9 years ago)
Author:
wonderboymusic
Message:

Media: in wp_get_attachment_image_sizes(), to streamline and for performance:

  • Change the 3rd arg from args to width
  • Change wp_image_sizes_args filter to wp_get_attachment_image_sizes

Updates unit tests.

Props joemcgill.
Fixes #34379.

File:
1 edited

Legend:

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

    r35325 r35355  
    813813
    814814        // Generate srcset and sizes if not already present.
    815         if ( empty( $attr['srcset'] ) && $srcset = wp_get_attachment_image_srcset( $attachment_id, $size ) ) {
     815        if ( empty( $attr['srcset'] ) && ( $srcset = wp_get_attachment_image_srcset( $attachment_id, $size ) ) && ( $sizes = wp_get_attachment_image_sizes( $attachment_id, $size, $width ) ) ) {
    816816            $attr['srcset'] = $srcset;
    817             $sizes_args = array(
    818                 'height' => $height,
    819                 'width'  => $width,
    820             );
    821             $attr['sizes'] = wp_get_attachment_image_sizes( $attachment_id, $size, $sizes_args );
     817
     818            if ( empty( $attr['sizes'] ) ) {
     819                $attr['sizes'] = $sizes;
     820            }
    822821        }
    823822
     
    867866 * @since 4.4.0
    868867 *
    869  * @param  int    $attachment_id Image attachment ID.
    870  * @param  string $size          Optional. Name of image size. Default 'medium'.
    871  * @return array|bool $images {
     868 * @param int          $attachment_id Image attachment ID.
     869 * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
     870 *                                    values in pixels (in that order). Default 'medium'.
     871 * @return array|bool $sources {
    872872 *     Array image candidate values containing a URL, descriptor type, and
    873873 *     descriptor value. False if none exist.
     
    976976     * @param array        $sources       An array of image urls and widths.
    977977     * @param int          $attachment_id Attachment ID for image.
    978      * @param array|string $size          Size of image, either array or string.
     978     * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
     979     *                                    values in pixels (in that order). Default 'medium'.
    979980     */
    980981    return apply_filters( 'wp_get_attachment_image_srcset_array', $sources, $attachment_id, $size );
     
    986987 * @since 4.4.0
    987988 *
    988  * @param int    $attachment_id Image attachment ID.
    989  * @param string $size          Optional. Name of image size. Default 'medium'.
     989 * @param int          $attachment_id Image attachment ID.
     990 * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
     991 *                                    values in pixels (in that order). Default 'medium'.
    990992 * @return string|bool A 'srcset' value string or false.
    991993 */
     
    10101012     * @param string       $srcset        A source set formated for a `srcset` attribute.
    10111013     * @param int          $attachment_id Attachment ID for image.
    1012      * @param array|string $size          Size of image, either array or string.
     1014     * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
     1015     *                                    values in pixels (in that order). Default 'medium'.
    10131016     */
    10141017    return apply_filters( 'wp_get_attachment_image_srcset', rtrim( $srcset, ', ' ), $attachment_id, $size );
     
    10201023 * @since 4.4.0
    10211024 *
    1022  * @param int    $attachment_id Image attachment ID.
    1023  * @param string $size          Optional. Name of image size. Default value: 'medium'.
    1024  * @param array  $args {
    1025  *     Optional. Arguments to retrieve attachments.
    1026  *
    1027  *     @type array|string $sizes An array or string containing of size information.
    1028  *     @type int          $width A single width value used in the default `sizes` string.
    1029  * }
     1025 * @param int          $attachment_id Image attachment ID.
     1026 * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
     1027 *                                    values in pixels (in that order). Default 'medium'.
     1028 * @param int          $width         Optional. Display width of the image.
    10301029 * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
    10311030 */
    1032 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $args = null ) {
    1033     $img_width = 0;
    1034     // Try to get the image width from $args.
    1035     if ( is_array( $args ) && ! empty( $args['width'] ) ) {
    1036         $img_width = (int) $args['width'];
    1037     } elseif ( $img = image_get_intermediate_size( $attachment_id, $size ) ) {
    1038         list( $img_width ) = image_constrain_size_for_editor( $img['width'], $img['height'], $size );
     1031function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $width = null ) {
     1032    // Try to get the image width from $args parameter.
     1033    if ( is_numeric( $width ) ) {
     1034        $img_width = (int) $width;
     1035    // Next, use see if a width value was passed in the $size parameter.
     1036    } elseif ( is_array( $size ) ) {
     1037        $img_width = $size[0];
     1038    // Finally, use the $size name to return the width of the image.
     1039    } else {
     1040        $image = image_get_intermediate_size( $attachment_id, $size );
     1041        $img_width = $image ? $image['width'] : false;
    10391042    }
    10401043
     
    10441047    }
    10451048
    1046     // Set the image width in pixels.
    1047     $img_width = $img_width . 'px';
    1048 
    1049     // Set up our default values.
    1050     $defaults = array(
    1051         'sizes' => array(
    1052             array(
    1053                 'size_value' => '100vw',
    1054                 'mq_value'   => $img_width,
    1055                 'mq_name'    => 'max-width'
    1056             ),
    1057             array(
    1058                 'size_value' => $img_width
    1059             ),
    1060         )
    1061     );
    1062 
    1063     $args = wp_parse_args( $args, $defaults );
    1064 
    1065     /**
    1066      * Filter arguments used to create 'sizes' attribute.
     1049    // Setup the default sizes attribute.
     1050    $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $img_width );
     1051
     1052    /**
     1053     * Filter the output of wp_get_attachment_image_sizes().
    10671054     *
    10681055     * @since 4.4.0
    10691056     *
    1070      * @param array   $args          An array of arguments used to create a 'sizes' attribute.
    1071      * @param int     $attachment_id Post ID of the original image.
    1072      * @param string  $size          Name of the image size being used.
    1073      */
    1074     $args = apply_filters( 'wp_image_sizes_args', $args, $attachment_id, $size );
    1075 
    1076     // If sizes is passed as a string, just use the string.
    1077     if ( is_string( $args['sizes'] ) ) {
    1078         $size_list = $args['sizes'];
    1079 
    1080     // Otherwise, breakdown the array and build a sizes string.
    1081     } elseif ( is_array( $args['sizes'] ) ) {
    1082 
    1083         $size_list = '';
    1084 
    1085         foreach ( $args['sizes'] as $size ) {
    1086 
    1087             // Use 100vw as the size value unless something else is specified.
    1088             $size_value = ( $size['size_value'] ) ? $size['size_value'] : '100vw';
    1089 
    1090             // If a media length is specified, build the media query.
    1091             if ( ! empty( $size['mq_value'] ) ) {
    1092 
    1093                 $media_length = $size['mq_value'];
    1094 
    1095                 // Use max-width as the media condition unless min-width is specified.
    1096                 $media_condition = ( ! empty( $size['mq_name'] ) ) ? $size['mq_name'] : 'max-width';
    1097 
    1098                 // If a media_length was set, create the media query.
    1099                 $media_query = '(' . $media_condition . ": " . $media_length . ') ';
    1100 
    1101             } else {
    1102                 // If no media length was set, $media_query is blank.
    1103                 $media_query = '';
    1104             }
    1105 
    1106             // Add to the source size list string.
    1107             $size_list .= $media_query . $size_value . ', ';
    1108         }
    1109 
    1110         // Remove the trailing comma and space from the end of the string.
    1111         $size_list = substr( $size_list, 0, -2 );
    1112     }
    1113 
    1114     // Return the sizes value as $size_list or false.
    1115     return ( $size_list ) ? $size_list : false;
     1057     * @param string       $sizes         A source size value for use in a 'sizes' attribute.
     1058     * @param int          $attachment_id Post ID of the original image.
     1059     * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
     1060     *                                    values in pixels (in that order). Default 'medium'.
     1061     * @param int          $width         Display width of the image.
     1062     */
     1063    return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $attachment_id, $size, $width );
    11161064}
    11171065
     
    11781126    $size   = preg_match( '/size-([^\s|"]+)/i',   $image, $match_size   ) ? $match_size[1]         : false;
    11791127    $width  = preg_match( '/ width="([0-9]+)"/',  $image, $match_width  ) ? (int) $match_width[1]  : false;
    1180     $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false;
    11811128
    11821129    if ( $id && false === $size ) {
    1183         $size = array( $width, $height );
     1130        $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false;
     1131
     1132        if ( $width && $height ) {
     1133            $size = array( $width, $height );
     1134        }
    11841135    }
    11851136
     
    12161167    }
    12171168
    1218     // If ID and size, try for 'srcset' and 'sizes' and update the markup.
    1219     if ( $id && $size && $srcset = wp_get_attachment_image_srcset( $id, $size ) ) {
    1220 
    1221         /*
    1222          * Pass the 'height' and 'width' to 'wp_get_attachment_image_sizes()' to avoid
    1223          * recalculating the image size.
    1224          */
    1225         $args = array(
    1226             'height' => $height,
    1227             'width'  => $width,
    1228         );
    1229 
    1230         $sizes = wp_get_attachment_image_sizes( $id, $size, $args );
    1231 
     1169    // If ID and size exist, try for 'srcset' and 'sizes' and update the markup.
     1170    if ( $id && $size && ( $srcset = wp_get_attachment_image_srcset( $id, $size ) ) && ( $sizes = wp_get_attachment_image_sizes( $id, $size, $width ) ) ) {
    12321171        // Format the srcset and sizes string and escape attributes.
    12331172        $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) );
Note: See TracChangeset for help on using the changeset viewer.