/** * Retrieve the cached html from transients, set class property * * @uses tribe_events_before_view hook */ public function get() { if (isset($this->html)) { return $this->html; } $this->html = $this->cache->get_transient($this->key, $this->expiration_trigger); return $this->html; }
/** * Customized WP_Query wrapper to setup event queries with default arguments. * * @param array $args * @param bool $full * * @return array|WP_Query */ public static function getEvents($args = array(), $full = false) { $defaults = array('post_type' => TribeEvents::POSTTYPE, 'orderby' => 'event_date', 'order' => 'ASC', 'posts_per_page' => tribe_get_option('postsPerPage', 10)); $args = wp_parse_args($args, $defaults); // 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 = 'get_events_' . serialize($args); $result = $cache->get($cache_key, 'save_post'); if ($result && is_a($result, 'WP_Query')) { do_action('log', 'cache hit', 'tribe-events-cache', $args); } else { do_action('log', 'no cache hit', 'tribe-events-cache', $args); $result = new WP_Query($args); $cache->set($cache_key, $result, TribeEventsCache::NON_PERSISTENT, 'save_post'); } if (!empty($result->posts)) { if ($full) { return $result; } else { $posts = $result->posts; return $posts; } } else { if ($full) { return $result; } else { return array(); } } }
/** * A recurring event will have the base post's slug in the * 'name' query var. We need to remove that and replace it * with the correct post's ID * * @param WP_Query $query * @return void */ private function set_post_id_for_recurring_event_query($query) { $date = $query->get('eventDate'); $slug = $query->get('name'); if (empty($date) || empty($slug)) { return; // we shouldn't be here } $cache = new TribeEventsCache(); $post_id = $cache->get('single_event_' . $slug . '_' . $date, 'save_post'); if (!empty($post_id)) { unset($query->query_vars['name']); unset($query->query_vars['tribe_events']); $query->set('p', $post_id); return; } global $wpdb; $parent_sql = "SELECT ID FROM {$wpdb->posts} WHERE post_name=%s AND post_type=%s"; $parent_sql = $wpdb->prepare($parent_sql, $slug, TribeEvents::POSTTYPE); $parent_id = $wpdb->get_var($parent_sql); $parent_start = get_post_meta($parent_id, '_EventStartDate', true); if (empty($parent_start)) { return; // how does this series not have a start date? } else { $parent_start_date = date('Y-m-d', strtotime($parent_start)); $parent_start_time = date('H:i:s', strtotime($parent_start)); } if ($parent_start_date == $date) { $post_id = $parent_id; } else { $child_sql = "SELECT ID FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} m ON m.post_id=p.ID AND m.meta_key='_EventStartDate' WHERE p.post_parent=%d AND p.post_type=%s AND m.meta_value=%s"; $child_sql = $wpdb->prepare($child_sql, $parent_id, TribeEvents::POSTTYPE, $date . ' ' . $parent_start_time); $post_id = $wpdb->get_var($child_sql); } if ($post_id) { unset($query->query_vars['name']); unset($query->query_vars['tribe_events']); $query->set('p', $post_id); $cache->set('single_event_' . $slug . '_' . $date, $post_id, TribeEventsCache::NO_EXPIRATION, 'save_post'); } }
/** * Custom SQL to retrieve post_id list of events marked to be hidden from upcoming lists. * * @return array */ public static function getHideFromUpcomingEvents() { global $wpdb; $cache = new TribeEventsCache(); $cache_key = 'tribe-hide-from-upcoming-events'; $found = $cache->get($cache_key, 'save_post'); if (is_array($found)) { return $found; } // custom sql to get ids of posts that hide_upcoming_ids $hide_upcoming_ids = $wpdb->get_col("SELECT {$wpdb->postmeta}.post_id FROM {$wpdb->postmeta} WHERE {$wpdb->postmeta}.meta_key = '_EventHideFromUpcoming' AND {$wpdb->postmeta}.meta_value = 'yes'"); $hide_upcoming_ids = apply_filters('tribe_events_hide_from_upcoming_ids', $hide_upcoming_ids); $cache->set($cache_key, $hide_upcoming_ids, 3600, 'save_post'); return $hide_upcoming_ids; }
/** * Get the start dates of all instances of the event, * in ascending order * * @param int $post_id * * @return array Start times, as Y-m-d H:i:s */ public static function get_start_dates($post_id) { if (empty($post_id)) { return array(); } $cache = new TribeEventsCache(); $dates = $cache->get('event_dates_' . $post_id, 'save_post'); if (is_array($dates)) { return $dates; } /** @var wpdb $wpdb */ global $wpdb; $ancestors = get_post_ancestors($post_id); $post_id = empty($ancestors) ? $post_id : end($ancestors); $sql = "SELECT meta_value FROM {$wpdb->postmeta} m INNER JOIN {$wpdb->posts} p ON p.ID=m.post_id AND (p.post_parent=%d OR p.ID=%d) WHERE meta_key='_EventStartDate' ORDER BY meta_value ASC"; $sql = $wpdb->prepare($sql, $post_id, $post_id); $result = $wpdb->get_col($sql); $cache->set('recurrence_start_dates_' . $post_id, $result, TribeEventsCache::NO_EXPIRATION, 'save_post'); return $result; }
/** * @param string $date * @return WP_Query */ private function get_daily_events($date) { global $wp_query; $tribe_ecp = TribeEvents::instance(); $post_status = is_user_logged_in() ? array('publish', 'private') : 'publish'; $args = wp_parse_args(array('eventDate' => $date, 'start_date' => tribe_event_beginning_of_day($date), 'end_date' => tribe_event_end_of_day($date), 'post__not_in' => self::$hide_upcoming_ids, 'hide_upcoming' => false, 'posts_per_page' => self::$posts_per_page_limit, 'orderby' => 'menu_order', 'order' => 'ASC', 'post_status' => $post_status, 'eventDisplay' => 'custom', 'no_found_rows' => true), self::$args); if (is_tax($tribe_ecp->get_event_taxonomy())) { $cat = get_term_by('slug', get_query_var('term'), $tribe_ecp->get_event_taxonomy()); $args['eventCat'] = (int) $cat->term_id; } $cache = new TribeEventsCache(); $cache_key = 'daily_events_' . serialize($args); $found = $cache->get($cache_key, 'save_post'); if ($found && is_a($found, 'WP_Query')) { // return $found; } $result = TribeEventsQuery::getEvents($args, true); $cache->set($cache_key, $result, self::$cache_expiration, 'save_post'); return $result; }