/** * Create new posts based on import information * * Posts marked as having a parent which doesn't exist will become top level items. * Doesn't create a new post if: the post type doesn't exist, the given post ID * is already noted as imported or a post with the same title and date already exists. * Note that new/updated terms, comments and meta are imported for the last of the above. */ function process_replace_posts() { $this->posts = apply_filters('tf_import_posts', $this->posts); foreach ($this->posts as $post) { $post = apply_filters('tf_import_post_data_raw', $post); if (!post_type_exists($post['post_type'])) { $this->errors[] = sprintf(__('Failed to import "%s": Invalid post type %s', 'themify-flow'), esc_html($post['post_title']), esc_html($post['post_type'])); do_action('tf_import_post_exists', $post); continue; } if (isset($this->processed_posts[$post['post_id']]) && !empty($post['post_id'])) { continue; } if ($post['status'] == 'auto-draft') { continue; } if ('nav_menu_item' == $post['post_type']) { continue; } $post_type_object = get_post_type_object($post['post_type']); if ('add' == $this->method && 'theme' != $this->source && 'tf_theme' != $post['post_type']) { $post['post_name'] = TF_Model::replace_theme_prefix_slug($post['post_name']); } // Replace Theme > Templates and Parts if ('theme' == $this->source && 'tf_theme' != $post['post_type']) { $the_original_theme = get_post($this->edit_import_id); $post['post_name'] = TF_Model::replace_theme_prefix_slug($post['post_name'], $the_original_theme->post_name); } if ('theme' == $this->source && 'tf_theme' == $post['post_type']) { $post_exists = $this->edit_import_id; } else { if ('theme' != $this->source && 'tf_theme' != $post['post_type']) { $post_exists = $this->edit_import_id; } else { $post_exists = TF_Model::post_exists($post['post_name']); } } $post_parent = (int) $post['post_parent']; if ($post_parent) { // if we already know the parent, map it to the new local ID if (isset($this->processed_posts[$post_parent])) { $post_parent = $this->processed_posts[$post_parent]; // otherwise record the parent for later } else { $this->post_orphans[intval($post['post_id'])] = $post_parent; $post_parent = 0; } } // map the post author $author = sanitize_user($post['post_author'], true); if (isset($this->author_mapping[$author])) { $author = $this->author_mapping[$author]; } else { $author = (int) get_current_user_id(); } $postdata = array('import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], 'post_status' => $post['status'], 'post_name' => $post['post_name'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], 'post_type' => $post['post_type'], 'post_password' => $post['post_password']); $original_post_ID = $post['post_id']; $postdata = apply_filters('tf_import_post_data_processed', $postdata, $post); if ('attachment' == $postdata['post_type']) { $remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; // try to use _wp_attached file for upload folder placement to ensure the same location as the export site // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() $postdata['upload_date'] = $post['post_date']; if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { if ($meta['key'] == '_wp_attached_file') { if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches)) { $postdata['upload_date'] = $matches[0]; } break; } } } if ($post_exists) { $post_id = $post_exists; } else { $post_id = $this->process_attachment($postdata, $remote_url); } } else { do_action('tf_import_before_insert_post', $postdata, $this->source); if ($post_exists) { $postdata['ID'] = $post_exists; $post_id = $post_exists; if (!$this->set_associated_theme_by_value) { unset($postdata['post_name']); unset($postdata['post_title']); } wp_update_post($postdata); } else { $post_id = wp_insert_post($postdata, true); } // Assign associated theme with current theme active if (in_array($this->source, array('template', 'template_part')) && in_array($post['post_type'], array('tf_template', 'tf_template_part'))) { global $TF; update_post_meta($post_id, 'associated_theme', $TF->active_theme->slug); } do_action('tf_import_insert_post', $post_id, $original_post_ID, $postdata, $post); } if (is_wp_error($post_id)) { $message = sprintf(__('Failed to import %s “%s”', 'themify-flow'), $post_type_object->labels->singular_name, esc_html($post['post_title'])); if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { $message .= ': ' . $post_id->get_error_message(); } $this->errors[] = $message; continue; } if ($post['is_sticky'] == 1) { stick_post($post_id); } // map pre-import ID to local ID $this->processed_posts[intval($post['post_id'])] = (int) $post_id; if (!isset($post['postmeta'])) { $post['postmeta'] = array(); } $post['postmeta'] = apply_filters('tf_import_post_meta', $post['postmeta'], $post_id, $post); // add/update post meta if (!empty($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { $key = apply_filters('tf_import_post_meta_key', $meta['key'], $post_id, $post); $value = false; if ('_edit_last' == $key) { if (isset($this->processed_authors[intval($meta['value'])])) { $value = $this->processed_authors[intval($meta['value'])]; } else { $key = false; } } if ($key) { // export gets meta straight from the DB so could have a serialized string if (!$value) { $value = maybe_unserialize($meta['value']); } // Replace Theme > Templates and Parts if ('associated_theme' == $key && 'theme' == $this->source && 'tf_theme' != $post['post_type']) { if ($this->set_associated_theme_by_value) { update_post_meta($post_id, 'associated_theme', $value); } elseif (isset($the_original_theme) && is_object($the_original_theme)) { update_post_meta($post_id, 'associated_theme', $the_original_theme->post_name); } } else { if ('theme' == $this->source && 'tf_theme' != $post['post_type'] && in_array($key, array('tf_template_region_header', 'tf_template_region_sidebar', 'tf_template_region_footer')) && !empty($value)) { $new_meta_value = TF_Model::replace_theme_prefix_slug($value, $the_original_theme->post_name); if ($this->set_associated_theme_by_value) { update_post_meta($post_id, $key, $value); } elseif (isset($the_original_theme) && is_object($the_original_theme)) { update_post_meta($post_id, $key, $new_meta_value); } } else { update_post_meta($post_id, $key, $value); } } do_action('tf_import_post_meta', $post_id, $key, $value); // if the post has a featured image, take note of this in case of remap if ('_thumbnail_id' == $key) { $this->featured_images[$post_id] = (int) $value; } } } } } unset($this->posts); }