Exemplo n.º 1
1
 /**
  * Set up the self::week_days array
  *
  * @return void
  * @see $this->setup_view()
  */
 private function setup_days()
 {
     global $wp_query;
     $week_days = array();
     $day = $wp_query->get('start_date');
     // Array used for calculation of php strtotime relative dates
     $weekday_array = array(0 => 'Sunday', 1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday');
     // build an array with the "day" elements,
     // each "day" is an array that contains the date and the associated all day / hourly events
     // $day_number corresponds to the day of the week in $weekday_array
     foreach (self::$day_range as $i => $day_number) {
         // figure out the $date that we're currently looking at
         if ($day_number >= self::$day_range[0]) {
             // usually we can just get the date for the next day
             $date = date('Y-m-d', strtotime($day . "+{$i} days"));
         } else {
             // fringe case - someone starts their week in the middle of the week
             // in this case, the "day number" will be less than the first day of the week once the week has looped around
             // so we use a relative strtotime() calc
             $date = date('Y-m-d', strtotime("Next {$weekday_array[$day_number]}", strtotime($day)));
         }
         $hourly_events = array();
         $all_day_events = array();
         if ($wp_query->have_posts()) {
             // loop through all the wordpress posts and sort them into all day vs hourly for the current $date
             foreach ($wp_query->posts as $j => $event) {
                 if (tribe_event_is_on_date($date, $event)) {
                     $event->days_between = tribe_get_days_between($event->EventStartDate, $event->EventEndDate, true);
                     if (tribe_event_is_all_day($event)) {
                         $all_day_events[] = $event;
                     } else {
                         // if the event starts after the end of the hour range we're displaying, or ends before the start, skip it
                         $start_hour_today = $date . ' ' . tribe_events_week_get_hours('first-hour');
                         $end_hour_today = tribe_end_of_day($date, 'Y-m-d ') . tribe_events_week_get_hours('last-hour');
                         if (tribe_get_start_time($event, 'U') > strtotime($end_hour_today) || tribe_get_end_time($event, 'U') < strtotime($start_hour_today)) {
                             continue;
                         }
                         $hourly_events[] = $event;
                     }
                 }
             }
         }
         $display_format = apply_filters('tribe_events_pro_week_header_date_format', tribe_get_date_option('weekDayFormat', 'D jS'));
         $formatted_date = date_i18n($display_format, strtotime($date));
         $timestamp_date = strtotime($date);
         $timestamp_today = strtotime(current_time('Y-m-d'));
         // create the "day" element
         $week_days[] = array('date' => $date, 'day_number' => $day_number, 'formatted_date' => $formatted_date, 'is_today' => $timestamp_date == $timestamp_today ? true : false, 'is_past' => $timestamp_date < $timestamp_today ? true : false, 'is_future' => $timestamp_date > $timestamp_today ? true : false, 'hourly_events' => $hourly_events, 'all_day_events' => $all_day_events, 'has_events' => $hourly_events || $all_day_events);
     }
     self::$week_days = $week_days;
 }
 /**
  * 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;
 }
Exemplo n.º 3
0
 /**
  * This Week Query
  *
  *
  *  @return object
  */
 public static function this_week_query($this_week_query_vars)
 {
     //Only Get Private Events if user can view
     $post_status = array('publish');
     if (current_user_can('read_private_tribe_events')) {
         $post_status[] = 'private';
     }
     //Get Events with Hide From Event Listings Checked
     $hide_upcoming_ids = Tribe__Events__Query::getHideFromUpcomingEvents();
     $this_week_widget_args = array('post_type' => Tribe__Events__Main::POSTTYPE, 'tax_query' => $this_week_query_vars['tax_query'], 'eventDisplay' => 'custom', 'start_date' => $this_week_query_vars['start_date'], 'end_date' => $this_week_query_vars['end_date'], 'post_status' => $post_status, 'tribeHideRecurrence' => false, 'post__not_in' => $hide_upcoming_ids, 'tribe_render_context' => 'widget', 'posts_per_page' => -1);
     /**
      * Filter This Week Widget args
      *
      * @param array $this_week_widget_args Arguments for This Week Widget
      */
     $this_week_widget_args = apply_filters('tribe_events_pro_this_week_widget_query_args', $this_week_widget_args);
     // Get all the upcoming events for this week
     $events = tribe_get_events($this_week_widget_args, true);
     //Days Array to set events for each day
     $week_days = array();
     //Set First Day
     $day = $this_week_query_vars['start_date'];
     //Get Day Range
     $day_range = self::get_day_range();
     //Todays Date According to WordPress
     $timestamp_today = strtotime(current_time(Tribe__Date_Utils::DBDATEFORMAT));
     //Date Formats from The Events Calendar
     $display_date_format = apply_filters('tribe_events_this_week_date_format', 'jS');
     $display_day_format = apply_filters('tribe_events_this_week_day_format', 'D ');
     // Array used for calculation of php strtotime relative dates
     $weekday_array = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
     //Build an Array for Each Day
     foreach ($day_range as $i => $day_number) {
         //If Hide Weekends True then skip those days
         if ($this_week_query_vars['hide_weekends'] === 'true' && ($day_number == 0 || $day_number == 6)) {
             continue;
         }
         // figure out the $date that we're currently looking at
         if ($day_number >= $day_range[0]) {
             // usually we can just get the date for the next day
             $date = date(Tribe__Date_Utils::DBDATEFORMAT, strtotime($day . "+{$i} days"));
         } else {
             //Start Day of week in the Middle and not in typical Sunday or Monday
             $date = date(Tribe__Date_Utils::DBDATEFORMAT, strtotime("Next {$weekday_array[$day_number]}", strtotime($day)));
         }
         $this_week_events_sticky = $this_week_events = array();
         if ($events->have_posts()) {
             //loop through all events and sort based on sticky or not
             foreach ($events->posts as $j => $event) {
                 if (tribe_event_is_on_date($date, $event)) {
                     $event->days_between = tribe_get_days_between($event->EventStartDate, $event->EventEndDate, true);
                     if ($event->menu_order == -1) {
                         $this_week_events_sticky[] = $event;
                     } else {
                         $this_week_events[] = $event;
                     }
                 }
             }
         }
         //Merge the two arrays for the day only if sticky events are included for that day
         if (!empty($this_week_events_sticky) && is_array($this_week_events_sticky) && is_array($this_week_events)) {
             $this_week_events = array_merge($this_week_events_sticky, $this_week_events);
         }
         $formatted_date = date_i18n($display_date_format, strtotime($date));
         $formatted_day = date_i18n($display_day_format, strtotime($date));
         $timestamp_date = strtotime($date);
         // create the "day" element to do display in the template
         $week_days[] = array('date' => $date, 'day_number' => $day_number, 'formatted_date' => $formatted_date, 'formatted_day' => $formatted_day, 'is_today' => $timestamp_date == $timestamp_today ? true : false, 'is_past' => $timestamp_date < $timestamp_today ? true : false, 'is_future' => $timestamp_date > $timestamp_today ? true : false, 'this_week_events' => $this_week_events, 'has_events' => $this_week_events, 'total_events' => count($this_week_events), 'events_limit' => $this_week_query_vars['count'], 'view_more' => count($this_week_events) > $this_week_query_vars['count'] ? esc_url_raw(tribe_get_day_link($date)) : false);
     }
     return $week_days;
 }