Make WordPress Core


Ignore:
Timestamp:
01/04/2022 03:22:40 PM (7 months ago)
Author:
audrasjb
Message:

Embeds: Fix oEmbed host script enqueueing on block-based themes.

This change fixes oEmbed host script enqueueing on front-end when using block themes.

It deprecates wp_oembed_add_host_js in favor of wp_maybe_enqueue_oembed_host_js. The action is still triggered in default-filters.php to ensure backward compatibility for websites that are removing the action. There is now a has_action() check in wp_maybe_enqueue_oembed_host_js() to see if wp_oembed_add_host_js() has not been unhooked from running at the wp_head action.

Follow-up to [52132], [52151], [52153], [52325].

Props swissspidy, westonruter, flixos90, kafleg.
Fixes #44632.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/oembed/template.php

    r52153 r52437  
    310310        remove_all_filters( 'embed_oembed_html' );
    311311
     312        // This function is now a no-op.
    312313        wp_oembed_add_host_js();
    313314
    314         $this->assertEquals( 10, has_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' ) );
     315        $this->assertFalse( has_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' ) );
    315316    }
    316317
    317318    /** @covers ::wp_maybe_enqueue_oembed_host_js() */
    318     function test_wp_maybe_enqueue_oembed_host_js() {
     319    public function test_wp_maybe_enqueue_oembed_host_js() {
    319320        $scripts = wp_scripts();
    320321
     
    329330        wp_maybe_enqueue_oembed_host_js( $post_embed );
    330331        $this->assertTrue( $scripts->query( 'wp-embed', 'enqueued' ) );
     332    }
     333
     334    /** @covers ::wp_maybe_enqueue_oembed_host_js() */
     335    public function test_wp_maybe_enqueue_oembed_host_js_without_wp_head_action() {
     336        $scripts = wp_scripts();
     337
     338        remove_action( 'wp_head', 'wp_oembed_add_host_js' );
     339        $this->assertFalse( $scripts->query( 'wp-embed', 'enqueued' ) );
     340
     341        $post_embed = '<blockquote class="wp-embedded-content" data-secret="S24AQCJW9i"><a href="https://make.wordpress.org/core/2016/03/11/embeds-changes-in-wordpress-4-5/">Embeds Changes in WordPress 4.5</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title="&#8220;Embeds Changes in WordPress 4.5&#8221; &#8212; Make WordPress Core" src="https://make.wordpress.org/core/2016/03/11/embeds-changes-in-wordpress-4-5/embed/#?secret=S24AQCJW9i" data-secret="S24AQCJW9i" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>';
     342
     343        wp_maybe_enqueue_oembed_host_js( $post_embed );
     344        $this->assertFalse( $scripts->query( 'wp-embed', 'enqueued' ) );
    331345    }
    332346
Note: See TracChangeset for help on using the changeset viewer.