After [32258], restore the parts of [31620] and [31626] that weren't changes to the UI, but were improvements to existing code.

* Use `wp.shortcode()` instead of manually constructing a shortcode in `views/embed/link`
* In `WP_Embed`, store the last URL and last set of attributes requested in class properties
* `wp_ajax_parse_embed()`, allow `[embed]`s to have attributes. Return `attr` in the response.

See #31139.


git-svn-id: https://develop.svn.wordpress.org/trunk@32330 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-04-30 21:39:52 +00:00
parent 345d34cad9
commit 625e7135f3
4 changed files with 40 additions and 7 deletions

View File

@ -2714,14 +2714,23 @@ function wp_ajax_parse_embed() {
}
$shortcode = wp_unslash( $_POST['shortcode'] );
$url = str_replace( '[embed]', '', str_replace( '[/embed]', '', $shortcode ) );
preg_match( '/' . get_shortcode_regex() . '/s', $shortcode, $matches );
$atts = shortcode_parse_atts( $matches[3] );
if ( ! empty( $matches[5] ) ) {
$url = $matches[5];
} elseif ( ! empty( $atts['src'] ) ) {
$url = $atts['src'];
} else {
$url = '';
}
$parsed = false;
setup_postdata( $post );
$wp_embed->return_false_on_fail = true;
if ( is_ssl() && preg_match( '%^\\[embed[^\\]]*\\]http://%i', $shortcode ) ) {
if ( is_ssl() && 0 === strpos( $url, 'http://' ) ) {
// Admin is ssl and the user pasted non-ssl URL.
// Check if the provider supports ssl embeds and use that for the preview.
$ssl_shortcode = preg_replace( '%^(\\[embed[^\\]]*\\])http://%i', '$1https://', $shortcode );
@ -2732,7 +2741,7 @@ function wp_ajax_parse_embed() {
}
}
if ( ! $parsed ) {
if ( $url && ! $parsed ) {
$parsed = $wp_embed->run_shortcode( $shortcode );
}
@ -2774,7 +2783,8 @@ function wp_ajax_parse_embed() {
}
wp_send_json_success( array(
'body' => $parsed
'body' => $parsed,
'attr' => $wp_embed->last_attr
) );
}

View File

@ -11,6 +11,8 @@ class WP_Embed {
public $post_ID;
public $usecache = true;
public $linkifunknown = true;
public $last_attr = array();
public $last_url = '';
/**
* When an URL cannot be embedded, return false instead of returning a link
@ -134,13 +136,18 @@ class WP_Embed {
$url = $attr['src'];
}
$this->last_url = $url;
if ( empty( $url ) )
if ( empty( $url ) ) {
$this->last_attr = $attr;
return '';
}
$rawattr = $attr;
$attr = wp_parse_args( $attr, wp_embed_defaults( $url ) );
$this->last_attr = $attr;
// kses converts & into & and we need to undo this
// See https://core.trac.wordpress.org/ticket/11311
$url = str_replace( '&', '&', $url );

View File

@ -4554,15 +4554,23 @@ EmbedLink = wp.media.view.Settings.extend({
}, 600 ),
fetch: function() {
var embed;
// check if they haven't typed in 500 ms
if ( $('#embed-url-field').val() !== this.model.get('url') ) {
return;
}
embed = new wp.shortcode({
tag: 'embed',
attrs: _.pick( this.model.attributes, [ 'width', 'height', 'src' ] ),
content: this.model.get('url')
});
wp.ajax.send( 'parse-embed', {
data : {
post_ID: wp.media.view.settings.post.id,
shortcode: '[embed]' + this.model.get('url') + '[/embed]'
shortcode: embed.string()
}
} )
.done( _.bind( this.renderoEmbed, this ) )

View File

@ -38,15 +38,23 @@ EmbedLink = wp.media.view.Settings.extend({
}, 600 ),
fetch: function() {
var embed;
// check if they haven't typed in 500 ms
if ( $('#embed-url-field').val() !== this.model.get('url') ) {
return;
}
embed = new wp.shortcode({
tag: 'embed',
attrs: _.pick( this.model.attributes, [ 'width', 'height', 'src' ] ),
content: this.model.get('url')
});
wp.ajax.send( 'parse-embed', {
data : {
post_ID: wp.media.view.settings.post.id,
shortcode: '[embed]' + this.model.get('url') + '[/embed]'
shortcode: embed.string()
}
} )
.done( _.bind( this.renderoEmbed, this ) )