Exemple #1
0
 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;
     }
 }