SHARE
TWEET

Untitled

BakerMan Feb 18th, 2014 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Tries to force the minicalendar widget to show the month of the next upcoming event by default, rather
  3.  * than simply showing the current month (which might be empty).
  4.  */
  5. class Tribe_Advance_Minical
  6. {
  7.         protected $target_date = false;
  8.  
  9.         /**
  10.          * Sets up auto advance for minicalendar widgets. If an optional target date is provided that will be used to
  11.          * set the month.
  12.          *
  13.          * @param bool $target_date
  14.          */
  15.         public function __construct($target_date = false) {
  16.                 if (is_string($target_date)) $this->set_target_date($target_date);
  17.                 add_filter('widget_display_callback', array($this, 'advance_minical'), 20, 2);
  18.         }
  19.  
  20.         /**
  21.          * Basic check to help filter out spurious date formats.
  22.          *
  23.          * @param $date
  24.          */
  25.         protected function set_target_date($date) {
  26.                 if (1 === preg_match('#^\d{4}-\d{2}(-\d{2})?$#', $date))
  27.                         $this->target_date = $date;
  28.         }
  29.  
  30.         public function advance_minical($instance, $widget) {
  31.                 if ('tribe-mini-calendar' !== $widget->id_base || isset($instance['eventDate'])) return $instance;
  32.                 add_action('tribe_events_before_view', array($this, 'modify_list_query'), 5);
  33.                 $instance['eventDate'] = $this->determine_date();
  34.                 return $instance;
  35.         }
  36.  
  37.         public function modify_list_query($template) {
  38.                 if (false === strpos($template, 'mini-calendar/list.php')) return;
  39.                 add_action('parse_query', array($this, 'amend_list_query'));
  40.         }
  41.  
  42.         public function amend_list_query($query) {
  43.                 remove_action('parse_query', array($this, 'amend_list_query')); // Run this once only
  44.                 $the_query = $query->query_vars;
  45.  
  46.                 $the_query['start_date'] = $this->determine_date() . '-01';
  47.                 $last_day = TribeDateUtils::getLastDayOfMonth(strtotime($the_query['start_date']));
  48.                 $the_query['end_date'] = substr_replace($the_query['start_date'], $last_day, -2);
  49.                 $the_query['end_date'] = TribeDateUtils::endOfDay($the_query['end_date']);
  50.  
  51.                 $query->query_vars = $the_query;
  52.         }
  53.  
  54.         protected function determine_date() {
  55.                 return (false !== $this->target_date) ? $this->target_date : $this->next_upcoming_date();
  56.         }
  57.  
  58.         protected function next_upcoming_date() {
  59.                 $next_event = tribe_get_events(array(
  60.                         'eventDisplay' => 'upcoming',
  61.                         'posts_per_page' => 1
  62.                 ));
  63.  
  64.                 if (empty($next_event) || ! isset($next_event[0]->EventStartDate)) return date('Y-m');
  65.                 return substr($next_event[0]->EventStartDate, 0, 7);
  66.         }
  67. }
  68.  
  69. /**
  70.  * If we don't pass a parameter it will try to advance the calendar to the same month as the next upcoming event.
  71.  * However, we can also pass in a specific month:
  72.  *
  73.  *     new Tribe_Advance_Minical('2014-12'); // Set it to December 2014
  74.  */
  75. new Tribe_Advance_Minical();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top