function process_attachment($post, $url)
{
if (!$this->fetch_attachments) {
return new WP_Error('attachment_processing_error', __('Fetching attachments is not enabled', 'wordpress-importer'));
}
// if the URL is absolute, but does not contain address, then upload it assuming base_site_url
if (preg_match('|^/[\\w\\W]+$|', $url)) {
$url = rtrim($this->base_url, '/') . $url;
}
$upload = $this->fetch_remote_file($url, $post);
if (is_wp_error($upload)) {
return $upload;
}
if ($info = wp_check_filetype($upload['file'])) {
$post['post_mime_type'] = $info['type'];
} else {
return new WP_Error('attachment_processing_error', __('Invalid file type', 'wordpress-importer'));
}
$post['guid'] = $upload['url'];
// as per wp-admin/includes/upload.php
$post_id = wp_insert_attachment($post, $upload['file']);
wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file']));
// remap resized image URLs, works by stripping the extension and remapping the URL stub.
if (preg_match('!^image/!', $info['type'])) {
$parts = pathinfo($url);
$name = basename($parts['basename'], ".{$parts['extension']}");
// PATHINFO_FILENAME in PHP 5.2
$parts_new = pathinfo($upload['url']);
$name_new = basename($parts_new['basename'], ".{$parts_new['extension']}");
$this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
}
return $post_id;
}