Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Tries to force the minicalendar widget to show the month of the next upcoming event by default, rather
- * than simply showing the current month (which might be empty).
- */
- class Tribe_Advance_Minical
- {
- protected $target_date = false;
- /**
- * Sets up auto advance for minicalendar widgets. If an optional target date is provided that will be used to
- * set the month.
- *
- * @param bool $target_date
- */
- public function __construct($target_date = false) {
- if (is_string($target_date)) $this->set_target_date($target_date);
- add_filter('widget_display_callback', array($this, 'advance_minical'), 20, 2);
- }
- /**
- * Basic check to help filter out spurious date formats.
- *
- * @param $date
- */
- protected function set_target_date($date) {
- if (1 === preg_match('#^\d{4}-\d{2}(-\d{2})?$#', $date))
- $this->target_date = $date;
- }
- public function advance_minical($instance, $widget) {
- if ('tribe-mini-calendar' !== $widget->id_base) return $instance;
- if (isset($instance['eventDate'])) return $instance;
- $instance['eventDate'] = $this->determine_date();
- return $instance;
- }
- protected function determine_date() {
- return (false !== $this->target_date) ? $this->target_date : $this->next_upcoming_date();
- }
- protected function next_upcoming_date() {
- $next_event = tribe_get_events(array(
- 'eventDisplay' => 'upcoming',
- 'posts_per_page' => 1
- ));
- if (empty($next_event) || ! isset($next_event[0]->EventStartDate)) return date('Y-m');
- return substr($next_event[0]->EventStartDate, 0, 7); // Pull out the yyyy-dd portion
- }
- }
- /**
- * If we don't pass a parameter it will try to advance the calendar to the same month as the next upcoming event.
- * However, we can also pass in a specific month:
- *
- * new Tribe_Advance_Minical('2014-12'); // Set it to December 2014
- */
- new Tribe_Advance_Minical();
Advertisement
Add Comment
Please, Sign In to add comment