Guest User

Advance the minical to the month of next event (TEC3)

a guest
Aug 7th, 2013
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  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.     /**
  11.      * Sets up auto advance for minicalendar widgets. If an optional target date is provided that will be used to
  12.      * set the month.
  13.      *
  14.      * @param bool $target_date
  15.      */
  16.     public function __construct($target_date = false) {
  17.         if (is_string($target_date)) $this->set_target_date($target_date);
  18.         add_filter('widget_display_callback', array($this, 'advance_minical'), 20, 2);
  19.     }
  20.  
  21.     /**
  22.      * Basic check to help filter out spurious date formats.
  23.      *
  24.      * @param $date
  25.      */
  26.     protected function set_target_date($date) {
  27.         if (1 === preg_match('#^\d{4}-\d{2}(-\d{2})?$#', $date))
  28.             $this->target_date = $date;
  29.     }
  30.  
  31.     public function advance_minical($instance, $widget) {
  32.         if ('tribe-mini-calendar' !== $widget->id_base) return $instance;
  33.         if (isset($instance['eventDate'])) return $instance;
  34.         $instance['eventDate'] = $this->determine_date();
  35.         return $instance;
  36.     }
  37.  
  38.     protected function determine_date() {
  39.         return (false !== $this->target_date) ? $this->target_date : $this->next_upcoming_date();
  40.     }
  41.  
  42.     protected function next_upcoming_date() {
  43.         $next_event = tribe_get_events(array(
  44.             'eventDisplay' => 'upcoming',
  45.             'posts_per_page' => 1
  46.         ));
  47.  
  48.         if (empty($next_event) || ! isset($next_event[0]->EventStartDate)) return date('Y-m');
  49.         return substr($next_event[0]->EventStartDate, 0, 7); // Pull out the yyyy-dd portion
  50.     }
  51. }
  52.  
  53.  
  54. /**
  55.  * If we don't pass a parameter it will try to advance the calendar to the same month as the next upcoming event.
  56.  * However, we can also pass in a specific month:
  57.  *
  58.  *     new Tribe_Advance_Minical('2014-12'); // Set it to December 2014
  59.  */
  60. new Tribe_Advance_Minical();
Advertisement
Add Comment
Please, Sign In to add comment