Ticket #19629: 19629.2.diff
File 19629.2.diff, 2.8 KB (added by , 12 years ago) |
---|
-
wp-admin/includes/media.php
260 260 * 261 261 * @since 2.6.0 262 262 * 263 * @param array $file_array Array similar to a {@link $_FILES} upload array263 * @param string|array $file A URL as a string, or an array similar to a {@link $_FILES} upload array 264 264 * @param int $post_id The post ID the media is associated with 265 265 * @param string $desc Description of the sideloaded file 266 266 * @param array $post_data allows you to overwrite some of the attachment 267 267 * @return int|object The ID of the attachment or a WP_Error on failure 268 268 */ 269 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) { 269 function media_handle_sideload( $file, $post_id, $desc = null, $post_data = array()) { 270 $downloading_url = is_string( $file ); 271 272 if ( $downloading_url ) { 273 // Download file to temp location 274 $tmp = download_url( $file ); 275 276 if ( is_wp_error( $tmp ) ) 277 return $tmp; 278 279 $file_array = array( 280 'name' => preg_replace( '/?.*/', '', $file ), // Strip query string (might need to be better) 281 'tmp_name' => $tmp, 282 ); 283 } 284 270 285 $overrides = array('test_form'=>false); 271 286 272 287 $time = current_time( 'mysql' ); … … 276 291 } 277 292 278 293 $file = wp_handle_sideload( $file_array, $overrides, $time ); 279 if ( isset($file['error']) ) 294 if ( isset( $file['error'] ) ) { 295 if ( $downloading_url ) 296 @unlink( $tmp ); 280 297 return new WP_Error( 'upload_error', $file['error'] ); 298 } 281 299 282 300 $url = $file['url']; 283 301 $type = $file['type']; … … 598 616 * @return string|WP_Error Populated HTML img tag on success 599 617 */ 600 618 function media_sideload_image($file, $post_id, $desc = null) { 601 if ( ! empty($file) ) {602 // Download file to temp location603 $tmp = download_url( $file);619 if ( ! $file ) 620 return; 621 $id = media_handle_sideload( $file, $post_id, $desc ); 604 622 605 // Set variables for storage 606 // fix file filename for query strings 607 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches ); 608 $file_array['name'] = basename($matches[0]); 609 $file_array['tmp_name'] = $tmp; 610 611 // If error storing temporarily, unlink 612 if ( is_wp_error( $tmp ) ) { 613 @unlink($file_array['tmp_name']); 614 $file_array['tmp_name'] = ''; 615 } 616 617 // do the validation and storage stuff 618 $id = media_handle_sideload( $file_array, $post_id, $desc ); 619 // If error storing permanently, unlink 620 if ( is_wp_error($id) ) { 621 @unlink($file_array['tmp_name']); 622 return $id; 623 } 624 625 $src = wp_get_attachment_url( $id ); 626 } 627 628 // Finally check to make sure the file has been saved, then return the html 629 if ( ! empty($src) ) { 623 if ( $src = wp_get_attachment_url( $id ) ) { 630 624 $alt = isset($desc) ? esc_attr($desc) : ''; 631 625 $html = "<img src='$src' alt='$alt' />"; 632 626 return $html;