Esempio n. 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;
     }
 }
Esempio n. 2
0
 function option($import, $field)
 {
     global $wpdb;
     switch ($field) {
         case 'template_load':
             $template_ids = TI_Template::get_list();
             if ($template_ids) {
                 $template_ids = array_combine($template_ids, $template_ids);
             }
             if (isset($template_ids)) {
                 $template_ids = array_merge(array('' => 'new template'), (array) $template_ids);
             } else {
                 $template_ids = array('' => 'new template');
             }
             $html = $this->dropdown('template_load_id', $import->template->_id, $template_ids) . "<input type='submit' class='button' name='button_load_template' value='" . __('Load') . "' />" . "<input type='submit' class='button' name='button_delete_template' value='" . __('Delete') . "' />";
             $this->field_setting('template_load', $html);
             break;
         case 'template_save':
             $html = "<input type='text' name='template_save_id' value='" . esc_attr($import->template->_id) . "' />" . "<input type='submit' class='button' name='button_save_template' value='" . __('Save') . "' />";
             $this->field_setting('template_save', $html);
             break;
         case 'blog_id':
             // Ignore this if it's not a multisite install
             if (!is_multisite()) {
                 return;
             }
             $query = "SELECT * FROM {$wpdb->blogs} ORDER BY {$wpdb->blogs}.site_id, {$wpdb->blogs}.blog_id ";
             $wp_blogs = $wpdb->get_results($query, OBJECT);
             $blog_ids = array();
             foreach ($wp_blogs as $wp_blog) {
                 $key = $wp_blog->blog_id;
                 $blog_ids[$key] = $wp_blog->path;
             }
             $dd_blog_ids = $this->dropdown('template[blog_id]', $import->template->blog_id, $blog_ids, true);
             $this->basic_option($import, 'template[blog_id_col]', $import->template->blog_id_col, $dd_blog_ids, 'blog_id');
             break;
         case 'post_title':
             foreach ((array) $import->headers as $header => $token) {
                 $tokens[] = "<a href='#'>" . esc_attr($token) . "</a>";
             }
             $html = __('Click on the title, body or excerpt field, then click on the column name to insert it') . "<br/>" . "<div id='ti_tokens'>" . implode(' | ', $tokens) . "</div>";
             $this->field_setting('', $html);
             $html = "<input id='post_title' class='ti-token-field ti-post-title' type='text' value='" . esc_attr($import->template->post_title) . "' name='template[post_title]'/>";
             $this->field_setting('post_title', $html);
             break;
         case 'post_content':
             $label = $this->labels['post_content']['label'];
             $tip = $this->labels['post_content']['tip'];
             echo "<tr><th scope='row'>";
             echo "<abbr style='cursor:help' class='ttip' title='" . esc_attr($tip) . "'>{$label}</abbr> ";
             echo "</th>";
             echo "<td style='vertical-align:top'>";
             if (function_exists('wp_editor')) {
                 wp_editor($import->template->post_content, 'ti_post_content', array('textarea_name' => 'template[post_content]', 'media_buttons' => false));
             } else {
                 echo "<textarea id='ti_post-content' name='template[post_content]'>" . $import->template->post_content . "</textarea>";
             }
             echo "</td>";
             echo "</tr>";
             break;
         case 'post_excerpt':
             $html = "<textarea id='post_excerpt' name='template[post_excerpt]' class='ti-token-field ti-post-excerpt'  rows='3' >" . esc_attr($import->template->post_excerpt) . "</textarea>";
             $this->field_setting('post_excerpt', $html);
             break;
         case 'post_type':
             $wp_post_types = get_post_types(array('public' => true, '_builtin' => false), 'names', 'and');
             $post_types = array('post' => 'post', 'page' => 'page');
             if (!empty($wp_post_types)) {
                 $post_types = array_merge($post_types, array_combine($wp_post_types, $wp_post_types));
             }
             $dd_post_types = $this->dropdown('template[post_type]', $import->template->post_type, $post_types, true);
             $this->basic_option($import, 'template[post_type_col]', $import->template->post_type_col, $dd_post_types, 'post_type');
             break;
         case 'post_parent':
             $dd_pages = wp_dropdown_pages(array('echo' => false, 'name' => 'template[post_parent]', 'selected' => $import->template->post_parent, 'show_option_none' => __('Main Page (no parent)'), 'sort_column' => 'menu_order, post_title', 'show_option_none' => ' ', 'show_option_none_value' => ''));
             $this->basic_option($import, 'template[post_parent_col]', $import->template->post_parent_col, $dd_pages, 'post_parent');
             break;
         case 'page_template':
             $wp_templates = get_page_templates();
             $templates = array();
             // WP returns the templates as name => key, transpose to key => name and put the key in parentheses
             foreach ($wp_templates as $value => $key) {
                 $templates[$key] = $value . " ({$key})";
             }
             asort($templates);
             // Insert a 'default' entry
             $templates = array_merge(array('default' => 'Default Template (default)'), $templates);
             $dd_templates = $this->dropdown('template[page_template]', $import->template->page_template, $templates, true);
             $this->basic_option($import, 'template[page_template_col]', $import->template->page_template_col, $dd_templates, 'page_template');
             break;
         case 'menu_order':
             $orders = array_combine(range("1", "99"), range("1", "99"));
             $dd_order = $this->dropdown('template[menu_order]', (int) $import->template->menu_order, $orders, true);
             $this->basic_option($import, 'template[menu_order_col]', $import->template->menu_order_col, $dd_order, 'menu_order');
             break;
         case 'post_status':
             $dd_statuses = $this->dropdown('template[post_status]', $import->template->post_status, array('publish' => 'Published', 'draft' => 'Draft', 'pending' => 'Pending Review'), true);
             $this->basic_option($import, 'template[post_status_col]', $import->template->post_status_col, $dd_statuses, 'post_status');
             break;
         case 'comment_status':
             $dd_statuses = $this->dropdown('template[comment_status]', $import->template->comment_status, array('open' => 'open', 'closed' => 'closed'), true);
             $this->basic_option($import, 'template[comment_status_col]', $import->template->comment_status_col, $dd_statuses, 'comment_status');
             break;
         case 'post_author':
             $args = array('orderby' => 'display_name', 'order' => 'ASC', 'blog_id' => $GLOBALS['blog_id'], 'who' => '');
             $user_objs = get_users($args);
             foreach ($user_objs as $user) {
                 $users[$user->ID] = $user->user_login . " (" . $user->ID . ")";
             }
             $dd_users = $this->dropdown('template[post_author]', (int) $import->template->post_author, $users, true);
             $this->basic_option($import, 'template[post_author_col]', $import->template->post_author_col, $dd_users, 'post_author');
             break;
         case 'post_name':
             $slug = "<input type='text' name='template[post_name]' value='" . esc_attr($import->template->post_name) . "' size='10' />";
             $this->basic_option($import, 'template[post_name_col]', $import->template->post_name_col, $slug, 'post_name');
             break;
         case 'post_date':
             $dates = "<input type='text' name='template[post_date_min]' value='" . esc_attr($import->template->post_date_min) . "' size='10' />" . " to <input type='text' name='template[post_date_max]' value='" . esc_attr($import->template->post_date_max) . "' size='10' />";
             $this->basic_option($import, 'template[post_date_col]', $import->template->post_date_col, $dates, 'post_date');
             break;
         case 'thumbnail':
             $html = $this->dropdown_headers($import, 'template[thumbnail_col]', $import->template->thumbnail_col);
             $this->field_setting('thumbnail', $html);
             break;
         case 'postid':
             // Input column
             $html = $this->dropdown_headers($import, 'template[postid_col]', $import->template->postid_col);
             $html .= ' ' . __('search in') . ': ';
             // Matching WordPress field
             $meta_keys = $this->get_meta_keys();
             $dd = array('' => 'Post ID');
             foreach ($meta_keys as $meta_key) {
                 $dd[$meta_key] = $meta_key;
             }
             $html .= $this->dropdown('template[update_field]', $import->template->update_field, $dd, false);
             $this->field_setting('postid', $html);
             break;
         case 'taxonomies':
             // Output custom taxonomies
             $taxonomies = array_merge(array('category', 'post_tag'), get_taxonomies(array('_builtin' => false)));
             foreach ($taxonomies as $taxonomy_name) {
                 $max_terms = 200;
                 // MAX # terms to return - some unusual blogs have many thousands
                 $wp_taxonomy = get_taxonomy($taxonomy_name);
                 // If the taxonomy no longer exists in WordPress (i.e. it was deleted), then just skip it
                 if (!$wp_taxonomy) {
                     continue;
                 }
                 // If the taxonomy is not yet part of the template, add it
                 if (!isset($import->template->taxonomies[$taxonomy_name])) {
                     $import->template->taxonomies[$taxonomy_name] = array('col' => null, 'values' => array());
                 }
                 // Get all the terms in the taxonomy (up to the maximum #)
                 $wp_terms = get_terms($taxonomy_name, array('number' => $max_terms, 'hide_empty' => false, 'orderby' => 'id'));
                 // Add a hierarchical description (with dashes) and sort by the 'slug'
                 $terms = $this->get_term_list($wp_terms);
                 $terms_dd = array();
                 foreach ($terms as $term_id => $term) {
                     $terms_dd[$term_id] = $term['description'];
                 }
                 $values = isset($import->template->taxonomies[$taxonomy_name]['values']) ? $import->template->taxonomies[$taxonomy_name]['values'] : array();
                 $col = isset($import->template->taxonomies[$taxonomy_name]['col']) ? $import->template->taxonomies[$taxonomy_name]['col'] : null;
                 $dd_terms = $this->dropdown("template[taxonomies][{$taxonomy_name}][values][]", $values, $terms_dd, false, true);
                 $this->basic_option($import, "template[taxonomies][{$taxonomy_name}][col]", $col, $dd_terms, 'taxonomies', $wp_taxonomy->labels->name);
             }
             break;
         case 'post_custom':
             $meta_keys = $this->get_meta_keys();
             $meta_keys_data = $meta_keys && is_array($meta_keys) ? array_combine(array_values($meta_keys), array_values($meta_keys)) : array();
             $columns_data = $meta_keys && is_array($meta_keys) ? array_combine(array_values($import->headers), array_values($import->headers)) : array();
             // Add a blank row for new entries
             $custom_keys = $import->template->post_custom ? array_merge($import->template->post_custom, array('' => '')) : array('' => '');
             $html = "<table class='edt'><tbody>";
             foreach ($custom_keys as $key => $col) {
                 $meta_keys_dd = $this->dropdown("template[post_custom][key][]", $key, $meta_keys_data, true);
                 $columns_dd = $this->dropdown("template[post_custom][col][]", $col, $columns_data, true);
                 $html .= "<tr><td>";
                 $html .= $columns_dd . ' ' . __('is mapped to') . ': ' . $meta_keys_dd;
                 $html .= "</td></tr>";
             }
             $html .= "</tbody></table>";
             $this->setting(__('Existing custom fields'), $html, $this->labels['post_custom']['tip']);
             break;
         case 'post_custom_new':
             $existing_metas = $this->get_meta_keys();
             $new_metas = array();
             // 'New' custom fields are any headers that are not already mapped and that don't begin with '!'
             foreach ($import->headers as $key => $header) {
                 if (!in_array($header, (array) $existing_metas) && substr($header, 0, 1) != '!') {
                     $new_metas[] = $header;
                 }
             }
             // If there are no new custom fields then don't show this section
             if (count($new_metas) == 0) {
                 break;
             }
             // The table contains a list of column names; convert it to a table of column name => column name
             $new_metas = array_combine(array_values($new_metas), array_values($new_metas));
             $dd_custom_new = $this->dropdown("template[post_custom_new][]", $import->template->post_custom_new, $new_metas, false, true);
             $this->setting(__('Create new custom fields'), $dd_custom_new, $this->labels['post_custom_new']['tip']);
             break;
     }
 }