Index: wp-admin/includes/media.php
===================================================================
--- wp-admin/includes/media.php	(revision 21425)
+++ wp-admin/includes/media.php	(working copy)
@@ -579,6 +579,39 @@
 }
 
 /**
+ * Download an image from the specified URL and attach it to a post, return the ID.
+ *
+ * @since 3.5.0
+ *
+ * @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 int|WP_Error ID of attachment on success
+ */
+function wp_sideload_image($file, $post_id, $desc = null) {
+	if ( empty( $file ) ) {
+		return new WP_Error( '', 'File URL cannot be empty.' );
+	}
+
+	// Download file to temp location
+	$tmp = download_url( $file );
+
+	// 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 error storing temporarily, unlink
+	if ( is_wp_error( $tmp ) ) {
+		@unlink($file_array['tmp_name']);
+		$file_array['tmp_name'] = '';
+	}
+
+	return media_handle_sideload( $file_array, $post_id, $desc );
+}
+
+/**
  * Download an image from the specified URL and attach it to a post.
  *
  * @since 2.6.0
@@ -589,36 +622,23 @@
  * @return string|WP_Error Populated HTML img tag on success
  */
 function media_sideload_image($file, $post_id, $desc = null) {
-	if ( ! empty($file) ) {
-		// Download file to temp location
-		$tmp = download_url( $file );
+	if ( empty( $file ) ) {
+		return new WP_Error( '', 'File URL cannot be empty.' );
+	}
 
-		// 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;
+	$id = wp_sideload_image( $file, $post_id, $desc );
 
-		// If error storing temporarily, unlink
-		if ( is_wp_error( $tmp ) ) {
-			@unlink($file_array['tmp_name']);
-			$file_array['tmp_name'] = '';
-		}
+	// If error storing permanently, unlink
+	if ( is_wp_error( $id ) ) {
+		@unlink( $file_array['tmp_name'] );
+		return $id;
+	}
 
-		// 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 );
 
-		$src = wp_get_attachment_url( $id );
-	}
-
 	// Finally check to make sure the file has been saved, then return the html
-	if ( ! empty($src) ) {
-		$alt = isset($desc) ? esc_attr($desc) : '';
+	if ( ! empty( $src ) ) {
+		$alt = isset( $desc ) ? esc_attr( $desc ) : '';
 		$html = "<img src='$src' alt='$alt' />";
 		return $html;
 	}
