Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback value for src attribute if not set #6700

Merged
merged 7 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading