Changeset 56690
- Timestamp:
- 09/25/2023 10:37:00 PM (15 months ago)
- Location:
- trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/deprecated.php
r56682 r56690 5995 5995 update_option( 'https_detection_errors', $support_errors ); 5996 5996 } 5997 5998 /** 5999 * Adds `decoding` attribute to an `img` HTML tag. 6000 * 6001 * The `decoding` attribute allows developers to indicate whether the 6002 * browser can decode the image off the main thread (`async`), on the 6003 * main thread (`sync`) or as determined by the browser (`auto`). 6004 * 6005 * By default WordPress adds `decoding="async"` to images but developers 6006 * can use the {@see 'wp_img_tag_add_decoding_attr'} filter to modify this 6007 * to remove the attribute or set it to another accepted value. 6008 * 6009 * @since 6.1.0 6010 * @deprecated 6.4.0 Use wp_img_tag_add_loading_optimization_attrs() instead. 6011 * @see wp_img_tag_add_loading_optimization_attrs() 6012 * 6013 * @param string $image The HTML `img` tag where the attribute should be added. 6014 * @param string $context Additional context to pass to the filters. 6015 * @return string Converted `img` tag with `decoding` attribute added. 6016 */ 6017 function wp_img_tag_add_decoding_attr( $image, $context ) { 6018 _deprecated_function( __FUNCTION__, '6.4.0', 'wp_img_tag_add_loading_optimization_attrs()' ); 6019 6020 /* 6021 * Only apply the decoding attribute to images that have a src attribute that 6022 * starts with a double quote, ensuring escaped JSON is also excluded. 6023 */ 6024 if ( ! str_contains( $image, ' src="' ) ) { 6025 return $image; 6026 } 6027 6028 /** This action is documented in wp-includes/media.php */ 6029 $value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context ); 6030 6031 if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) { 6032 $image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image ); 6033 } 6034 6035 return $image; 6036 } -
trunk/src/wp-includes/media.php
r56680 r56690 1061 1061 1062 1062 $default_attr = array( 1063 'src' => $src, 1064 'class' => "attachment-$size_class size-$size_class", 1065 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), 1066 'decoding' => 'async', 1063 'src' => $src, 1064 'class' => "attachment-$size_class size-$size_class", 1065 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), 1067 1066 ); 1068 1067 … … 1893 1892 $filtered_image = wp_img_tag_add_loading_optimization_attrs( $filtered_image, $context ); 1894 1893 1895 // Add 'decoding=async' attribute unless a 'decoding' attribute is already present.1896 if ( ! str_contains( $filtered_image, ' decoding=' ) ) {1897 $filtered_image = wp_img_tag_add_decoding_attr( $filtered_image, $context );1898 }1899 1900 1894 /** 1901 1895 * Filters an img tag within the content for a given context. … … 1958 1952 $loading_val = preg_match( '/ loading=["\']([A-Za-z]+)["\']/', $image, $match_loading ) ? $match_loading[1] : null; 1959 1953 $fetchpriority_val = preg_match( '/ fetchpriority=["\']([A-Za-z]+)["\']/', $image, $match_fetchpriority ) ? $match_fetchpriority[1] : null; 1954 $decoding_val = preg_match( '/ decoding=["\']([A-Za-z]+)["\']/', $image, $match_decoding ) ? $match_decoding[1] : null; 1960 1955 1961 1956 /* … … 1971 1966 'loading' => $loading_val, 1972 1967 'fetchpriority' => $fetchpriority_val, 1968 'decoding' => $decoding_val, 1973 1969 ), 1974 1970 $context 1975 1971 ); 1976 1972 1977 // Images should have source and dimension attributes for the loading optimization attributes to be added. 1978 if ( ! str_contains( $image, ' src="' ) || ! str_contains( $image, ' width="' ) || ! str_contains( $image, ' height="' ) ) { 1973 // Images should have source for the loading optimization attributes to be added. 1974 if ( ! str_contains( $image, ' src="' ) ) { 1975 return $image; 1976 } 1977 1978 if ( empty( $decoding_val ) ) { 1979 /** 1980 * Filters the `decoding` attribute value to add to an image. Default `async`. 1981 * 1982 * Returning a falsey value will omit the attribute. 1983 * 1984 * @since 6.1.0 1985 * 1986 * @param string|false|null $value The `decoding` attribute value. Returning a falsey value 1987 * will result in the attribute being omitted for the image. 1988 * Otherwise, it may be: 'async', 'sync', or 'auto'. Defaults to false. 1989 * @param string $image The HTML `img` tag to be filtered. 1990 * @param string $context Additional context about how the function was called 1991 * or where the img tag is. 1992 */ 1993 $filtered_decoding_attr = apply_filters( 1994 'wp_img_tag_add_decoding_attr', 1995 isset( $optimization_attrs['decoding'] ) ? $optimization_attrs['decoding'] : false, 1996 $image, 1997 $context 1998 ); 1999 2000 // Validate the values after filtering. 2001 if ( isset( $optimization_attrs['decoding'] ) && ! $filtered_decoding_attr ) { 2002 // Unset `decoding` attribute if `$filtered_decoding_attr` is set to `false`. 2003 unset( $optimization_attrs['decoding'] ); 2004 } elseif ( in_array( $filtered_decoding_attr, array( 'async', 'sync', 'auto' ), true ) ) { 2005 $optimization_attrs['decoding'] = $filtered_decoding_attr; 2006 } 2007 2008 if ( ! empty( $optimization_attrs['decoding'] ) ) { 2009 $image = str_replace( '<img', '<img decoding="' . esc_attr( $optimization_attrs['decoding'] ) . '"', $image ); 2010 } 2011 } 2012 2013 // Images should have dimension attributes for the 'loading' and 'fetchpriority' attributes to be added. 2014 if ( ! str_contains( $image, ' width="' ) || ! str_contains( $image, ' height="' ) ) { 1979 2015 return $image; 1980 2016 } … … 2039 2075 if ( empty( $fetchpriority_val ) && ! empty( $optimization_attrs['fetchpriority'] ) ) { 2040 2076 $image = str_replace( '<img', '<img fetchpriority="' . esc_attr( $optimization_attrs['fetchpriority'] ) . '"', $image ); 2041 }2042 2043 return $image;2044 }2045 2046 /**2047 * Adds `decoding` attribute to an `img` HTML tag.2048 *2049 * The `decoding` attribute allows developers to indicate whether the2050 * browser can decode the image off the main thread (`async`), on the2051 * main thread (`sync`) or as determined by the browser (`auto`).2052 *2053 * By default WordPress adds `decoding="async"` to images but developers2054 * can use the {@see 'wp_img_tag_add_decoding_attr'} filter to modify this2055 * to remove the attribute or set it to another accepted value.2056 *2057 * @since 6.1.02058 *2059 * @param string $image The HTML `img` tag where the attribute should be added.2060 * @param string $context Additional context to pass to the filters.2061 *2062 * @return string Converted `img` tag with `decoding` attribute added.2063 */2064 function wp_img_tag_add_decoding_attr( $image, $context ) {2065 /*2066 * Only apply the decoding attribute to images that have a src attribute that2067 * starts with a double quote, ensuring escaped JSON is also excluded.2068 */2069 if ( ! str_contains( $image, ' src="' ) ) {2070 return $image;2071 }2072 2073 /**2074 * Filters the `decoding` attribute value to add to an image. Default `async`.2075 *2076 * Returning a falsey value will omit the attribute.2077 *2078 * @since 6.1.02079 *2080 * @param string|false|null $value The `decoding` attribute value. Returning a falsey value2081 * will result in the attribute being omitted for the image.2082 * Otherwise, it may be: 'async' (default), 'sync', or 'auto'.2083 * @param string $image The HTML `img` tag to be filtered.2084 * @param string $context Additional context about how the function was called2085 * or where the img tag is.2086 */2087 $value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context );2088 2089 if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) {2090 $image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image );2091 2077 } 2092 2078 … … 5609 5595 * - `loading` attribute with a value of "lazy" 5610 5596 * - `fetchpriority` attribute with a value of "high" 5597 * - `decoding` attribute with a value of "async" 5611 5598 * 5612 5599 * If any of these attributes are already present in the given attributes, they will not be modified. Note that no … … 5658 5645 // For now this function only supports images and iframes. 5659 5646 if ( 'img' !== $tag_name && 'iframe' !== $tag_name ) { 5660 /** This filter is documented in wp-includes/media.php */5661 return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );5662 }5663 5664 // For any resources, width and height must be provided, to avoid layout shifts.5665 if ( ! isset( $attr['width'], $attr['height'] ) ) {5666 5647 /** This filter is documented in wp-includes/media.php */ 5667 5648 return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); … … 5677 5658 // TODO: Handle shortcode images together with the content (see https://core.trac.wordpress.org/ticket/58853). 5678 5659 if ( 'the_content' !== $context && 'do_shortcode' !== $context && doing_filter( 'the_content' ) ) { 5660 /** This filter is documented in wp-includes/media.php */ 5661 return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); 5662 } 5663 5664 /* 5665 * Add `decoding` with a value of "async" for every image unless it has a 5666 * conflicting `decoding` attribute already present. 5667 */ 5668 if ( 'img' === $tag_name ) { 5669 if ( isset( $attr['decoding'] ) ) { 5670 $loading_attrs['decoding'] = $attr['decoding']; 5671 } else { 5672 $loading_attrs['decoding'] = 'async'; 5673 } 5674 } 5675 5676 // For any resources, width and height must be provided, to avoid layout shifts. 5677 if ( ! isset( $attr['width'], $attr['height'] ) ) { 5679 5678 /** This filter is documented in wp-includes/media.php */ 5680 5679 return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); -
trunk/src/wp-includes/pluggable.php
r56245 r56690 2846 2846 'fetchpriority' => null, 2847 2847 'extra_attr' => '', 2848 'decoding' => 'async',2849 2848 ); 2850 2849 -
trunk/src/wp-includes/theme.php
r56687 r56690 1289 1289 $attr, 1290 1290 array( 1291 'src' => $header->url, 1292 'width' => $width, 1293 'height' => $height, 1294 'alt' => $alt, 1295 'decoding' => 'async', 1291 'src' => $header->url, 1292 'width' => $width, 1293 'height' => $height, 1294 'alt' => $alt, 1296 1295 ) 1297 1296 ); -
trunk/src/wp-includes/widgets/class-wp-widget-media-image.php
r56597 r56690 241 241 242 242 $attr = array( 243 'class' => $classes, 244 'src' => $instance['url'], 245 'alt' => $instance['alt'], 246 'width' => $instance['width'], 247 'height' => $instance['height'], 248 'decoding' => 'async', 243 'class' => $classes, 244 'src' => $instance['url'], 245 'alt' => $instance['alt'], 246 'width' => $instance['width'], 247 'height' => $instance['height'], 249 248 ); 250 249 -
trunk/tests/phpunit/tests/media.php
r56680 r56690 2264 2264 $respimg_html5 2265 2265 ); 2266 $content_filtered = wp_img_tag_add_decoding_attr( $content_filtered, 'the_content' );2267 2266 2268 2267 // Do not add width, height, and loading. 2269 2268 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2270 2269 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2270 add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 2271 2271 2272 2272 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); … … 2274 2274 remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2275 2275 remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2276 remove_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 2276 2277 } 2277 2278 … … 2290 2291 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2291 2292 $img = wp_img_tag_add_loading_optimization_attrs( $img, 'test' ); 2292 $img = wp_img_tag_add_decoding_attr( $img, 'the_content' );2293 2293 2294 2294 // Replace the src URL. … … 2305 2305 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2306 2306 $img = wp_img_tag_add_loading_optimization_attrs( $img, 'test' ); 2307 $img = wp_img_tag_add_decoding_attr( $img, 'the_content' );2308 2307 $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img ); 2309 2308 … … 2481 2480 $respimg_relative 2482 2481 ); 2483 $expected = wp_img_tag_add_decoding_attr( $expected, 'the_content' );2484 2482 2485 2483 $actual = wp_filter_content_tags( $unfiltered ); … … 2974 2972 $img_no_height 2975 2973 ); 2976 $content_filtered = wp_img_tag_add_decoding_attr( $content_filtered, 'the_content' );2977 2974 2978 2975 // Do not add loading, srcset, and sizes. 2979 2976 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2980 2977 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2978 add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 2981 2979 2982 2980 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); … … 2984 2982 remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2985 2983 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2984 remove_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 2986 2985 } 2987 2986 … … 3004 3003 $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; 3005 3004 $iframe_no_width_height = '<iframe src="https://www.example.com"></iframe>'; 3005 3006 add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 3006 3007 3007 3008 $lazy_img = wp_img_tag_add_loading_optimization_attrs( $img, 'test' ); … … 3055 3056 $iframe_no_width_height 3056 3057 ); 3057 $content_filtered = wp_img_tag_add_decoding_attr( $content_filtered, 'the_content' );3058 3058 3059 3059 // Do not add width, height, srcset, and sizes. … … 3065 3065 remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 3066 3066 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3067 remove_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 3067 3068 } 3068 3069 … … 3075 3076 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 3076 3077 $lazy_img = wp_img_tag_add_loading_optimization_attrs( $img, 'test' ); 3077 $lazy_img = wp_img_tag_add_decoding_attr( $lazy_img, 'the_content' );3078 3078 $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; 3079 3079 $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' ); … … 3105 3105 public function test_wp_filter_content_tags_loading_lazy_opted_out() { 3106 3106 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 3107 $img = wp_img_tag_add_decoding_attr( $img, 'the_content' );3108 3107 $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; 3109 3108 … … 3120 3119 // Disable globally for all tags. 3121 3120 add_filter( 'wp_lazy_loading_enabled', '__return_false' ); 3121 add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 3122 3122 3123 3123 $this->assertSame( $content, wp_filter_content_tags( $content ) ); 3124 3124 remove_filter( 'wp_lazy_loading_enabled', '__return_false' ); 3125 3125 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3126 remove_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 3126 3127 } 3127 3128 … … 3187 3188 * 3188 3189 * @ticket 56969 3190 * 3191 * @expectedDeprecated wp_img_tag_add_decoding_attr 3189 3192 */ 3190 3193 public function test_wp_img_tag_add_decoding_attr_with_single_quotes() { … … 3436 3439 'false' => array( 3437 3440 'decoding' => false, 3438 'expected' => 'no value',3439 ),3440 'null' => array(3441 'decoding' => null,3442 3441 'expected' => 'no value', 3443 3442 ), … … 3720 3719 * @ticket 53675 3721 3720 * @ticket 58235 3721 * @ticket 58892 3722 3722 */ 3723 3723 public function test_wp_omit_loading_attr_threshold_filter() { … … 3739 3739 // Due to the filter, now the first five elements should not be lazy-loaded, i.e. return `false`. 3740 3740 for ( $i = 0; $i < 5; $i++ ) { 3741 $this->assertEmpty( 3741 $this->assertSameSetsWithIndex( 3742 array( 3743 'decoding' => 'async', 3744 ), 3742 3745 wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), 3743 3746 'Expected second image to not be lazy-loaded.' … … 3746 3749 3747 3750 // For following elements, lazy-load them again. 3748 $this->assertSame( 3749 array( 'loading' => 'lazy' ), 3751 $this->assertSameSetsWithIndex( 3752 array( 3753 'decoding' => 'async', 3754 'loading' => 'lazy', 3755 ), 3750 3756 wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ) 3751 3757 ); … … 3762 3768 */ 3763 3769 public function test_wp_filter_content_tags_with_loading_optimization_attrs() { 3770 add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 3764 3771 $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); 3765 3772 $iframe1 = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; … … 3778 3785 $content_unfiltered = $img1 . $iframe1 . $img2 . $img3 . $iframe2; 3779 3786 $content_expected = $prio_img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2; 3780 $content_expected = wp_img_tag_add_decoding_attr( $content_expected, 'the_content' );3781 3787 3782 3788 $query = $this->get_new_wp_query_for_published_post(); … … 3790 3796 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3791 3797 } 3798 remove_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 3792 3799 3793 3800 // After filtering, the first image should not be lazy-loaded while the other ones should be. … … 4129 4136 * @ticket 58089 4130 4137 * @ticket 58235 4138 * @ticket 58892 4131 4139 * 4132 4140 * @covers ::wp_filter_content_tags … … 4144 4152 'loading' => false, 4145 4153 'fetchpriority' => false, 4154 'decoding' => false, 4146 4155 ) 4147 4156 ); … … 4182 4191 4183 4192 // Ensure that parsed content has the image with fetchpriority as it is the first large image. 4184 $expected_content = wpautop( str_replace( '<img ', '<img fetchpriority="high" ', $expected_image ) );4193 $expected_content = wpautop( str_replace( '<img ', '<img fetchpriority="high" decoding="async" ', $expected_image ) ); 4185 4194 $this->assertSame( $expected_content, $content, 'Post content with programmatically injected image is missing loading optimization attributes' ); 4186 4195 } … … 4262 4271 * @ticket 56930 4263 4272 * @ticket 58235 4273 * @ticket 58892 4264 4274 * 4265 4275 * @covers ::wp_get_loading_optimization_attributes … … 4273 4283 4274 4284 // Return 'lazy' by default. 4275 $this->assertSame( 4276 array( 'loading' => 'lazy' ), 4285 $this->assertSameSetsWithIndex( 4286 array( 4287 'decoding' => 'async', 4288 'loading' => 'lazy', 4289 ), 4277 4290 wp_get_loading_optimization_attributes( 'img', $attr, 'test' ) 4278 4291 ); 4279 $this->assertSame( 4280 array( 'loading' => 'lazy' ), 4292 $this->assertSameSetsWithIndex( 4293 array( 4294 'decoding' => 'async', 4295 'loading' => 'lazy', 4296 ), 4281 4297 wp_get_loading_optimization_attributes( 'img', $attr, 'wp_get_attachment_image' ) 4282 4298 ); 4283 4299 4284 4300 // Return 'lazy' if not in the loop or the main query. 4285 $this->assertSame( 4286 array( 'loading' => 'lazy' ), 4301 $this->assertSameSetsWithIndex( 4302 array( 4303 'decoding' => 'async', 4304 'loading' => 'lazy', 4305 ), 4287 4306 wp_get_loading_optimization_attributes( 'img', $attr, $context ) 4288 4307 ); … … 4294 4313 4295 4314 // Return 'lazy' if in the loop but not in the main query. 4296 $this->assertSame( 4297 array( 'loading' => 'lazy' ), 4315 $this->assertSameSetsWithIndex( 4316 array( 4317 'decoding' => 'async', 4318 'loading' => 'lazy', 4319 ), 4298 4320 wp_get_loading_optimization_attributes( 'img', $attr, $context ) 4299 4321 ); … … 4303 4325 4304 4326 // First three element are not lazy loaded. However, first image is loaded with fetchpriority high. 4305 $this->assertSame( 4306 array( 'fetchpriority' => 'high' ), 4327 $this->assertSameSetsWithIndex( 4328 array( 4329 'decoding' => 'async', 4330 'fetchpriority' => 'high', 4331 ), 4307 4332 wp_get_loading_optimization_attributes( 'img', $attr, $context ), 4308 4333 "Expected first image to not be lazy-loaded. First large image get's high fetchpriority." 4309 4334 ); 4310 $this->assertEmpty( 4335 $this->assertSameSetsWithIndex( 4336 array( 4337 'decoding' => 'async', 4338 ), 4311 4339 wp_get_loading_optimization_attributes( 'img', $attr, $context ), 4312 4340 'Expected second image to not be lazy-loaded.' 4313 4341 ); 4314 $this->assertEmpty( 4342 $this->assertSameSetsWithIndex( 4343 array( 4344 'decoding' => 'async', 4345 ), 4315 4346 wp_get_loading_optimization_attributes( 'img', $attr, $context ), 4316 4347 'Expected third image to not be lazy-loaded.' … … 4318 4349 4319 4350 // Return 'lazy' if in the loop and in the main query for any subsequent elements. 4320 $this->assertSame( 4321 array( 'loading' => 'lazy' ), 4351 $this->assertSameSetsWithIndex( 4352 array( 4353 'decoding' => 'async', 4354 'loading' => 'lazy', 4355 ), 4322 4356 wp_get_loading_optimization_attributes( 'img', $attr, $context ) 4323 4357 ); 4324 4358 4325 4359 // Yes, for all subsequent elements. 4326 $this->assertSame( 4327 array( 'loading' => 'lazy' ), 4360 $this->assertSameSetsWithIndex( 4361 array( 4362 'decoding' => 'async', 4363 'loading' => 'lazy', 4364 ), 4328 4365 wp_get_loading_optimization_attributes( 'img', $attr, $context ) 4329 4366 ); … … 4345 4382 $attr = $this->get_width_height_for_high_priority(); 4346 4383 4347 $this->assertSame( 4348 array( 'loading' => 'lazy' ), 4384 $this->assertSameSetsWithIndex( 4385 array( 4386 'decoding' => 'async', 4387 'loading' => 'lazy', 4388 ), 4349 4389 wp_get_loading_optimization_attributes( 'img', $attr, $context ), 4350 4390 'The "loading" attribute should be "lazy" when not in the loop or the main query.' … … 4359 4399 the_post(); 4360 4400 4361 $this->assertSame( 4362 array( 'fetchpriority' => 'high' ), 4401 $this->assertSameSetsWithIndex( 4402 array( 4403 'decoding' => 'async', 4404 'fetchpriority' => 'high', 4405 ), 4363 4406 wp_get_loading_optimization_attributes( 'img', $attr, $context ), 4364 4407 'The "fetchpriority" attribute should be "high" while in the loop and the main query.' … … 4389 4432 $this->set_main_query( $query ); 4390 4433 4391 $this->assertSame( 4392 array( 'loading' => 'lazy' ), 4434 $this->assertSameSetsWithIndex( 4435 array( 4436 'decoding' => 'async', 4437 'loading' => 'lazy', 4438 ), 4393 4439 wp_get_loading_optimization_attributes( 'img', $attr, $context ), 4394 4440 'The "loading" attribute should be "lazy" before the main query loop.' … … 4398 4444 the_post(); 4399 4445 4400 $this->assertSame( 4401 array( 'fetchpriority' => 'high' ), 4446 $this->assertSameSetsWithIndex( 4447 array( 4448 'decoding' => 'async', 4449 'fetchpriority' => 'high', 4450 ), 4402 4451 wp_get_loading_optimization_attributes( 'img', $attr, $context ), 4403 4452 'The "fetchpriority" attribute should be "high" while in the loop and the main query.' … … 4468 4517 add_filter( 'wp_loading_optimization_force_header_contexts', '__return_empty_array' ); 4469 4518 4470 $this->assertSame( 4471 array( 'loading' => 'lazy' ), 4519 $this->assertSameSetsWithIndex( 4520 array( 4521 'decoding' => 'async', 4522 'loading' => 'lazy', 4523 ), 4472 4524 wp_get_loading_optimization_attributes( 'img', $attr, $context ), 4473 4525 'Images in the header context should get lazy-loaded after the wp_loading_optimization_force_header_contexts filter.' … … 4503 4555 ); 4504 4556 4505 $this->assertSame( 4506 array( 'fetchpriority' => 'high' ), 4557 $this->assertSameSetsWithIndex( 4558 array( 4559 'decoding' => 'async', 4560 'fetchpriority' => 'high', 4561 ), 4507 4562 wp_get_loading_optimization_attributes( 'img', $attr, 'something_completely_arbitrary' ) 4508 4563 ); … … 4514 4569 * @ticket 58211 4515 4570 * @ticket 58235 4571 * @ticket 58892 4516 4572 * 4517 4573 * @covers ::wp_get_loading_optimization_attributes … … 4531 4587 4532 4588 // Lazy if not main query. 4533 $this->assertSame( 4534 array( 'loading' => 'lazy' ), 4589 $this->assertSameSetsWithIndex( 4590 array( 4591 'decoding' => 'async', 4592 'loading' => 'lazy', 4593 ), 4535 4594 wp_get_loading_optimization_attributes( 'img', $attr, $context ) 4536 4595 ); … … 4542 4601 * @ticket 58211 4543 4602 * @ticket 58235 4603 * @ticket 58892 4544 4604 * 4545 4605 * @covers ::wp_get_loading_optimization_attributes … … 4558 4618 4559 4619 // Lazy if header not called. 4560 $this->assertSame( 4561 array( 'loading' => 'lazy' ), 4620 $this->assertSameSetsWithIndex( 4621 array( 4622 'decoding' => 'async', 4623 'loading' => 'lazy', 4624 ), 4562 4625 wp_get_loading_optimization_attributes( 'img', $attr, $context ) 4563 4626 ); … … 4586 4649 4587 4650 // First image is loaded with high fetchpriority. 4588 $this->assertSame( 4589 array( 'fetchpriority' => 'high' ), 4651 $this->assertSameSetsWithIndex( 4652 array( 4653 'decoding' => 'async', 4654 'fetchpriority' => 'high', 4655 ), 4590 4656 wp_get_loading_optimization_attributes( 'img', $attr, $context ), 4591 4657 'Expected first image to not be lazy-loaded. First large image is loaded with high fetchpriority.' … … 4598 4664 * @ticket 58211 4599 4665 * @ticket 58235 4666 * @ticket 58892 4600 4667 * 4601 4668 * @covers ::wp_get_loading_optimization_attributes … … 4618 4685 4619 4686 $attr = $this->get_width_height_for_high_priority(); 4620 $this->assertSame( 4621 array( 'loading' => 'lazy' ), 4687 $this->assertSameSetsWithIndex( 4688 array( 4689 'decoding' => 'async', 4690 'loading' => 'lazy', 4691 ), 4622 4692 wp_get_loading_optimization_attributes( 'img', $attr, $context ) 4623 4693 ); … … 4629 4699 * @ticket 58211 4630 4700 * @ticket 58235 4701 * @ticket 58892 4631 4702 * 4632 4703 * @covers ::wp_get_loading_optimization_attributes … … 4649 4720 4650 4721 // Load lazy if the there is no loop and footer was called. 4651 $this->assertSame( 4652 array( 'loading' => 'lazy' ), 4722 $this->assertSameSetsWithIndex( 4723 array( 4724 'decoding' => 'async', 4725 'loading' => 'lazy', 4726 ), 4653 4727 wp_get_loading_optimization_attributes( 'img', $attr, $context ) 4654 4728 ); … … 4660 4734 * @ticket 58089 4661 4735 * @ticket 58235 4736 * @ticket 58892 4662 4737 * 4663 4738 * @covers ::wp_get_loading_optimization_attributes … … 4669 4744 public function test_wp_get_loading_optimization_attributes_should_return_lazy_for_special_contexts_outside_of_the_content( $context ) { 4670 4745 $attr = $this->get_width_height_for_high_priority(); 4671 $this->assertSame( 4672 array( 'loading' => 'lazy' ), 4746 $this->assertSameSetsWithIndex( 4747 array( 4748 'decoding' => 'async', 4749 'loading' => 'lazy', 4750 ), 4673 4751 wp_get_loading_optimization_attributes( 'img', $attr, $context ) 4674 4752 ); … … 4705 4783 4706 4784 /** 4785 * Tests to cover the decoding attribute within wp_get_loading_optimization_attributes(). 4786 * 4787 * @ticket 58892 4788 * 4789 * @covers ::wp_get_loading_optimization_attributes 4790 */ 4791 public function test_wp_get_loading_optimization_attributes_decoding_attribute() { 4792 4793 $this->assertSameSetsWithIndex( 4794 array( 4795 'decoding' => 'async', 4796 ), 4797 wp_get_loading_optimization_attributes( 'img', array(), 'the_content' ), 4798 'Expected decoding attribute to be async.' 4799 ); 4800 4801 $this->assertSameSetsWithIndex( 4802 array( 4803 'decoding' => 'auto', 4804 ), 4805 wp_get_loading_optimization_attributes( 'img', array( 'decoding' => 'auto' ), 'the_content' ), 4806 'Expected decoding attribute to be auto.' 4807 ); 4808 4809 $result = null; 4810 add_filter( 4811 'the_content', 4812 static function ( $content ) use ( &$result ) { 4813 $result = wp_get_loading_optimization_attributes( 'img', array(), 'something_completely_arbitrary' ); 4814 return $content; 4815 } 4816 ); 4817 apply_filters( 'the_content', '' ); 4818 4819 $this->assertSameSetsWithIndex( 4820 array(), 4821 $result, 4822 'Expected decoding attribute to be empty for img on arbitrary context, while running the_content.' 4823 ); 4824 4825 $this->assertSameSetsWithIndex( 4826 array(), 4827 wp_get_loading_optimization_attributes( 'iframe', array(), 'the_content' ), 4828 'Expected decoding attribute to be empty for iframe.' 4829 ); 4830 } 4831 4832 /** 4707 4833 * @ticket 44427 4708 4834 * @ticket 50367 … … 4810 4936 array( 4811 4937 'loading' => false, 4938 'decoding' => 'async', 4812 4939 'fetchpriority' => 'high', 4813 4940 ) … … 5016 5143 'height' => 100, 5017 5144 ), 5018 array( 'loading' => 'lazy' ), 5019 'Expected default `loading="lazy"`.', 5145 array( 5146 'decoding' => 'async', 5147 'loading' => 'lazy', 5148 ), 5149 'Expected default `decoding="async"` and `loading="lazy"`.', 5020 5150 ), 5021 5151 'img_without_height' => array( 5022 5152 'img', 5023 5153 array( 'width' => 100 ), 5024 array(), 5025 'Expected blank array as height is required.', 5154 array( 5155 'decoding' => 'async', 5156 ), 5157 'Only `decoding` is set as height is required for `loading` attribute.', 5026 5158 ), 5027 5159 'img_without_width' => array( 5028 5160 'img', 5029 5161 array( 'height' => 100 ), 5030 array(), 5031 'Expected blank array as width is required.', 5162 array( 5163 'decoding' => 'async', 5164 ), 5165 'Only `decoding` is set as width is required for `loading` attribute.', 5032 5166 ), 5033 5167 ); … … 5062 5196 'img' => array( 5063 5197 'img', 5064 array( 'loading' => 'lazy' ), 5065 'Expected `loading="lazy"` for the img.', 5198 array( 5199 'decoding' => 'async', 5200 'loading' => 'lazy', 5201 ), 5202 'Expected `decoding="async"` and `loading="lazy"` and `decoding="async"` for the img.', 5066 5203 ), 5067 5204 'iframe' => array( … … 5106 5243 5107 5244 // Skip logic if context is `template`. 5108 $this->assertSame( 5109 array( 'fetchpriority' => 'high' ), 5245 $this->assertSameSetsWithIndex( 5246 array( 5247 'decoding' => 'async', 5248 'fetchpriority' => 'high', 5249 ), 5110 5250 wp_get_loading_optimization_attributes( 'img', $attr, 'template_part_' . WP_TEMPLATE_PART_AREA_HEADER ), 5111 5251 'Images in the header block template part should not be lazy-loaded and first large image is set high fetchpriority.' … … 5115 5255 /** 5116 5256 * @ticket 58235 5257 * @ticket 58892 5117 5258 * 5118 5259 * @covers ::wp_get_loading_optimization_attributes … … 5126 5267 $this->assertEqualSetsWithIndex( 5127 5268 array( 5269 'decoding' => 'async', 5128 5270 'loading' => 'lazy', 5129 5271 'fetchpriority' => 'high', … … 5144 5286 5145 5287 // Check fetchpriority high logic if loading attribute is present. 5146 $this->assertSame( 5147 array( 5288 $this->assertSameSetsWithIndex( 5289 array( 5290 'decoding' => 'async', 5148 5291 'fetchpriority' => 'high', 5149 5292 ), … … 5167 5310 // fetchpriority not set as image is of lower resolution. 5168 5311 $this->assertSame( 5169 array(), 5312 array( 5313 'decoding' => 'async', 5314 ), 5170 5315 wp_get_loading_optimization_attributes( 'img', $attr, 'test' ), 5171 5316 'loading optimization attr array should be empty.' … … 5183 5328 5184 5329 // The first image processed in a shortcode should have fetchpriority set to high. 5185 $this->assertSame (5330 $this->assertSameSetsWithIndex( 5186 5331 $expected, 5187 5332 wp_get_loading_optimization_attributes( 'img', $attr, 'do_shortcode' ), … … 5201 5346 }, 5202 5347 'expected' => array( 5348 'decoding' => 'async', 5203 5349 'fetchpriority' => 'high', 5204 5350 ), … … 5218 5364 }, 5219 5365 'expected' => array( 5220 'loading' => 'lazy', 5366 'decoding' => 'async', 5367 'loading' => 'lazy', 5221 5368 ), 5222 'message' => 'Lazy-loading not applied to during shortcode rendering.',5369 'message' => 'Lazy-loading or decoding not applied to during shortcode rendering.', 5223 5370 ), 5224 5371 'shortcode_image_outside_of_the_loop_are_loaded_lazy' => array( … … 5228 5375 }, 5229 5376 'expected' => array( 5230 'loading' => 'lazy', 5377 'decoding' => 'async', 5378 'loading' => 'lazy', 5231 5379 ), 5232 'message' => 'Lazy-loading not applied to shortcodes outside the loop.',5380 'message' => 'Lazy-loading or decoding not applied to shortcodes outside the loop.', 5233 5381 ), 5234 5382 ); … … 5327 5475 ), 5328 5476 'image with loading=lazy' => array( 5329 array( 'loading' => 'lazy' ), 5477 array( 5478 'loading' => 'lazy', 5479 'decoding' => 'async', 5480 ), 5330 5481 'img', 5331 5482 $this->get_width_height_for_high_priority(), … … 5464 5615 $attr = $this->get_width_height_for_high_priority(); 5465 5616 5466 $this->assertSame (5617 $this->assertSameSetsWithIndex( 5467 5618 array( 'fetchpriority' => 'high' ), 5468 5619 wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), … … 5473 5624 add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_false' ); 5474 5625 5475 $this->assertSameSets( 5476 array( 'loading' => 'lazy' ), 5626 $this->assertSameSetsWithIndex( 5627 array( 5628 'decoding' => 'async', 5629 'loading' => 'lazy', 5630 ), 5477 5631 wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), 5478 5632 'The filter did not return the default attributes.' … … 5482 5636 add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_empty_array' ); 5483 5637 5484 $this->assertSameSets (5638 $this->assertSameSetsWithIndex( 5485 5639 array(), 5486 5640 wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), … … 5504 5658 ); 5505 5659 5506 $this->assertSameSets (5660 $this->assertSameSetsWithIndex( 5507 5661 array( 'custom_attr' => 'custom_value' ), 5508 5662 wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), … … 5519 5673 $attr = $this->get_width_height_for_high_priority(); 5520 5674 5521 $this->assertSameSets( 5522 array( 'loading' => 'lazy' ), 5675 $this->assertSameSetsWithIndex( 5676 array( 5677 'decoding' => 'async', 5678 'loading' => 'lazy', 5679 ), 5523 5680 wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), 5524 5681 'Before the filter it will not return the loading attribute.' … … 5537 5694 ); 5538 5695 5539 $this->assertSameSets( 5540 array( 'fetchpriority' => 'high' ), 5696 $this->assertSameSetsWithIndex( 5697 array( 5698 'decoding' => 'async', 5699 'fetchpriority' => 'high', 5700 ), 5541 5701 wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), 5542 5702 'After the filter it will not return the fetchpriority attribute.' -
trunk/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php
r56559 r56690 20 20 * @param string $decoding The value for the 'decoding' attribute. 'no value' for default. 21 21 * @param string $expected The expected `img` tag. 22 * 23 * @expectedDeprecated wp_img_tag_add_decoding_attr 22 24 */ 23 25 public function test_should_add_decoding_attr( $image, $context, $decoding, $expected ) { … … 81 83 * @param mixed $decoding The value for the 'decoding' attribute. 'no value' for default. 82 84 * @param string $expected The expected `img` tag. 85 * 86 * @expectedDeprecated wp_img_tag_add_decoding_attr 83 87 */ 84 88 public function test_should_not_add_decoding_attr( $image, $context, $decoding, $expected ) {
Note: See TracChangeset
for help on using the changeset viewer.