/** * 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); }
/** * Import theme files * Using Example flow_import(array('source1'=>array('method'=>'import','type'=>'theme','activate'=>false), * 'source2'=>array('method'=>'edit','type'=>'template_part')) * ); * @since 1.0.0 * @access public * @param array $sources * @return mixed */ public static function flow_import(array $sources) { if (empty($sources)) { return false; } include_once 'classes/import-export/class-tf-import.php'; include_once ABSPATH . 'wp-admin/includes/file.php'; include_once ABSPATH . 'wp-admin/includes/image.php'; WP_Filesystem(); global $wp_filesystem, $TF; $errors = array(); foreach ($sources as $path => $data) { if (!isset($data['method']) || !isset($data['type']) || !in_array($data['type'], array('theme', 'template', 'template_part')) || !in_array($data['method'], array('import', 'replace')) || !$wp_filesystem->exists($path)) { continue; } $file = wp_check_filetype($path); if (!isset($file['ext']) || !in_array($file['ext'], array('plain', 'rar', 'zip', 'xml'))) { continue; } $is_zip = FALSE; if ($file['ext'] == 'zip' || $file['ext'] == 'rar') { $tmp_dir = sys_get_temp_dir() . '/' . uniqid('flow') . '/'; $unzipfile = unzip_file($path, $tmp_dir); if (!$unzipfile) { $errors[$path] = 'There was an error unzipping the file ' . basename($path); continue; } $tmp_file = scandir($tmp_dir); if (empty($tmp_file)) { $errors[$path] = 'There was an error unzipping the file ' . basename($path); continue; } $xml_file = false; foreach ($tmp_file as $tmp) { if ($tmp != '.' && $tmp != '..' && $wp_filesystem->exists($tmp_dir . $tmp)) { $xml_file = $tmp; break; } } if (!$xml_file) { $errors[$path] = 'There was an error unzipping the file ' . basename($path); continue; } $xml = wp_check_filetype($tmp_dir . $xml_file, array('xml' => 'application/xml')); if ($xml['ext'] != 'xml') { $errors[$path] = 'The ' . $file['ext'] . ' file ' . basename($path) . ' doesn`t contain xml file'; continue; } $is_zip = true; $path = $tmp_dir . $xml_file; } $import = new TF_Import(); $posts = $import->parse($path); remove_filter('wp_unique_post_slug', array($TF->active_theme, 'add_prefix_post_slug'), 10, 6); if (isset($posts['posts']) && !empty($posts['posts'])) { $import->method = $data['method'] == 'import' ? 'add' : 'edit'; $import->source = $data['type']; $import->fetch_attachments = true; $import->edit_import_id = FALSE; $import->set_associated_theme_by_value = true; $errors[$path] = array(); if ($data['type'] == 'theme') { $import->exclude_theme_post = FALSE; if ($import->method == 'add') { $import->import($path); } else { foreach ($posts['posts'] as $post) { if ($post['post_type'] == 'tf_' . $data['type']) { $post_exists = TF_Model::post_exists($post['post_name']); if ($post_exists && get_post_type($post_exists) == $post['post_type']) { $import->edit_import_id = $post_exists; $import->import($path); } else { $errors[$path][] = $post['post_title'] . ' ' . $data['type'] . ' doesn`t exist.'; } break; } } } if (isset($data['activate']) && $data['activate'] && !$import->fails()) { $activate = $import->return_ID > 0 ? $import->return_ID : $import->edit_import_id; } } else { $import->exclude_theme_post = TRUE; $import->set_associated_theme_by_value = true; $import->get_authors_from_import($posts); $import->get_author_mapping(); wp_defer_term_counting(true); wp_defer_comment_counting(true); do_action('tf_import_start'); foreach ($posts['posts'] as $post) { if ('tf_' . $data['type'] == $post['post_type']) { $post_exists = TF_Model::post_exists($post['post_name']); $post_type = $post_exists ? get_post_type($post_exists) == $post['post_type'] : FALSE; $import->posts = array($post); if ($import->method == 'add') { if ($post_exists && $post_type) { $errors[$path][] = $post['post_title'] . ' ' . $data['type'] . ' already exist.'; } else { $import->processed_posts(); } } elseif ($post_exists && $post_type) { $import->edit_import_id = $post_exists; $import->process_replace_posts(); } else { $errors[$path][] = $post['post_title'] . ' ' . $data['type'] . ' doesn`t exists.'; } } } } if ($is_zip) { $wp_filesystem->delete($path); } $messages = $import->get_error_messages(); if (!empty($messages)) { foreach ($messages as $m) { $errors[$path][] = $m; } } elseif (empty($errors[$path])) { unset($errors[$path]); } } } if (isset($activate) && $activate) { global $TF_Theme, $TF; $TF_Theme->set_active_theme($activate); $TF->active_theme = new TF_Engine_Theme_Loader(); do_action('tf_import_end'); } return empty($errors) ? TRUE : $errors; }