From 625e7135f3436956dc2a7647d5d9d0c3bab63c28 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 30 Apr 2015 21:39:52 +0000 Subject: [PATCH] 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 --- src/wp-admin/includes/ajax-actions.php | 18 ++++++++++++++---- src/wp-includes/class-wp-embed.php | 9 ++++++++- src/wp-includes/js/media-views.js | 10 +++++++++- src/wp-includes/js/media/views/embed/link.js | 10 +++++++++- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 40f8f5d195..1addff7934 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -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 ) ); } diff --git a/src/wp-includes/class-wp-embed.php b/src/wp-includes/class-wp-embed.php index 9569f38df3..9af3c224fe 100644 --- a/src/wp-includes/class-wp-embed.php +++ b/src/wp-includes/class-wp-embed.php @@ -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 ); diff --git a/src/wp-includes/js/media-views.js b/src/wp-includes/js/media-views.js index 605459f4a9..cbacc82dcd 100644 --- a/src/wp-includes/js/media-views.js +++ b/src/wp-includes/js/media-views.js @@ -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 ) ) diff --git a/src/wp-includes/js/media/views/embed/link.js b/src/wp-includes/js/media/views/embed/link.js index 4a30230105..6793848100 100644 --- a/src/wp-includes/js/media/views/embed/link.js +++ b/src/wp-includes/js/media/views/embed/link.js @@ -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 ) )