function control() { global $blog_id; $_POST = stripslashes_deep($_POST); // Strip out slashes added by wordpress $page = isset($_GET['page']) ? $_GET['page'] : null; $id = isset($_GET['id']) ? $_GET['id'] : null; $cmd = isset($_GET['cmd']) ? $_GET['cmd'] : null; $noheader = isset($_GET['noheader']) ? true : false; $to = add_query_arg(array('page' => $page, 'id' => $id)); switch ($page) { case 'ti_new': if (isset($_POST['button_file_upload']) || isset($_POST['button_file_url']) || isset($_POST['button_file_path'])) { // Create an import object $import = new TI_Import(); // File upload $input_file = isset($_FILES['input_file']) ? $_FILES['input_file'] : null; $server_url = isset($_POST['server_url']) ? $_POST['server_url'] : null; $server_path = isset($_POST['server_path']) ? $_POST['server_path'] : null; $result = $import->upload_file($input_file, $server_url, $server_path); if (is_wp_error($result)) { $error = $result->get_error_message(); break; } $this->get_template_from_columns($import); $import->save(); $to = add_query_arg(array('page' => 'ti_display', 'id' => $import->_id), $to); break; } if (!$noheader) { $this->view_header($page); $this->view_upload(); $this->view_footer(); } break; case 'ti_display': $import = TI_Import::get($id); if (is_wp_error($import) || !$import) { wp_die(sprintf(__('Internal error. Unable to read import with id=%s: %s'), $id, $import->get_error_message())); } // If the import has just finished - WP clean_term_cache() has internal bug: it uses a static variable // so cache cannot be cleared until a new page is displayed. See http://core.trac.wordpress.org/ticket/14485 // The ticket has been open for 17 months... if (isset($_GET['cache'])) { $import->clean_term_cache(); } // Process the import if (isset($_POST['button_process'])) { // Update the template with POST variables if (isset($_POST['template'])) { $import->template->update($_POST['template']); } $import->import(); $message = __("Import processed"); break; } // Save template if (isset($_POST['button_save_template'])) { $save_id = $_POST['template_save_id']; if (!$save_id) { $error = __('Enter a template name.'); break; } // Update import from $_POST and assign ID $import->template->update($_POST['template']); $import->template->_id = $save_id; // Save the import $result = $import->save(); if (is_wp_error($result)) { $error = __('Error saving import: ') . $result->get_error_message('ERROR'); break; } // Save the template $result = $import->template->save(); if (is_wp_error($result)) { $error = __('Error saving template: ') . $result->get_error_message('ERROR'); break; } // Everything was OK $message = __('Template saved.'); break; } // Load template if (isset($_POST['button_load_template'])) { $load_id = $_POST['template_load_id']; // If 'new template' then create a new one; otherwise load the specified template if (!$load_id) { $import->template = new TI_Template(); $this->get_template_from_columns($import); } else { $template = TI_Template::get($load_id); if (is_wp_error($template)) { $error = __('Error loading template: ') . $template->get_error_messages(); break; } else { $import->template = $template; } } // Assign the template to the import and save the import $result = $import->save(); if (is_wp_error($result)) { $error = __('Error saving import: ') . $result->get_error_messages(); break; } // Everything was OK $message = __('Template loaded'); break; } // Delete template if (isset($_POST['button_delete_template'])) { $result = TI_Template::delete($_POST['template_load_id']); if (is_wp_error($result)) { $error = __('Error deleting template: ') . $result->get_error_messages(); break; } // Set the import to a blank template and save the import $import->template = new TI_Template(); $result = $import->save(); if (is_wp_error($result)) { $error = __('Error saving import: ') . $result->get_error_messages(); break; } // Everything was OK $message = __('Template deleted'); break; } if (!$noheader) { $this->view_header($page, $import); $this->view_display($import); $this->view_footer($import); } break; case 'ti_history': // Handle commands from history section if ($noheader) { $to = add_query_arg(array('cmd' => null, 'id' => null), $to); switch ($cmd) { case 'delete': $result = TI_Import::delete($id); if (is_wp_error($result)) { $error = $result->get_error_message(); } else { $result = TI_Import_Line::delete($id); if (is_wp_error($result)) { $error = $result->get_error_message(); } else { $message = __('Import deleted.'); } } break; case 'undo': if (method_exists('TI_Pro', 'undo')) { $result = $this->undo($id); if (is_wp_error($result)) { $error = $result->get_error_message(); } else { $message = __('Undo complete. Check import log for any messages.'); } $to = add_query_arg(array('page' => 'ti_display', 'id' => $id), $to); } else { $error = __('Sorry, undo is only available in the PRO version'); } break; } } else { $this->view_header($page); $this->view_history(); $this->view_footer(); } break; case 'ti_settings': // Save settings if (isset($_POST['save']) && isset($_POST['turbocsv'])) { $options = shortcode_atts($this->option_defaults, $_POST['turbocsv']); update_option('turbocsv', $options); $message = __('Options saved'); break; } if (isset($_POST['reset'])) { $options = $this->option_defaults; update_option('turbocsv', $options); $message = __('Options reset'); break; } if (!$noheader) { $this->view_header($page); $this->view_settings(); $this->view_footer(); } break; } // Save any error/warning messages for display if (isset($error)) { update_option('turbocsv_error', $error); } if (isset($message)) { update_option('turbocsv_message', $message); } // Redirect to next page if needed if ($noheader) { $to = add_query_arg(array('noheader' => null), $to); wp_redirect($to); exit; } }
function import_line($line, $options) { global $wpdb, $wp_object_cache; $post = array(); // Post to be sent to WP $imported_post = new TI_Import_Line(); // Post details saved to the database after the import $update = false; // Whether this is an update // Get blog ID and switch to that blog if needed $post_blog_id = $this->import_blog_id($line); if (is_wp_error($post_blog_id)) { return $post_blog_id; } else { $imported_post->blog_id = $post_blog_id; } // Determine if this is an insert or an update (it's an update if a post ID was specified AND that post exists already) if ($this->template->postid_col) { // ID value to match $update_id = $this->read_line_col($line, $this->template->postid_col); if (is_wp_error($update_id)) { return $update_id; } // If column is not empty, then try to find the matching post ID if ($update_id) { // Field to match against $update_field = $this->template->update_field; // Try to find the post to be updated $found_id = $this->find_post_id($update_field, $update_id); if ($found_id) { $post['ID'] = $found_id; $update = true; } } } // Parse the tokens in the post body/title $result = $this->import_post_content($line, $post, $update); if (is_wp_error($result)) { return $result; } // Assign basic settings fields and validate them $result = $this->import_post_settings($line, $post); if (is_wp_error($result)) { return $result; } // Assign post dates $result = $this->import_post_date($line, $post, $update); if (is_wp_error($result)) { return $result; } // Taxonomies $result = $this->import_post_taxonomies($line, $options); if (is_wp_error($result)) { return $result; } elseif (!empty($result)) { $post['tax_input'] = $result; } // Update or insert the post // If revisions are active, this may return a new post ID if ($update) { $post_id = wp_update_post($post); } else { $post_id = wp_insert_post($post, true); } // Record memory usage after update/insert $memory_usage = memory_get_usage(); $this->max_memory = $memory_usage > $this->max_memory ? $memory_usage : $this->max_memory; // Flush the object cache - if this isn't done WordPress leaks 4k of memory with each new post. // Even with the flush, it's about 900B. $wp_object_cache->flush(); // Any error from posting is fatal. wp_insert gives a full error, wp_update just returns FALSE if (is_wp_error($post_id)) { return $this->log_line($post_id); } elseif ($post_id === false) { return $this->log_line(sprintf(__("Wordpress error when updating post id=%s"), $post_id)); } // If updating, remember revision post number for later undo if ($update) { // Figure out if revisions are supported and switched on if (defined('WP_POST_REVISIONS') && WP_POST_REVISIONS && !empty($post['post_type']) && post_type_supports($post['post_type'], 'revisions')) { // A revision was created, save it for later UNDO $imported_post->revision_id = $this->get_last_revision($post_id); } else { // If revision management not turned on, store FALSE in revision $imported_post->revision_id = false; } } // Post was OK - save the post data to the import; content and excerpt are NOT saved to conserve memory $imported_post->updated = $update; $imported_post->post_id = $post_id; $imported_post->import_id = $this->_id; $result = $imported_post->save(); if (is_wp_error($result)) { return $result; } // Featured image / thumbnail - update/insert after post is complete $result = $this->import_thumbnail($line, $post_id); if (is_wp_error($result)) { return $result; } // Update/insert post metadata after the post is complete $result = $this->import_post_meta($line, $post_id, $options, $update); if (is_wp_error($result)) { return $result; } return true; }