diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 373d3436e3..43f59b66ec 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -2993,9 +2993,10 @@ function wp_ajax_query_themes() { * @global WP_Post $post Global $post. * @global WP_Embed $wp_embed Embed API instance. * @global WP_Scripts $wp_scripts + * @global int $content_width */ function wp_ajax_parse_embed() { - global $post, $wp_embed; + global $post, $wp_embed, $content_width; if ( empty( $_POST['shortcode'] ) ) { wp_send_json_error(); @@ -3037,6 +3038,15 @@ function wp_ajax_parse_embed() { } } + // Set $content_width so any embeds fit in the destination iframe. + if ( isset( $_POST['maxwidth'] ) && is_numeric( $_POST['maxwidth'] ) && $_POST['maxwidth'] > 0 ) { + if ( ! isset( $content_width ) ) { + $content_width = intval( $_POST['maxwidth'] ); + } else { + $content_width = min( $content_width, intval( $_POST['maxwidth'] ) ); + } + } + if ( $url && ! $parsed ) { $parsed = $wp_embed->run_shortcode( $shortcode ); } diff --git a/wp-includes/js/mce-view.js b/wp-includes/js/mce-view.js index 51dc518f8d..f5aa2337ea 100644 --- a/wp-includes/js/mce-view.js +++ b/wp-includes/js/mce-view.js @@ -85,10 +85,11 @@ * and creates a new instance for every match. * * @param {String} content The string to scan. + * @param {tinymce.Editor} editor The editor. * * @return {String} The string with markers. */ - setMarkers: function( content ) { + setMarkers: function( content, editor ) { var pieces = [ { content: content } ], self = this, instance, current; @@ -115,6 +116,7 @@ pieces.push( { content: remaining.substring( 0, result.index ) } ); } + result.options.editor = editor; instance = self.createInstance( type, result.content, result.options ); text = instance.loader ? '.' : instance.text; @@ -850,7 +852,7 @@ action: 'parse-media-shortcode', initialize: function() { - var self = this; + var self = this, maxwidth = null; if ( this.url ) { this.loader = false; @@ -859,10 +861,16 @@ } ); } + // Obtain the target width for the embed. + if ( self.editor ) { + maxwidth = self.editor.iframeElement.clientWidth - 20; // Minus the sum of horizontal margins and borders. + } + wp.ajax.post( this.action, { post_ID: media.view.settings.post.id, type: this.shortcode.tag, - shortcode: this.shortcode.string() + shortcode: this.shortcode.string(), + maxwidth: maxwidth } ) .done( function( response ) { self.render( response ); diff --git a/wp-includes/js/mce-view.min.js b/wp-includes/js/mce-view.min.js index 9e10347b45..e00c6eaabe 100644 --- a/wp-includes/js/mce-view.min.js +++ b/wp-includes/js/mce-view.min.js @@ -1 +1 @@ -!function(a,b,c,d){"use strict";var e={},f={};b.mce=b.mce||{},b.mce.views={register:function(a,c){e[a]=b.mce.View.extend(_.extend(c,{type:a}))},unregister:function(a){delete e[a]},get:function(a){return e[a]},unbind:function(){_.each(f,function(a){a.unbind()})},setMarkers:function(a){var b,c,d=[{content:a}],f=this;return _.each(e,function(a,e){c=d.slice(),d=[],_.each(c,function(c){var g,h,i=c.content;if(c.processed)return void d.push(c);for(;i&&(g=a.prototype.match(i));)g.index&&d.push({content:i.substring(0,g.index)}),b=f.createInstance(e,g.content,g.options),h=b.loader?".":b.text,d.push({content:b.ignore?h:'
'+h+"
",processed:!0}),i=i.slice(g.index+g.content.length);i&&d.push({content:i})})}),a=_.pluck(d,"content").join(""),a.replace(/\s*
")},createInstance:function(a,b,c,d){var e,g,h=this.get(a);return b.indexOf("[")!==-1&&b.indexOf("]")!==-1&&(b=b.replace(/\[[^\]]+\]/g,function(a){return a.replace(/[\r\n]/g,"")})),!d&&(g=this.getInstance(b))?g:(e=encodeURIComponent(b),c=_.extend(c||{},{text:b,encodedText:e}),f[e]=new h(c))},getInstance:function(a){return"string"==typeof a?f[encodeURIComponent(a)]:f[d(a).attr("data-wpview-text")]},getText:function(a){return decodeURIComponent(d(a).attr("data-wpview-text")||"")},render:function(a){_.each(f,function(b){b.render(null,a)})},update:function(a,b,c,d){var e=this.getInstance(c);e&&e.update(a,b,c,d)},edit:function(a,b){var c=this.getInstance(b);c&&c.edit&&c.edit(c.text,function(d,e){c.update(d,a,b,e)})},remove:function(a,b){var c=this.getInstance(b);c&&c.remove(a,b)}},b.mce.View=function(a){_.extend(this,a),this.initialize()},b.mce.View.extend=Backbone.View.extend,_.extend(b.mce.View.prototype,{content:null,loader:!0,initialize:function(){},getContent:function(){return this.content},render:function(a,b){null!=a&&(this.content=a),a=this.getContent(),(this.loader||a)&&(b&&this.unbind(),this.replaceMarkers(),a?this.setContent(a,function(a,b){d(b).data("rendered",!0),this.bindNode.call(this,a,b)},!!b&&null):this.setLoader())},bindNode:function(){},unbindNode:function(){},unbind:function(){this.getNodes(function(a,b){this.unbindNode.call(this,a,b)},!0)},getEditors:function(a){_.each(tinymce.editors,function(b){b.plugins.wpview&&a.call(this,b)},this)},getNodes:function(a,b){this.getEditors(function(c){var e=this;d(c.getBody()).find('[data-wpview-text="'+e.encodedText+'"]').filter(function(){var a;return null==b||(a=d(this).data("rendered")===!0,b?a:!a)}).each(function(){a.call(e,c,this,this)})})},getMarkers:function(a){this.getEditors(function(b){var c=this;d(b.getBody()).find('[data-wpview-marker="'+this.encodedText+'"]').each(function(){a.call(c,b,this)})})},replaceMarkers:function(){this.getMarkers(function(a,b){var c,e=b===a.selection.getNode();return this.loader||d(b).text()===tinymce.DOM.decode(this.text)?(c=a.$('
'),a.$(b).replaceWith(c),void(e&&setTimeout(function(){a.selection.select(c[0]),a.selection.collapse()}))):void a.dom.setAttrib(b,"data-wpview-marker",null)})},removeMarkers:function(){this.getMarkers(function(a,b){a.dom.setAttrib(b,"data-wpview-marker",null)})},setContent:function(a,b,c){_.isObject(a)&&(a.sandbox||a.head||a.body.indexOf("