diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 94c9436ee9..d9663946d6 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -642,7 +642,8 @@ add_action( 'rest_api_init', 'wp_oembed_register_route' ); add_filter( 'rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4 ); add_action( 'wp_head', 'wp_oembed_add_discovery_links' ); -add_action( 'wp_head', 'wp_oembed_add_host_js' ); +add_action( 'wp_head', 'wp_oembed_add_host_js' ); // Back-compat for sites disabling oEmbed host JS by removing action. +add_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' ); add_action( 'embed_head', 'enqueue_embed_scripts', 1 ); add_action( 'embed_head', 'print_emoji_detection_script' ); diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php index ec39b03422..7efee7107d 100644 --- a/src/wp-includes/embed.php +++ b/src/wp-includes/embed.php @@ -356,11 +356,23 @@ function wp_oembed_add_discovery_links() { /** * Adds the necessary JavaScript to communicate with the embedded iframes. * + * This function is no longer used directly. For back-compat it exists exclusively as a way to indicate that the oEmbed + * host JS _should_ be added. In `default-filters.php` there remains this code: + * + * add_action( 'wp_head', 'wp_oembed_add_host_js' ) + * + * Historically a site has been able to disable adding the oEmbed host script by doing: + * + * remove_action( 'wp_head', 'wp_oembed_add_host_js' ) + * + * In order to ensure that such code still works as expected, this function remains. 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. + * * @since 4.4.0 + * @deprecated 5.9.0 Use {@see wp_maybe_enqueue_oembed_host_js()} instead. */ -function wp_oembed_add_host_js() { - add_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' ); -} +function wp_oembed_add_host_js() {} /** * Enqueue the wp-embed script if the provided oEmbed HTML contains a post embed. @@ -374,7 +386,11 @@ function wp_oembed_add_host_js() { * @return string Embed markup (without modifications). */ function wp_maybe_enqueue_oembed_host_js( $html ) { - if ( preg_match( '/
]*?wp-embedded-content/', $html ) ) { + if ( + has_action( 'wp_head', 'wp_oembed_add_host_js' ) + && + preg_match( '/]*?wp-embedded-content/', $html ) + ) { wp_enqueue_script( 'wp-embed' ); } return $html; diff --git a/tests/phpunit/tests/oembed/template.php b/tests/phpunit/tests/oembed/template.php index f24b421a87..28a14ceac4 100644 --- a/tests/phpunit/tests/oembed/template.php +++ b/tests/phpunit/tests/oembed/template.php @@ -309,13 +309,14 @@ class Tests_Embed_Template extends WP_UnitTestCase { public function test_add_host_js() { remove_all_filters( 'embed_oembed_html' ); + // This function is now a no-op. wp_oembed_add_host_js(); - $this->assertEquals( 10, has_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' ) ); + $this->assertFalse( has_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' ) ); } /** @covers ::wp_maybe_enqueue_oembed_host_js() */ - function test_wp_maybe_enqueue_oembed_host_js() { + public function test_wp_maybe_enqueue_oembed_host_js() { $scripts = wp_scripts(); $this->assertFalse( $scripts->query( 'wp-embed', 'enqueued' ) ); @@ -330,6 +331,19 @@ class Tests_Embed_Template extends WP_UnitTestCase { $this->assertTrue( $scripts->query( 'wp-embed', 'enqueued' ) ); } + /** @covers ::wp_maybe_enqueue_oembed_host_js() */ + public function test_wp_maybe_enqueue_oembed_host_js_without_wp_head_action() { + $scripts = wp_scripts(); + + remove_action( 'wp_head', 'wp_oembed_add_host_js' ); + $this->assertFalse( $scripts->query( 'wp-embed', 'enqueued' ) ); + + $post_embed = 'Embeds Changes in WordPress 4.5'; + + wp_maybe_enqueue_oembed_host_js( $post_embed ); + $this->assertFalse( $scripts->query( 'wp-embed', 'enqueued' ) ); + } + /** * Confirms that no ampersands exist in src/wp-includes/js/wp-embed.js. *