From 90100ff8831cb7e834afd6823527a4e846b5eba0 Mon Sep 17 00:00:00 2001 From: audrasjb Date: Tue, 4 Jan 2022 15:23:59 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@52437 git-svn-id: http://core.svn.wordpress.org/trunk@52029 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/default-filters.php | 3 ++- wp-includes/embed.php | 24 ++++++++++++++++++++---- wp-includes/version.php | 2 +- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 94c9436ee9..d9663946d6 100644 --- a/wp-includes/default-filters.php +++ b/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/wp-includes/embed.php b/wp-includes/embed.php index ec39b03422..7efee7107d 100644 --- a/wp-includes/embed.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index d8d7c96d69..7e5185087a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-beta4-52436'; +$wp_version = '5.9-beta4-52437'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.