Make WordPress Core

Ticket #58773: 58773.diff

File 58773.diff, 1.8 KB (added by adamsilverstein, 22 months ago)
  • src/wp-includes/class-wp-oembed.php

    diff --git a/src/wp-includes/class-wp-oembed.php b/src/wp-includes/class-wp-oembed.php
    index 91f9cd68be..55bfccb310 100644
    a b class WP_oEmbed { 
    414414                        return false;
    415415                }
    416416
     417                // Lazy load oEmbed iframes.
     418                if ( wp_lazy_loading_enabled( 'iframe', 'oembed' ) && isset( $data->html ) && str_starts_with( $data->html, '<iframe' ) && ! str_contains( $data->html, 'loading' ) ) {
     419                        $data->html = wp_iframe_tag_add_loading_attr( $data->html, 'oembed' );
     420                }
     421
    417422                /**
    418423                 * Filters the HTML returned by the oEmbed provider.
    419424                 *
  • tests/phpunit/tests/oembed/controller.php

    diff --git a/tests/phpunit/tests/oembed/controller.php b/tests/phpunit/tests/oembed/controller.php
    index 238b1faf1f..a067d118d6 100644
    a b class Test_oEmbed_Controller extends WP_UnitTestCase { 
    813813
    814814                $this->assertStringStartsWith( '<b>Unfiltered</b>', $data->html );
    815815        }
     816
     817        /**
     818         * Test that oembed iframes are lazy loaded by default.
     819         */
     820        public function test_oembed_iframes_are_lazy_loaded_by_default() {
     821                wp_set_current_user( self::$editor );
     822                $post = self::factory()->post->create_and_get(
     823                        array(
     824                                'post_author'  => self::$editor,
     825                                'post_title'   => 'Hello World',
     826                                'post_content' => 'https://www.youtube.com/watch?v=' . self::YOUTUBE_VIDEO_ID,
     827                        )
     828                );
     829
     830                // Get the post content via the REST API.
     831                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post->ID );
     832                $request->set_param( '_embed', true );
     833                $response = rest_get_server()->dispatch( $request );
     834                $data     = $response->get_data();
     835
     836                $this->assertIsArray( $data );
     837                $this->assertStringContainsString( 'loading="lazy"', $data['content']['rendered'] );
     838        }
    816839}