Ticket #19629: 19629.3.diff
File 19629.3.diff, 2.8 KB (added by , 11 years ago) |
---|
-
wp-admin/includes/media.php
312 312 * 313 313 * @since 2.6.0 314 314 * 315 * @param array $file_array Array similar to a {@link $_FILES} upload array315 * @param string|array $file A URL as a string, or an array similar to a {@link $_FILES} upload array 316 316 * @param int $post_id The post ID the media is associated with 317 317 * @param string $desc Description of the sideloaded file 318 318 * @param array $post_data allows you to overwrite some of the attachment 319 319 * @return int|object The ID of the attachment or a WP_Error on failure 320 320 */ 321 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) { 321 function media_handle_sideload( $file, $post_id, $desc = null, $post_data = array()) { 322 $downloading_url = is_string( $file ); 323 324 if ( $downloading_url ) { 325 // Download file to temp location 326 $tmp = download_url( $file ); 327 328 if ( is_wp_error( $tmp ) ) 329 return $tmp; 330 331 $file_array = array( 332 'name' => preg_replace( '/\?.*/', '', $file ), // Strip query string (might need to be better) 333 'tmp_name' => $tmp, 334 ); 335 } 336 322 337 $overrides = array('test_form'=>false); 323 338 324 339 $time = current_time( 'mysql' ); … … 328 343 } 329 344 330 345 $file = wp_handle_sideload( $file_array, $overrides, $time ); 331 if ( isset($file['error']) ) 346 if ( isset( $file['error'] ) ) { 347 if ( $downloading_url ) 348 @unlink( $tmp ); 332 349 return new WP_Error( 'upload_error', $file['error'] ); 350 } 333 351 334 352 $url = $file['url']; 335 353 $type = $file['type']; … … 660 678 * @return string|WP_Error Populated HTML img tag on success 661 679 */ 662 680 function media_sideload_image($file, $post_id, $desc = null) { 663 if ( ! empty($file) ) {664 // Download file to temp location665 $tmp = download_url( $file);681 if ( ! $file ) 682 return; 683 $id = media_handle_sideload( $file, $post_id, $desc ); 666 684 667 // Set variables for storage 668 // fix file filename for query strings 669 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches ); 670 $file_array['name'] = basename($matches[0]); 671 $file_array['tmp_name'] = $tmp; 672 673 // If error storing temporarily, unlink 674 if ( is_wp_error( $tmp ) ) { 675 @unlink($file_array['tmp_name']); 676 $file_array['tmp_name'] = ''; 677 } 678 679 // do the validation and storage stuff 680 $id = media_handle_sideload( $file_array, $post_id, $desc ); 681 // If error storing permanently, unlink 682 if ( is_wp_error($id) ) { 683 @unlink($file_array['tmp_name']); 684 return $id; 685 } 686 687 $src = wp_get_attachment_url( $id ); 688 } 689 690 // Finally check to make sure the file has been saved, then return the html 691 if ( ! empty($src) ) { 685 if ( $src = wp_get_attachment_url( $id ) ) { 692 686 $alt = isset($desc) ? esc_attr($desc) : ''; 693 687 $html = "<img src='$src' alt='$alt' />"; 694 688 return $html;