+ function process_attachment($postdata, $remote_url) {
+ if ($this->fetch_attachments and $remote_url) {
+ printf( __('Importing attachment <em>%s</em>... '), htmlspecialchars($remote_url) );
+
+ // If the URL is absolute, but does not contain http, upload it assuming the base_site_url variable
+ if ( preg_match('/^\/[\w\W]+$/', $remote_url) )
+ $remote_url = rtrim($this->base_url,'/').$remote_url;
+
+ $upload = $this->fetch_remote_file($postdata, $remote_url);
+ if ( is_wp_error($upload) ) {
+ printf( __('Remote file error: %s'), htmlspecialchars($upload->get_error_message()) );
+ return $upload;
+ }
+ else {
+ print '('.size_format(filesize($upload['file'])).')';
+ }
+
+ if ( $info = wp_check_filetype($upload['file']) ) {
+ $postdata['post_mime_type'] = $info['type'];
+ }
+ else {
+ print __('Invalid file type');
+ return;
+ }
+
+ $postdata['guid'] = $upload['url'];
+
+ // as per wp-admin/includes/upload.php
+ $post_id = wp_insert_attachment($postdata, $upload['file']);
+ wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
+
+ // remap the thumbnail url. this isn't perfect because we're just guessing the original url.
+ if ( preg_match('@^image/@', $info['type']) && $thumb_url = wp_get_attachment_thumb_url($post_id) ) {
+ $parts = pathinfo($remote_url);
+ $ext = $parts['extension'];
+ $name = basename($parts['basename'], ".{$ext}");
+ $this->url_remap[$parts['dirname'] . '/' . $name . '.thumbnail.' . $ext] = $thumb_url;
+ }
+
+ return $post_id;
+ }
+ else {
+ printf( __('Skipping attachment <em>%s</em>'), htmlspecialchars($remote_url) );
+ }
+ }
+
+ function fetch_remote_file($post, $url) {
+ $upload = wp_upload_dir($post['post_date']);
+
+ // extract the file name and extension from the url
+ $file_name = basename($url);
+
+ // get placeholder file in the upload dir with a unique sanitized filename
+ $upload = wp_upload_bits( $file_name, 0, '', $post['post_date']);
+ if ( $upload['error'] ) {
+ echo $upload['error'];
+ return new WP_Error( 'upload_dir_error', $upload['error'] );
+ }
+
+ // fetch the remote url and write it to the placeholder file
+ $headers = wp_get_http($url, $upload['file']);
+
+ //Request failed
+ if ( ! $headers ) {
+ @unlink($upload['file']);
+ return new WP_Error( 'import_file_error', __('Remote server did not respond') );
+ }
+
+ // make sure the fetch was successful
+ if ( $headers['response'] != '200' ) {
+ @unlink($upload['file']);
+ return new WP_Error( 'import_file_error', sprintf(__('Remote file returned error response %1$d %2$s'), $headers['response'], get_status_header_desc($headers['response']) ) );
+ }
+ elseif ( isset($headers['content-length']) && filesize($upload['file']) != $headers['content-length'] ) {
+ @unlink($upload['file']);
+ return new WP_Error( 'import_file_error', __('Remote file is incorrect size') );
+ }
+
+ $max_size = $this->max_attachment_size();
+ if ( !empty($max_size) and filesize($upload['file']) > $max_size ) {
+ @unlink($upload['file']);
+ return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', size_format($max_size))) );
+ }
+
+ // keep track of the old and new urls so we can substitute them later
+ $this->url_remap[$url] = $upload['url'];
+ // if the remote url is redirected somewhere else, keep track of the destination too
+ if ( $headers['x-final-location'] != $url )
+ $this->url_remap[$headers['x-final-location']] = $upload['url'];
+
+ return $upload;
+
+ }