From 8b7b72d75eaaf6c940b867216fc4ac8c4af2e2e8 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 23 May 2014 21:33:14 +0000 Subject: [PATCH] In `media_sideload_image()`, check the returned value of `download_url()` earlier to avoid attempting to `unlink()` an instance of `WP_Error`. Props georgestephanis. Fixes #28204. Built from https://develop.svn.wordpress.org/trunk@28567 git-svn-id: http://core.svn.wordpress.org/trunk@28392 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/media.php | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 69b790d053..e5ccf1e104 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -800,28 +800,25 @@ function wp_media_upload_handler() { * @param string $desc Optional. Description of the image * @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 ); - +function media_sideload_image( $file, $post_id, $desc = null ) { + if ( ! empty( $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; + $file_array['name'] = basename( $matches[0] ); + // Download file to temp location + $file_array['tmp_name'] = download_url( $file ); - // If error storing temporarily, unlink - if ( is_wp_error( $tmp ) ) { - @unlink($file_array['tmp_name']); - $file_array['tmp_name'] = ''; + // If error storing temporarily, return the error. + if ( is_wp_error( $file_array['tmp_name'] ) ) { + return $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']); + if ( is_wp_error( $id ) ) { + @unlink( $file_array['tmp_name'] ); return $id; } @@ -829,8 +826,8 @@ function media_sideload_image($file, $post_id, $desc = null) { } // 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 = "$alt"; return $html; }