diff --git a/blocks/library/embed/index.js b/blocks/library/embed/index.js index fcae1b0d16b6e..a8fd4081a9927 100644 --- a/blocks/library/embed/index.js +++ b/blocks/library/embed/index.js @@ -123,9 +123,13 @@ function getEmbedBlockSettings( { title, icon, category = 'embed', transforms, k return; } response.json().then( ( obj ) => { - const { html, type, provider_name: providerName } = obj; + const { html, provider_name: providerName } = obj; const providerNameSlug = kebabCase( toLower( providerName ) ); + let { type } = obj; + if ( includes( html, 'class="wp-embedded-content" data-secret' ) ) { + type = 'wp-embed'; + } if ( html ) { this.setState( { html, type, providerNameSlug } ); setAttributes( { type, providerNameSlug } ); @@ -194,6 +198,20 @@ function getEmbedBlockSettings( { title, icon, category = 'embed', transforms, k const parsedUrl = parse( url ); const cannotPreview = includes( HOSTS_NO_PREVIEWS, parsedUrl.host.replace( /^www\./, '' ) ); const iframeTitle = sprintf( __( 'Embedded content from %s' ), parsedUrl.host ); + const embedWrapper = 'wp-embed' === type ? ( +
+ ) : ( +
+ +
+ ); let typeClassName = 'wp-block-embed'; if ( 'video' === type ) { typeClassName += ' is-video'; @@ -207,15 +225,7 @@ function getEmbedBlockSettings( { title, icon, category = 'embed', transforms, k

{ url }

{ __( 'Previews for this are unavailable in the editor, sorry!' ) }

- ) : ( -
- -
- ) } + ) : embedWrapper } { ( caption && caption.length > 0 ) || isSelected ? ( get_method() ) { + return $response; + } + + if ( is_wp_error( $response ) && 'oembed_invalid_url' !== $response->get_error_code() ) { + 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 ) { + // Not a local post, return the original error. + return $response; + } + $response = (object) $data; + } + + // Make sure the HTML is run through the oembed sanitisation routines. + $response->html = wp_oembed_get( $_GET['url'], $_GET ); + } + + return $response; +} +add_filter( 'rest_request_after_callbacks', 'gutenberg_filter_oembed_result', 10, 3 );