diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
index bf39f29b4d..5037469a41 100644
a
|
b
|
add_filter( 'rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4 ); |
668 | 668 | add_action( 'wp_head', 'wp_oembed_add_discovery_links' ); |
669 | 669 | add_action( 'wp_head', 'wp_oembed_add_host_js' ); // Back-compat for sites disabling oEmbed host JS by removing action. |
670 | 670 | add_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' ); |
| 671 | add_filter( 'embed_oembed_html', 'wp_lazy_load_oembed_iframe' ); |
671 | 672 | |
672 | 673 | add_action( 'embed_head', 'enqueue_embed_scripts', 1 ); |
673 | 674 | add_action( 'embed_head', 'print_emoji_detection_script' ); |
diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php
index d75939cce6..2e2ad2f8a8 100644
a
|
b
|
function wp_filter_pre_oembed_result( $result, $url, $args ) { |
1244 | 1244 | |
1245 | 1245 | return $result; |
1246 | 1246 | } |
| 1247 | |
| 1248 | /** |
| 1249 | * Apply the loading="lazy" attribute to the oEmbed iframe. |
| 1250 | * |
| 1251 | * @since 6.4.0 |
| 1252 | * |
| 1253 | * @param string $html The oEmbed HTML, usually containing an iframe. |
| 1254 | * |
| 1255 | * @return string The oEmbed HTML with the loading="lazy" attribute added to the iframe. |
| 1256 | */ |
| 1257 | function wp_lazy_load_oembed_iframe( $html ) { |
| 1258 | $tag_parser = new WP_HTML_Tag_Processor( $html ); |
| 1259 | if ( wp_lazy_loading_enabled( 'iframe', 'oembed' ) && isset( $html ) && $tag_parser->next_tag( 'iframe' ) && ! $tag_parser->get_attribute( 'loading' ) ) { |
| 1260 | $html = wp_iframe_tag_add_loading_attr( $html, 'oembed' ); |
| 1261 | } |
| 1262 | |
| 1263 | return $html; |
| 1264 | } |
diff --git a/tests/phpunit/tests/oembed/WpEmbed.php b/tests/phpunit/tests/oembed/WpEmbed.php
index eb64f084e7..e6ed1e3b04 100644
a
|
b
|
class Tests_WP_Embed extends WP_UnitTestCase { |
9 | 9 | */ |
10 | 10 | protected $wp_embed; |
11 | 11 | |
| 12 | const YOUTUBE_VIDEO_ID = 'OQSNhk5ICTI'; |
| 13 | |
12 | 14 | public function set_up() { |
13 | 15 | parent::set_up(); |
14 | 16 | $this->wp_embed = new WP_Embed(); |
… |
… |
class Tests_WP_Embed extends WP_UnitTestCase { |
377 | 379 | $this->wp_embed->linkifunknown = false; |
378 | 380 | $this->assertSame( $url, $this->wp_embed->maybe_make_link( $url ) ); |
379 | 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Test that oembed iframes are lazy loaded by default. |
| 385 | * |
| 386 | * @ticket 58773 |
| 387 | */ |
| 388 | public function test_oembed_iframes_are_lazy_loaded_by_default() { |
| 389 | $url = 'https://www.youtube.com/watch?v=' . self::YOUTUBE_VIDEO_ID; |
| 390 | $actual = $this->wp_embed->run_shortcode( '[embed]' . $url . '[/embed]' ); |
| 391 | $this->assertContains( 'loading="lazy"', $actual ); |
| 392 | } |
| 393 | |
380 | 394 | } |
diff --git a/tests/phpunit/tests/oembed/wpOembed.php b/tests/phpunit/tests/oembed/wpOembed.php
index 051a7efe3d..8f85e6da8c 100644
a
|
b
|
class Tests_WP_oEmbed extends WP_UnitTestCase { |
9 | 9 | */ |
10 | 10 | protected $oembed; |
11 | 11 | |
| 12 | const YOUTUBE_VIDEO_ID = 'OQSNhk5ICTI'; |
| 13 | |
12 | 14 | public $pre_oembed_result_filtered = false; |
13 | 15 | |
14 | 16 | public function set_up() { |
… |
… |
class Tests_WP_oEmbed extends WP_UnitTestCase { |
237 | 239 | $this->assertFalse( $actual ); |
238 | 240 | $this->assertSame( $current_blog_id, get_current_blog_id() ); |
239 | 241 | } |
| 242 | |
240 | 243 | } |