Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @for Calendar Template
- * This file contains the hook logic required to create an effective calendar month view.
- *
- * @package TribeEventsCalendar
- * @since 2.1
- * @author Modern Tribe Inc.
- *
- */
- if ( !defined('ABSPATH') ) { die('-1'); }
- if( !class_exists('Tribe_Events_Month_Template')){
- /**
- * Grid view template class
- */
- class Tribe_Events_Month_Template extends Tribe_Template_Factory {
- private static $has_posts = array();
- /**
- * @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';
- //// Changes start here
- $args = wp_parse_args( array(
- // setup our own custom hide upcoming
- '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 we know this date does not have events, build a "empty WP_Query" that can be cached for all other "empty days"
- if ( isset($this->has_posts[$date]) ) {
- $args = array_merge( $args, array(
- 'eventDate' => $date,
- 'start_date' => tribe_event_beginning_of_day( $date ),
- 'end_date' => tribe_event_end_of_day( $date ),
- ) );
- 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;
- }
- }
- else
- $args['p'] = 965060879876; // Now pray that this one does not exist
- //// Changes stop here
- $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;
- }
- /**
- * Sets up an array of $days based on the current query, that can be used in the calendar loop
- *
- * @return void
- * @since 3.0
- **/
- public function setup_view() {
- $tribe_ecp = TribeEvents::instance();
- $tribe_ecp->date = isset( self::$args['eventDate'] ) ? self::$args['eventDate'] : tribe_get_month_view_date();
- // get all upcoming ids to hide so we're not querying 31 times
- self::$hide_upcoming_ids = TribeEventsQuery::getHideFromUpcomingEvents();
- list( $year, $month ) = explode( '-', $tribe_ecp->date );
- $first_date_of_month = mktime( 12, 0, 0, $month, 1, $year ); // 1st day of month as unix stamp
- //// Changes start here
- // The idea is to fetch all events at once. This way we can tell which days have no events and use the cache.
- $first_day_of_month = date('Y-m-01', $first_date_of_month);
- $last_day_of_month = date('Y-m-t', $first_date_of_month);
- $post_status = is_user_logged_in() ? array( 'publish', 'private' ) : 'publish';
- $args = array(
- 'post_status' => $post_status,
- 'posts_per_page'=> -1,
- 'eventDate' => $first_day_of_month,
- 'start_date' => tribe_event_beginning_of_day( $first_day_of_month ),
- 'end_date' => tribe_event_end_of_day( $last_day_of_month ),
- 'post__not_in' => self::$hide_upcoming_ids,
- 'eventDisplay' => 'custom',
- 'hide_upcoming' => false,
- 'no_found_rows' => true,
- 'orderby' => 'menu_order',
- );
- if ( is_tax( $tribe_ecp->get_event_taxonomy() ) ) {
- $cat = get_term_by( 'slug', get_query_var( 'term' ), $tribe_ecp->get_event_taxonomy() );
- $args['tax_query'] = array(
- array(
- 'taxonomy' => TribeEvents::TAXONOMY,
- 'field' => 'id',
- 'terms' => (int) $cat->term_id
- )
- );
- }
- // Get all events of this month
- $result = TribeEventsQuery::getEvents( $args, true );
- // Store the info
- if ( count($result->posts) ) {
- foreach ( $result->posts as $r ) {
- $this->has_posts[reset(explode(' ', $r->EventStartDate))] = 1;
- }
- }
- unset($result);
- //// Changes stop here
- $startOfWeek = get_option( 'start_of_week', 0 );
- self::$event_daily_counts = self::get_daily_counts($first_date_of_month);
- if ( empty(self::$tribe_bar_args) ) {
- foreach ( $_REQUEST as $key => $value ) {
- if ( $value && strpos($key, 'tribe-bar-') === 0 && $key != 'tribe-bar-date' ) {
- self::$tribe_bar_args[$key] = $value;
- }
- }
- }
- // Var'ng up days, months and years
- self::$today = date_i18n( 'd' );
- self::$current_month = date_i18n( 'm' );
- self::$current_year = date_i18n( 'Y' );
- // single dimensional array of days for the month
- $days = array();
- // setup counters
- $rawOffset = date( 'w', $first_date_of_month ) - $startOfWeek;
- $prev_month_offset = (int) ( ( $rawOffset < 0 ) ? $rawOffset + 7 : $rawOffset ); // month begins on day x
- $days_in_month = (int) date( 't', intval( $first_date_of_month ) );
- $days_in_calendar = $days_in_month + $prev_month_offset;
- while ($days_in_calendar % 7 > 0) {
- $days_in_calendar++;
- }
- $week = 0;
- $cur_calendar_day = 0;
- // fill month with required days for previous month
- if ( $prev_month_offset > 0 )
- $days = array_fill( 0, $prev_month_offset, array( 'date' => 'previous' ) );
- // get $cur_calendar_day up to speed
- $cur_calendar_day += $prev_month_offset;
- // add days for this month
- for ($i = 0; $i < $days_in_month; $i++) {
- $day = $i + 1;
- $date = date( 'Y-m-d', strtotime("$year-$month-$day"));
- $total_events = ! empty( self::$event_daily_counts[$date] ) ? self::$event_daily_counts[$date] : 0;
- $days[] = array(
- 'daynum' => $day,
- 'date' => $date,
- 'events' => self::get_daily_events( $date ),
- 'total_events' => $total_events,
- 'view_more' => self::view_more_link( $date, self::$tribe_bar_args ),
- );
- }
- // get $cur_calendar_day up to speed
- $cur_calendar_day += $days_in_month;
- // check if $cur_calendar_day is less than $days_in_calendar, if so, add days for next month
- if ($cur_calendar_day < $days_in_calendar) {
- $days = array_merge($days, array_fill($cur_calendar_day, $days_in_calendar - $cur_calendar_day, array('date' => 'next')));
- }
- // store set of found days for use in calendar loop functions
- self::$calendar_days = $days;
- }
- } // class Tribe_Events_Month_Template
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement