Make WordPress Core

Changeset 28567


Ignore:
Timestamp:
05/23/2014 09:32:12 PM (11 years ago)
Author:
wonderboymusic
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/media.php

    r28497 r28567  
    801801 * @return string|WP_Error Populated HTML img tag on success
    802802 */
    803 function media_sideload_image($file, $post_id, $desc = null) {
    804     if ( ! empty($file) ) {
    805         // Download file to temp location
    806         $tmp = download_url( $file );
    807 
     803function media_sideload_image( $file, $post_id, $desc = null ) {
     804    if ( ! empty( $file ) ) {
    808805        // Set variables for storage
    809806        // fix file filename for query strings
    810807        preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
    811         $file_array['name'] = basename($matches[0]);
    812         $file_array['tmp_name'] = $tmp;
    813 
    814         // If error storing temporarily, unlink
    815         if ( is_wp_error( $tmp ) ) {
    816             @unlink($file_array['tmp_name']);
    817             $file_array['tmp_name'] = '';
     808        $file_array['name'] = basename( $matches[0] );
     809        // Download file to temp location
     810        $file_array['tmp_name'] = download_url( $file );
     811
     812        // If error storing temporarily, return the error.
     813        if ( is_wp_error( $file_array['tmp_name'] ) ) {
     814            return $file_array['tmp_name'];
    818815        }
    819816
     
    821818        $id = media_handle_sideload( $file_array, $post_id, $desc );
    822819        // If error storing permanently, unlink
    823         if ( is_wp_error($id) ) {
    824             @unlink($file_array['tmp_name']);
     820        if ( is_wp_error( $id ) ) {
     821            @unlink( $file_array['tmp_name'] );
    825822            return $id;
    826823        }
     
    830827
    831828    // Finally check to make sure the file has been saved, then return the html
    832     if ( ! empty($src) ) {
    833         $alt = isset($desc) ? esc_attr($desc) : '';
     829    if ( ! empty( $src ) ) {
     830        $alt = isset( $desc ) ? esc_attr( $desc ) : '';
    834831        $html = "<img src='$src' alt='$alt' />";
    835832        return $html;
Note: See TracChangeset for help on using the changeset viewer.