Skip to content

Commit 17601a5

Browse files
authored
Recipes: take into account new Lazy attribute that may be added (#15366)
See https://core.trac.wordpress.org/changeset/47554
1 parent fda6ffb commit 17601a5

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

modules/shortcodes/recipe.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,16 +618,25 @@ private static function output_image_html( $src ) {
618618
return '';
619619
}
620620

621+
$image_attrs = array(
622+
'class' => 'jetpack-recipe-image u-photo photo',
623+
'itemprop' => 'image',
624+
);
625+
626+
if (
627+
function_exists( 'wp_lazy_loading_enabled' )
628+
&& wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' )
629+
) {
630+
$image_attrs['loading'] = 'lazy';
631+
}
632+
621633
// If it's numeric, this may be an attachment.
622634
if ( is_numeric( $src ) ) {
623635
return wp_get_attachment_image(
624636
$src,
625637
'full',
626638
false,
627-
array(
628-
'class' => 'jetpack-recipe-image u-photo photo',
629-
'itemprop' => 'image',
630-
)
639+
$image_attrs
631640
);
632641
}
633642

@@ -639,8 +648,18 @@ private static function output_image_html( $src ) {
639648
return '';
640649
}
641650

651+
$image_attrs_markup = '';
652+
foreach ( $image_attrs as $name => $value ) {
653+
$image_attrs_markup .= sprintf(
654+
' %1$s="%2$s"',
655+
esc_attr( $name ),
656+
esc_attr( $value )
657+
);
658+
}
659+
642660
return sprintf(
643-
'<img class="jetpack-recipe-image u-photo photo" itemprop="image" src="%1$s" />',
661+
'<img%1$s src="%2$s" />',
662+
$image_attrs_markup,
644663
esc_url( $src )
645664
);
646665
}

tests/php/modules/shortcodes/test-class.recipe.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,16 @@ public function test_shortcodes_recipe_image_shortcode_src() {
185185
$content = '[recipe-image https://example.com]';
186186

187187
$shortcode_content = do_shortcode( $content );
188-
$this->assertContains( '<img class="jetpack-recipe-image u-photo photo" itemprop="image" src="https://example.com" />', $shortcode_content );
188+
189+
// We expect a different image markup in WP 5.5 when Lazy Load is enabled.
190+
if (
191+
function_exists( 'wp_lazy_loading_enabled' )
192+
&& wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' )
193+
) {
194+
$this->assertContains( '<img class="jetpack-recipe-image u-photo photo" itemprop="image" loading="lazy" src="https://example.com" />', $shortcode_content );
195+
} else {
196+
$this->assertContains( '<img class="jetpack-recipe-image u-photo photo" itemprop="image" src="https://example.com" />', $shortcode_content );
197+
}
189198
}
190199

191200
/**
@@ -198,7 +207,16 @@ public function test_shortcodes_recipe_image_shortcode_src_attr() {
198207
$content = '[recipe-image image="https://example.com"]';
199208

200209
$shortcode_content = do_shortcode( $content );
201-
$this->assertContains( '<img class="jetpack-recipe-image u-photo photo" itemprop="image" src="https://example.com" />', $shortcode_content );
210+
211+
// We expect a different image markup in WP 5.5 when Lazy Load is enabled.
212+
if (
213+
function_exists( 'wp_lazy_loading_enabled' )
214+
&& wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' )
215+
) {
216+
$this->assertContains( '<img class="jetpack-recipe-image u-photo photo" itemprop="image" loading="lazy" src="https://example.com" />', $shortcode_content );
217+
} else {
218+
$this->assertContains( '<img class="jetpack-recipe-image u-photo photo" itemprop="image" src="https://example.com" />', $shortcode_content );
219+
}
202220
}
203221

204222
/**
@@ -245,7 +263,7 @@ public function test_shortcodes_recipe_image_shortcode_attachment() {
245263
$content = '[recipe-image ' . $attachment_id . ']';
246264

247265
$shortcode_content = do_shortcode( $content );
248-
$this->assertContains( '<img src="http://example.org/wp-content/uploads/example.jpg" class="jetpack-recipe-image u-photo photo" alt="" itemprop="image" />', $shortcode_content );
266+
$this->assertContains( '<img src="http://example.org/wp-content/uploads/example.jpg" class="jetpack-recipe-image u-photo photo"', $shortcode_content );
249267
}
250268

251269
/**
@@ -268,7 +286,7 @@ public function test_shortcodes_recipe_image_shortcode_attachment_attr() {
268286
$content = '[recipe-image image="' . $attachment_id . '"]';
269287

270288
$shortcode_content = do_shortcode( $content );
271-
$this->assertContains( '<img src="http://example.org/wp-content/uploads/example.jpg" class="jetpack-recipe-image u-photo photo" alt="" itemprop="image" />', $shortcode_content );
289+
$this->assertContains( '<img src="http://example.org/wp-content/uploads/example.jpg" class="jetpack-recipe-image u-photo photo"', $shortcode_content );
272290
}
273291

274292
/**

0 commit comments

Comments
 (0)