public function where($where, $wp_query) { global $ecp_apm, $wpdb; // run once remove_filter('posts_where', array($this, 'where'), 10, 2); foreach ($this->active as $key => $active) { $field = ''; if ($key === 'ecp_start_date') { $field = 'tribe_event_start_date.meta_value'; } if ($key === 'ecp_end_date') { $field = 'tribe_event_end_date.meta_value'; } if (empty($field)) { continue; } $value = $active['value']; switch ($active['is']) { case 'is': $where .= $wpdb->prepare(" AND {$field} BETWEEN %s AND %s ", tribe_beginning_of_day($value), tribe_end_of_day($value)); break; case 'not': $where .= $wpdb->prepare(" AND {$field} NOT BETWEEN %s AND %s ", tribe_beginning_of_day($value), tribe_end_of_day($value)); break; case 'gte': $where .= $wpdb->prepare(" AND {$field} >= %s ", tribe_beginning_of_day($value)); break; case 'lte': $where .= $wpdb->prepare(" AND {$field} <= %s ", tribe_end_of_day($value)); break; } } return $where; }
/** * Provides all of the recurring events for the provided date that have the same event parent * * @since 4.0.3 * * @param int $event_id Event ID * @param string $date Date to fetch recurring events from * * @return string */ public function get_recurring_events_for_date($event_id, $date) { if (!($event = tribe_events_get_event($event_id))) { return array(); } $parent_id = empty($event->post_parent) ? $event->ID : $event->post_parent; $post_status = array('publish'); if (is_user_logged_in()) { $post_status[] = 'private'; } $args = array('start_date' => tribe_beginning_of_day($date), 'end_date' => tribe_end_of_day($date), 'post_status' => $post_status, 'post_parent' => $parent_id, 'tribeHideRecurrence' => false); $events = tribe_get_events($args); return $events; }
/** * Provides all of the recurring events for the provided date that have the same event parent * * @since 4.0.3 * * @param int $event_id Event ID * @param string $date Date to fetch recurring events from * * @return string */ public function get_recurring_events_for_date($event_id, $date) { if (!($event = tribe_events_get_event($event_id))) { return array(); } $parent_id = empty($event->post_parent) ? $event->ID : $event->post_parent; $post_status = array('publish'); if (is_user_logged_in()) { $post_status[] = 'private'; } $args = array('start_date' => tribe_beginning_of_day($date), 'end_date' => tribe_end_of_day($date), 'post_status' => $post_status, 'tribeHideRecurrence' => false); // we want event times regardless of whether or not the event is a parent or a child // recurring event. We have to fetch those slightly differently depending on which // it is if (empty($event->post_parent)) { // we're looking at the master event, so grab the info via the ID $args['p'] = $parent_id; } else { // we're looking at a child event, so grab the info via post_parent $args['post_parent'] = $parent_id; } $events = tribe_get_events($args); return $events; }
/** * Retrieves beginning/end times for a given date * * @param string $date Y-m-d date string * @param string $key Key of cached data to retrieve * * return string|int */ private function get_cutoff_details($date, $key) { static $beginnings_and_ends = array(); if (empty($beginnings_and_ends[$date])) { $beginnings_and_ends[$date] = array('beginning' => tribe_beginning_of_day($date), 'end' => tribe_end_of_day($date)); $beginnings_and_ends[$date]['beginning_timestamp'] = strtotime($beginnings_and_ends[$date]['beginning']); $beginnings_and_ends[$date]['end_timestamp'] = strtotime($beginnings_and_ends[$date]['end']); } return $beginnings_and_ends[$date][$key]; }
/** * Account for :30 EOD cutoffs, which break week view * * @param $date * @param string $format * * @return bool|string */ protected static function get_rounded_beginning_of_day($date, $format = 'U') { $beginning_of_day = tribe_beginning_of_day($date, 'U'); reset(self::$hour_range); $date = max($beginning_of_day, strtotime($date . ' ' . tribe_events_week_get_hours('first-hour'))); $date = date('Y-m-d H:00:00', $date); $date = date($format, strtotime($date)); return $date; }
/** * Given a date and an event, returns true or false if the event is happening on that date * This function properly adjusts for the EOD cutoff and multi-day events * * @param null $date * @param null $event * * @return mixed|void */ function tribe_event_is_on_date($date = null, $event = null) { if (null === $date) { $date = current_time('mysql'); } if (null === $event) { global $post; $event = $post; if (empty($event)) { _doing_it_wrong(__FUNCTION__, esc_html__('The function needs to be passed an $event or used in the loop.', 'the-events-calendar')); return false; } } $start_of_day = tribe_beginning_of_day($date, 'U'); $end_of_day = tribe_end_of_day($date, 'U'); $event_start = tribe_get_start_date($event, null, 'U'); $event_end = tribe_get_end_date($event, null, 'U'); // kludge if (!empty($event->_end_date_fixed)) { // @todo remove this once we can have all day events without a start / end time $event_end = date_create(date(Tribe__Date_Utils::DBDATETIMEFORMAT, $event_end)); $event_end->modify('+1 day'); $event_end = $event_end->format('U'); } /* note: * events that start exactly on the EOD cutoff will count on the following day * events that end exactly on the EOD cutoff will count on the previous day */ $event_is_on_date = Tribe__Date_Utils::range_coincides($start_of_day, $end_of_day, $event_start, $event_end); return apply_filters('tribe_event_is_on_date', $event_is_on_date, $date, $event); }
/** * Return the details of the start/end date/time. * * The highest level means of customizing this function's output is simply to adjust the date format settings under * Events > Settings > Display, and WordPress time formats (via the General Settings admin screen). * Beyond that, however, there are two filters which can be used to exercise further control here. * * The first is 'tribe_events_event_schedule_details_formatting' which allows an array of format settings to be * altered - it's basic make-up is as a simple set of key:value pairs as follows. * * "show_end_time": for single day events only (not including all day events) it may not always be desirable to * include the end time. In that situation, this setting can be set to false and the end time will not be * displayed. * * "time": if it is undesirable to show times and only dates should be displayed then this setting can be set to * false. If it is false it will by extension cause 'show_end_time' to be false. * * The resulting string can also be caught and manipulated, or completely overridden, using the * 'tribe_events_event_schedule_details' filter, should none of the above settings be sufficient. * * @category Events * @TODO use tribe_get_datetime_format() and related functions if possible * * @param int|null $event * @param string $before * @param string $after * * @return mixed|void */ function tribe_events_event_schedule_details($event = null, $before = '', $after = '') { if (is_null($event)) { global $post; $event = $post; } if (is_numeric($event)) { $event = get_post($event); } $inner = '<span class="tribe-event-date-start">'; $format = ''; $date_without_year_format = tribe_get_date_format(); $date_with_year_format = tribe_get_date_format(true); $time_format = get_option('time_format'); $datetime_separator = tribe_get_option('dateTimeSeparator', ' @ '); $time_range_separator = tribe_get_option('timeRangeSeparator', ' - '); $settings = array('show_end_time' => true, 'time' => true); $settings = wp_parse_args(apply_filters('tribe_events_event_schedule_details_formatting', $settings), $settings); if (!$settings['time']) { $settings['show_end_time'] = false; } /** * @var $show_end_time * @var $time */ extract($settings); $format = $date_with_year_format; // if it starts and ends in the current year then there is no need to display the year if (tribe_get_start_date($event, false, 'Y') === date('Y') && tribe_get_end_date($event, false, 'Y') === date('Y')) { $format = $date_without_year_format; } if (tribe_event_is_multiday($event)) { // multi-date event $format2ndday = apply_filters('tribe_format_second_date_in_range', $format, $event); if (tribe_event_is_all_day($event)) { $inner .= tribe_get_start_date($event, true, $format); $inner .= '</span>' . $time_range_separator; $inner .= '<span class="tribe-event-date-end">'; $end_date_full = tribe_get_end_date($event, true, Tribe__Date_Utils::DBDATETIMEFORMAT); $end_date_full_timestamp = strtotime($end_date_full); // if the end date is <= the beginning of the day, consider it the previous day if ($end_date_full_timestamp <= strtotime(tribe_beginning_of_day($end_date_full))) { $end_date = tribe_format_date($end_date_full_timestamp - DAY_IN_SECONDS, false, $format2ndday); } else { $end_date = tribe_get_end_date($event, false, $format2ndday); } $inner .= $end_date; } else { $inner .= tribe_get_start_date($event, false, $format) . ($time ? $datetime_separator . tribe_get_start_date($event, false, $time_format) : ''); $inner .= '</span>' . $time_range_separator; $inner .= '<span class="tribe-event-date-end">'; $inner .= tribe_get_end_date($event, false, $format2ndday) . ($time ? $datetime_separator . tribe_get_end_date($event, false, $time_format) : ''); } } elseif (tribe_event_is_all_day($event)) { // all day event $inner .= tribe_get_start_date($event, true, $format); } else { // single day event if (tribe_get_start_date($event, false, 'g:i A') === tribe_get_end_date($event, false, 'g:i A')) { // Same start/end time $inner .= tribe_get_start_date($event, false, $format) . ($time ? $datetime_separator . tribe_get_start_date($event, false, $time_format) : ''); } else { // defined start/end time $inner .= tribe_get_start_date($event, false, $format) . ($time ? $datetime_separator . tribe_get_start_date($event, false, $time_format) : ''); $inner .= '</span>' . ($show_end_time ? $time_range_separator : ''); $inner .= '<span class="tribe-event-time">'; $inner .= $show_end_time ? tribe_get_end_date($event, false, $time_format) : ''; } } $inner .= '</span>'; /** * Provides an opportunity to modify the *inner* schedule details HTML (ie before it is * wrapped). * * @param string $inner_html the output HTML * @param int $event_id post ID of the event we are interested in */ $inner = apply_filters('tribe_events_event_schedule_details_inner', $inner, $event->ID); // Wrap the schedule text $schedule = $before . $inner . $after; /** * Provides an opportunity to modify the schedule details HTML for a specific event after * it has been wrapped in the before and after markup. * * @param string $schedule the output HTML * @param int $event_id post ID of the event we are interested in * @param string $before part of the HTML wrapper that was prepended * @param string $after part of the HTML wrapper that was appended */ return apply_filters('tribe_events_event_schedule_details', $schedule, $event->ID, $before, $after); }
/** * Gets the event counts for individual days. * * @param array $args * * @return array The counts array. */ public static function getEventCounts($args = array()) { _deprecated_function(__METHOD__, '3.10.1'); global $wpdb; $date = date('Y-m-d'); $defaults = array('post_type' => Tribe__Events__Main::POSTTYPE, 'start_date' => tribe_beginning_of_day($date), 'end_date' => tribe_end_of_day($date), 'display_type' => 'daily', 'hide_upcoming_ids' => null); $args = wp_parse_args($args, $defaults); $args['posts_per_page'] = -1; $args['fields'] = 'ids'; // remove empty args and sort by key, this increases chance of a cache hit $args = array_filter($args, array(__CLASS__, 'filter_args')); ksort($args); $cache = new Tribe__Cache(); $cache_key = 'daily_counts_and_ids_' . serialize($args); $found = $cache->get($cache_key, 'save_post'); if ($found) { return $found; } $cache_key = 'month_post_ids_' . serialize($args); $found = $cache->get($cache_key, 'save_post'); if ($found && is_array($found)) { $post_ids = $found; } else { $post_id_query = new WP_Query(); $post_ids = $post_id_query->query($args); $cache->set($cache_key, $post_ids, Tribe__Cache::NON_PERSISTENT, 'save_post'); } $counts = array(); $event_ids = array(); if (!empty($post_ids)) { switch ($args['display_type']) { case 'daily': default: global $wp_query; $output_date_format = '%Y-%m-%d %H:%i:%s'; $raw_counts = $wpdb->get_results($wpdb->prepare("\n\t\t\t\t\t\t\tSELECT \ttribe_event_start.post_id as ID,\n\t\t\t\t\t\t\t\t\ttribe_event_start.meta_value as EventStartDate,\n\t\t\t\t\t\t\t\t\tDATE_FORMAT( tribe_event_end_date.meta_value, '%1\$s') as EventEndDate,\n\t\t\t\t\t\t\t\t\t{$wpdb->posts}.menu_order as menu_order\n\t\t\t\t\t\t\tFROM {$wpdb->postmeta} AS tribe_event_start\n\t\t\t\t\t\t\t\t\tLEFT JOIN {$wpdb->posts} ON (tribe_event_start.post_id = {$wpdb->posts}.ID)\n\t\t\t\t\t\t\tLEFT JOIN {$wpdb->postmeta} as tribe_event_end_date ON ( tribe_event_start.post_id = tribe_event_end_date.post_id AND tribe_event_end_date.meta_key = '_EventEndDate' )\n\t\t\t\t\t\t\tWHERE tribe_event_start.meta_key = '_EventStartDate'\n\t\t\t\t\t\t\tAND tribe_event_start.post_id IN ( %5\$s )\n\t\t\t\t\t\t\tAND ( (tribe_event_start.meta_value >= '%3\$s' AND tribe_event_start.meta_value <= '%4\$s')\n\t\t\t\t\t\t\t\tOR (tribe_event_start.meta_value <= '%3\$s' AND tribe_event_end_date.meta_value >= '%3\$s')\n\t\t\t\t\t\t\t\tOR ( tribe_event_start.meta_value >= '%3\$s' AND tribe_event_start.meta_value <= '%4\$s')\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tORDER BY menu_order ASC, DATE(tribe_event_start.meta_value) ASC, TIME(tribe_event_start.meta_value) ASC;", $output_date_format, $output_date_format, $post_id_query->query_vars['start_date'], $post_id_query->query_vars['end_date'], implode(',', array_map('intval', $post_ids)))); $start_date = new DateTime($post_id_query->query_vars['start_date']); $end_date = new DateTime($post_id_query->query_vars['end_date']); $days = Tribe__Date_Utils::date_diff($start_date->format('Y-m-d'), $end_date->format('Y-m-d')); $term_id = isset($wp_query->query_vars[Tribe__Events__Main::TAXONOMY]) ? $wp_query->query_vars[Tribe__Events__Main::TAXONOMY] : null; $terms = array(); if (is_int($term_id)) { $terms[0] = $term_id; } elseif (is_string($term_id)) { $term = get_term_by('slug', $term_id, Tribe__Events__Main::TAXONOMY); if ($term) { $terms[0] = $term->term_id; } } if (!empty($terms) && is_tax(Tribe__Events__Main::TAXONOMY)) { $terms = array_merge($terms, get_term_children($terms[0], Tribe__Events__Main::TAXONOMY)); } for ($i = 0, $date = $start_date; $i <= $days; $i++, $date->modify('+1 day')) { $formatted_date = $date->format('Y-m-d'); $count = 0; $_day_event_ids = array(); foreach ($raw_counts as $record) { $event = new stdClass(); $event->EventStartDate = $record->EventStartDate; $event->EventEndDate = $record->EventEndDate; $per_day_limit = apply_filters('tribe_events_month_day_limit', tribe_get_option('monthEventAmount', '3')); if (tribe_event_is_on_date($formatted_date, $event)) { if (!empty($terms) && !has_term($terms, Tribe__Events__Main::TAXONOMY, $record->ID)) { continue; } if (count($_day_event_ids) < $per_day_limit) { $_day_event_ids[] = $record->ID; } $count++; } } $event_ids[$formatted_date] = $_day_event_ids; $counts[$formatted_date] = $count; } break; } // get a unique list of the event IDs that will be displayed, and update all their postmeta and term caches at once $final_event_ids = call_user_func_array('array_merge', $event_ids); $final_event_ids = array_unique($final_event_ids); update_object_term_cache($final_event_ids, Tribe__Events__Main::POSTTYPE); update_postmeta_cache($final_event_ids); } // return IDs per day and total counts per day $return = array('counts' => $counts, 'event_ids' => $event_ids); $cache = new Tribe__Cache(); $cache_key = 'daily_counts_and_ids_' . serialize($args); $cache->set($cache_key, $return, Tribe__Cache::NON_PERSISTENT, 'save_post'); return $return; }
/** * Returns formatted date for the official beginning of the day according to the Multi-day cutoff time option * * @category Events * @param string $date The date to find the beginning of the day, defaults to today * @param string $format Allows date and time formating using standard php syntax (http://php.net/manual/en/function.date.php) * * @return string */ function tribe_event_beginning_of_day($date = null, $format = 'Y-m-d H:i:s') { _deprecated_function(__FUNCTION__, '4.0', 'tribe_beginning_of_day'); return tribe_beginning_of_day($date, $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__Date_Utils::is_all_day($data['EventAllDay'])) { $data['EventAllDay'] = 'yes'; } else { $data['EventAllDay'] = 'no'; } } $datepicker_format = Tribe__Date_Utils::datepicker_formats(tribe_get_option('datepickerFormat')); if (isset($data['EventStartDate'])) { $data['EventStartDate'] = Tribe__Date_Utils::datetime_from_format($datepicker_format, $data['EventStartDate']); } if (isset($data['EventEndDate'])) { $data['EventEndDate'] = Tribe__Date_Utils::datetime_from_format($datepicker_format, $data['EventEndDate']); } if (isset($data['EventAllDay']) && 'yes' === $data['EventAllDay']) { $date_provided = true; $data['EventStartDate'] = tribe_beginning_of_day($data['EventStartDate']); $data['EventEndDate'] = tribe_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__Date_Utils::DBDATETIMEFORMAT, strtotime($start_date_string)); $data['EventEndDate'] = date(Tribe__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 if (empty($data['EventStartDateUTC'])) { $data['EventStartDateUTC'] = Tribe__Events__Timezones::to_utc($data['EventStartDate'], $data['EventTimezone']); } if (empty($data['EventEndDateUTC'])) { $data['EventEndDateUTC'] = Tribe__Events__Timezones::to_utc($data['EventEndDate'], $data['EventTimezone']); } if (empty($data['EventTimezoneAbbr'])) { $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; }
public function ajax_select_day_set_date($query) { if (isset($_POST['eventDate']) && $_POST['eventDate']) { $query->set('eventDate', $_POST['eventDate']); $query->set('eventDisplay', 'day'); $query->set('start_date', tribe_beginning_of_day($_POST['eventDate'])); $query->set('end_date', tribe_end_of_day($_POST['eventDate'])); $query->set('hide_upcoming', false); } return $query; }