diff -r 34716fd837a4 -r be944660c56a wp/wp-includes/media.php --- a/wp/wp-includes/media.php Tue Dec 15 15:52:01 2020 +0100 +++ b/wp/wp-includes/media.php Wed Sep 21 18:19:35 2022 +0200 @@ -42,13 +42,12 @@ * * @since 2.5.0 * - * @global int $content_width + * @global int $content_width * * @param int $width Width of the image in pixels. * @param int $height Height of the image in pixels. - * @param string|array $size Optional. Image size. Accepts any valid image size, or an array - * of width and height values in pixels (in that order). - * Default 'medium'. + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array + * of width and height values in pixels (in that order). Default 'medium'. * @param string $context Optional. Could be 'display' (like in a theme) or 'edit' * (like inserting into an editor). Default null. * @return int[] { @@ -71,23 +70,23 @@ $max_width = $size[0]; $max_height = $size[1]; } elseif ( 'thumb' === $size || 'thumbnail' === $size ) { - $max_width = intval( get_option( 'thumbnail_size_w' ) ); - $max_height = intval( get_option( 'thumbnail_size_h' ) ); + $max_width = (int) get_option( 'thumbnail_size_w' ); + $max_height = (int) get_option( 'thumbnail_size_h' ); // Last chance thumbnail size defaults. if ( ! $max_width && ! $max_height ) { $max_width = 128; $max_height = 96; } } elseif ( 'medium' === $size ) { - $max_width = intval( get_option( 'medium_size_w' ) ); - $max_height = intval( get_option( 'medium_size_h' ) ); + $max_width = (int) get_option( 'medium_size_w' ); + $max_height = (int) get_option( 'medium_size_h' ); } elseif ( 'medium_large' === $size ) { - $max_width = intval( get_option( 'medium_large_size_w' ) ); - $max_height = intval( get_option( 'medium_large_size_h' ) ); - - if ( intval( $content_width ) > 0 ) { - $max_width = min( intval( $content_width ), $max_width ); + $max_width = (int) get_option( 'medium_large_size_w' ); + $max_height = (int) get_option( 'medium_large_size_h' ); + + if ( (int) $content_width > 0 ) { + $max_width = min( (int) $content_width, $max_width ); } } elseif ( 'large' === $size ) { /* @@ -96,18 +95,18 @@ * itself, and within the theme's content width if it's known. The user * can resize it in the editor if they wish. */ - $max_width = intval( get_option( 'large_size_w' ) ); - $max_height = intval( get_option( 'large_size_h' ) ); - - if ( intval( $content_width ) > 0 ) { - $max_width = min( intval( $content_width ), $max_width ); + $max_width = (int) get_option( 'large_size_w' ); + $max_height = (int) get_option( 'large_size_h' ); + + if ( (int) $content_width > 0 ) { + $max_width = min( (int) $content_width, $max_width ); } } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) { - $max_width = intval( $_wp_additional_image_sizes[ $size ]['width'] ); - $max_height = intval( $_wp_additional_image_sizes[ $size ]['height'] ); + $max_width = (int) $_wp_additional_image_sizes[ $size ]['width']; + $max_height = (int) $_wp_additional_image_sizes[ $size ]['height']; // Only in admin. Assume that theme authors know what they're doing. - if ( intval( $content_width ) > 0 && 'edit' === $context ) { - $max_width = min( intval( $content_width ), $max_width ); + if ( (int) $content_width > 0 && 'edit' === $context ) { + $max_width = min( (int) $content_width, $max_width ); } } else { // $size === 'full' has no constraint. $max_width = $width; @@ -125,10 +124,11 @@ * @type int $0 The maximum width in pixels. * @type int $1 The maximum height in pixels. * } - * @param string|array $size Size of what the result image should be. - * @param string $context The context the image is being resized for. - * Possible values are 'display' (like in a theme) - * or 'edit' (like inserting into an editor). + * @param string|int[] $size Requested image size. Can be any registered image size name, or + * an array of width and height values in pixels (in that order). + * @param string $context The context the image is being resized for. + * Possible values are 'display' (like in a theme) + * or 'edit' (like inserting into an editor). */ list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context ); @@ -155,10 +155,10 @@ function image_hwstring( $width, $height ) { $out = ''; if ( $width ) { - $out .= 'width="' . intval( $width ) . '" '; + $out .= 'width="' . (int) $width . '" '; } if ( $height ) { - $out .= 'height="' . intval( $height ) . '" '; + $out .= 'height="' . (int) $height . '" '; } return $out; } @@ -177,9 +177,8 @@ * @since 2.5.0 * * @param int $id Attachment ID for image. - * @param string|int[] $size Optional. Image size to scale to. Accepts any valid image size name, - * or an array of width and height values in pixels (in that order). - * Default 'medium'. + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array + * of width and height values in pixels (in that order). Default 'medium'. * @return array|false { * Array of image data, or boolean false if no image is available. * @@ -202,8 +201,8 @@ * * @param bool|array $downsize Whether to short-circuit the image downsize. * @param int $id Attachment ID for image. - * @param array|string $size Requested size of image. Image size name, or array of width - * and height values (in that order). + * @param string|int[] $size Requested image size. Can be any registered image size name, or + * an array of width and height values in pixels (in that order). */ $out = apply_filters( 'image_downsize', false, $id, $size ); @@ -245,7 +244,7 @@ $info = null; if ( $thumb_file ) { - $info = @getimagesize( $thumb_file ); + $info = wp_getimagesize( $thumb_file ); } if ( $thumb_file && $info ) { @@ -367,9 +366,8 @@ * @param string $alt Image description for the alt attribute. * @param string $title Image description for the title attribute. * @param string $align Part of the class name for aligning the image. - * @param string|array $size Optional. Registered image size to retrieve a tag for. Accepts any - * valid image size, or an array of width and height values in pixels - * (in that order). Default 'medium'. + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of + * width and height values in pixels (in that order). Default 'medium'. * @return string HTML IMG element for given image attachment */ function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { @@ -379,7 +377,8 @@ $title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; - $class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size ) . ' wp-image-' . $id; + $size_class = is_array( $size ) ? implode( 'x', $size ) : $size; + $class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size_class ) . ' wp-image-' . $id; /** * Filters the value of the attachment's image tag class attribute. @@ -389,8 +388,8 @@ * @param string $class CSS class name or space-separated list of classes. * @param int $id Attachment ID. * @param string $align Part of the class name for aligning the image. - * @param string|array $size Size of image. Image size or array of width and height values (in that order). - * Default 'medium'. + * @param string|int[] $size Requested image size. Can be any registered image size name, or + * an array of width and height values in pixels (in that order). */ $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); @@ -406,8 +405,8 @@ * @param string $alt Image description for the alt attribute. * @param string $title Image description for the title attribute. * @param string $align Part of the class name for aligning the image. - * @param string|array $size Size of image. Image size or array of width and height values (in that order). - * Default 'medium'. + * @param string|int[] $size Requested image size. Can be any registered image size name, or + * an array of width and height values in pixels (in that order). */ return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); } @@ -744,18 +743,17 @@ * @since 2.5.0 * * @param int $post_id Attachment ID. - * @param array|string $size Optional. Image size. Accepts any valid image size, or an array - * of width and height values in pixels (in that order). - * Default 'thumbnail'. + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array + * of width and height values in pixels (in that order). Default 'thumbnail'. * @return array|false { * Array of file relative path, width, and height on success. Additionally includes absolute - * path and URL if registered size is passed to $size parameter. False on failure. - * - * @type string $file Image's path relative to uploads directory - * @type int $width Width of image - * @type int $height Height of image - * @type string $path Image's absolute filesystem path. - * @type string $url Image's URL. + * path and URL if registered size is passed to `$size` parameter. False on failure. + * + * @type string $file Path of image relative to uploads directory. + * @type int $width Width of image in pixels. + * @type int $height Height of image in pixels. + * @type string $path Absolute filesystem path of image. + * @type string $url URL of image. * } */ function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { @@ -778,7 +776,7 @@ foreach ( $imagedata['sizes'] as $_size => $data ) { // If there's an exact match to an existing image size, short circuit. - if ( intval( $data['width'] ) === intval( $size[0] ) && intval( $data['height'] ) === intval( $size[1] ) ) { + if ( (int) $data['width'] === (int) $size[0] && (int) $data['height'] === (int) $size[1] ) { $candidates[ $data['width'] * $data['height'] ] = $data; break; } @@ -844,9 +842,9 @@ * * @param array $data Array of file relative path, width, and height on success. May also include * file absolute path and URL. - * @param int $post_id The post_id of the image attachment - * @param string|array $size Registered image size or flat array of initially-requested height and width - * dimensions (in that order). + * @param int $post_id The ID of the image attachment. + * @param string|int[] $size Requested image size. Can be any registered image size name, or + * an array of width and height values in pixels (in that order). */ return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size ); } @@ -899,16 +897,16 @@ if ( isset( $additional_sizes[ $size_name ]['width'] ) ) { // For sizes added by plugins and themes. - $size_data['width'] = intval( $additional_sizes[ $size_name ]['width'] ); + $size_data['width'] = (int) $additional_sizes[ $size_name ]['width']; } else { // For default sizes set in options. - $size_data['width'] = intval( get_option( "{$size_name}_size_w" ) ); + $size_data['width'] = (int) get_option( "{$size_name}_size_w" ); } if ( isset( $additional_sizes[ $size_name ]['height'] ) ) { - $size_data['height'] = intval( $additional_sizes[ $size_name ]['height'] ); + $size_data['height'] = (int) $additional_sizes[ $size_name ]['height']; } else { - $size_data['height'] = intval( get_option( "{$size_name}_size_h" ) ); + $size_data['height'] = (int) get_option( "{$size_name}_size_h" ); } if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) { @@ -938,8 +936,8 @@ * @since 2.5.0 * * @param int $attachment_id Image attachment ID. - * @param string|int[] $size Optional. Image size. Accepts any valid image size name, or an array of width - * and height values in pixels (in that order). Default 'thumbnail'. + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of + * width and height values in pixels (in that order). Default 'thumbnail'. * @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false. * @return array|false { * Array of image data, or boolean false if no image is available. @@ -964,7 +962,7 @@ $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); $src_file = $icon_dir . '/' . wp_basename( $src ); - list( $width, $height ) = @getimagesize( $src_file ); + list( $width, $height ) = wp_getimagesize( $src_file ); } } @@ -986,15 +984,15 @@ * @type bool $3 Whether the image is a resized image. * } * @param int $attachment_id Image attachment ID. - * @param string|int[] $size Requested size of image. Image size name, or array of width - * and height values (in that order). + * @param string|int[] $size Requested image size. Can be any registered image size name, or + * an array of width and height values in pixels (in that order). * @param bool $icon Whether the image should be treated as an icon. */ return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon ); } /** - * Get an HTML img element representing an image attachment + * Get an HTML img element representing an image attachment. * * While `$size` will accept an array, it is better to register a size with * add_image_size() so that a cropped version is generated. It's much more @@ -1006,8 +1004,8 @@ * @since 5.5.0 The `$loading` attribute was added. * * @param int $attachment_id Image attachment ID. - * @param string|array $size Optional. Image size. Accepts any valid image size, or an array of width - * and height values in pixels (in that order). Default 'thumbnail'. + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array + * of width and height values in pixels (in that order). Default 'thumbnail'. * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. * @param string|array $attr { * Optional. Attributes for the image markup. @@ -1037,7 +1035,7 @@ $size_class = $size; if ( is_array( $size_class ) ) { - $size_class = join( 'x', $size_class ); + $size_class = implode( 'x', $size_class ); } $default_attr = array( @@ -1083,11 +1081,11 @@ * * @since 2.8.0 * - * @param array $attr Array of attribute values for the image markup, keyed by attribute name. + * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. * See wp_get_attachment_image(). * @param WP_Post $attachment Image attachment post. - * @param string|array $size Requested size. Image size or array of width and height values - * (in that order). Default 'thumbnail'. + * @param string|int[] $size Requested image size. Can be any registered image size name, or + * an array of width and height values in pixels (in that order). */ $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); @@ -1101,7 +1099,20 @@ $html .= ' />'; } - return $html; + /** + * HTML img element representing an image attachment. + * + * @since 5.6.0 + * + * @param string $html HTML img element or empty string on failure. + * @param int $attachment_id Image attachment ID. + * @param string|int[] $size Requested image size. Can be any registered image size name, or + * an array of width and height values in pixels (in that order). + * @param bool $icon Whether the image should be treated as an icon. + * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. + * See wp_get_attachment_image(). + */ + return apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr ); } /** @@ -1110,10 +1121,11 @@ * @since 4.4.0 * * @param int $attachment_id Image attachment ID. - * @param string|array $size Optional. Image size to retrieve. Accepts any valid image size, or an array - * of width and height values in pixels (in that order). Default 'thumbnail'. + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of + * width and height values in pixels (in that order). Default 'thumbnail'. * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. - * @return string|false Attachment URL or false if no image is available. + * @return string|false Attachment URL or false if no image is available. If `$size` does not match + * any registered image size, the original image URL will be returned. */ function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) { $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); @@ -1153,9 +1165,14 @@ * @since 4.4.0 * @access private * - * @param string $size_name Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.). + * @param string $size_name Image size. Accepts any registered image size name. * @param array $image_meta The image meta data. - * @return array|bool The image meta data as returned by `wp_get_attachment_metadata()`. + * @return array|false { + * Array of width and height or false if the size isn't present in the meta data. + * + * @type int $0 Image width. + * @type int $1 Image height. + * } */ function _wp_get_image_size_from_meta( $size_name, $image_meta ) { if ( 'full' === $size_name ) { @@ -1181,11 +1198,11 @@ * @see wp_calculate_image_srcset() * * @param int $attachment_id Image attachment ID. - * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of * width and height values in pixels (in that order). Default 'medium'. * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. * Default null. - * @return string|bool A 'srcset' value string or false. + * @return string|false A 'srcset' value string or false. */ function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { $image = wp_get_attachment_image_src( $attachment_id, $size ); @@ -1221,7 +1238,7 @@ * @param string $image_src The 'src' of the image. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. * @param int $attachment_id Optional. The image attachment ID. Default 0. - * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. + * @return string|false The 'srcset' attribute value. False on error or when only one source exists. */ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) { /** @@ -1422,11 +1439,11 @@ * @see wp_calculate_image_sizes() * * @param int $attachment_id Image attachment ID. - * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of width - * and height values in pixels (in that order). Default 'medium'. + * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of + * width and height values in pixels (in that order). Default 'medium'. * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. * Default null. - * @return string|bool A valid source size value for use in a 'sizes' attribute or false. + * @return string|false A valid source size value for use in a 'sizes' attribute or false. */ function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { $image = wp_get_attachment_image_src( $attachment_id, $size ); @@ -1453,14 +1470,14 @@ * * @since 4.4.0 * - * @param array|string $size Image size to retrieve. Accepts any valid image size, or an array - * of width and height values in pixels (in that order). Default 'medium'. + * @param string|int[] $size Image size. Accepts any registered image size name, or an array of + * width and height values in pixels (in that order). * @param string $image_src Optional. The URL to the image file. Default null. * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. * Default null. * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` * is needed when using the image size name as argument for `$size`. Default 0. - * @return string|bool A valid source size value for use in a 'sizes' attribute or false. + * @return string|false A valid source size value for use in a 'sizes' attribute or false. */ function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) { $width = 0; @@ -1493,8 +1510,8 @@ * @since 4.4.0 * * @param string $sizes A source size value for use in a 'sizes' attribute. - * @param array|string $size Requested size. Image size or array of width and height values - * in pixels (in that order). + * @param string|int[] $size Requested image size. Can be any registered image size name, or + * an array of width and height values in pixels (in that order). * @param string|null $image_src The URL to the image file or null. * @param array|null $image_meta The image meta data as returned by wp_get_attachment_metadata() or null. * @param int $attachment_id Image attachment ID of the original image or 0. @@ -1558,7 +1575,7 @@ } /** - * Filter whether an image path or URI matches image meta. + * Filters whether an image path or URI matches image meta. * * @since 5.5.0 * @@ -1583,32 +1600,48 @@ * or false if dimensions cannot be determined. */ function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) { - if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta, $attachment_id ) ) { - return false; - } + $dimensions = false; // Is it a full size image? - if ( strpos( $image_src, $image_meta['file'] ) !== false ) { - return array( + if ( + isset( $image_meta['file'] ) && + strpos( $image_src, wp_basename( $image_meta['file'] ) ) !== false + ) { + $dimensions = array( (int) $image_meta['width'], (int) $image_meta['height'], ); } - if ( ! empty( $image_meta['sizes'] ) ) { + if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) { $src_filename = wp_basename( $image_src ); foreach ( $image_meta['sizes'] as $image_size_data ) { if ( $src_filename === $image_size_data['file'] ) { - return array( + $dimensions = array( (int) $image_size_data['width'], (int) $image_size_data['height'], ); + + break; } } } - return false; + /** + * Filters the 'wp_image_src_get_dimensions' value. + * + * @since 5.7.0 + * + * @param array|false $dimensions Array with first element being the width + * and second element being the height, or + * false if dimensions could not be determined. + * @param string $image_src The image source file. + * @param array $image_meta The image meta data as returned by + * 'wp_get_attachment_metadata()'. + * @param int $attachment_id The image attachment ID. Default 0. + */ + return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id ); } /** @@ -1684,18 +1717,21 @@ } /** - * Determine whether to add the `loading` attribute to the specified tag in the specified context. + * Determines whether to add the `loading` attribute to the specified tag in the specified context. * * @since 5.5.0 + * @since 5.7.0 Now returns `true` by default for `iframe` tags. * * @param string $tag_name The tag name. - * @param string $context Additional context, like the current filter name or the function name from where this was called. + * @param string $context Additional context, like the current filter name + * or the function name from where this was called. * @return bool Whether to add the attribute. */ function wp_lazy_loading_enabled( $tag_name, $context ) { - // By default add to all 'img' tags. + // By default add to all 'img' and 'iframe' tags. // See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading - $default = ( 'img' === $tag_name ); + // See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading + $default = ( 'img' === $tag_name || 'iframe' === $tag_name ); /** * Filters whether to add the `loading` attribute to the specified tag in the specified context. @@ -1704,7 +1740,8 @@ * * @param bool $default Default value. * @param string $tag_name The tag name. - * @param string $context Additional context, like the current filter name or the function name from where this was called. + * @param string $context Additional context, like the current filter name + * or the function name from where this was called. */ return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context ); } @@ -1714,14 +1751,17 @@ * * Modifies HTML tags in post content to include new browser and HTML technologies * that may not have existed at the time of post creation. These modifications currently - * include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags. + * include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags, as well + * as adding `loading` attributes to `iframe` HTML tags. * Future similar optimizations should be added/expected here. * * @since 5.5.0 + * @since 5.7.0 Now supports adding `loading` attributes to `iframe` tags. * * @see wp_img_tag_add_width_and_height_attr() * @see wp_img_tag_add_srcset_and_sizes_attr() * @see wp_img_tag_add_loading_attr() + * @see wp_iframe_tag_add_loading_attr() * * @param string $content The HTML content to be filtered. * @param string $context Optional. Additional context to pass to the filters. @@ -1733,32 +1773,40 @@ $context = current_filter(); } - $add_loading_attr = wp_lazy_loading_enabled( 'img', $context ); - - if ( false === strpos( $content, ']+>/', $content, $matches ) ) { + $add_img_loading_attr = wp_lazy_loading_enabled( 'img', $context ); + $add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context ); + + if ( ! preg_match_all( '/<(img|iframe)\s[^>]+>/', $content, $matches, PREG_SET_ORDER ) ) { return $content; } // List of the unique `img` tags found in $content. $images = array(); - foreach ( $matches[0] as $image ) { - if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) { - $attachment_id = absint( $class_id[1] ); - - if ( $attachment_id ) { - // If exactly the same image tag is used more than once, overwrite it. - // All identical tags will be replaced later with 'str_replace()'. - $images[ $image ] = $attachment_id; - continue; - } + // List of the unique `iframe` tags found in $content. + $iframes = array(); + + foreach ( $matches as $match ) { + list( $tag, $tag_name ) = $match; + + switch ( $tag_name ) { + case 'img': + if ( preg_match( '/wp-image-([0-9]+)/i', $tag, $class_id ) ) { + $attachment_id = absint( $class_id[1] ); + + if ( $attachment_id ) { + // If exactly the same image tag is used more than once, overwrite it. + // All identical tags will be replaced later with 'str_replace()'. + $images[ $tag ] = $attachment_id; + break; + } + } + $images[ $tag ] = 0; + break; + case 'iframe': + $iframes[ $tag ] = 0; + break; } - - $images[ $image ] = 0; } // Reduce the array to unique attachment IDs. @@ -1786,7 +1834,7 @@ } // Add 'loading' attribute if applicable. - if ( $add_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) { + if ( $add_img_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) { $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context ); } @@ -1795,6 +1843,19 @@ } } + foreach ( $iframes as $iframe => $attachment_id ) { + $filtered_iframe = $iframe; + + // Add 'loading' attribute if applicable. + if ( $add_iframe_loading_attr && false === strpos( $filtered_iframe, ' loading=' ) ) { + $filtered_iframe = wp_iframe_tag_add_loading_attr( $filtered_iframe, $context ); + } + + if ( $filtered_iframe !== $iframe ) { + $content = str_replace( $iframe, $filtered_iframe, $content ); + } + } + return $content; } @@ -1808,8 +1869,13 @@ * @return string Converted `img` tag with `loading` attribute added. */ function wp_img_tag_add_loading_attr( $image, $context ) { + // Images should have source and dimension attributes for the `loading` attribute to be added. + if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) { + return $image; + } + /** - * Filters the `loading` attribute value. Default `lazy`. + * Filters the `loading` attribute value to add to an image. Default `lazy`. * * Returning `false` or an empty string will not add the attribute. * Returning `true` will add the default value. @@ -1817,7 +1883,7 @@ * @since 5.5.0 * * @param string|bool $value The `loading` attribute value. Returning a falsey value will result in - * the attribute being omitted for the image. Default is `lazy`. + * the attribute being omitted for the image. Default 'lazy'. * @param string $image The HTML `img` tag to be filtered. * @param string $context Additional context about how the function was called or where the img tag is. */ @@ -1828,11 +1894,6 @@ $value = 'lazy'; } - // Images should have source and dimension attributes for the `loading` attribute to be added. - if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) { - return $image; - } - return str_replace( '