Skip to content

Commit a8a343d

Browse files
committed
Script Loader: Ignore image preloads that have no URL.
An image resource passed through the `wp_preload_resources` filter without an `href` was accepted when it had either `imagesrcset` or `imagesizes`, but `imagesrcset` was always used as the key for removing duplicates. A resource with only `imagesizes` therefore raised an undefined array key warning (plus a null array offset deprecation on PHP 8.5), collapsed every such resource onto the same empty key, and printed a `<link>` with no URL at all, since `imagesizes` is only output alongside `imagesrcset`. A resource with neither `href` nor `as` raised a warning as well. Such a resource is now skipped unless `as` is `image` and `imagesrcset` is set. PHPStan reports the missing offsets once the filter's value is typed from its documented hash notation. Developed in #15380. Follow-up to r53846. Props westonruter, adamsilverstein. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@65215 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fe84ed8 commit a8a343d

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎src/wp-includes/general-template.php‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4107,9 +4107,7 @@ function wp_preload_resources() {
41074107
}
41084108
$unique_resources[ $href ] = $attributes;
41094109
// Media can use imagesrcset and not href.
4110-
} elseif ( ( 'image' === $resource['as'] ) &&
4111-
( isset( $resource['imagesrcset'] ) || isset( $resource['imagesizes'] ) )
4112-
) {
4110+
} elseif ( isset( $resource['as'], $resource['imagesrcset'] ) && 'image' === $resource['as'] ) {
41134111
if ( isset( $unique_resources[ $resource['imagesrcset'] ] ) ) {
41144112
continue;
41154113
}

‎tests/phpunit/tests/general/wpPreloadResources.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,33 @@ public static function data_preload_resources() {
257257
),
258258
),
259259
),
260+
'imagesizes_only' => array(
261+
'expected' => "<link rel='preload' as='image' imagesrcset='640.png 640w, 800.png 800w' imagesizes='100vw' />\n",
262+
'resources' => array(
263+
// No href and no imagesrcset, so there is nothing to preload.
264+
array(
265+
'as' => 'image',
266+
'imagesizes' => '100vw',
267+
),
268+
array(
269+
'as' => 'image',
270+
'imagesizes' => '50vw',
271+
),
272+
array(
273+
'as' => 'image',
274+
'imagesrcset' => '640.png 640w, 800.png 800w',
275+
'imagesizes' => '100vw',
276+
),
277+
),
278+
),
279+
'missing_href_and_as' => array(
280+
'expected' => '',
281+
'resources' => array(
282+
array(
283+
'imagesrcset' => '640.png 640w, 800.png 800w',
284+
),
285+
),
286+
),
260287
);
261288
}
262289
}

0 commit comments

Comments
 (0)