+
+ do_action('import_post_added', $post_id);
+ print "</li>\n";
+ }
+
+ function process_post_meta($post_id, $key, $value) {
+ // the filter can return false to skip a particular metadata key
+ $_key = apply_filters('import_post_meta_key', $key);
+ if ( $_key ) {
+ add_post_meta( $post_id, $_key, $value );
+ do_action('import_post_meta', $post_id, $_key, $value);
+ }
+ }
+
+ 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;
+
+ }
+
+ // sort by strlen, longest string first
+ function cmpr_strlen($a, $b) {
+ return strlen($b) - strlen($a);
+ }
+
+ // update url references in post bodies to point to the new local files
+ function backfill_attachment_urls() {
+
+ // make sure we do the longest urls first, in case one is a substring of another
+ uksort($this->url_remap, array(&$this, 'cmpr_strlen'));
+
+ global $wpdb;
+ foreach ($this->url_remap as $from_url => $to_url) {
+ // remap urls in post_content
+ $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, '%s', '%s')", $from_url, $to_url) );
+ // remap enclosure urls
+ $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, '%s', '%s') WHERE meta_key='enclosure'", $from_url, $to_url) );
+ }
+ }
+
+ // update the post_parent of orphans now that we know the local id's of all parents
+ function backfill_parents() {
+ global $wpdb;
+
+ foreach ($this->orphans as $child_id => $parent_id) {
+ $local_child_id = $this->post_ids_processed[$child_id];
+ $local_parent_id = $this->post_ids_processed[$parent_id];
+ if ($local_child_id and $local_parent_id) {
+ $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE ID = %d", $local_parent_id, $local_child_id));
+ }
+ }
+ }
+
+ function is_valid_meta_key($key) {
+ // skip attachment metadata since we'll regenerate it from scratch
+ if ( $key == '_wp_attached_file' || $key == '_wp_attachment_metadata' )
+ return false;
+ return $key;
+ }
+
+ // give the user the option of creating new users to represent authors in the import file?
+ function allow_create_users() {
+ return apply_filters('import_allow_create_users', true);
+ }
+
+ // give the user the option of downloading and importing attached files
+ function allow_fetch_attachments() {
+ return apply_filters('import_allow_fetch_attachments', true);
+ }
+
+ function max_attachment_size() {
+ // can be overridden with a filter - 0 means no limit
+ return apply_filters('import_attachment_size_limit', 0);
+ }
+
+ function import_start() {
+ wp_defer_term_counting(true);
+ wp_defer_comment_counting(true);
+ do_action('import_start');
+ }
+
+ function import_end() {
+ do_action('import_end');
+
+ // clear the caches after backfilling
+ foreach ($this->post_ids_processed as $post_id)
+ clean_post_cache($post_id);
+
+ wp_defer_term_counting(false);
+ wp_defer_comment_counting(false);
+ }
+
+ function import($id, $fetch_attachments = false) {
+ $this->id = (int) $id;
+ $this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments);
+
+ add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
+ $file = get_attached_file($this->id);
+ $this->import_file($file);