Advertisement
Guest User

Save some queries with the class Tribe_Events_Month_Template

a guest
Jul 20th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.57 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @for Calendar Template
  4.  * This file contains the hook logic required to create an effective calendar month view.
  5.  *
  6.  * @package TribeEventsCalendar
  7.  * @since  2.1
  8.  * @author Modern Tribe Inc.
  9.  *
  10.  */
  11.  
  12. if ( !defined('ABSPATH') ) { die('-1'); }
  13.  
  14. if( !class_exists('Tribe_Events_Month_Template')){
  15.     /**
  16.      * Grid view template class
  17.      */
  18.     class Tribe_Events_Month_Template extends Tribe_Template_Factory {
  19.         private static $has_posts = array();
  20.  
  21.         /**
  22.          * @param string $date
  23.          * @return WP_Query
  24.          */
  25.         private function get_daily_events( $date ) {
  26.             global $wp_query;
  27.             $tribe_ecp = TribeEvents::instance();
  28.  
  29.             $post_status = is_user_logged_in() ? array( 'publish', 'private' ) : 'publish';
  30.  
  31.             //// Changes start here
  32.             $args = wp_parse_args( array(
  33.                 // setup our own custom hide upcoming
  34.                 'post__not_in' => self::$hide_upcoming_ids,
  35.                 'hide_upcoming' => false,
  36.                 'posts_per_page' => self::$posts_per_page_limit,
  37.                 'orderby' => 'menu_order',
  38.                 'order' => 'ASC',
  39.                 'post_status' => $post_status,
  40.                 'eventDisplay' => 'custom',
  41.                 'no_found_rows' => true
  42.             ), self::$args );
  43.  
  44.             // If we know this date does not have events, build a "empty WP_Query" that can be cached for all other "empty days"
  45.             if ( isset($this->has_posts[$date]) ) {
  46.                 $args = array_merge( $args, array(
  47.                     'eventDate' => $date,
  48.                     'start_date' => tribe_event_beginning_of_day( $date ),
  49.                     'end_date' => tribe_event_end_of_day( $date ),
  50.                 ) );
  51.                 if ( is_tax( $tribe_ecp->get_event_taxonomy() ) ) {
  52.                     $cat = get_term_by( 'slug', get_query_var( 'term' ), $tribe_ecp->get_event_taxonomy() );
  53.                     $args['eventCat'] = (int) $cat->term_id;
  54.                 }
  55.             }
  56.             else
  57.                 $args['p'] = 965060879876;  // Now pray that this one does not exist
  58.             //// Changes stop here
  59.  
  60.             $cache = new TribeEventsCache();
  61.             $cache_key = 'daily_events_'.serialize($args);
  62.             $found = $cache->get($cache_key, 'save_post');
  63.             if ( $found && is_a($found, 'WP_Query') ) {
  64.                 // return $found;
  65.             }
  66.  
  67.             $result = TribeEventsQuery::getEvents( $args, true );
  68.             $cache->set($cache_key, $result, self::$cache_expiration, 'save_post');
  69.             return $result;
  70.         }
  71.  
  72.         /**
  73.          * Sets up an array of $days based on the current query, that can be used in the calendar loop
  74.          *
  75.          * @return void
  76.          * @since 3.0
  77.          **/
  78.         public function setup_view() {
  79.  
  80.             $tribe_ecp = TribeEvents::instance();
  81.             $tribe_ecp->date = isset( self::$args['eventDate'] ) ? self::$args['eventDate'] : tribe_get_month_view_date();
  82.  
  83.             // get all upcoming ids to hide so we're not querying 31 times
  84.             self::$hide_upcoming_ids = TribeEventsQuery::getHideFromUpcomingEvents();
  85.  
  86.             list( $year, $month ) = explode( '-', $tribe_ecp->date );
  87.  
  88.             $first_date_of_month = mktime( 12, 0, 0, $month, 1, $year ); // 1st day of month as unix stamp
  89.  
  90.             //// Changes start here
  91.             // The idea is to fetch all events at once. This way we can tell which days have no events and use the cache.
  92.             $first_day_of_month  = date('Y-m-01', $first_date_of_month);
  93.             $last_day_of_month   = date('Y-m-t', $first_date_of_month);
  94.  
  95.             $post_status = is_user_logged_in() ? array( 'publish', 'private' ) : 'publish';
  96.             $args = array(
  97.                 'post_status'   => $post_status,
  98.                 'posts_per_page'=> -1,
  99.                 'eventDate'     => $first_day_of_month,
  100.                 'start_date'    => tribe_event_beginning_of_day( $first_day_of_month ),
  101.                 'end_date'      => tribe_event_end_of_day( $last_day_of_month ),
  102.                 'post__not_in'  => self::$hide_upcoming_ids,
  103.                 'eventDisplay'  => 'custom',
  104.                 'hide_upcoming' => false,
  105.                 'no_found_rows' => true,
  106.                 'orderby'       => 'menu_order',
  107.             );
  108.  
  109.             if ( is_tax( $tribe_ecp->get_event_taxonomy() ) ) {
  110.                 $cat = get_term_by( 'slug', get_query_var( 'term' ), $tribe_ecp->get_event_taxonomy() );
  111.                 $args['tax_query'] = array(
  112.                     array(
  113.                         'taxonomy'  => TribeEvents::TAXONOMY,
  114.                         'field'     => 'id',
  115.                         'terms'     => (int) $cat->term_id
  116.                     )
  117.                 );
  118.             }
  119.  
  120.             // Get all events of this month
  121.             $result = TribeEventsQuery::getEvents( $args, true );
  122.  
  123.             // Store the info
  124.             if ( count($result->posts) ) {
  125.                 foreach ( $result->posts as $r ) {
  126.                     $this->has_posts[reset(explode(' ', $r->EventStartDate))] = 1;
  127.                 }
  128.             }
  129.             unset($result);
  130.             //// Changes stop here
  131.  
  132.             $startOfWeek = get_option( 'start_of_week', 0 );
  133.  
  134.             self::$event_daily_counts = self::get_daily_counts($first_date_of_month);
  135.  
  136.             if ( empty(self::$tribe_bar_args) ) {
  137.                 foreach ( $_REQUEST as $key => $value ) {
  138.                     if ( $value && strpos($key, 'tribe-bar-') === 0 && $key != 'tribe-bar-date' ) {
  139.                         self::$tribe_bar_args[$key] = $value;
  140.                     }
  141.                 }
  142.             }
  143.  
  144.             // Var'ng up days, months and years
  145.             self::$today = date_i18n( 'd' );
  146.             self::$current_month = date_i18n( 'm' );
  147.             self::$current_year = date_i18n( 'Y' );
  148.  
  149.             // single dimensional array of days for the month
  150.             $days = array();
  151.  
  152.             // setup counters
  153.             $rawOffset = date( 'w', $first_date_of_month ) - $startOfWeek;
  154.             $prev_month_offset = (int) ( ( $rawOffset < 0 ) ? $rawOffset + 7 : $rawOffset ); // month begins on day x
  155.             $days_in_month = (int) date( 't', intval( $first_date_of_month ) );
  156.             $days_in_calendar =  $days_in_month + $prev_month_offset;
  157.             while ($days_in_calendar % 7 > 0) {
  158.                 $days_in_calendar++;
  159.             }
  160.             $week = 0;
  161.             $cur_calendar_day = 0;
  162.  
  163.             // fill month with required days for previous month
  164.             if ( $prev_month_offset > 0 )
  165.                 $days = array_fill( 0, $prev_month_offset, array( 'date' => 'previous' ) );
  166.  
  167.             // get $cur_calendar_day up to speed
  168.             $cur_calendar_day += $prev_month_offset;
  169.  
  170.             // add days for this month
  171.             for ($i = 0; $i < $days_in_month; $i++) {
  172.                 $day = $i + 1;
  173.                 $date = date( 'Y-m-d', strtotime("$year-$month-$day"));
  174.  
  175.                 $total_events = ! empty( self::$event_daily_counts[$date] ) ? self::$event_daily_counts[$date] : 0;
  176.  
  177.                 $days[] = array(
  178.                     'daynum'       => $day,
  179.                     'date'         => $date,
  180.                     'events'       => self::get_daily_events( $date ),
  181.                     'total_events' => $total_events,
  182.                     'view_more'    => self::view_more_link( $date, self::$tribe_bar_args ),
  183.                 );
  184.             }
  185.  
  186.             // get $cur_calendar_day up to speed
  187.             $cur_calendar_day += $days_in_month;
  188.  
  189.             // check if $cur_calendar_day is less than $days_in_calendar, if so, add days for next month
  190.             if ($cur_calendar_day < $days_in_calendar) {
  191.                 $days = array_merge($days, array_fill($cur_calendar_day, $days_in_calendar - $cur_calendar_day, array('date' => 'next')));
  192.             }
  193.  
  194.             // store set of found days for use in calendar loop functions
  195.             self::$calendar_days = $days;
  196.         }
  197.  
  198.     } // class Tribe_Events_Month_Template
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement