Index: wp-admin/includes/media.php
===================================================================
--- wp-admin/includes/media.php	(revision 21358)
+++ wp-admin/includes/media.php	(working copy)
@@ -7,11 +7,13 @@
  */
 
 /**
- * {@internal Missing Short Description}}
+ * Defines media tabs to be used in thickbox
  *
  * @since 2.5.0
  *
- * @return unknown
+ * @uses apply_filters() calls media_upload_tabs
+ *
+ * @return an associative array of tab names in the form of type=>name
  */
 function media_upload_tabs() {
 	$_default_tabs = array(
@@ -25,12 +27,13 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Added to media_upload_tabs filter to define the gallery tab
+ * checks for attachments and removes gallery tab if the current post has none.
  *
  * @since 2.5.0
  *
- * @param unknown_type $tabs
- * @return unknown
+ * @param array $tabs
+ * @return an associative array of tab names in the form of type=>name
  */
 function update_gallery_tab($tabs) {
 	global $wpdb;
@@ -57,7 +60,7 @@
 add_filter('media_upload_tabs', 'update_gallery_tab');
 
 /**
- * {@internal Missing Short Description}}
+ * Echoes the thickbox media upload tabs to the screen
  *
  * @since 2.5.0
  */
@@ -65,9 +68,10 @@
 	global $redir_tab;
 	$tabs = media_upload_tabs();
 	$default = 'type';
-
+	$html = '';
+	
 	if ( !empty($tabs) ) {
-		echo "<ul id='sidemenu'>\n";
+		$html = "<ul id='sidemenu'>\n";
 		if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) )
 			$current = $redir_tab;
 		elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
@@ -83,17 +87,19 @@
 
 			$href = add_query_arg(array('tab' => $callback, 's' => false, 'paged' => false, 'post_mime_type' => false, 'm' => false));
 			$link = "<a href='" . esc_url($href) . "'$class>$text</a>";
-			echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
+			$html .= "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
 		}
-		echo "</ul>\n";
+		$html .= "</ul>\n";
 	}
+	echo $html;
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Creates the html for an image as it is added to the editor
  *
  * @since 2.5.0
  *
+ * @uses apply_filters() calls image_send_to_editor
  * @param unknown_type $id
  * @param unknown_type $alt
  * @param unknown_type $title
@@ -101,7 +107,7 @@
  * @param unknown_type $url
  * @param unknown_type $rel
  * @param unknown_type $size
- * @return unknown
+ * @return string - the html for an image tag
  */
 function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
 
@@ -118,7 +124,7 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Wraps an image in the caption code
  *
  * @since 2.6.0
  *
@@ -170,11 +176,11 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Echoes the javascript used to send html back to the editor
  *
  * @since 2.5.0
  *
- * @param unknown_type $html
+ * @param string $html
  */
 function media_send_to_editor($html) {
 ?>
@@ -189,8 +195,6 @@
 }
 
 /**
- * {@internal Missing Short Description}}
- *
  * This handles the file upload POST itself, creating the attachment post.
  *
  * @since 2.5.0
@@ -260,6 +264,11 @@
  *
  * @since 2.6.0
  *
+ * @uses wp_handle_sideload
+ * @uses wp_read_image_metadata
+ * @uses wp_insert_attachment
+ * @uses wp_update_attachment_metadata
+ * @uses wp_generate_attachment_metadata
  * @param array $file_array Array similar to a {@link $_FILES} upload array
  * @param int $post_id The post ID the media is associated with
  * @param string $desc Description of the sideloaded file
@@ -312,7 +321,7 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Generates html for the iframe used in the thickbox window for uploads
  *
  * Wrap iframe content (produced by $content_func) in a doctype, html head/body
  * etc any additional function args will be passed to content_func.
@@ -373,7 +382,8 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Echoes the html for media upload buttons
+ * added to the action call for 'media_buttons' 
  *
  * @since 2.5.0
  */
@@ -386,6 +396,15 @@
 }
 add_action( 'media_buttons', 'media_buttons' );
 
+/**
+ * Determine url of the upload thickbox to be used in the media_buttons() link
+ *
+ * @uses apply_filters() calls $type . '_upload_iframe_src'
+ * @param string $type
+ * @param int $post_id
+ * @param string $tab
+ * @since unknown
+ */
 function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
 	global $post_ID;
 
@@ -565,7 +584,7 @@
 
 	if ( isset($_POST['save']) ) {
 		$errors['upload_notice'] = __('Saved.');
-		return media_upload_gallery();
+		return media_upload('gallery');
 	}
 
 	if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
@@ -625,47 +644,86 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Download an image from the specified URL and attach it to a post.
  *
- * @since 2.5.0
+ * @since 2.6.0
  *
- * @return unknown
+ * @param string $file The URL of the image to download
+ * @param int $post_id The post ID the media is to be associated with
+ * @param string $desc Optional. Description of the image
+ * @return string|WP_Error Populated HTML img tag on success
  */
-function media_upload_gallery() {
-	$errors = array();
+function media_sideload_image($file, $post_id, $desc = null) {
+	if ( ! empty($file) ) {
+		// Download file to temp location
+		$tmp = download_url( $file );
 
-	if ( !empty($_POST) ) {
-		$return = media_upload_form_handler();
+		// Set variables for storage
+		// fix file filename for query strings
+		preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
+		$file_array['name'] = basename($matches[0]);
+		$file_array['tmp_name'] = $tmp;
 
-		if ( is_string($return) )
-			return $return;
-		if ( is_array($return) )
-			$errors = $return;
+		// If error storing temporarily, unlink
+		if ( is_wp_error( $tmp ) ) {
+			@unlink($file_array['tmp_name']);
+			$file_array['tmp_name'] = '';
+		}
+
+		// do the validation and storage stuff
+		$id = media_handle_sideload( $file_array, $post_id, $desc );
+		// If error storing permanently, unlink
+		if ( is_wp_error($id) ) {
+			@unlink($file_array['tmp_name']);
+			return $id;
+		}
+
+		$src = wp_get_attachment_url( $id );
 	}
 
-	wp_enqueue_script('admin-gallery');
-	return wp_iframe( 'media_upload_gallery_form', $errors );
+	// Finally check to make sure the file has been saved, then return the html
+	if ( ! empty($src) ) {
+		$alt = isset($desc) ? esc_attr($desc) : '';
+		$html = "<img src='$src' alt='$alt' />";
+		return $html;
+	}
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Returns wp_iframe with the proper upload form for media uploads
+ * added to the media_upload filter in media-upload.php
  *
- * @since 2.5.0
+ * @since 3.5.0
  *
- * @return unknown
+ * @return HTML from the wp_iframe function
  */
-function media_upload_library() {
+function media_upload_form() {
 	$errors = array();
-	if ( !empty($_POST) ) {
+	$current_filter = current_filter();
+	$type = str_replace( 'media_upload_' , '' , $current_filter );
+	
+	if ( !empty($_POST) || empty($type) ) {
 		$return = media_upload_form_handler();
-
 		if ( is_string($return) )
 			return $return;
 		if ( is_array($return) )
 			$errors = $return;
 	}
 
-	return wp_iframe( 'media_upload_library_form', $errors );
+	wp_enqueue_script('admin-gallery');
+	
+	switch($type) {
+		case 'library' :
+				$return = 'media_upload_library_form';
+			break;
+		case 'gallery' :
+				$return = 'media_upload_gallery_form';
+			break;
+		default :
+				$return = 'wp_media_upload_handler';
+			break;				
+	}
+	return wp_iframe( $return , $errors );
 }
 
 /**
@@ -2100,10 +2158,63 @@
 add_filter( 'async_upload_video', 'get_media_item', 10, 2 );
 add_filter( 'async_upload_file',  'get_media_item', 10, 2 );
 
-add_action( 'media_upload_image', 'wp_media_upload_handler' );
-add_action( 'media_upload_audio', 'wp_media_upload_handler' );
-add_action( 'media_upload_video', 'wp_media_upload_handler' );
-add_action( 'media_upload_file',  'wp_media_upload_handler' );
+add_action( 'media_upload_image', 'media_upload_form' );
+add_action( 'media_upload_audio', 'media_upload_form' );
+add_action( 'media_upload_video', 'media_upload_form' );
+add_action( 'media_upload_file',  'media_upload_form' );
 
-add_filter( 'media_upload_gallery', 'media_upload_gallery' );
-add_filter( 'media_upload_library', 'media_upload_library' );
+add_filter( 'media_upload_gallery', 'media_upload_form' );
+add_filter( 'media_upload_library', 'media_upload_form' );
+
+
+// These can be deprecated ( if needed at all )
+
+/* 
+ * was only used once. Replaced media_upload_gallery() with media_upload( 'gallery' )
+ */
+function media_upload_gallery() {
+	media_upload( 'gallery' );
+}
+
+/* 
+ * was not used anywhere
+ */
+function media_upload_library() {
+	media_upload( 'library' );
+}
+
+/* 
+ * consolidated into new media_upload_form
+ */
+function media_upload_gallery() {
+	$errors = array();
+
+	if ( !empty($_POST) ) {
+		$return = media_upload_form_handler();
+
+		if ( is_string($return) )
+			return $return;
+		if ( is_array($return) )
+			$errors = $return;
+	}
+
+	wp_enqueue_script('admin-gallery');
+	return wp_iframe( 'media_upload_gallery_form', $errors );
+}
+
+/* 
+ * consolidated into new media_upload_form
+ */
+function media_upload_library() {
+	$errors = array();
+	if ( !empty($_POST) ) {
+		$return = media_upload_form_handler();
+
+		if ( is_string($return) )
+			return $return;
+		if ( is_array($return) )
+			$errors = $return;
+	}
+
+	return wp_iframe( 'media_upload_library_form', $errors );
+}
\ No newline at end of file
