Skip to content

Commit 86909e1

Browse files
committed
Avoid treating fetchpriority=auto as maybe_in_viewport and prevent incrementing media count for them
1 parent a1bf2a3 commit 86909e1

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

src/wp-includes/media.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6115,7 +6115,6 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) {
61156115
* on an IMG following any number of initial IMGs with `fetchpriority=auto` since those initial images may not
61166116
* be displayed.
61176117
*/
6118-
$maybe_in_viewport = true;
61196118

61206119
// Preserve fetchpriority=auto.
61216120
$loading_attrs['fetchpriority'] = 'auto';
@@ -6195,16 +6194,20 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) {
61956194
/*
61966195
* If flag was set based on contextual logic above, increase the content
61976196
* media count, either unconditionally, or based on whether the image size
6198-
* is larger than the threshold.
6197+
* is larger than the threshold. This does not apply when the IMG has
6198+
* fetchpriority=auto because it may be conditionally displayed by viewport
6199+
* size.
61996200
*/
6200-
if ( $increase_count ) {
6201-
wp_increase_content_media_count();
6202-
} elseif ( $maybe_increase_count ) {
6203-
/** This filter is documented in wp-includes/media.php */
6204-
$wp_min_priority_img_pixels = apply_filters( 'wp_min_priority_img_pixels', 50000 );
6205-
6206-
if ( $wp_min_priority_img_pixels <= $attr['width'] * $attr['height'] ) {
6201+
if ( 'auto' !== $existing_fetchpriority ) {
6202+
if ( $increase_count ) {
62076203
wp_increase_content_media_count();
6204+
} elseif ( $maybe_increase_count ) {
6205+
/** This filter is documented in wp-includes/media.php */
6206+
$wp_min_priority_img_pixels = apply_filters( 'wp_min_priority_img_pixels', 50000 );
6207+
6208+
if ( $wp_min_priority_img_pixels <= $attr['width'] * $attr['height'] ) {
6209+
wp_increase_content_media_count();
6210+
}
62086211
}
62096212
}
62106213

tests/phpunit/tests/media.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4726,6 +4726,20 @@ public function test_wp_get_loading_optimization_attributes_with_fetchpriority_a
47264726
);
47274727

47284728
$this->assert_fetchpriority_low_loading_attrs( $attr, $context );
4729+
4730+
$this->assertSameSetsWithIndex(
4731+
array(
4732+
'decoding' => 'async',
4733+
'fetchpriority' => 'auto',
4734+
'loading' => 'lazy',
4735+
),
4736+
wp_get_loading_optimization_attributes(
4737+
'img',
4738+
array_merge( $attr, array( 'fetchpriority' => 'auto' ) ),
4739+
$context
4740+
),
4741+
'Expected a fetchpriority=auto IMG appearing after the media count threshold to till be lazy-loaded.'
4742+
);
47294743
}
47304744
}
47314745

0 commit comments

Comments
 (0)