Skip to content

Commit

Permalink
Fallback value for src attribute if not set (#6700)
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuLamiot authored Jun 6, 2024
1 parent 6fc1a47 commit 78395ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
23 changes: 7 additions & 16 deletions inc/Engine/Media/AboveTheFold/AJAX/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,6 @@ private function create_object( $image, $keys ) {
$object = new \stdClass();
$object->type = $image->type ?? 'img';

if ( is_array( $image->src ) ) {
$sanitized_object_array = array_map(
function ( $item ) {
if ( ! empty( $item->src ) ) {
$item->src = $this->sanitize_image_url( $item->src );
}
return $item;
},
$image->src
);

$object->src = $sanitized_object_array;
} elseif ( ! empty( $image->src ) ) {
$object->src = $this->sanitize_image_url( $image->src );
}

switch ( $object->type ) {
case 'img-srcset':
// If the type is 'img-srcset', add all the required parameters to the object.
Expand Down Expand Up @@ -209,6 +193,13 @@ function ( $item ) {
return null;
}

// Returned objects must always have a src for front-end optimization.
// Except bg-img and bg-img-set for which we use bg_set only.
// To keep it simple and safe for now, we enforce src for all, pending a refactor.
if ( ! isset( $object->src ) ) {
$object->src = '';
}

return $object;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,15 @@
'lcp' => json_encode(
(object) [
'type' => 'bg-img',
'src' => 'http://example.org/lcp.jpg',
'bg_set' => [
[
'src' => 'http://example.org/anotherlcp.jpg'
],
[
'src' => 'http://example.org/anotherlcp2.jpg'
],
]
],
'src' => ''
],
),
'viewport' => json_encode( [
Expand All @@ -352,15 +352,15 @@
'lcp' => json_encode(
(object) [
'type' => 'bg-img',
'src' => 'http://example.org/lcp.jpg',
'bg_set' => [
[
'src' => 'http://example.org/anotherlcp.jpg'
'src' => 'http://example.org/anotherlcp.jpg'
],
[
'src' => 'http://example.org/anotherlcp2.jpg'
],
]
],
'src' => ''
],
),
'viewport' => json_encode( [
Expand Down Expand Up @@ -418,22 +418,15 @@
'lcp' => json_encode(
(object) [
'type' => 'bg-img-set',
'src' => [
[
'src' => 'http://example.org/lcp.jpg'
],
[
'src' => 'http://example.org/random.jpg'
]
],
'bg_set' => [
[
'src' => 'http://example.org/anotherlcp.jpg'
],
[
'src' => 'http://example.org/anotherlcp2.jpg'
],
]
],
'src' => ''
],
),
'viewport' => json_encode( [
Expand All @@ -453,22 +446,15 @@
'lcp' => json_encode(
(object) [
'type' => 'bg-img-set',
'src' => [
[
'src' => 'http://example.org/lcp.jpg'
],
[
'src' => 'http://example.org/random.jpg'
]
],
'bg_set' => [
[
'src' => 'http://example.org/anotherlcp.jpg'
],
[
'src' => 'http://example.org/anotherlcp2.jpg'
],
]
],
'src' => ''
],
),
'viewport' => json_encode( [
Expand Down

0 comments on commit 78395ea

Please sign in to comment.