public function user_update() { if (!tribe_is_event($this->event_id)) { return; } $notice = __('Recurring event data is still being generated for this event. Don’t worry, you can safely navigate away – the process will resume in a bit in the background.', 'tribe-events-pro'); $percent = $this->sanitize_progress($this->queue->progress_percentage()); $spinner = '<img src="' . get_admin_url(null, '/images/spinner.gif') . '">'; $indicator = '<div> <div class="progress" title="' . sprintf(__('%d%% complete', 'tribe-events-pro'), $percent) . '"> <div class="bar"></div> </div>' . $spinner . '</div>'; return "<p> {$notice} </p> {$indicator}"; }
public function ajax() { $event_id = (int) $_POST['event']; // Nonce check if (!wp_verify_nonce($_POST['check'], 'generate_recurring_instances_' . $event_id . get_current_user_id())) { exit(json_encode(array('html' => __('Unable to continue processing recurring event data. Please reload this page to continue/try again.', 'tribe-events-pro'), 'progress' => false, 'continue' => false, 'complete' => false))); } // Load the queue $queue = new Tribe__Events__Pro__Recurrence__Queue($event_id); if (!$queue->is_empty()) { Tribe__Events__Pro__Main::instance()->queue_processor->process_batch($event_id); } $done = $queue->is_empty(); $percentage = $queue->progress_percentage(); exit(json_encode(array('html' => false, 'progress' => $percentage, 'progressText' => sprintf(__('%d%% complete', 'tribe-events-pro'), $percentage), 'continue' => !$done, 'complete' => $done))); }
protected function do_creations() { $exclusions = $this->current_queue->instances_to_exclude(); $instances_to_create = $this->current_queue->instances_to_create(); foreach ($instances_to_create as $key => $start_date) { // Don't process more than the current batch size allows if ($this->batch_complete()) { break; } // Some instances may deliberately have been removed - let's not recreate them if (in_array($start_date, $exclusions)) { continue; } $instance = new Tribe__Events__Pro__Recurrence_Instance($this->current_event_id, $start_date); $instance->save(); unset($instances_to_create[$key]); $this->processed++; } $this->current_queue->instances_to_create($instances_to_create); }
protected function do_creations() { $exclusions = $this->current_queue->instances_to_exclude(); $instances_to_create = array_values($this->current_queue->instances_to_create()); try { $sequence = new Tribe__Events__Pro__Recurrence__Sequence($instances_to_create, $this->current_event_id); } catch (Exception $e) { $exception = new Tribe__Exception($e); $exception->handle(); return; } foreach ($sequence->get_sorted_sequence() as $key => $date_duration) { // Don't process more than the current batch size allows if ($this->batch_complete()) { break; } // Some instances may deliberately have been removed - let's remove // them from the list of events to create and move on if (in_array($date_duration, $exclusions)) { unset($instances_to_create[$date_duration['original_index']]); $this->processed++; continue; } if (!$this->current_queue->have_ownership_of_job()) { return; } $sequence_number = isset($date_duration['sequence']) ? $date_duration['sequence'] : 1; $instance = new Tribe__Events__Pro__Recurrence__Instance($this->current_event_id, $date_duration, 0, $sequence_number); if (!$instance->already_exists()) { $instance->save(); } unset($instances_to_create[$date_duration['original_index']]); $this->processed++; } $this->current_queue->instances_to_create($instances_to_create); }
/** * Do the actual work of saving a recurring series of events * * @return bool */ public function save_events() { $existing_instances = Tribe__Events__Pro__Recurrence__Children_Events::instance()->get_ids($this->event_id); $recurrences = Tribe__Events__Pro__Recurrence__Meta::get_recurrence_for_event($this->event_id); $to_create = array(); $exclusions = array(); $to_update = array(); $to_delete = array(); $possible_next_pending = array(); $earliest_date = strtotime(Tribe__Events__Pro__Recurrence__Meta::$scheduler->get_earliest_date()); $latest_date = strtotime(Tribe__Events__Pro__Recurrence__Meta::$scheduler->get_latest_date()); foreach ($recurrences['rules'] as &$recurrence) { if (!$recurrence) { continue; } $recurrence->setMinDate($earliest_date); $recurrence->setMaxDate($latest_date); $to_create = array_merge($to_create, $recurrence->getDates()); if ($recurrence->constrainedByMaxDate() !== false) { $possible_next_pending[] = $recurrence->constrainedByMaxDate(); } } $to_create = tribe_array_unique($to_create); // find days we should exclude foreach ($recurrences['exclusions'] as &$recurrence) { if (!$recurrence) { continue; } $recurrence->setMinDate($earliest_date); $recurrence->setMaxDate($latest_date); $exclusions = array_merge($exclusions, $recurrence->getDates()); } // make sure we don't create excluded dates $exclusions = tribe_array_unique($exclusions); $to_create = $this->exclusions->remove_exclusions($to_create, $exclusions); if ($possible_next_pending) { update_post_meta($this->event_id, '_EventNextPendingRecurrence', date(Tribe__Events__Pro__Date_Series_Rules__Rules_Interface::DATE_FORMAT, min($possible_next_pending))); } foreach ($existing_instances as $instance) { $start_date = strtotime(get_post_meta($instance, '_EventStartDate', true) . '+00:00'); $end_date = strtotime(get_post_meta($instance, '_EventEndDate', true) . '+00:00'); $duration = $end_date - $start_date; $existing_date_duration = array('timestamp' => $start_date, 'duration' => $duration); $found = array_search($existing_date_duration, $to_create); $should_be_excluded = in_array($existing_date_duration, $exclusions); if ($found === false || false !== $should_be_excluded) { $to_delete[$instance] = $existing_date_duration; } else { $to_update[$instance] = $to_create[$found]; unset($to_create[$found]); // so we don't re-add it } } // Store the list of instances to create/update/delete etc for future processing $queue = new Tribe__Events__Pro__Recurrence__Queue($this->event_id); $queue->update($to_create, $to_update, $to_delete, $exclusions); // ...but don't wait around, process a small initial batch right away Tribe__Events__Pro__Main::instance()->queue_processor->process_batch($this->event_id); return true; }
/** * Do the actual work of saving a recurring series of events * * @param int $event_id The event that is being saved * * @return void */ public static function saveEvents($event_id) { // don't use self::get_child_event_ids() due to caching that hasn't yet flushed $existing_instances = get_posts(array('post_parent' => $event_id, 'post_type' => Tribe__Events__Main::POSTTYPE, 'posts_per_page' => -1, 'fields' => 'ids', 'post_status' => get_post_stati(), 'meta_key' => '_EventStartDate', 'orderby' => 'meta_value', 'order' => 'ASC')); $recurrences = self::get_recurrence_for_event($event_id); $to_create = array(); $exclusions = array(); $to_update = array(); $to_delete = array(); $possible_next_pending = array(); $earliest_date = strtotime(self::$scheduler->get_earliest_date()); $latest_date = strtotime(self::$scheduler->get_latest_date()); foreach ($recurrences['rules'] as &$recurrence) { if (!$recurrence) { continue; } $recurrence->setMinDate($earliest_date); $recurrence->setMaxDate($latest_date); $to_create = array_merge($to_create, $recurrence->getDates()); if ($recurrence->constrainedByMaxDate() !== false) { $possible_next_pending[] = $recurrence->constrainedByMaxDate(); } } $to_create = self::array_unique($to_create); // find days we should exclude foreach ($recurrences['exclusions'] as &$recurrence) { if (!$recurrence) { continue; } $recurrence->setMinDate($earliest_date); $recurrence->setMaxDate($latest_date); $exclusions = array_merge($exclusions, $recurrence->getDates()); } // make sure we don't create excluded dates $exclusions = self::array_unique($exclusions); $to_create = self::remove_exclusions($to_create, $exclusions); if ($possible_next_pending) { update_post_meta($event_id, '_EventNextPendingRecurrence', date(Tribe__Events__Pro__Date_Series_Rules__Rules_Interface::DATE_FORMAT, min($possible_next_pending))); } foreach ($existing_instances as $instance) { $start_date = strtotime(get_post_meta($instance, '_EventStartDate', true) . '+00:00'); $end_date = strtotime(get_post_meta($instance, '_EventEndDate', true) . '+00:00'); $duration = $end_date - $start_date; $existing_date_duration = array('timestamp' => $start_date, 'duration' => $duration); $found = array_search($existing_date_duration, $to_create); $should_be_excluded = in_array($existing_date_duration, $exclusions); if ($found === false || false !== $should_be_excluded) { $to_delete[$instance] = $existing_date_duration; } else { $to_update[$instance] = $to_create[$found]; unset($to_create[$found]); // so we don't re-add it } } // Store the list of instances to create/update/delete etc for future processing $queue = new Tribe__Events__Pro__Recurrence__Queue($event_id); $queue->update($to_create, $to_update, $to_delete, $exclusions); // ...but don't wait around, process a small initial batch right away Tribe__Events__Pro__Main::instance()->queue_processor->process_batch($event_id); }
/** * Do the actual work of saving a recurring series of events * * @param int $event_id The event that is being saved * * @return void */ public static function saveEvents($event_id) { // don't use self::get_child_event_ids() due to caching that hasn't yet flushed $existing_instances = get_posts(array('post_parent' => $event_id, 'post_type' => Tribe__Events__Main::POSTTYPE, 'posts_per_page' => -1, 'fields' => 'ids', 'post_status' => get_post_stati(), 'meta_key' => '_EventStartDate', 'orderby' => 'meta_value', 'order' => 'ASC')); $recurrence = self::getRecurrenceForEvent($event_id); if ($recurrence) { $recurrence->setMinDate(strtotime(self::$scheduler->get_earliest_date())); $recurrence->setMaxDate(strtotime(self::$scheduler->get_latest_date())); $to_create = (array) $recurrence->getDates(); $to_update = array(); $to_delete = array(); if ($recurrence->constrainedByMaxDate() !== false) { update_post_meta($event_id, '_EventNextPendingRecurrence', date(Tribe__Events__Pro__Date_Series_Rules__Rules_Interface::DATE_FORMAT, $recurrence->constrainedByMaxDate())); } foreach ($existing_instances as $instance) { $start_date = strtotime(get_post_meta($instance, '_EventStartDate', true) . '+00:00'); $found = array_search($start_date, $to_create); if ($found === false) { $to_delete[$instance] = $start_date; } else { $to_update[$instance] = $to_create[$found]; unset($to_create[$found]); // so we don't re-add it } } $exclusions = array_map('strtotime', self::get_excluded_dates($event_id)); // Store the list of instances to create/update/delete etc for future processing $queue = new Tribe__Events__Pro__Recurrence__Queue($event_id); $queue->update($to_create, $to_update, $to_delete, $exclusions); // ...but don't wait around, process a small initial batch right away Tribe__Events__Pro__Main::instance()->queue_processor->process_batch($event_id); } }