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.


git-svn-id: https://develop.svn.wordpress.org/trunk@52437 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras
2022-01-04 15:22:40 +00:00
parent 18aa343ec2
commit 107050f7a3
3 changed files with 38 additions and 7 deletions
+20 -4
View File
@@ -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( '/<blockquote\s[^>]*?wp-embedded-content/', $html ) ) {
if (
has_action( 'wp_head', 'wp_oembed_add_host_js' )
&&
preg_match( '/<blockquote\s[^>]*?wp-embedded-content/', $html )
) {
wp_enqueue_script( 'wp-embed' );
}
return $html;