Skip to content

Commit

Permalink
Add support for shortcode embeds that enqueue scripts (#9734)
Browse files Browse the repository at this point in the history
  • Loading branch information
notnownikki authored Sep 18, 2018
1 parent bee8b55 commit 94be4e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,17 @@ function gutenberg_filter_oembed_result( $response, $handler, $request ) {
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,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ export function getEmbedEdit( title, icon ) {
}

const html = 'photo' === type ? this.getPhotoHtml( preview ) : preview.html;
const { scripts } = preview;
const parsedUrl = parse( url );
const cannotPreview = includes( HOSTS_NO_PREVIEWS, parsedUrl.host.replace( /^www\./, '' ) );
// translators: %s: host providing embed content e.g: www.youtube.com
Expand All @@ -283,6 +284,7 @@ export function getEmbedEdit( title, icon ) {
<div className="wp-block-embed__wrapper">
<SandBox
html={ html }
scripts={ scripts }
title={ iframeTitle }
type={ sandboxClassnames }
/>
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/sandbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ class Sandbox extends Component {
<head>
<title>{ this.props.title }</title>
<style dangerouslySetInnerHTML={ { __html: style } } />
{ ( this.props.scripts && this.props.scripts.map(
( src ) => <script key={ src } src={ src } />
) ) }
</head>
<body data-resizable-iframe-connected="data-resizable-iframe-connected" className={ this.props.type }>
<div dangerouslySetInnerHTML={ { __html: this.props.html } } />
Expand Down

0 comments on commit 94be4e3

Please sign in to comment.