/** * Accepts two dates and returns the number of days between them * * @param string $start_date * @param string $end_date * @param string|bool $day_cutoff * * @return int * @see TribeDateUtils::dateDiff() **/ function tribe_get_days_between($start_date, $end_date, $day_cutoff = '00:00') { if ($day_cutoff === false) { $day_cutoff = '00:00'; } elseif ($day_cutoff === true) { $day_cutoff = tribe_get_option('multiDayCutoff', '00:00'); } $start_date = new DateTime($start_date); if ($start_date < new DateTime($start_date->format('Y-m-d ' . $day_cutoff))) { $start_date->modify('-1 day'); } $end_date = new DateTime($end_date); if ($end_date <= new DateTime($end_date->format('Y-m-d ' . $day_cutoff))) { $end_date->modify('-1 day'); } return TribeDateUtils::dateDiff($start_date->format('Y-m-d ' . $day_cutoff), $end_date->format('Y-m-d ' . $day_cutoff)); }
/** * Gets the event counts for individual days. * * @param array $args * * @return array The counts array. */ public static function getEventCounts($args = array()) { global $wpdb; do_action('log', 'getEventCounts() $args', 'tribe-events-query', $args); $date = date('Y-m-d'); $defaults = array('post_type' => TribeEvents::POSTTYPE, 'start_date' => tribe_event_beginning_of_day($date), 'end_date' => tribe_event_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 TribeEventsCache(); $cache_key = 'daily_counts_and_ids_' . serialize($args); $found = $cache->get($cache_key, 'save_post'); if ($found) { do_action('log', 'cache hit ' . __LINE__, 'tribe-events-cache', $args); return $found; } do_action('log', 'no cache hit ' . __LINE__, 'tribe-events-cache', $args); $cache_key = 'month_post_ids_' . serialize($args); $found = $cache->get($cache_key, 'save_post'); if ($found && is_array($found)) { do_action('log', 'cache hit ' . __LINE__, 'tribe-events-cache', $args); $post_ids = $found; } else { do_action('log', 'no cache hit ' . __LINE__, 'tribe-events-cache', $args); $post_id_query = new WP_Query(); $post_ids = $post_id_query->query($args); do_action('log', 'final args for month view post ids', 'tribe-events-query', $post_id_query->query_vars); do_action('log', 'Month view getEventCounts SQL', 'tribe-events-query', $post_id_query->request); $cache->set($cache_key, $post_ids, TribeEventsCache::NON_PERSISTENT, 'save_post'); } do_action('log', 'Month view post ids found', 'tribe-events-query', $post_ids); $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'; do_action('log', 'raw counts args', 'tribe-events-query', $args); $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)))); do_action('log', 'raw counts query', 'tribe-events-query', $wpdb->last_query); $start_date = new DateTime($post_id_query->query_vars['start_date']); $end_date = new DateTime($post_id_query->query_vars['end_date']); $days = TribeDateUtils::dateDiff($start_date->format('Y-m-d'), $end_date->format('Y-m-d')); $term_id = isset($wp_query->query_vars[TribeEvents::TAXONOMY]) ? $wp_query->query_vars[TribeEvents::TAXONOMY] : null; if (is_int($term_id)) { $term = get_term_by('id', $term_id, TribeEvents::TAXONOMY); } elseif (is_string($term_id)) { $term = get_term_by('slug', $term_id, TribeEvents::TAXONOMY); } for ($i = 0, $date = $start_date; $i <= $days; $i++, $date->modify('+1 day')) { $formatted_date = $date->format('Y-m-d'); $start_of_day = strtotime(tribe_event_beginning_of_day($formatted_date)); $end_of_day = strtotime(tribe_event_end_of_day($formatted_date)) + 1; $count = 0; $_day_event_ids = array(); foreach ($raw_counts as $record) { $record_start = strtotime($record->EventStartDate); $record_end = strtotime($record->EventEndDate); /** * conditions: * event starts on this day (event start time is between start and end of day) * event ends on this day (event end time is between start and end of day) * event starts before start of day and ends after end of day (spans across this day) * 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_starts_today = $record_start >= $start_of_day && $record_start < $end_of_day; $event_ends_today = $record_end > $start_of_day && $record_end <= $end_of_day; $event_spans_across_today = $record_start < $start_of_day && $record_end > $end_of_day; if ($event_starts_today || $event_ends_today || $event_spans_across_today) { if (isset($term->term_id)) { if (!has_term($term, TribeEvents::TAXONOMY, $record->ID)) { continue; } } if (count($_day_event_ids) < apply_filters('tribe_events_month_day_limit', tribe_get_option('monthEventAmount', '3'))) { $_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 = array(); $final_event_ids = call_user_func_array('array_merge', $event_ids); $final_event_ids = array_unique($final_event_ids); do_action('log', 'updating term and postmeta caches for events', 'tribe-events-cache', $final_event_ids); update_object_term_cache($final_event_ids, TribeEvents::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 TribeEventsCache(); $cache_key = 'daily_counts_and_ids_' . serialize($args); $cache->set($cache_key, $return, TribeEventsCache::NON_PERSISTENT, 'save_post'); do_action('log', 'final event counts result', 'tribe-events-query', $return); return $return; }
/** * Accepts two dates and returns the number of days between them * * @param string $start_date * @param string $end_date * @param string|bool $day_cutoff * @return int * @author Modern Tribe * @see TribeDateUtils::dateDiff() **/ function tribe_get_days_between($start_date, $end_date, $day_cutoff = '00:00') { if ($day_cutoff === FALSE) { $day_cutoff = '00:00'; } elseif ($day_cutoff === TRUE) { $day_cutoff = tribe_get_option('multiDayCutoff', '00:00'); } $start_date = new DateTime($start_date); if ($start_date < new DateTime($start_date->format('Y-m-d ' . $day_cutoff))) { $start_date->modify('-1 day'); } $end_date = new DateTime($end_date); if ($end_date <= new DateTime($end_date->format('Y-m-d ' . $day_cutoff))) { $end_date->modify('-1 day'); } // This doesn't work on php 5.2 // $interval = $start_date->diff($end_date); return TribeDateUtils::dateDiff($start_date->format('Y-m-d ' . $day_cutoff), $end_date->format('Y-m-d ' . $day_cutoff)); }