Ticket #5586: download-url-wp_error.diff

File download-url-wp_error.diff, 2.0 kB (added by DD32, 3 months ago)

Return WP_Error from download_url() on HTTP Errors

  • wp-admin/includes/file.php

    old new  
    192210* Downloads a url to a local file using the Snoopy HTTP Class 
    193211* 
    194212* @param string $url the URL of the file to download 
    195 * @return mixed false on failure, string Filename on success. 
     213* @return mixed WP_Error on failure, string Filename on success. 
    196214*/ 
    197215function download_url( $url ) { 
    198216        //WARNING: The file is not automatically deleted, The script must unlink() the file. 
    199217        if( ! $url ) 
    200                 return false
     218                return new WP_Error('http_no_url', __('Invalid URL Provided'))
    201219 
    202220        $tmpfname = tempnam(get_temp_dir(), 'wpupdate'); 
    203221        if( ! $tmpfname ) 
    204                 return false
     222                return new WP_Error('http_no_file', __('Could not create Temporary file'))
    205223 
    206224        $handle = @fopen($tmpfname, 'w'); 
    207225        if( ! $handle ) 
    208                 return false
     226                return new WP_Error('http_no_file', __('Could not create Temporary file'))
    209227 
    210228        require_once( ABSPATH . 'wp-includes/class-snoopy.php' ); 
    211229        $snoopy = new Snoopy(); 
    212230        $snoopy->fetch($url); 
    213231 
     232        if( $snoopy->status != '200' ){ 
     233                fclose($handle); 
     234                unlink($tmpfname); 
     235                return new WP_Error('http_404', trim($snoopy->response_code)); 
     236        } 
    214237        fwrite($handle, $snoopy->results); 
    215238        fclose($handle); 
    216239 
  • wp-admin/includes/update.php

    old new  
    176176        apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package)); 
    177177        $file = download_url($package); 
    178178 
    179         if ( !$file
    180                 return new WP_Error('download_failed', __('Download failed.')); 
     179        if ( is_wp_error($file)
     180                return new WP_Error('download_failed', __('Download failed.'), $file->get_error_message()); 
    181181 
    182182        $working_dir = $base . 'wp-content/upgrade/' . basename($plugin, '.php'); 
    183183