Changeset 59415 for trunk/src/wp-includes/media.php
- Timestamp:
- 11/18/2024 07:50:06 PM (13 days ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/media.php
r59406 r59415 1138 1138 } 1139 1139 1140 /** This filter is documented in wp-includes/media.php */ 1141 $add_auto_sizes = apply_filters( 'wp_img_tag_add_auto_sizes', true ); 1142 1140 1143 // Adds 'auto' to the sizes attribute if applicable. 1141 1144 if ( 1145 $add_auto_sizes && 1142 1146 isset( $attr['loading'] ) && 1143 1147 'lazy' === $attr['loading'] && … … 1986 1990 */ 1987 1991 function wp_img_tag_add_auto_sizes( string $image ): string { 1992 /** 1993 * Filters whether auto-sizes for lazy loaded images is enabled. 1994 * 1995 * @since 6.7.1 1996 * 1997 * @param boolean $enabled Whether auto-sizes for lazy loaded images is enabled. 1998 */ 1999 if ( ! apply_filters( 'wp_img_tag_add_auto_sizes', true ) ) { 2000 return $image; 2001 } 2002 1988 2003 $processor = new WP_HTML_Tag_Processor( $image ); 1989 2004 … … 1994 2009 1995 2010 // Bail early if the image is not lazy-loaded. 1996 $value = $processor->get_attribute( 'loading' ); 1997 if ( ! is_string( $value ) || 'lazy' !== strtolower( trim( $value, " \t\f\r\n" ) ) ) { 2011 $loading = $processor->get_attribute( 'loading' ); 2012 if ( ! is_string( $loading ) || 'lazy' !== strtolower( trim( $loading, " \t\f\r\n" ) ) ) { 2013 return $image; 2014 } 2015 2016 /* 2017 * Bail early if the image doesn't have a width attribute. 2018 * Per WordPress Core itself, lazy-loaded images should always have a width attribute. 2019 * However, it is possible that lazy-loading could be added by a plugin, where we don't have that guarantee. 2020 * As such, it still makes sense to ensure presence of a width attribute here in order to use `sizes=auto`. 2021 */ 2022 $width = $processor->get_attribute( 'width' ); 2023 if ( ! is_string( $width ) || '' === $width ) { 1998 2024 return $image; 1999 2025 } … … 2028 2054 list( $first_size ) = explode( ',', $sizes_attr, 2 ); 2029 2055 return 'auto' === strtolower( trim( $first_size, " \t\f\r\n" ) ); 2056 } 2057 2058 /** 2059 * Prints a CSS rule to fix potential visual issues with images using `sizes=auto`. 2060 * 2061 * This rule overrides the similar rule in the default user agent stylesheet, to avoid images that use e.g. 2062 * `width: auto` or `width: fit-content` to appear smaller. 2063 * 2064 * @since 6.7.1 2065 * @see https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size 2066 * @see https://core.trac.wordpress.org/ticket/62413 2067 */ 2068 function wp_print_auto_sizes_contain_css_fix() { 2069 /** This filter is documented in wp-includes/media.php */ 2070 $add_auto_sizes = apply_filters( 'wp_img_tag_add_auto_sizes', true ); 2071 if ( ! $add_auto_sizes ) { 2072 return; 2073 } 2074 2075 ?> 2076 <style>img:is([sizes="auto" i], [sizes^="auto," i]) { contain-intrinsic-size: 3000px 1500px }</style> 2077 <?php 2030 2078 } 2031 2079
Note: See TracChangeset
for help on using the changeset viewer.