Ticket #19629: wp-sideload-media-id-ak.diff
File wp-sideload-media-id-ak.diff, 2.8 KB (added by , 12 years ago) |
---|
-
wp-admin/includes/media.php
579 579 } 580 580 581 581 /** 582 * Download an image from the specified URL and attach it to a post, return the ID. 583 * 584 * @since 3.5.0 585 * 586 * @param string $file The URL of the image to download 587 * @param int $post_id The post ID the media is to be associated with 588 * @param string $desc Optional. Description of the image 589 * @return int|WP_Error ID of attachment on success 590 */ 591 function media_sideload_image_id($file, $post_id, $desc = null) { 592 if ( empty( $file ) ) { 593 return new WP_Error( '', 'File URL cannot be empty.' ); 594 } 595 596 // Download file to temp location 597 $tmp = download_url( $file ); 598 599 // Set variables for storage 600 // fix file filename for query strings 601 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches ); 602 $file_array['name'] = basename($matches[0]); 603 $file_array['tmp_name'] = $tmp; 604 605 // If error storing temporarily, unlink 606 if ( is_wp_error( $tmp ) ) { 607 @unlink($file_array['tmp_name']); 608 $file_array['tmp_name'] = ''; 609 } 610 611 return media_handle_sideload( $file_array, $post_id, $desc ); 612 } 613 614 /** 582 615 * Download an image from the specified URL and attach it to a post. 583 616 * 584 617 * @since 2.6.0 … … 589 622 * @return string|WP_Error Populated HTML img tag on success 590 623 */ 591 624 function media_sideload_image($file, $post_id, $desc = null) { 592 if ( ! empty($file) ) {593 // Download file to temp location594 $tmp = download_url( $file );625 if ( empty( $file ) ) { 626 return new WP_Error( '', 'File URL cannot be empty.' ); 627 } 595 628 596 // Set variables for storage 597 // fix file filename for query strings 598 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches ); 599 $file_array['name'] = basename($matches[0]); 600 $file_array['tmp_name'] = $tmp; 629 $id = media_sideload_image_id( $file, $post_id, $desc ); 601 630 602 // If error storing temporarily, unlink603 if ( is_wp_error( $tmp) ) {604 @unlink($file_array['tmp_name']);605 $file_array['tmp_name'] = '';606 631 // If error storing permanently, unlink 632 if ( is_wp_error( $id ) ) { 633 @unlink( $file_array['tmp_name'] ); 634 return $id; 635 } 607 636 608 // do the validation and storage stuff 609 $id = media_handle_sideload( $file_array, $post_id, $desc ); 610 // If error storing permanently, unlink 611 if ( is_wp_error($id) ) { 612 @unlink($file_array['tmp_name']); 613 return $id; 614 } 637 $src = wp_get_attachment_url( $id ); 615 638 616 $src = wp_get_attachment_url( $id );617 }618 619 639 // Finally check to make sure the file has been saved, then return the html 620 if ( ! empty( $src) ) {621 $alt = isset( $desc) ? esc_attr($desc) : '';640 if ( ! empty( $src ) ) { 641 $alt = isset( $desc ) ? esc_attr( $desc ) : ''; 622 642 $html = "<img src='$src' alt='$alt' />"; 623 643 return $html; 624 644 }