Advertisement
Guest User

Tribe_Advance_Minical

a guest
Jul 5th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Tries to force the minicalendar widget to show the month of the next upcoming event by default, rather
  5. * than simply showing the current month (which might be empty).
  6. */
  7. class Tribe_Advance_Minical
  8. {
  9. protected $target_date = false;
  10.  
  11. /**
  12. * Sets up auto advance for minicalendar widgets. If an optional target date is provided that will be used to
  13. * set the month.
  14. *
  15. * @param bool $target_date
  16. */
  17. public function __construct( $target_date = false ) {
  18. if ( is_admin() ) return;
  19. $this->target_date = $target_date;
  20. add_action( 'wp_loaded', array( $this, 'set_target_date' ) );
  21. add_filter( 'widget_display_callback', array( $this, 'advance_minical' ), 20, 2 );
  22. }
  23.  
  24. /**
  25. * Basic check to help filter out spurious date formats or automatically determine
  26. * the next most appropriate date to use.
  27. */
  28. public function set_target_date() {
  29. if ( ! is_string($this->target_date) || 1 !== preg_match('#^\d{4}-\d{2}(-\d{2})?$#', $this->target_date ) )
  30. $this->target_date = $this->next_upcoming_date();
  31. }
  32.  
  33. public function advance_minical( $instance, $widget ) {
  34. if ( 'tribe-mini-calendar' !== $widget->id_base || isset( $instance['eventDate'] ) ) return $instance;
  35. if ( date( 'Y-m' ) === $this->target_date) return;
  36. add_action( 'tribe_before_get_template_part', array( $this, 'modify_list_query' ), 5 );
  37. $instance['eventDate'] = $this->target_date;
  38. return $instance;
  39. }
  40.  
  41. public function modify_list_query( $template ) {
  42. if ( false === strpos( $template, 'mini-calendar/list.php' ) ) return;
  43. add_action( 'parse_query', array( $this, 'amend_list_query' ) );
  44. }
  45.  
  46. public function amend_list_query( $query ) {
  47. remove_action( 'parse_query', array( $this, 'amend_list_query' ) ); // Run this once only
  48. $the_query = $query->query_vars;
  49.  
  50. $the_query['start_date'] = $this->target_date . '-01';
  51. $last_day = TribeDateUtils::getLastDayOfMonth( strtotime( $the_query['start_date'] ) );
  52. $the_query['end_date'] = substr_replace( $the_query['start_date'], $last_day, -2 );
  53. $the_query['end_date'] = TribeDateUtils::endOfDay( $the_query['end_date'] );
  54.  
  55. $query->query_vars = $the_query;
  56. }
  57.  
  58. protected function next_upcoming_date() {
  59. $next_event = tribe_get_events( array(
  60. 'eventDisplay' => 'upcoming',
  61. 'posts_per_page' => 1,
  62. 'start_date' => date( 'Y-m-d' )
  63. ));
  64.  
  65. $start_date = date( 'Y-m' );
  66.  
  67. if ( !empty( $next_event ) || isset( $next_event[0] ) ) {
  68. $next_event_date = tribe_get_start_date( $next_event[0]->ID, false, 'Y-m' );
  69.  
  70. // Prevent calendar from rewinding to the start of a currently ongoing event
  71. $start_date = ($next_event_date > $start_date) ? $next_event_date : $start_date;
  72. }
  73.  
  74. return $start_date;
  75. }
  76. }
  77.  
  78. /**
  79. * If we don't pass a parameter it will try to advance the calendar to the same month as the next upcoming event.
  80. * However, we can also pass in a specific month:
  81. *
  82. * new Tribe_Advance_Minical('2014-12'); // Set it to December 2014
  83. */
  84. new Tribe_Advance_Minical();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement