From e076a246fda462cfb9740d27ef635bd0d3aaebd6 Mon Sep 17 00:00:00 2001
From: azaozz <azaozz@1a063a9b-81f0-0310-95a4-ce76da25c4cd>
Date: Wed, 28 Sep 2011 07:26:34 +0000
Subject: [PATCH] Fix enqueueing of the right scripts when more than one
 instance of the editor, fix inserting images in full screen mode, see #17144

git-svn-id: http://svn.automattic.com/wordpress/trunk@18799 1a063a9b-81f0-0310-95a4-ce76da25c4cd
---
 wp-admin/js/media-upload.dev.js | 10 ++++++----
 wp-admin/js/media-upload.js     |  2 +-
 wp-includes/class-wp-editor.php | 23 ++++++++++++++++++-----
 wp-includes/script-loader.php   |  2 +-
 4 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/wp-admin/js/media-upload.dev.js b/wp-admin/js/media-upload.dev.js
index c5dbfed08c..1e63385082 100644
--- a/wp-admin/js/media-upload.dev.js
+++ b/wp-admin/js/media-upload.dev.js
@@ -12,10 +12,12 @@ function send_to_editor(h) {
 		} else if ( !qt ) {
 			return false;
 		}
-	} 
-
-	if ( !ed && mce && wpActiveEditor )
-		ed = tinymce.get(wpActiveEditor);
+	} else {
+		if ( mce && tinymce.activeEditor && (tinymce.activeEditor.id == 'mce_fullscreen' || tinymce.activeEditor.id == 'wp_mce_fullscreen') )
+			ed = tinymce.activeEditor;
+		else
+			ed = tinymce.get(wpActiveEditor);
+	}
 
 	if ( ed && !ed.isHidden() ) {
 		// restore caret position on IE
diff --git a/wp-admin/js/media-upload.js b/wp-admin/js/media-upload.js
index 40444ac5a8..3f1ce42c46 100644
--- a/wp-admin/js/media-upload.js
+++ b/wp-admin/js/media-upload.js
@@ -1 +1 @@
-var wpActiveEditor;function send_to_editor(c){var b,a=typeof(tinymce)!="undefined",f=typeof(QTags)!="undefined";if(!wpActiveEditor){if(a&&tinymce.activeEditor){b=tinymce.activeEditor;wpActiveEditor=b.id}else{if(!f){return false}}}if(!b&&a&&wpActiveEditor){b=tinymce.get(wpActiveEditor)}if(b&&!b.isHidden()){if(tinymce.isIE&&b.windowManager.insertimagebookmark){b.selection.moveToBookmark(b.windowManager.insertimagebookmark)}if(c.indexOf("[caption")===0){if(b.plugins.wpeditimage){c=b.plugins.wpeditimage._do_shcode(c)}}else{if(c.indexOf("[gallery")===0){if(b.plugins.wpgallery){c=b.plugins.wpgallery._do_gallery(c)}}else{if(c.indexOf("[embed")===0){if(b.plugins.wordpress){c=b.plugins.wordpress._setEmbed(c)}}}}b.execCommand("mceInsertContent",false,c)}else{if(f){QTags.insertContent(c)}else{document.getElementById(wpActiveEditor).value+=c}}try{tb_remove()}catch(d){}}var tb_position;(function(a){tb_position=function(){var f=a("#TB_window"),e=a(window).width(),d=a(window).height(),c=(720<e)?720:e,b=0;if(a("body.admin-bar").length){b=28}if(f.size()){f.width(c-50).height(d-45-b);a("#TB_iframeContent").width(c-50).height(d-75-b);f.css({"margin-left":"-"+parseInt(((c-50)/2),10)+"px"});if(typeof document.body.style.maxWidth!="undefined"){f.css({top:20+b+"px","margin-top":"0"})}}return a("a.thickbox").each(function(){var g=a(this).attr("href");if(!g){return}g=g.replace(/&width=[0-9]+/g,"");g=g.replace(/&height=[0-9]+/g,"");a(this).attr("href",g+"&width="+(c-80)+"&height="+(d-85-b))})};a(window).resize(function(){tb_position()});a(document).ready(function(b){b("a.thickbox").click(function(){var c;if(typeof(tinymce)!="undefined"&&tinymce.isIE&&(c=tinymce.get(wpActiveEditor))&&!c.isHidden()){c.focus();c.windowManager.insertimagebookmark=c.selection.getBookmark()}})})})(jQuery);
\ No newline at end of file
+var wpActiveEditor;function send_to_editor(c){var b,a=typeof(tinymce)!="undefined",f=typeof(QTags)!="undefined";if(!wpActiveEditor){if(a&&tinymce.activeEditor){b=tinymce.activeEditor;wpActiveEditor=b.id}else{if(!f){return false}}}else{if(a&&tinymce.activeEditor&&(tinymce.activeEditor.id=="mce_fullscreen"||tinymce.activeEditor.id=="wp_mce_fullscreen")){b=tinymce.activeEditor}else{b=tinymce.get(wpActiveEditor)}}if(b&&!b.isHidden()){if(tinymce.isIE&&b.windowManager.insertimagebookmark){b.selection.moveToBookmark(b.windowManager.insertimagebookmark)}if(c.indexOf("[caption")===0){if(b.plugins.wpeditimage){c=b.plugins.wpeditimage._do_shcode(c)}}else{if(c.indexOf("[gallery")===0){if(b.plugins.wpgallery){c=b.plugins.wpgallery._do_gallery(c)}}else{if(c.indexOf("[embed")===0){if(b.plugins.wordpress){c=b.plugins.wordpress._setEmbed(c)}}}}b.execCommand("mceInsertContent",false,c)}else{if(f){QTags.insertContent(c)}else{document.getElementById(wpActiveEditor).value+=c}}try{tb_remove()}catch(d){}}var tb_position;(function(a){tb_position=function(){var f=a("#TB_window"),e=a(window).width(),d=a(window).height(),c=(720<e)?720:e,b=0;if(a("body.admin-bar").length){b=28}if(f.size()){f.width(c-50).height(d-45-b);a("#TB_iframeContent").width(c-50).height(d-75-b);f.css({"margin-left":"-"+parseInt(((c-50)/2),10)+"px"});if(typeof document.body.style.maxWidth!="undefined"){f.css({top:20+b+"px","margin-top":"0"})}}return a("a.thickbox").each(function(){var g=a(this).attr("href");if(!g){return}g=g.replace(/&width=[0-9]+/g,"");g=g.replace(/&height=[0-9]+/g,"");a(this).attr("href",g+"&width="+(c-80)+"&height="+(d-85-b))})};a(window).resize(function(){tb_position()});a(document).ready(function(b){b("a.thickbox").click(function(){var c;if(typeof(tinymce)!="undefined"&&tinymce.isIE&&(c=tinymce.get(wpActiveEditor))&&!c.isHidden()){c.focus();c.windowManager.insertimagebookmark=c.selection.getBookmark()}})})})(jQuery);
\ No newline at end of file
diff --git a/wp-includes/class-wp-editor.php b/wp-includes/class-wp-editor.php
index 178f2c9bb5..dfd3fac76a 100644
--- a/wp-includes/class-wp-editor.php
+++ b/wp-includes/class-wp-editor.php
@@ -25,6 +25,9 @@ class WP_Editor {
 	var $first_init;
 	var $this_tinymce = false;
 	var $this_quicktags = false;
+	var $has_tinymce = false;
+	var $has_quicktags = false;
+	var $has_medialib = false;
 
 	function __construct() {
 		$this->can_richedit = user_can_richedit();
@@ -67,6 +70,7 @@ class WP_Editor {
 
 		if ( $this->this_quicktags && $this->this_tinymce ) {
 			$switch_class = 'html-active';
+			$this->has_tinymce = $this->has_quicktags = true;
 
 			if ( 'html' == $this->default_editor ) {
 				add_filter('the_editor_content', 'wp_htmledit_pre');
@@ -77,6 +81,12 @@ class WP_Editor {
 
 			$buttons .= '<a id="' . $editor_id . '-html" class="hide-if-no-js wp-switch-editor switch-html" onclick="switchEditors.go(this);return false;">' . __('HTML') . "</a>\n";
 			$buttons .= '<a id="' . $editor_id . '-tmce" class="hide-if-no-js wp-switch-editor switch-tmce" onclick="switchEditors.go(this);return false;">' . __('Visual') . "</a>\n";
+		} else {
+			if ( $this->this_tinymce )
+				$this->has_tinymce = true;
+
+			if ( $this->this_quicktags )
+				$this->has_quicktags = true;
 		}
 
 		echo '<div id="wp-' . $editor_id . '-wrap" class="wp-editor-wrap ' . $switch_class . '">';
@@ -89,6 +99,8 @@ class WP_Editor {
 			echo $buttons;
 
 			if ( $set['media_buttons'] ) {
+				$this->has_medialib = true;
+
 				if ( !function_exists('media_buttons') )
 					include(ABSPATH . 'wp-admin/includes/media.php');
 
@@ -442,10 +454,10 @@ class WP_Editor {
 		wp_enqueue_script('word-count');
 		wp_enqueue_style('editor-buttons');
 
-		if ( $this->this_tinymce )
+		if ( $this->has_tinymce )
 			wp_enqueue_script('editor');
 
-		if ( $this->this_quicktags )
+		if ( $this->has_quicktags )
 			wp_enqueue_script('quicktags');
 
 		if ( in_array('wplink', $this->plugins, true) || in_array('link', $this->qt_buttons, true) ) {
@@ -457,8 +469,10 @@ class WP_Editor {
 		if ( in_array('wpfullscreen', $this->plugins, true) || in_array('fullscreen', $this->qt_buttons, true) )
 			wp_enqueue_script('wp-fullscreen');
 
-		add_thickbox();
-		wp_enqueue_script('media-upload');
+		if ( $this->has_medialib ) {
+			add_thickbox();
+			wp_enqueue_script('media-upload');
+		}
 	}
 
 	function editor_js() {
@@ -800,4 +814,3 @@ class WP_Editor {
 	}
 }
 
-
diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php
index c2b5d40a3b..4eb5fd9df3 100644
--- a/wp-includes/script-loader.php
+++ b/wp-includes/script-loader.php
@@ -276,7 +276,7 @@ function wp_default_scripts( &$scripts ) {
 
 	$scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array( 'jquery' ), '20110515', 1 );
 
-	$scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox' ), '20110922', 1 );
+	$scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox' ), '20110927', 1 );
 
 	if ( is_admin() ) {
 		$scripts->add( 'ajaxcat', "/wp-admin/js/cat$suffix.js", array( 'wp-lists' ), '20090102' );