public function render() { $aggregator = Tribe__Events__Aggregator::instance(); $event_id = get_the_ID(); $record = Tribe__Events__Aggregator__Records::instance()->get_by_event_id($event_id); $last_import = null; $source = null; $origin = null; if (is_wp_error($record)) { $last_import = get_post_meta($event_id, Tribe__Events__Aggregator__Event::$updated_key, true); $source = get_post_meta($event_id, Tribe__Events__Aggregator__Event::$source_key, true); $origin = get_post_meta($event_id, Tribe__Events__Aggregator__Event::$origin_key, true); } else { $last_import = $record->post->post_modified; $source_info = $record->get_source_info(); $source = $source_info['title']; $origin = $record->origin; } $origin = $aggregator->api('origins')->get_name($origin); $datepicker_format = Tribe__Date_Utils::datepicker_formats(tribe_get_option('datepickerFormat')); $last_import = $last_import ? tribe_format_date($last_import, true, $datepicker_format . ' h:i a') : null; $settings_link = Tribe__Settings::instance()->get_url(array('tab' => 'imports')); $import_setting = tribe_get_option('tribe_aggregator_default_update_authority', Tribe__Events__Aggregator__Settings::$default_update_authority); include Tribe__Events__Main::instance()->plugin_path . 'src/admin-views/aggregator/meta-box.php'; }
protected function create_post(array $record) { $event = $this->build_event_array(false, $record); $id = Tribe__Events__API::createEvent($event); if ($this->is_aggregator && !empty($this->aggregator_record)) { Tribe__Events__Aggregator__Records::instance()->add_record_to_event($id, $this->aggregator_record->id, 'csv'); $this->aggregator_record->meta['activity']->add('event', 'created', $id); } return $id; }
/** * Setup all the hooks and filters * * @return void */ private function __construct() { // Make it an object for easier usage if (!is_object(self::$status)) { self::$status = (object) self::$status; } // Register the Custom Post Type add_action('init', array($this, 'get_post_type')); // Register the Custom Post Statuses add_action('init', array($this, 'get_status')); // Run the Import when Hitting the Event Aggregator Endpoint add_action('tribe_aggregator_endpoint_insert', array($this, 'action_do_import')); // Delete Link Filter add_filter('get_delete_post_link', array($this, 'filter_delete_link'), 15, 3); // Edit Link Filter add_filter('get_edit_post_link', array($this, 'filter_edit_link'), 15, 3); // Filter facebook events to force an event URL add_filter('tribe_aggregator_before_save_event', array('Tribe__Events__Aggregator__Record__Facebook', 'filter_event_to_force_url'), 10, 2); // Filter meetup events to force an event URL add_filter('tribe_aggregator_before_save_event', array('Tribe__Events__Aggregator__Record__Meetup', 'filter_event_to_force_url'), 10, 2); }
private function action_run_import($records = array()) { $service = Tribe__Events__Aggregator__Service::instance(); $record_obj = Tribe__Events__Aggregator__Records::instance()->get_post_type(); $records = array_filter((array) $records, 'is_numeric'); $success = array(); $errors = array(); foreach ($records as $record_id) { $record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id($record_id); if (is_wp_error($record)) { $errors[$record_id] = $record; continue; } // if there are no remaining imports for today, log that and skip if ($service->is_over_limit(true)) { $errors[$record->id] = new WP_Error('error:usage-limit-exceeded', $service->get_service_message('error:usage-limit-exceeded', Tribe__Events__Aggregator::instance()->get_daily_limit())); $record->update_meta('last_import_status', 'error:usage-limit-exceeded'); continue; } $child = $record->create_child_record(); $child->update_meta('interactive', true); $status = $child->queue_import(); if (is_wp_error($status)) { $errors[$record->id] = $status; $record->update_meta('last_import_status', 'error:import-failed'); continue; } $record->update_meta('last_import_status', 'success:queued'); $child->finalize(); $child->process_posts(); $success[$record->id] = $record; } return array($success, $errors); }
/** * Saves queue data to relevant meta keys on the post * * @return self */ public function save() { $this->record->update_meta(self::$activity_key, $this->activity); if (empty($this->items)) { $this->record->delete_meta(self::$queue_key); } else { $this->record->update_meta(self::$queue_key, $this->items); } // If we have a parent also update that if (!empty($this->record->post->post_parent)) { $parent = Tribe__Events__Aggregator__Records::instance()->get_by_post_id($this->record->post->post_parent); if (isset($parent->meta[self::$activity_key])) { $activity = $parent->meta[self::$activity_key]; if ($activity instanceof Tribe__Events__Aggregator__Record__Activity) { $parent->update_meta(self::$activity_key, $activity->merge($this->activity)); } } } // Updates the Modified time for the Record Log $args = array('ID' => $this->record->post->ID, 'post_modified' => date(Tribe__Date_Utils::DBDATETIMEFORMAT, current_time('timestamp'))); if (empty($this->items)) { $args['post_status'] = Tribe__Events__Aggregator__Records::$status->success; } wp_update_post($args); return $this; }
public function column_total($post) { $html = array(); $record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id($post); $last_imported = $record->get_child_record_by_status('success', 1); // is this the scheduled import page? if ($last_imported && $last_imported->have_posts()) { // Fetches the Record Object $last_imported = Tribe__Events__Aggregator__Records::instance()->get_by_post_id($last_imported->post->ID); $html[] = '<div class="tribe-ea-total">' . number_format_i18n($record->get_event_count('created')) . ' ' . esc_html__('all time', 'the-events-calendar') . '</div>'; $html[] = '<label>' . esc_html__('Latest Import:', 'the-events-calendar') . '</label>'; $html[] = '<ul class="tribe-ea-raw-list">'; $created = $last_imported->get_event_count('created'); $html[] = '<li>' . number_format_i18n($created ? $created : 0) . ' ' . esc_html__('new', 'the-events-calendar') . '</li>'; if ($last_imported_updated = $last_imported->get_event_count('updated')) { $html[] = '<li>' . number_format_i18n($last_imported_updated) . ' ' . esc_html__('updated', 'the-events-calendar') . '</li>'; } $html[] = '</ul>'; } elseif ('schedule' === $record->type && !empty($record->post->post_parent)) { // is this a child of a schedule record on History page $created = $record->get_event_count('created'); $html[] = number_format_i18n($created ? $created : 0) . ' ' . esc_html__('new', 'the-events-calendar') . '<br>'; if (!empty($record->post->post_parent) && ($updated = $record->get_event_count('updated'))) { $html[] = number_format_i18n($updated) . ' ' . esc_html__('updated', 'the-events-calendar') . '<br>'; } } else { // manual on History page $created = $record->get_event_count('created'); $html[] = number_format_i18n($created ? $created : 0) . ' ' . esc_html__('new', 'the-events-calendar') . '<br>'; if ($updated = $record->get_event_count('updated')) { $html[] = number_format_i18n($updated) . ' ' . esc_html__('updated', 'the-events-calendar') . '<br>'; } } return $this->render($html, "\n"); }
<?php $record = new stdClass(); if (!empty($_GET['id'])) { $record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id((int) $_GET['id']); } $aggregator_action = 'edit'; ?> <input type="hidden" name="aggregator[post_id]" id="tribe-post_id" value="<?php echo esc_attr($record->post->ID); ?> "> <?php include dirname(__FILE__) . '/import-form.php';
public function is_visible() { $records = Tribe__Events__Aggregator__Records::instance(); return $records->has_scheduled() || $records->has_history(); }
public function ajax_fetch_import() { $import_id = $_GET['import_id']; $record = Tribe__Events__Aggregator__Records::instance()->get_by_import_id($import_id); if (is_wp_error($record)) { wp_send_json_error($record); } $result = $record->get_import_data(); if (is_wp_error($result)) { wp_send_json_error($result); } // if we've received a source name, let's set that in the record as soon as possible if (!empty($result->data->source_name)) { $record->update_meta('source_name', $result->data->source_name); if (!empty($record->post->post_parent)) { $parent_record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id($record->post->post_parent); $parent_record->update_meta('source_name', $result->data->source_name); } } wp_send_json_success($result); }
private function action_run_import($records = array()) { $record_obj = Tribe__Events__Aggregator__Records::instance()->get_post_type(); $records = array_filter((array) $records, 'is_numeric'); $success = array(); $errors = array(); foreach ($records as $record_id) { $record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id($record_id); if (is_wp_error($record)) { $errors[$record_id] = $record; continue; } $child = $record->create_child_record(); $child->update_meta('interactive', true); $status = $child->queue_import(); if (is_wp_error($status)) { $errors[$record->id] = $status; continue; } $child->finalize(); $child->process_posts(); $success[$record->id] = $record; } return array($success, $errors); }
/** * @since 4.3.2 * @return void */ public function purge_expired_records() { global $wpdb; $records = Tribe__Events__Aggregator__Records::instance(); $statuses = Tribe__Events__Aggregator__Records::$status; $sql = "\n\t\t\tSELECT\n\t\t\t\tmeta_value\n\t\t\tFROM\n\t\t\t\t{$wpdb->postmeta}\n\t\t\t\tJOIN {$wpdb->posts}\n\t\t\t\tON ID = post_id\n\t\t\t\tAND post_status = %s\n\t\t\tWHERE\n\t\t\t\tmeta_key = %s\n\t\t"; // let's make sure we don't purge the most recent record for each import $records_to_retain = $wpdb->get_col($wpdb->prepare($sql, $statuses->schedule, Tribe__Events__Aggregator__Record__Abstract::$meta_key_prefix . 'recent_child')); $args = array('post_status' => array($statuses->pending, $statuses->success, $statuses->failed, $statuses->draft), 'date_query' => array(array('before' => date('Y-m-d H:i:s', time() - $records->get_retention()), 'column' => 'post_date_gmt')), 'order' => 'ASC', 'posts_per_page' => 100); if ($records_to_retain) { $args['post__not_in'] = $records_to_retain; } $query = $records->query($args); if (!$query->have_posts()) { $this->log('debug', 'No Records over retetion limit, skipped pruning expired'); return; } foreach ($query->posts as $post) { $record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id($post); if (!$record->has_passed_retention_time()) { $this->log('debug', sprintf('Record (%d) skipped, not passed retetion time', $record->id)); continue; } // Creating the child records based on this Parent $deleted = wp_delete_post($record->id, true); if ($deleted) { $this->log('debug', sprintf('Record (%d), was pruned', $deleted->ID)); } else { $this->log('debug', sprintf('Record (%d), was not pruned', $deleted)); } } }
/** * Checks if any record data needs to be fetched from the service, this will run on the Cron every 15m * * @return void */ public function verify_fetching_from_service() { // if the service isn't active, don't do anything if (!Tribe__Events__Aggregator::instance()->is_service_active()) { return; } $records = Tribe__Events__Aggregator__Records::instance(); $query = $records->query(array('post_status' => Tribe__Events__Aggregator__Records::$status->pending, 'posts_per_page' => -1, 'order' => 'ASC', 'meta_query' => array(array('key' => '_tribe_aggregator_origin', 'value' => 'csv', 'compare' => '!=')))); if (!$query->have_posts()) { $this->log('debug', 'No Records Pending, skipped Fetching from service'); return false; } foreach ($query->posts as $post) { $record = $records->get_by_post_id($post); // Just double Check for CSV if ('csv' === $record->origin) { $this->log('debug', sprintf('Record (%d) skipped, has CSV origin', $record->id)); continue; } // Open a Queue to try to process the posts $queue = $record->process_posts(); if (!is_wp_error($queue)) { /** @var Tribe__Events__Aggregator__Record__Queue $queue */ $this->log('debug', sprintf('Record (%d) has processed queue ', $queue->record->id)); $activity = $queue->activity()->get(); foreach ($activity as $key => $actions) { foreach ($actions as $action => $ids) { if (empty($ids)) { continue; } $this->log('debug', sprintf("\t" . '%s — %s: %s', $key, $action, implode(', ', $ids))); } } } else { $this->log('debug', sprintf('Record (%d) — %s', $record->id, $queue->get_error_message())); } } }
/** * Filters the Contents of the Columns for the Ignored Events View * * @param string $column Which column we are dealing with * @param int|WP_Post $post WP Post ID or Object * * @return string|null|false */ public function action_column_contents($column, $post) { $record = Tribe__Events__Aggregator__Records::instance()->get_by_event_id($post); if (is_wp_error($record)) { return false; } $html = array(); if ('source' === $column) { $html[] = '<p>' . esc_html_x('via ', 'record via origin', 'the-events-calendar') . '<strong>' . $record->get_label() . '</strong></p>'; if ('ea/ics' === $record->post->post_mime_type || 'ea/csv' === $record->post->post_mime_type) { $file_path = get_attached_file(absint($record->meta['file'])); $filename = basename($file_path); $html[] = '<p>' . esc_html__('Source:', 'the-events-calendar') . ' <code>' . esc_html($filename) . '</code></p>'; } else { $html[] = '<p>' . esc_html__('Source:', 'the-events-calendar') . ' <code>' . esc_html($record->meta['source']) . '</code></p>'; } } elseif ('last-import' === $column) { $last_import = null; $original = $record->post->post_modified; $time = strtotime($original); $now = current_time('timestamp'); $html[] = '<span title="' . esc_attr($original) . '">'; if ($now - $time <= DAY_IN_SECONDS) { $diff = human_time_diff($time, $now); if ($now - $time > 0) { $html[] = sprintf(esc_html_x('about %s ago', 'human readable time ago', 'the-events-calendar'), $diff); } else { $html[] = sprintf(esc_html_x('in about %s', 'in human readable time', 'the-events-calendar'), $diff); } } else { $html[] = date(Tribe__Date_Utils::DATEONLYFORMAT, $time) . '<br>' . date(Tribe__Date_Utils::TIMEFORMAT, $time); } $html[] = '</span>'; } echo implode("\r\n", (array) $html); }
?> " data-depends="#tribe-ea-field-facebook_import_type" data-condition="schedule" data-width-rule="all-triggers" ></span> <p class="tribe-timezone-message"> <?php echo sprintf(esc_html__('Events will be imported with the timezone defined by the source. If no time zone is specified, events will be assigned your site\'s default timezone (see %1$sSettings > General%2$s).', 'the-events-calendar'), '<a href="' . esc_url(Tribe__Settings::instance()->get_url()) . '#tribe-field-tribe_events_timezone_mode">', '</a>'); ?> </p> </div> <?php echo Tribe__Events__Aggregator__Tabs__New::instance()->maybe_display_aggregator_upsell(); $csv_record = Tribe__Events__Aggregator__Records::instance()->get_by_origin('csv'); $post_types = $csv_record->get_import_post_types(); foreach ($post_types as $post_type) { $type = str_replace('tribe_', '', $post_type->name); ?> <script id="tribe-csv-column-map-<?php echo esc_attr($type); ?> " type="text/html"> <?php $mapper = new Tribe__Events__Importer__Column_Mapper($type); echo $mapper->make_select_box(''); ?> </script> <?php }
/** * Method that Handles the AJAX converting of Legacy iCal Settings * AJAX methods will not return anything, only print a JSON string * * @return void */ public function ajax_convert_ical_settings() { $response = (object) array('status' => false, 'text' => esc_html__('Error: we were not able to migrate your iCal Importer settings to Event Aggregator. Please try again later.', 'the-events-calendar')); $post_type = get_post_type_object(Tribe__Events__Main::POSTTYPE); if (empty($post_type->cap->edit_posts) || !current_user_can($post_type->cap->edit_posts)) { $response->status = false; $response->text = esc_html__('You do not have permission to migrate iCal Importer settings to Event Aggregator', 'the-events-calendar'); wp_send_json($response); } if (!$this->has_ical_setting()) { $response->status = false; $response->text = esc_html__('We did not find any iCal Importer settings to migrate.', 'the-events-calendar'); wp_send_json($response); } $settings = $this->get_ical_setting(); $status = (object) array('error' => array(), 'success' => array()); $origin = 'ical'; foreach ($settings->imports as $time => $import) { $import = (object) $import; $record = Tribe__Events__Aggregator__Records::instance()->get_by_origin($origin); /** * @todo Include the Deactivated logic */ $meta = array('origin' => $origin, 'type' => 'schedule', 'frequency' => $this->convert_ical_frequency($import->schedule), 'file' => null, 'keywords' => $import->keywords, 'location' => $import->location, 'start' => $import->start, 'radius' => $import->radius, 'source' => $import->url, 'content_type' => null, 'is_legacy' => true, 'import_id' => null, 'post_status' => $import->post_status, 'category' => $import->import_category); $post = $record->create('schedule', array(), $meta); if (is_wp_error($post)) { $status->error[] = $post; } else { $status->success[] = $post->id; // Update status from Draft to Schedule $args['ID'] = absint($post->id); $args['post_status'] = Tribe__Events__Aggregator__Records::$status->schedule; wp_update_post($args); } } /** * @todo Create a real Logic for Messaging what happened */ $response->status = true; $response->text = esc_html__('Success! The settings from iCal Importer have been migrated to Event Aggregator. You can view your migrated imports on the Scheduled Imports tab.', 'the-events-calendar'); $response->statuses = $status; update_option(self::$migrated_ical_key, true); wp_send_json($response); }
/** * A private method to prevent it to be created twice. * It will add the methods and setup any dependencies * * Note: This should load on `plugins_loaded@P10` */ private function __construct() { /** * As previously seen by other major features some users would rather have it not active * @var bool */ $should_load = (bool) apply_filters('tribe_aggregator_should_load', true); // You shall not Load! if (false === $should_load) { return; } // Loads the Required Classes and saves them as proprieties $this->meta_box = Tribe__Events__Aggregator__Meta_Box::instance(); $this->migrate = Tribe__Events__Aggregator__Migrate::instance(); $this->page = Tribe__Events__Aggregator__Page::instance(); $this->service = Tribe__Events__Aggregator__Service::instance(); $this->settings = Tribe__Events__Aggregator__Settings::instance(); $this->records = Tribe__Events__Aggregator__Records::instance(); $this->cron = Tribe__Events__Aggregator__Cron::instance(); $this->queue_processor = new Tribe__Events__Aggregator__Record__Queue_Processor(); $this->queue_realtime = new Tribe__Events__Aggregator__Record__Queue_Realtime(null, null, $this->queue_processor); $this->errors = Tribe__Events__Aggregator__Errors::instance(); $this->pue_checker = new Tribe__PUE__Checker('http://tri.be/', 'event-aggregator', array('context' => 'service')); // Initializes the Classes related to the API $this->api(); // Flags that the Aggregator has been fully loaded $this->is_loaded = true; // Register the Aggregator Endpoint add_action('tribe_events_pre_rewrite', array($this, 'action_endpoint_configuration')); // Intercept the Endpoint and trigger actions add_action('parse_request', array($this, 'action_endpoint_parse_request')); // Add endpoint query vars add_filter('query_vars', array($this, 'filter_endpoint_query_vars')); // Filter the "plugin name" for Event Aggregator add_filter('pue_get_plugin_name', array($this, 'filter_pue_plugin_name'), 10, 2); // To make sure that meaningful cache is purged when settings are changed add_action('updated_option', array($this, 'action_purge_transients')); // Remove aggregator records from ET add_filter('tribe_tickets_settings_post_types', array($this, 'filter_remove_record_post_type')); // Notify users about expiring Facebook Token if oauth is enabled add_action('plugins_loaded', array($this, 'setup_notices'), 11); // Let's prevent events-importer-ical from DESTROYING its saved recurring imports when it gets deactivated if (class_exists('Tribe__Events__Ical_Importer__Main')) { remove_action('deactivate_' . plugin_basename(Tribe__Events__Ical_Importer__Main::$plugin_path . 'the-events-calendar-ical-importer.php'), 'tribe_events_ical_deactivate'); } add_action('admin_init', array($this, 'add_status_to_help')); }
/** * Inserts events, venues, and organizers for the Import Record * * @param array $data Dummy data var to allow children to optionally react to passed in data * * @return array|WP_Error */ public function insert_posts($items = array()) { add_filter('tribe-post-origin', array(Tribe__Events__Aggregator__Records::instance(), 'filter_post_origin'), 10); // Creates an Activity to log what Happened $activity = new Tribe__Events__Aggregator__Record__Activity(); $args = array('post_status' => $this->meta['post_status']); $unique_field = $this->get_unique_field(); $existing_ids = $this->get_existing_ids_from_import_data($items); //cache $possible_parents = array(); $found_organizers = array(); $found_venues = array(); //if we have no non recurring events the message may be different $non_recurring = false; $show_map_setting = Tribe__Events__Aggregator__Settings::instance()->default_map($this->meta['origin']); $update_authority_setting = Tribe__Events__Aggregator__Settings::instance()->default_update_authority($this->meta['origin']); $unique_inserted = array(); foreach ($items as $item) { $event = Tribe__Events__Aggregator__Event::translate_service_data($item); // set the event ID if it can be set if ($unique_field && isset($event[$unique_field['target']]) && isset($existing_ids[$event[$unique_field['target']]])) { $event['ID'] = $existing_ids[$event[$unique_field['target']]]->post_id; } // only set the post status if there isn't an ID if (empty($event['ID'])) { $event['post_status'] = $args['post_status']; } /** * Should events that have previously been imported be overwritten? * * By default this is turned off (since it would reset the post status, description * and any other fields that have subsequently been edited) but it can be enabled * by returning true on this filter. * * @var bool $overwrite * @var int $event_id */ if (!empty($event['ID']) && 'retain' === $update_authority_setting) { // Log this Event was Skipped $activity->add('event', 'skipped', $event['ID']); continue; } if ($show_map_setting) { $event['EventShowMap'] = $show_map_setting; $event['EventShowMapLink'] = $show_map_setting; } if (empty($event['recurrence'])) { $non_recurring = true; } // set the parent if (!empty($event['ID']) && ($id = wp_get_post_parent_id($event['ID']))) { $event['post_parent'] = $id; } elseif (!empty($event['parent_uid']) && ($k = array_search($event['parent_uid'], $possible_parents))) { $event['post_parent'] = $k; } //if we should create a venue or use existing if (!empty($event['Venue']['Venue'])) { $v_id = array_search($event['Venue']['Venue'], $found_venues); if (false !== $v_id) { $event['EventVenueID'] = $v_id; } elseif ($venue = get_page_by_title($event['Venue']['Venue'], 'OBJECT', Tribe__Events__Main::VENUE_POST_TYPE)) { $found_venues[$venue->ID] = $event['Venue']['Venue']; $event['EventVenueID'] = $venue->ID; } else { $event['Venue']['ShowMap'] = $show_map_setting; $event['Venue']['ShowMapLink'] = $show_map_setting; $event['EventVenueID'] = Tribe__Events__Venue::instance()->create($event['Venue'], $this->meta['post_status']); // Log this Venue was created $activity->add('venue', 'created', $event['EventVenueID']); } // Remove the Venue to avoid duplicates unset($event['Venue']); } //if we should create an organizer or use existing if (!empty($event['Organizer']['Organizer'])) { $o_id = array_search($event['Organizer']['Organizer'], $found_organizers); if (false !== $o_id) { $event['EventOrganizerID'] = $o_id; } elseif ($organizer = get_page_by_title($event['Organizer']['Organizer'], 'OBJECT', Tribe__Events__Main::ORGANIZER_POST_TYPE)) { $found_organizers[$organizer->ID] = $event['Organizer']['Organizer']; $event['EventOrganizerID'] = $organizer->ID; } else { $event['EventOrganizerID'] = Tribe__Events__Organizer::instance()->create($event['Organizer'], $this->meta['post_status']); // Log this Organizer was created $activity->add('organizer', 'created', $event['EventOrganizerID']); } // Remove the Organizer to avoid duplicates unset($event['Organizer']); } $event['post_type'] = Tribe__Events__Main::POSTTYPE; /** * Filters the event data before any sort of saving of the event * * @param array $event Event data to save * @param Tribe__Events__Aggregator__Record__Abstract Importer record */ $event = apply_filters('tribe_aggregator_before_save_event', $event, $this); if (!empty($event['ID'])) { if ('preserve_changes' === $update_authority_setting) { $event = Tribe__Events__Aggregator__Event::preserve_changed_fields($event); } add_filter('tribe_aggregator_track_modified_fields', '__return_false'); /** * Filters the event data before updating event * * @param array $event Event data to save * @param Tribe__Events__Aggregator__Record__Abstract Importer record */ $event = apply_filters('tribe_aggregator_before_update_event', $event, $this); $event['ID'] = tribe_update_event($event['ID'], $event); // since the Event API only supports the _setting_ of these meta fields, we need to manually // delete them rather than relying on Tribe__Events__API::saveEventMeta() if (isset($event['EventShowMap']) && (empty($event['EventShowMap']) || 'no' === $event['EventShowMap'])) { delete_post_meta($event['ID'], '_EventShowMap'); } if (isset($event['EventShowMapLink']) && (empty($event['EventShowMapLink']) || 'no' === $event['EventShowMapLink'])) { delete_post_meta($event['ID'], '_EventShowMapLink'); } remove_filter('tribe_aggregator_track_modified_fields', '__return_false'); // Log that this event was updated $activity->add('event', 'updated', $event['ID']); } else { /** * Filters the event data before inserting event * * @param array $event Event data to save * @param Tribe__Events__Aggregator__Record__Abstract Importer record */ $event = apply_filters('tribe_aggregator_before_insert_event', $event, $this); $event['ID'] = tribe_create_event($event); // Log this event was created $activity->add('event', 'created', $event['ID']); } Tribe__Events__Aggregator__Records::instance()->add_record_to_event($event['ID'], $this->id, $this->origin); //add post parent possibility if (empty($event['parent_uid'])) { $possible_parents[$event['ID']] = $event[$unique_field['target']]; } if (!empty($event[$unique_field['target']])) { update_post_meta($event['ID'], "_{$unique_field['target']}", $event[$unique_field['target']]); } //Save the meta data in case of updating to pro later on if (!empty($event['EventRecurrenceRRULE'])) { update_post_meta($event['ID'], '_EventRecurrenceRRULE', $event['EventRecurrenceRRULE']); } $terms = array(); if (!empty($event['categories'])) { foreach ($event['categories'] as $cat) { if (!($term = term_exists($cat, Tribe__Events__Main::TAXONOMY))) { $term = wp_insert_term($cat, Tribe__Events__Main::TAXONOMY); if (!is_wp_error($term)) { $terms[] = (int) $term['term_id']; // Track that we created a Term $activity->add('cat', 'created', $term['term_id']); } } else { $terms[] = (int) $term['term_id']; } } } // if we are setting all events to a category specified in saved import if (!empty($this->meta['category'])) { $terms[] = (int) $this->meta['category']; } wp_set_object_terms($event['ID'], $terms, Tribe__Events__Main::TAXONOMY, false); // If we have a Image Field from Service if (!empty($event['image'])) { // Attempt to grab the event image $image_import = Tribe__Events__Aggregator::instance()->api('image')->get($event['image']->id); /** * Filters the returned event image url * * @param array|bool $image Attachment information * @param array $event Event array */ $image = apply_filters('tribe_aggregator_event_image', $image_import, $event); // If there was a problem bail out if (false === $image) { continue; } // Verify for more Complex Errors if (is_wp_error($image)) { continue; } if (isset($image->status) && 'created' === $image->status) { // Set as featured image $featured_status = set_post_thumbnail($event['ID'], $image->post_id); if ($featured_status) { // Log this attachment was created $activity->add('attachment', 'created', $image->post_id); } } } } remove_filter('tribe-post-origin', array(Tribe__Events__Aggregator__Records::instance(), 'filter_post_origin'), 10); return $activity; }
/** * 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; }
public function handle_submit() { $data = array('message' => __('There was a problem processing your import. Please try again.', 'the-events-calendar')); if ((!defined('DOING_AJAX') || !DOING_AJAX) && !$this->is_active()) { return; } if ('POST' !== $_SERVER['REQUEST_METHOD']) { return; } if (empty($_POST['aggregator'])) { return; } // validate nonce if (empty($_POST['tribe_aggregator_nonce']) || !wp_verify_nonce($_POST['tribe_aggregator_nonce'], 'tribe-aggregator-save-import')) { wp_send_json_error($data); } $post_data = $_POST['aggregator']; if (empty($post_data['origin']) || empty($post_data[$post_data['origin']])) { wp_send_json_error($data); } $data = $post_data[$post_data['origin']]; $record = Tribe__Events__Aggregator__Records::instance()->get_by_origin($post_data['origin']); $meta = array('origin' => $post_data['origin'], 'type' => empty($data['import_type']) ? 'manual' : $data['import_type'], 'frequency' => empty($data['import_frequency']) ? null : $data['import_frequency'], 'file' => empty($data['file']) ? null : $data['file'], 'keywords' => empty($data['keywords']) ? null : $data['keywords'], 'location' => empty($data['location']) ? null : $data['location'], 'start' => empty($data['start']) ? null : $data['start'], 'radius' => empty($data['radius']) ? null : $data['radius'], 'source' => empty($data['source']) ? null : $data['source'], 'content_type' => empty($data['content_type']) ? null : $data['content_type']); // make sure there's data if (empty($meta['file']) && empty($meta['source'])) { if ('csv' === $meta['origin'] || 'ics' === $meta['origin']) { wp_send_json_error(array('message' => __('Please provide the file that you wish to import.', 'the-events-calendar'))); } else { wp_send_json_error(array('message' => __('Please provide the URL that you wish to import.', 'the-events-calendar'))); } } // validate that the URLs are accurate for the relevant origin if ('facebook' === $meta['origin'] && !preg_match('!(https?://)?(www\\.)?facebook\\.com!', $meta['source'])) { wp_send_json_error(array('message' => __('Please provide a Facebook URL when importing from Facebook.', 'the-events-calendar'))); } elseif ('meetup' === $meta['origin'] && !preg_match('!(https?://)?(www\\.)?meetup\\.com!', $meta['source'])) { wp_send_json_error(array('message' => __('Please provide a Meetup URL when importing from Meetup.', 'the-events-calendar'))); } return array('record' => $record, 'post_data' => $post_data, 'meta' => $meta); }