-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for shortcode embeds that enqueue scripts #9734
Conversation
// Check if any scripts were enqueued by the shortcode, and | ||
// include them in the response. | ||
$enqueued_scripts = array(); | ||
foreach ( $wp_scripts->queue as $script ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my local tests, I'm still having problems when I try to embed http://pinterest.co.uk/heald0081/hair-ideas. I checked that $wp_scripts->queue is empty which I assume was unexpected.
I changed Gutenberg in a poopy.life sandbox to contain:
if ( $html ) {
global $wp_scripts;
var_dump( $wp_scripts->queue );
exit;
And I checked $wp_scripts->queue was also empty there. I'm not certain about the reason for this. Maybe I'm doing something wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bet is you tested to make sure it was broken, applied the patch, then tested again? Clear all your embed transients and see if it works :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If not (it hit me a few times!) Can you check that the call to enqueued script happens in the short code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still having problems with the embed of Pinterest, I used a plugin the clear all the transients and the situation persisted. I checked that Pinterest is not a WordPress supported embed in the classic editor https://codex.wordpress.org/Embeds, but JetPack adds a shortcode for it. Do you have JetPack installed in your testing environment? maybe that explains the reason for the difference in the tests because I'm not using JetPack on the environment I'm testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this needs Jetpack with the shortcodes module active.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry for the troubles, it works great with JetPAck shortcodes module active.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Things worked correctly in my tests 👍 I had some concerns about the security implications of loading this scripts, but they are loaded inside the sandbox iframe and we had a comment there saying "we can use this in the future to inject custom styles or scripts" so it looks like the component was already built with script injection in mind.
@@ -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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an upstream Trac ticket tracking these enhancements to the oEmbed endpoint?
If so, should we add a comment about this addition?
If not, should we create one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There isn't yet. I was leaving this one until we're looking to merge for 5.0, as gutenberg_filter_oembed_result()
is a giant hack that will be have to be mostly rewritten. So far, the things that have been merged to Core have been the ones we could mostly just copy/pasta.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm looking at this again in relation to the ongoing effort to remove PHP from the Gutenberg plugin. I found these:
But it's not clear from these that the changes that the revisions intended from the pull request were ever included as part of WordPress 5.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've confirmed that the original testing instructions of this pull request fail in a stock WordPress 5.0 install. Seems there's still some remaining work to port. I'll try to put together a combination Gutenberg pull request / Trac ticket.
Description
If the embed preview is handled by a shortcode, it might enqueue scripts to make it work.
This change returns the enqueued scripts as part of the embed response, and has
the Sandbox component put them into the preview document.
How has this been tested?
Install Jetpack and activate the shortcodes module.
Try to embed https://www.pinterest.co.uk/heald0081/hair-ideas/
The preview should appear as expected. Without this fix, only the caption appears.
Types of changes
Bug fix
Checklist: