/** * Handles import data before queuing * * Ensures the import record source name is accurate, checks for errors, and limits import items * based on selection * * @param array $data Import data * * @return array|WP_Error */ public function prep_import_data($data = array()) { if (empty($this->meta['finalized'])) { return tribe_error('core:aggregator:record-not-finalized'); } // if $data is an object already, don't attempt to manipulate it into an importer object if (is_object($data)) { return $data; } // if $data is empty, grab the data from meta if (empty($data)) { $data = $this->meta; } $record = Tribe__Events__Aggregator__Records::instance()->get_by_import_id($data['import_id']); if (empty($data['column_map'])) { return tribe_error('core:aggregator:missing-csv-column-map'); } $content_type = $this->get_csv_content_type(); update_option('tribe_events_import_column_mapping_' . $content_type, $data['column_map']); try { $importer = $this->get_importer(); } catch (RuntimeException $e) { return tribe_error('core:aggregator:missing-csv-file'); } if (!empty($data['category'])) { $importer = $this->maybe_set_default_category($importer); } if (!empty($data['post_status'])) { $importer = $this->maybe_set_default_post_status($importer); } $required_fields = $importer->get_required_fields(); $missing = array_diff($required_fields, $data['column_map']); if (!empty($missing)) { $mapper = new Tribe__Events__Importer__Column_Mapper($content_type); /** * @todo allow to overwrite the default message */ $message = '<p>' . esc_html__('The following fields are required for a successful import:', 'the-events-calendar') . '</p>'; $message .= '<ul style="list-style-type: disc; margin-left: 1.5em;">'; foreach ($missing as $key) { $message .= '<li>' . $mapper->get_column_label($key) . '</li>'; } $message .= '</ul>'; return new WP_Error('csv-invalid-column-mapping', $message); } update_option('tribe_events_import_column_mapping_' . $content_type, $data['column_map']); return $importer; }
private function handle_column_mapping() { // Deconstruct mapping. if (empty($_POST['column_map'])) { return false; } $column_mapping = $_POST['column_map']; try { $importer = $this->get_importer(); } catch (RuntimeException $e) { $this->errors[] = esc_html__('The file went away. Please try again.', 'the-events-calendar'); return false; } $required_fields = $importer->get_required_fields(); $missing = array_diff($required_fields, $column_mapping); if (!empty($missing)) { $mapper = new Tribe__Events__Importer__Column_Mapper(get_option('tribe_events_import_type')); $message = '<p>' . esc_html__('The following fields are required for a successful import:', 'the-events-calendar') . '</p>'; $message .= '<ul style="list-style-type: disc; margin-left: 1.5em;">'; foreach ($missing as $key) { $message .= '<li>' . $mapper->get_column_label($key) . '</li>'; } $message .= '</ul>'; $this->errors[] = $message; return false; } update_option('tribe_events_import_column_mapping', $column_mapping); return true; }