WordPress.org

Make WordPress Core

Ticket #34430: 34430.6.diff

File 34430.6.diff, 15.6 KB (added by jaspermdegroot, 6 years ago)
  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index fc69bdc..a88188f 100644
    function _wp_get_image_size_from_meta( $size, $image_meta ) { 
    906906 * @param int          $attachment_id Optional. Image attachment ID.
    907907 * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
    908908 *                                    values in pixels (in that order). Default 'medium'.
    909  * @param array        $image_meta    Optional. The image meta data.
     909 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
    910910 * @return string|bool A 'srcset' value string or false.
    911911 */
    912912function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
    function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag 
    924924                absint( $image[2] )
    925925        );
    926926
    927         // Calculate the sources for the srcset.
     927        // Calculate the sources for the 'srcset'.
    928928        $sources = wp_calculate_image_srcset( $image_url, $size_array, $image_meta, $attachment_id );
    929929
    930         // Only return a srcset value if there is more than one source.
     930        // Only return a 'srcset' value if there is more than one source.
    931931        if ( count( $sources ) < 2 ) {
    932932                return false;
    933933        }
    function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag 
    937937
    938938
    939939/**
    940  * A helper function to concatenate and filter the srcset attribute value.
     940 * A helper function to concatenate and filter the 'srcset' attribute value.
    941941 *
    942942 * @since 4.4.0
    943943 *
    944  * @param array   $sources       The array containing image sizes data as returned by wp_calculate_image_srcset().
     944 * @param array   $sources       The array containing image sizes data as returned by 'wp_calculate_image_srcset()'.
    945945 * @param array   $size_array    Array of width and height values in pixels (in that order).
    946  * @param array   $image_meta    The image meta data.
    947  * @param int     $attachment_id The image attachment post id to pass to the filter.
    948  * @return string The srcset attribute value.
     946 * @param array   $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
     947 * @param int     $attachment_id The image attachment ID to pass to the filter.
     948 * @return string The 'srcset' attribute value.
    949949 */
    950950function wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id ) {
    951951        $srcset = '';
    function wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_i 
    955955        }
    956956
    957957        /**
    958          * Filter the output of wp_get_attachment_image_srcset().
     958         * Filter the output of 'wp_image_srcset_attr()'.
    959959         *
    960960         * @since 4.4.0
    961961         *
    962          * @param string       $srcset        A source set formatted for a `srcset` attribute.
     962         * @param string       $srcset        A source set formatted for a 'srcset' attribute.
    963963         * @param int          $attachment_id Image attachment ID.
    964          * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
    965          *                                    values in pixels (in that order). Default 'medium'.
     964         * @param array|string $size          Image size. Image size name, or an array of width and height
     965         *                                    values in pixels (in that order).
    966966         * @param array        $image_meta    The image meta data.
    967967         */
    968         return apply_filters( 'wp_get_attachment_image_srcset', rtrim( $srcset, ', ' ), $attachment_id, $size_array, $image_meta );
     968        return apply_filters( 'wp_image_srcset_attr', rtrim( $srcset, ', ' ), $attachment_id, $size_array, $image_meta );
    969969}
    970970
    971971/**
    972  * A helper function to caclulate the image sources to include in a srcset attribute.
     972 * A helper function to calculate the image sources to include in a 'srcset' attribute.
    973973 *
    974974 * @since 4.4.0
    975975 *
    976  * @param string $image_name    The file name, path, URL or partial path or URL of the image being matched.
     976 * @param string $image_name    The file name, path, URL, or partial path or URL, of the image being matched.
    977977 * @param array  $size_array    Array of width and height values in pixels (in that order).
    978  * @param array  $image_meta    The image meta data.
    979  * @param int    $attachment_id Optional. The image attachment post id to pass to the filter.
     978 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
     979 * @param int    $attachment_id Optional. The image attachment ID to pass to the filter.
    980980 * @return array|bool $sources {
    981981 *     Array image candidate values containing a URL, descriptor type, and
    982982 *     descriptor value. False if none exist.
    983983 *
    984984 *     @type array $values {
    985985 *        @type string $url        An image URL.
    986  *        @type string $descriptor A width or density descriptor used in a srcset.
     986 *        @type string $descriptor A width or density descriptor used in a 'srcset'.
    987987 *        @type int    $value      The descriptor value representing a width or
    988988 *                                 or pixel density.
    989989 *     }
    990990 * }
    991  *
    992991 */
    993992function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $attachment_id = 0 ) {
    994993        if ( empty( $image_meta['sizes'] ) ) {
    function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta 
    997996
    998997        $image_sizes = $image_meta['sizes'];
    999998
    1000         // Get the height and width for the image.
     999        // Get the width and height of the image.
    10011000        $image_width = (int) $size_array[0];
    10021001        $image_height = (int) $size_array[1];
    10031002
    function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta 
    10061005                return false;
    10071006        }
    10081007
    1009         // Add full size to the img_sizes array.
     1008        // Add full size to the '$img_sizes' array.
    10101009        $image_sizes['full'] = array(
    10111010                'width'  => $image_meta['width'],
    10121011                'height' => $image_meta['height'],
    function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta 
    10311030        $image_edited = preg_match( '/-e[0-9]{13}/', $image_name, $image_edit_hash );
    10321031
    10331032        /**
    1034          * Filter the maximum width included in a 'srcset' attribute.
     1033         * Filter the maximum image width to be included in a 'srcset' attribute.
    10351034         *
    10361035         * @since 4.4.0
    10371036         *
    1038          * @param int          $max_width  The maximum width to include in the 'srcset'. Default '1600'.
     1037         * @param int          $max_width  The maximum image width to be included in the 'srcset'. Default '1600'.
    10391038         * @param array|string $size_array Array of width and height values in pixels (in that order).
    10401039         */
    1041         $max_srcset_width = apply_filters( 'max_srcset_image_width', 1600, $size_array );
     1040        $max_srcset_image_width = apply_filters( 'max_srcset_image_width', 1600, $size_array );
    10421041
    10431042        // Array to hold URL candidates.
    10441043        $sources = array();
    function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta 
    10541053                        continue;
    10551054                }
    10561055
    1057                 // Filter out images that are wider than $max_srcset_width.
    1058                 if ( $max_srcset_width && $image['width'] > $max_srcset_width ) {
     1056                // Filter out images that are wider than '$max_srcset_image_width'.
     1057                if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width ) {
    10591058                        continue;
    10601059                }
    10611060
    function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta 
    10801079        }
    10811080
    10821081        /**
    1083          * Filter the output of wp_get_attachment_image_srcset_array().
     1082         * Filter the output of 'wp_calculate_image_srcset()'.
    10841083         *
    10851084         * @since 4.4.0
    10861085         *
    10871086         * @param array        $sources       An array of image URLs and widths.
    1088          * @param int          $attachment_id Attachment ID for image.
    1089          * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
    1090          *                                    values in pixels (in that order). Default 'medium'.
    1091          * @param array        $image_meta    The image meta data.
     1087         * @param int          $attachment_id Image attachment ID.
     1088         * @param array|string $size          Image size. Image size name, or an array of width and height
     1089         *                                    values in pixels (in that order).
     1090         * @param array        $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    10921091
    10931092         */
    1094         return apply_filters( 'wp_get_attachment_image_srcset_array', array_values( $sources ), $attachment_id, $size_array, $image_meta );
     1093        return apply_filters( 'wp_calculate_image_srcset', array_values( $sources ), $attachment_id, $size_array, $image_meta );
    10951094}
    10961095
    10971096/**
    1098  * Create `sizes` attribute value for an image.
     1097 * Create 'sizes' attribute value for an image.
    10991098 *
    11001099 * @since 4.4.0
    11011100 *
    1102  * @param array|string $size          Image size. Accepts any valid image size name (thumbnail, medium, etc.),
     1101 * @param array|string $size          Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.),
    11031102 *                                    or an array of width and height values in pixels (in that order).
    1104  * @param array        $image_meta    Optional. The image meta data.
    1105  * @param int          $attachment_id Optional. Image attachment ID. Either $image_meta or $attachment_id is needed
    1106  *                                    when using image size name.
     1103 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
     1104 * @param int          $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed
     1105 *                                    when using the image size name as argument for `$size`.
    11071106 *
    11081107 * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
    11091108 */
    function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i 
    11221121                if ( $image_meta ) {
    11231122                        $width = _wp_get_image_size_from_meta( $size, $image_meta );
    11241123                        if ( $width ) {
    1125                                 $width = $width[0];
     1124                                $width = absint( $width[0] );
    11261125                        }
    11271126                }
    11281127        }
    function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i 
    11311130                return false;
    11321131        }
    11331132
    1134         // Setup the default sizes attribute.
    1135         $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', (int) $width );
     1133        // Setup the default 'sizes' attribute.
     1134        $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width );
    11361135
    11371136        /**
    1138          * Filter the output of wp_get_attachment_image_sizes().
     1137         * Filter the output of 'wp_get_attachment_image_sizes()'.
    11391138         *
    11401139         * @since 4.4.0
    11411140         *
    11421141         * @param string       $sizes         A source size value for use in a 'sizes' attribute.
    1143          * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
    1144          *                                    values in pixels (in that order). Default 'medium'.
    1145          * @param array        $image_meta    The image meta data.
     1142         * @param array|string $size          Image size. Image size name, or an array of width and height
     1143         *                                    values in pixels (in that order).
     1144         * @param array        $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    11461145         * @param int          $attachment_id Post ID of the original image.
    11471146         */
    11481147        return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id );
    function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i 
    11531152 *
    11541153 * @since 4.4.0
    11551154 *
    1156  * @see wp_image_add_srcset_and_sizes()
     1155 * @see 'wp_image_add_srcset_and_sizes()'
    11571156 *
    11581157 * @param string $content The raw post content to be filtered.
    11591158 * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
    function wp_make_content_images_responsive( $content ) { 
    11671166                if ( false === strpos( $image, ' srcset="' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
    11681167                        ( $attachment_id = absint( $class_id[1] ) ) ) {
    11691168
    1170                         // If exactly the same image tag is used more than once, overwrite it.
    1171                         // All identical tags will be replaced later with str_replace().
     1169                        /*
     1170                         * If exactly the same image tag is used more than once, overwrite it.
     1171                         * All identical tags will be replaced later with 'str_replace()'.
     1172                         */
    11721173                        $selected_images[ $image ] = $attachment_id;
    11731174                        // Overwrite the ID when the same image is included more than once.
    11741175                        $attachment_ids[ $attachment_id ] = true;
    function wp_make_content_images_responsive( $content ) { 
    11771178
    11781179        if ( count( $attachment_ids ) > 1 ) {
    11791180                /*
    1180                  * Warm object cache for use with get_post_meta().
     1181                 * Warm object cache for use with 'get_post_meta()'.
    11811182                 *
    11821183                 * To avoid making a database call for each image, a single query
    11831184                 * warms the object cache with the meta information for all images.
    function wp_make_content_images_responsive( $content ) { 
    11981199 *
    11991200 * @since 4.4.0
    12001201 *
    1201  * @see wp_get_attachment_image_srcset()
    1202  * @see wp_get_attachment_image_sizes()
     1202 * @see 'wp_get_attachment_image_srcset()'
     1203 * @see 'wp_get_attachment_image_sizes()'
    12031204 *
    12041205 * @param string $image         An HTML 'img' element to be filtered.
    1205  * @param array  $image_meta    The image meta data.
     1206 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    12061207 * @param int    $attachment_id Image attachment ID.
    1207  * @return string Converted 'img' element with `srcset` and `sizes` attributes added.
     1208 * @return string Converted 'img' element with 'srcset' and 'sizes' attributes added.
    12081209 */
    12091210function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {
    1210         // Ensure the image meta exists
     1211        // Ensure the image meta exists.
    12111212        if ( empty( $image_meta['sizes'] ) ) {
    12121213                return $image;
    12131214        }
    function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 
    12151216        $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
    12161217        list( $src ) = explode( '?', $src );
    12171218
    1218         // Return early if we coudn't get the image source.
     1219        // Return early if we couldn't get the image source.
    12191220        if ( ! $src ) {
    12201221                return $image;
    12211222        }
    12221223
    1223         // Bail early when an image has been inserted and later edited.
     1224        // Bail early if an image has been inserted and later edited.
    12241225        if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) &&
    12251226                strpos( wp_basename( $src ), $img_edit_hash[0] ) === false ) {
    12261227
    function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 
    12321233
    12331234        if ( ! $width || ! $height ) {
    12341235                /*
    1235                  * If attempts to parse the size value failed, attempt to use the image
    1236                  * metadata to match the image file name from 'src' against the available sizes for an attachment.
     1236                 * If attempts to parse the size value failed, attempt to use the image meta data to match
     1237                 * the image file name from 'src' against the available sizes for an attachment.
    12371238                 */
    12381239                $image_filename = wp_basename( $src );
    12391240
    function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 
    12591260        $sources = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
    12601261
    12611262        $srcset = $sizes = '';
    1262         // Only calculate srcset and sizes values if there is more than one source.
     1263        // Only calculate 'srcset' and 'sizes' values if there is more than one source.
    12631264        if ( count( $sources ) > 1 ) {
    12641265                $srcset = wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id );
    12651266                $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id );
    12661267        }
    12671268
    12681269        if ( $srcset && $sizes ) {
    1269                 // Format the srcset and sizes string and escape attributes.
     1270                // Format the 'srcset' and 'sizes' string and escape attributes.
    12701271                $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes ) );
    12711272
    1272                 // Add srcset and sizes attributes to the image markup.
     1273                // Add 'srcset' and 'sizes' attributes to the image markup.
    12731274                $image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $srcset_and_sizes . ' />', $image );
    12741275        }
    12751276