/** * 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; }
/** * Break the $wp_query post loop apart into sorted events by type * * @return void * @since 3.0 * @author tim@imaginesimplicty.com */ function setup_loop() { global $wp_query; self::$events = (object) array('all_day_map' => array(), 'all_day' => array(), 'hourly' => array(), 'hours' => array('start' => null, 'end' => null)); // get it started off with at least 1 row self::$events->all_day_map[] = array_fill(self::$start_of_week, self::$week_length, null); foreach ($wp_query->posts as $event_key_id => $event) { // convert the start and end dates of the event into timestamps $event_start_time = strtotime($event->EventStartDate); $event_end_time = strtotime($event->EventEndDate); // if the event start time is greater than the start time of the week then we use the event date otherwise use the beginning of the week date $start_date_compare = strtotime(self::$start_of_week_date) < $event_start_time ? $event->EventStartDate : self::$start_of_week_date; $end_date_compare = strtotime(self::$end_of_week_date) > $event_end_time ? $event->EventEndDate : self::$end_of_week_date; // convert the starting event or week date into day of the week $event_start_day_of_week = date('w', strtotime($start_date_compare)); // determine the number of days between the starting date and the end of the event $event->days_between = tribe_get_days_between($start_date_compare, $end_date_compare); // make sure that our days between will not extend past the end of the week $event->days_between = $event->days_between >= self::$week_length - $event_start_day_of_week ? self::$week_length - $event_start_day_of_week : (int) $event->days_between; // if this is an all day event if (tribe_get_event_meta($event->ID, '_EventAllDay')) { // let's build our hashtable for add day events foreach (self::$events->all_day_map as $hash_id => $days) { // set bool for if we should inset the event id on the current hash row $insert_current_row = false; // loop through the columns of this hash row for ($n = $event_start_day_of_week; $n <= $event_start_day_of_week + $event->days_between; $n++) { // create an offset id for cases where the day of the week is less the starting day of the week // thus looping the beginning days of the start week starting at 0 around to the end $all_day_offset = $n < self::$start_of_week ? self::$week_length + $n : $n; // check for hash collision and setup bool for going to the next row if we can't fit it on this row if (!empty(self::$events->all_day_map[$hash_id][$all_day_offset]) || self::$events->all_day_map[$hash_id][$all_day_offset] == '0') { $insert_current_row = true; break; } else { $insert_current_row = false; } } // if we should actually insert a new row vs going to the next row if ($insert_current_row && count(self::$events->all_day_map) == $hash_id + 1) { // create a new row and fill with week day columns self::$events->all_day_map[] = array_fill(self::$start_of_week, self::$week_length, null); // change the row id to the last row $hash_id = count(self::$events->all_day_map) - 1; } else { if ($insert_current_row) { // nullify the hash id $hash_id = null; } } // if we still have a hash id then fill the row with the event id if (!is_null($hash_id)) { // loop through each week day we want the event to be inserted for ($n = $event_start_day_of_week; $n <= $event_start_day_of_week + $event->days_between; $n++) { // create an offset id for cases where the day of the week is less the starting day of the week // thus looping the beginning days of the start week starting at 0 around to the end $all_day_offset = $n < self::$start_of_week ? self::$week_length + $n : $n; // add the event array key id into the week day column self::$events->all_day_map[$hash_id][$all_day_offset] = $event_key_id; } // break the hashtable since we have successfully added the event into a row break; } } // using the array key for the event id for uniqueness of recurring events self::$events->all_day[$event_key_id] = $event; } else { $start_hour = date('G', strtotime($event->EventStartDate)); $end_hour = date('G', strtotime($event->EventEndDate)); if (is_null(self::$events->hours['start']) || $start_hour < self::$events->hours['start']) { self::$events->hours['start'] = $start_hour; } if (is_null(self::$events->hours['end']) || $end_hour > self::$events->hours['end']) { self::$events->hours['end'] = $end_hour; } self::$events->hourly[$event_key_id] = $event; } } }
/** * 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; }