/** * 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; } } }