/** * 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; }