Skip to content

Commit

Permalink
REST API: Remove oEmbed proxy HTML filtering (#13575)
Browse files Browse the repository at this point in the history
* Plugin: Remove oEmbed proxy HTML filtering

* Plugin: Use updated Trac ticket link for oEmbed filter reference

* Plugin: Use updated Trac ticket link for oEmbed filter reference
  • Loading branch information
aduth authored Jan 30, 2019
1 parent b09c855 commit 62e1522
Showing 1 changed file with 20 additions and 41 deletions.
61 changes: 20 additions & 41 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ function gutenberg_register_rest_routes() {
}

/**
* Make sure oEmbed REST Requests apply the WP Embed security mechanism for WordPress embeds.
* Handle a failing oEmbed proxy request to try embedding as a shortcode.
*
* @see https://core.trac.wordpress.org/ticket/32522
*
* TODO: This is a temporary solution. Next step would be to edit the WP_oEmbed_Controller,
* once merged into Core.
* @see https://core.trac.wordpress.org/ticket/45447
*
* @since 2.3.0
*
Expand All @@ -36,50 +33,32 @@ function gutenberg_register_rest_routes() {
* @return WP_HTTP_Response|object|WP_Error The REST Request response.
*/
function gutenberg_filter_oembed_result( $response, $handler, $request ) {
if ( 'GET' !== $request->get_method() ) {
if ( ! is_wp_error( $response ) || 'oembed_invalid_url' !== $response->get_error_code() ||
'/oembed/1.0/proxy' !== $request->get_route() ) {
return $response;
}

if ( is_wp_error( $response ) && 'oembed_invalid_url' !== $response->get_error_code() ) {
// Try using a classic embed instead.
global $wp_embed;
$html = $wp_embed->shortcode( array(), $_GET['url'] );
if ( ! $html ) {
return $response;
}

// External embeds.
if ( '/oembed/1.0/proxy' === $request->get_route() ) {
if ( is_wp_error( $response ) ) {
// It's possibly a local post, so lets try and retrieve it that way.
$post_id = url_to_postid( $_GET['url'] );
$data = get_oembed_response_data( $post_id, apply_filters( 'oembed_default_width', 600 ) );

if ( $data ) {
// It's a local post!
$response = (object) $data;
} else {
// Try using a classic embed, instead.
global $wp_embed;
$html = $wp_embed->shortcode( array(), $_GET['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 array(
'provider_name' => __( 'Embed Handler', 'gutenberg' ),
'html' => $html,
'scripts' => $enqueued_scripts,
);
}
}
}

// Make sure the HTML is run through the oembed sanitisation routines.
$response->html = wp_oembed_get( $_GET['url'], $_GET );
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 $response;
return array(
'provider_name' => __( 'Embed Handler', 'gutenberg' ),
'html' => $html,
'scripts' => $enqueued_scripts,
);
}
add_filter( 'rest_request_after_callbacks', 'gutenberg_filter_oembed_result', 10, 3 );

Expand Down

0 comments on commit 62e1522

Please sign in to comment.