Make WordPress Core

Ticket #58773: 58773.2.diff

File 58773.2.diff, 3.2 KB (added by adamsilverstein, 17 months ago)
  • src/wp-includes/default-filters.php

    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 ); 
    668668add_action( 'wp_head', 'wp_oembed_add_discovery_links' );
    669669add_action( 'wp_head', 'wp_oembed_add_host_js' ); // Back-compat for sites disabling oEmbed host JS by removing action.
    670670add_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' );
     671add_filter( 'embed_oembed_html', 'wp_lazy_load_oembed_iframe' );
    671672
    672673add_action( 'embed_head', 'enqueue_embed_scripts', 1 );
    673674add_action( 'embed_head', 'print_emoji_detection_script' );
  • src/wp-includes/embed.php

    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 ) { 
    12441244
    12451245        return $result;
    12461246}
     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 */
     1257function 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}
  • tests/phpunit/tests/oembed/WpEmbed.php

    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 { 
    99         */
    1010        protected $wp_embed;
    1111
     12        const YOUTUBE_VIDEO_ID = 'OQSNhk5ICTI';
     13
    1214        public function set_up() {
    1315                parent::set_up();
    1416                $this->wp_embed = new WP_Embed();
    class Tests_WP_Embed extends WP_UnitTestCase { 
    377379                $this->wp_embed->linkifunknown = false;
    378380                $this->assertSame( $url, $this->wp_embed->maybe_make_link( $url ) );
    379381        }
     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
    380394}
  • tests/phpunit/tests/oembed/wpOembed.php

    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 { 
    99         */
    1010        protected $oembed;
    1111
     12        const YOUTUBE_VIDEO_ID = 'OQSNhk5ICTI';
     13
    1214        public $pre_oembed_result_filtered = false;
    1315
    1416        public function set_up() {
    class Tests_WP_oEmbed extends WP_UnitTestCase { 
    237239                $this->assertFalse( $actual );
    238240                $this->assertSame( $current_blog_id, get_current_blog_id() );
    239241        }
     242
    240243}