Embeds: Ensure that classic embed support works in the block editor.

See https://github.com/WordPress/gutenberg/pull/6345

Fixes #45447.

Props swisspidy, pento, audrasjb, aduth, jrchamp, thrijith, TimothyBlynJacobs, whyisjake. 
 

Built from https://develop.svn.wordpress.org/trunk@48135


git-svn-id: http://core.svn.wordpress.org/trunk@47904 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
whyisjake 2020-06-23 06:08:08 +00:00
parent a918d853aa
commit 07f71058c3
3 changed files with 73 additions and 22 deletions

View File

@ -125,6 +125,53 @@ class WP_Embed {
unset( $this->handlers[ $priority ][ $id ] );
}
/**
* Returns embed HTML for a given URL from embed handlers.
*
* Attempts to convert a URL into embed HTML by checking the URL
* against the regex of the registered embed handlers.
*
* @since 5.5.0
*
* @param array $attr {
* Shortcode attributes. Optional.
*
* @type int $width Width of the embed in pixels.
* @type int $height Height of the embed in pixels.
* }
* @param string $url The URL attempting to be embedded.
* @return string|false The embed HTML on success, false otherwise.
*/
public function get_embed_handler_html( $attr, $url ) {
$rawattr = $attr;
$attr = wp_parse_args( $attr, wp_embed_defaults( $url ) );
ksort( $this->handlers );
foreach ( $this->handlers as $priority => $handlers ) {
foreach ( $handlers as $id => $handler ) {
if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
$return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr );
if ( false !== $return ) {
/**
* Filters the returned embed HTML.
*
* @since 2.9.0
*
* @see WP_Embed::shortcode()
*
* @param string|false $return The HTML result of the shortcode, or false on failure.
* @param string $url The embed URL.
* @param array $attr An array of shortcode attributes.
*/
return apply_filters( 'embed_handler_html', $return, $url, $attr );
}
}
}
}
return false;
}
/**
* The do_shortcode() callback function.
*
@ -166,27 +213,9 @@ class WP_Embed {
$url = str_replace( '&', '&', $url );
// Look for known internal handlers.
ksort( $this->handlers );
foreach ( $this->handlers as $priority => $handlers ) {
foreach ( $handlers as $id => $handler ) {
if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
$return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr );
if ( false !== $return ) {
/**
* Filters the returned embed HTML.
*
* @since 2.9.0
*
* @see WP_Embed::shortcode()
*
* @param string|false $return The HTML result of the shortcode, or false on failure.
* @param string $url The embed URL.
* @param array $attr An array of shortcode attributes.
*/
return apply_filters( 'embed_handler_html', $return, $url, $attr );
}
}
}
$embed_handler_html = $this->get_embed_handler_html( $rawattr, $url );
if ( false !== $embed_handler_html ) {
return $embed_handler_html;
}
$post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;

View File

@ -193,6 +193,28 @@ final class WP_oEmbed_Controller {
$data = _wp_oembed_get_object()->get_data( $url, $args );
if ( false === $data ) {
// Try using a classic embed, instead.
global $wp_embed;
/* @var WP_Embed $wp_embed */
$html = $wp_embed->get_embed_handler_html( $args, $url );
if ( $html ) {
global $wp_scripts;
// Check if any scripts were enqueued by the shortcode, and include them in the response.
$enqueued_scripts = array();
foreach ( $wp_scripts->queue as $script ) {
$enqueued_scripts[] = $wp_scripts->registered[ $script ]->src;
}
return (object) array(
'provider_name' => __( 'Embed Handler' ),
'html' => $html,
'scripts' => $enqueued_scripts,
);
}
return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.5-alpha-48134';
$wp_version = '5.5-alpha-48135';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.