protected function setup_where_clause() { /** @var wpdb $wpdb */ global $wpdb; $clauses = array(); $values = array_map('intval', $this->currentValue); $values = implode(',', $values); $eod_cutoff = tribe_get_option('multiDayCutoff', '00:00'); if ($eod_cutoff != '00:00') { $eod_time_difference = TribeDateUtils::timeBetween('1/1/2014 00:00:00', "1/1/2014 {$eod_cutoff}:00"); $start_date = "DATE_SUB({$wpdb->postmeta}.meta_value, INTERVAL {$eod_time_difference} SECOND)"; $end_date = "DATE_SUB(tribe_event_end_date.meta_value, INTERVAL {$eod_time_difference} SECOND)"; } else { $start_date = "{$wpdb->postmeta}.meta_value"; $end_date = "tribe_event_end_date.meta_value"; } $clauses[] = "(DAYOFWEEK({$start_date}) IN ({$values}))"; // is it on at least 7 days (first day is 0) $clauses[] = "(DATEDIFF({$end_date}, {$start_date}) >=6)"; // determine if the start of the nearest matching day is between the start and end dates $distance_to_day = array(); foreach ($this->currentValue as $day_of_week_index) { $day_of_week_index = (int) $day_of_week_index; $distance_to_day[] = "MOD( 7 + {$day_of_week_index} - DAYOFWEEK({$start_date}), 7 )"; } if (count($distance_to_day) > 1) { $distance_to_next_matching_day = "LEAST(" . implode(',', $distance_to_day) . ")"; } else { $distance_to_next_matching_day = reset($distance_to_day); } $clauses[] = "(DATE(DATE_ADD({$start_date}, INTERVAL {$distance_to_next_matching_day} DAY)) < {$end_date})"; $this->whereClause = ' AND (' . implode(' OR ', $clauses) . ')'; }
public function iCalFeed($post = null, $eventCatSlug = null, $eventDate = null) { $tribeEvents = TribeEvents::instance(); $postId = $post ? $post->ID : null; $getstring = isset($_GET['ical']) ? $_GET['ical'] : null; $wpTimezoneString = get_option('timezone_string'); $postType = TribeEvents::POSTTYPE; $events = ''; $lastBuildDate = ''; $eventsTestArray = array(); $blogHome = get_bloginfo('url'); $blogName = get_bloginfo('name'); $includePosts = $postId ? '&include=' . $postId : ''; $eventsCats = $eventCatSlug ? '&' . TribeEvents::TAXONOMY . '=' . $eventCatSlug : ''; if ($post) { $eventPosts = array(); $eventPosts[] = $post; } else { $eventPosts = get_posts('posts_per_page=-1&post_type=' . $postType . $includePosts . $eventsCats); } foreach ($eventPosts as $eventPost) { if ($eventDate) { $duration = TribeDateUtils::timeBetween($eventPost->EventStartDate, $eventPost->EventEndDate); $startDate = TribeDateUtils::addTimeToDate($eventDate, TribeDateUtils::timeOnly($eventPost->EventStartDate)); $endDate = TribeDateUtils::dateAndTime(strtotime($startDate) + $duration, true); } else { $startDate = $eventPost->EventStartDate; $endDate = $eventPost->EventEndDate; } // convert 2010-04-08 00:00:00 to 20100408T000000 or YYYYMMDDTHHMMSS $startDate = str_replace(array('-', ' ', ':'), array('', 'T', ''), $startDate); $endDate = str_replace(array('-', ' ', ':'), array('', 'T', ''), $endDate); if (get_post_meta($eventPost->ID, '_EventAllDay', true) == 'yes') { $startDate = substr($startDate, 0, 8); $endDate = substr($endDate, 0, 8); // endDate bumped ahead one day to counter iCal's off-by-one error $endDateStamp = strtotime($endDate); $endDate = date('Ymd', $endDateStamp + 86400); $type = 'DATE'; } else { $type = 'DATE-TIME'; } $description = preg_replace("/[\n\t\r]/", ' ', strip_tags($eventPost->post_content)); // add fields to iCal output $item = array(); $item[] = "DTSTART;VALUE={$type}:" . $startDate; $item[] = "DTEND;VALUE={$type}:" . $endDate; $item[] = 'DTSTAMP:' . date('Ymd\\THis', time()); $item[] = 'CREATED:' . str_replace(array('-', ' ', ':'), array('', 'T', ''), $eventPost->post_date); $item[] = 'LAST-MODIFIED:' . str_replace(array('-', ' ', ':'), array('', 'T', ''), $eventPost->post_modified); $item[] = 'UID:' . $eventPost->ID . '-' . strtotime($startDate) . '-' . strtotime($endDate) . '@' . $blogHome; $item[] = 'SUMMARY:' . $eventPost->post_title; $item[] = 'DESCRIPTION:' . str_replace(',', '\\,', $description); $item[] = 'LOCATION:' . html_entity_decode($tribeEvents->fullAddressString($eventPost->ID), ENT_QUOTES); $item[] = 'URL:' . get_permalink($eventPost->ID); $item = apply_filters('tribe_ical_feed_item', $item, $eventPost); $events .= "BEGIN:VEVENT\n" . implode("\n", $item) . "\nEND:VEVENT\n"; } header('Content-type: text/calendar'); header('Content-Disposition: attachment; filename="iCal-TribeEvents.ics"'); $content = "BEGIN:VCALENDAR\n"; $content .= "VERSION:2.0\n"; $content .= 'PRODID:-//' . $blogName . ' - ECPv' . TribeEvents::VERSION . "//NONSGML v1.0//EN\n"; $content .= "CALSCALE:GREGORIAN\n"; $content .= "METHOD:PUBLISH\n"; $content .= 'X-WR-CALNAME:' . apply_filters('tribe_ical_feed_calname', $blogName) . "\n"; $content .= 'X-ORIGINAL-URL:' . $blogHome . "\n"; $content .= 'X-WR-CALDESC:Events for ' . $blogName . "\n"; if ($wpTimezoneString) { $content .= 'X-WR-TIMEZONE:' . $wpTimezoneString . "\n"; } $content = apply_filters('tribe_ical_properties', $content); $content .= $events; $content .= 'END:VCALENDAR'; echo $content; exit; }