Make WordPress Core


Ignore:
Timestamp:
11/07/2015 09:35:34 PM (8 years ago)
Author:
azaozz
Message:

Responsive images: make the new functions and filters signatures more consistent.

Props joemcgill.
Fixes #34612.

File:
1 edited

Legend:

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

    r35567 r35569  
    827827            if ( is_array( $image_meta ) ) {
    828828                $size_array = array( absint( $width ), absint( $height ) );
    829                 $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
    830                 $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src );
     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 );
    831831
    832832                if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
     
    934934 *
    935935 * @param int          $attachment_id Image attachment ID.
    936  * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
    937  *                                    values in pixels (in that order). Default 'medium'.
     936 * @param array|string $size          Optional. Image size. Accepts any valid image size, or an array of
     937 *                                    width and height values in pixels (in that order). Default 'medium'.
    938938 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
    939939 * @return string|bool A 'srcset' value string or false.
     
    954954    );
    955955
    956     return wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id );
     956    return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
    957957}
    958958
     
    962962 * @since 4.4.0
    963963 *
     964 * @param array  $size_array    Array of width and height values in pixels (in that order).
    964965 * @param string $image_src     The 'src' of the image.
    965  * @param array  $size_array    Array of width and height values in pixels (in that order).
    966966 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    967967 * @param int    $attachment_id Optional. The image attachment ID to pass to the filter.
    968968 * @return string|bool          The 'srcset' attribute value. False on error or when only one source exists.
    969969 */
    970 function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id = 0 ) {
     970function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
    971971    if ( empty( $image_meta['sizes'] ) ) {
    972972        return false;
     
    11071107
    11081108/**
     1109 * Retrieves the value for an image attachment's 'sizes' attribute.
     1110 *
     1111 * @since 4.4.0
     1112 *
     1113 * @param int          $attachment_id Image attachment ID.
     1114 * @param array|string $size          Optional. Image size. Accepts any valid image size, or an array of width
     1115 *                                    and height values in pixels (in that order). Default 'medium'.
     1116 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
     1117 * @return string|bool A 'srcset' value string or false.
     1118 */
     1119function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
     1120    if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) {
     1121        return false;
     1122    }
     1123
     1124    if ( ! is_array( $image_meta ) ) {
     1125        $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     1126    }
     1127
     1128    $image_src = $image[0];
     1129    $size_array = array(
     1130        absint( $image[1] ),
     1131        absint( $image[2] )
     1132    );
     1133
     1134    return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
     1135}
     1136
     1137/**
    11091138 * Create 'sizes' attribute value for an image.
    11101139 *
    11111140 * @since 4.4.0
    11121141 *
    1113  * @param array|string $size          Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.),
    1114  *                                    or an array of width and height values in pixels (in that order).
     1142 * @param array|string $size          Image size to retrieve. Accepts any valid image size, or an array
     1143 *                                    of width and height values in pixels (in that order). Default 'medium'.
    11151144 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
    11161145 * @param int          $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed
     
    11201149 * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
    11211150 */
    1122 function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_src = null ) {
     1151function wp_calculate_image_sizes( $size, $image_src, $image_meta, $attachment_id = 0 ) {
    11231152    $width = 0;
    11241153
     
    11461175
    11471176    /**
    1148      * Filter the output of 'wp_get_attachment_image_sizes()'.
     1177     * Filter the output of 'wp_calculate_image_sizes()'.
    11491178     *
    11501179     * @since 4.4.0
    11511180     *
    11521181     * @param string       $sizes         A source size value for use in a 'sizes' attribute.
    1153      * @param array|string $size          Image size. Image size name, or an array of width and height
    1154      *                                    values in pixels (in that order).
     1182     * @param array|string $size          Requested size. Image size or array of width and height values
     1183     *                                    in pixels (in that order). Default 'medium'.
     1184     * @param string       $image_src     The URL to the image file.
    11551185     * @param array        $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    11561186     * @param int          $attachment_id Image attachment ID of the original image.
    1157      * @param string       $image_src     Optional. The URL to the image file.
    1158      */
    1159     return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_src );
     1187     */
     1188    return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );
    11601189}
    11611190
     
    12261255    }
    12271256
    1228     $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
    1229     list( $src ) = explode( '?', $src );
     1257    $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
     1258    list( $image_src ) = explode( '?', $image_src );
    12301259
    12311260    // Return early if we couldn't get the image source.
    1232     if ( ! $src ) {
     1261    if ( ! $image_src ) {
    12331262        return $image;
    12341263    }
     
    12361265    // Bail early if an image has been inserted and later edited.
    12371266    if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) &&
    1238         strpos( wp_basename( $src ), $img_edit_hash[0] ) === false ) {
     1267        strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
    12391268
    12401269        return $image;
     
    12491278         * the image file name from 'src' against the available sizes for an attachment.
    12501279         */
    1251         $image_filename = wp_basename( $src );
     1280        $image_filename = wp_basename( $image_src );
    12521281
    12531282        if ( $image_filename === wp_basename( $image_meta['file'] ) ) {
     
    12701299
    12711300    $size_array = array( $width, $height );
    1272     $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
     1301    $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
    12731302
    12741303    if ( $srcset ) {
    1275         $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src );
     1304        $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
    12761305    }
    12771306
Note: See TracChangeset for help on using the changeset viewer.