/** * Update event recurrence when a recurring event is saved * * @param integer $event_id id of the event to update * @param array $data data defining the recurrence of this event * * @return void */ public static function updateRecurrenceMeta($event_id, $data) { if (!isset($data['recurrence'])) { return; } $recurrence_meta = array('rules' => array(), 'exclusions' => array()); $datepicker_format = Tribe__Events__Date_Utils::datepicker_formats(tribe_get_option('datepickerFormat')); if (!empty($data['recurrence'])) { if (isset($data['recurrence']['recurrence-description'])) { unset($data['recurrence']['recurrence-description']); } foreach (array('rules', 'exclusions') as $rule_type) { if (!isset($data['recurrence'][$rule_type])) { continue; } //end if foreach ($data['recurrence'][$rule_type] as $key => &$recurrence) { if (!$recurrence) { continue; } // Ignore the rule if the type isn't set OR the type is set to 'None' // (we're not interested in exclusions here) if ((empty($recurrence['type']) || 'None' === $recurrence['type']) && $rule_type !== 'exclusions') { continue; } if (empty($recurrence['type']) && empty($recurrence['custom']['type']) || 'None' === $recurrence['custom']['type']) { unset($data['recurrence'][$rule_type][$key]); continue; } unset($recurrence['occurrence-count-text'], $recurrence['custom']['type-text']); if (!empty($recurrence['end'])) { $recurrence['end'] = Tribe__Events__Date_Utils::datetime_from_format($datepicker_format, $recurrence['end']); } // if this isn't an exclusion and it isn't a Custom rule, then we don't need the custom array index if ('rules' === $rule_type && 'Custom' !== $recurrence['type']) { unset($recurrence['custom']); } else { $custom_types = array('date', 'day', 'week', 'month', 'year'); $custom_type_key = self::custom_type_to_key($recurrence['custom']['type']); // clean up extraneous array elements foreach ($custom_types as $type) { if ($type === $custom_type_key) { continue; } if (!isset($recurrence['custom'][$type])) { continue; } unset($recurrence['custom'][$type]); } } //end else $recurrence['EventStartDate'] = $data['EventStartDate']; $recurrence['EventEndDate'] = $data['EventEndDate']; if (self::isRecurrenceValid($event_id, $recurrence)) { $recurrence_meta[$rule_type][] = $recurrence; } } } } //end if $updated = update_post_meta($event_id, '_EventRecurrence', $recurrence_meta); self::saveEvents($event_id, $updated); }
protected function set_end_date_time() { $this->vars['endMinuteOptions'] = Tribe__Events__View_Helpers::getMinuteOptions($this->vars['_EventEndDate']); $this->vars['endHourOptions'] = Tribe__Events__View_Helpers::getHourOptions($this->vars['_EventAllDay'] == 'yes' ? null : $this->vars['_EventEndDate']); $this->vars['endMeridianOptions'] = Tribe__Events__View_Helpers::getMeridianOptions($this->vars['_EventEndDate']); $datepicker_format = Tribe__Events__Date_Utils::datepicker_formats(tribe_get_option('datepickerFormat')); if ($this->vars['_EventEndDate']) { $end = Tribe__Events__Date_Utils::date_only($this->vars['_EventEndDate'], false, $datepicker_format); } // If we don't have a valid end date, assume today's date $this->vars['EventEndDate'] = isset($end) && $end ? $end : date($datepicker_format); }
/** * given a set of meta data, prepare date data if it exists * * @param $data array Associative array of event meta data * * @return array */ protected static function prepare_event_date_meta($event_id, $data) { $date_provided = false; if (isset($data['EventAllDay'])) { if (Tribe__Events__Date_Utils::is_all_day($data['EventAllDay'])) { $data['EventAllDay'] = 'yes'; } else { $data['EventAllDay'] = 'no'; } } $datepicker_format = Tribe__Events__Date_Utils::datepicker_formats(tribe_get_option('datepickerFormat')); if (isset($data['EventStartDate'])) { $data['EventStartDate'] = Tribe__Events__Date_Utils::datetime_from_format($datepicker_format, $data['EventStartDate']); } if (isset($data['EventEndDate'])) { $data['EventEndDate'] = Tribe__Events__Date_Utils::datetime_from_format($datepicker_format, $data['EventEndDate']); } if (isset($data['EventAllDay']) && 'yes' === $data['EventAllDay']) { $date_provided = true; $data['EventStartDate'] = tribe_event_beginning_of_day($data['EventStartDate']); $data['EventEndDate'] = tribe_event_end_of_day($data['EventEndDate']); } elseif (isset($data['EventStartDate']) && isset($data['EventEndDate'])) { $date_provided = true; delete_post_meta($event_id, '_EventAllDay'); $start_date_string = "{$data['EventStartDate']} {$data['EventStartHour']}:{$data['EventStartMinute']}:00"; $end_date_string = "{$data['EventEndDate']} {$data['EventEndHour']}:{$data['EventEndMinute']}:00"; if (isset($data['EventStartMeridian'])) { $start_date_string .= " {$data['EventStartMeridian']}"; } if (isset($data['EventEndMeridian'])) { $end_date_string .= " {$data['EventEndMeridian']}"; } $data['EventStartDate'] = date(Tribe__Events__Date_Utils::DBDATETIMEFORMAT, strtotime($start_date_string)); $data['EventEndDate'] = date(Tribe__Events__Date_Utils::DBDATETIMEFORMAT, strtotime($end_date_string)); } if (!$date_provided) { $data['EventStartDate'] = get_post_meta($event_id, '_EventStartDate', true); $data['EventEndDate'] = get_post_meta($event_id, '_EventEndDate', true); return $data; } // If a specific timezone was not specified, default to the sitewide timezone if (!isset($data['EventTimezone'])) { $data['EventTimezone'] = Tribe__Events__Timezones::wp_timezone_string(); } // Additionally store datetimes in UTC $data['EventStartDateUTC'] = Tribe__Events__Timezones::to_utc($data['EventStartDate'], $data['EventTimezone']); $data['EventEndDateUTC'] = Tribe__Events__Timezones::to_utc($data['EventEndDate'], $data['EventTimezone']); $data['EventTimezoneAbbr'] = Tribe__Events__Timezones::abbr($data['EventStartDate'], $data['EventTimezone']); // sanity check that start date < end date $start_timestamp = strtotime($data['EventStartDate']); $end_timestamp = strtotime($data['EventEndDate']); if ($start_timestamp > $end_timestamp) { $data['EventEndDate'] = $data['EventStartDate']; } $data['EventDuration'] = strtotime($data['EventEndDate']) - $start_timestamp; return $data; }