/** * Do the actual work of saving a recurring series of events * * @return void */ 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); }
/** * Alter the Recurrence rule end-count value for appropriate rules (which, in this case have an end-type of "After") * when the given child event date exists within the rule as a valid date * * @param int $parent_id Event ID of the parent recurrence event * @param array $recurrence_meta Collection of recurrence rules for the event * @param string $child_date Date of the child event we are testing for * @param string $action Type of action we are taking on the field: reduce or set. Reduce subtracts, while set overrides the value * @param int $value Value to either subtract or set the end-count to */ private function maybe_alter_recurrence_end_count($parent_id, $recurrence_meta, $child_date, $action = 'reduce', $value = 1) { $recurrences = Tribe__Events__Pro__Recurrence_Meta::get_recurrence_for_event($parent_id); $earliest_date = strtotime(Tribe__Events__Pro__Recurrence_Meta::$scheduler->get_earliest_date()); $latest_date = strtotime(Tribe__Events__Pro__Recurrence_Meta::$scheduler->get_latest_date()); // if the recurrence rule has an end-type of "After", then we'll need to reduce the number of events it repeats with foreach ($recurrence_meta['rules'] as $rule_key => $rule) { if (empty($rule['end-type']) || 'After' !== $rule['end-type']) { continue; } if (empty($recurrences['rules'][$rule_key])) { continue; } $recurrences['rules'][$rule_key]->setMinDate($earliest_date); $recurrences['rules'][$rule_key]->setMaxDate($latest_date); $dates = $recurrences['rules'][$rule_key]->getDates(); if (!in_array($child_date, $dates)) { continue; } if ('reduce' === $action) { $recurrence_meta['rules'][$rule_key]['end-count'] -= $value; } else { $recurrence_meta['rules'][$rule_key]['end-count'] = $value; } } return $recurrence_meta; }