Forked from barryhughes/fast-forward-mini-cal.php
Last active
October 5, 2015 16:42
-
-
Save michaelwclark/6f64a50a98cedbad8b94 to your computer and use it in GitHub Desktop.
Revision for 'fast forward the calendar widget' code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 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_admin() ) return; | |
$this->target_date = $target_date; | |
add_action( 'wp_loaded', array( $this, 'set_target_date' ) ); | |
add_filter( 'widget_display_callback', array( $this, 'advance_minical' ), 20, 2 ); | |
} | |
/** | |
* Basic check to help filter out spurious date formats or automatically determine | |
* the next most appropriate date to use. | |
*/ | |
public function set_target_date() { | |
if ( ! is_string($this->target_date) || 1 !== preg_match( '#^\d{4}-\d{2}(-\d{2})?$# ', $this->target_date ) ) | |
$this->target_date = $this->next_upcoming_date(); | |
} | |
public function advance_minical( $instance, $widget ) { | |
if ( 'tribe-mini-calendar' !== $widget->id_base || isset( $instance['eventDate'] ) ) return $instance; | |
if ( date( 'Y-m' ) === $this->target_date) return; | |
add_action('pre_get_posts', 'include_categories',100,1); | |
add_action( 'tribe_before_get_template_part', array( $this, 'modify_list_query' ), 5 ); | |
$instance['eventDate'] = $this->target_date; | |
return $instance; | |
} | |
public function include_categories() { | |
//Categories to include | |
$include_cats = array('featured'); | |
//Join current tax query if it's set | |
if(is_array($wp_query->tax_query)) | |
$tax_query = $wp_query->tax_query; | |
else | |
$tax_query = array(); | |
//Setup an include from the tribe_events_cat taxonomy | |
$tax_query = array( | |
'taxonomy' => 'tribe_events_cat', | |
'field' => 'slug', | |
'terms' => $include_cats, | |
'operator' => 'IN' | |
); | |
if(tribe_is_event_query()){ | |
$wp_query->set('tax_query',$tax_query); | |
} | |
return $wp_query; | |
} | |
public function modify_list_query( $template ) { | |
if ( false === strpos( $template, 'mini-calendar/list.php' ) ) return; | |
add_action( 'parse_query', array( $this, 'amend_list_query' ) ); | |
} | |
public function amend_list_query( $query ) { | |
remove_action( 'parse_query', array( $this, 'amend_list_query' ) ); // Run this once only | |
$the_query = $query->query_vars; | |
$the_query['start_date'] = $this->target_date . '-01'; | |
$last_day = TribeDateUtils::getLastDayOfMonth( strtotime( $the_query['start_date'] ) ); | |
$the_query['end_date'] = substr_replace( $the_query['start_date'], $last_day, -2 ); | |
$the_query['end_date'] = TribeDateUtils::endOfDay( $the_query['end_date'] ); | |
$query->query_vars = $the_query; | |
} | |
protected function next_upcoming_date() { | |
$next_event = tribe_get_events( array( | |
'eventDisplay' => 'list', | |
'posts_per_page' => 1, | |
'start_date' => date( 'Y-m-d' ) | |
)); | |
$start_date = date( 'Y-m' ); | |
if ( !empty( $next_event ) || isset( $next_event[0] ) ) { | |
$next_event_date = tribe_get_start_date( $next_event[0]->ID, false, 'Y-m' ); | |
// Prevent calendar from rewinding to the start of a currently ongoing event | |
$start_date = ( $next_event_date > $start_date ) ? $next_event_date : $start_date; | |
} | |
return $start_date; | |
} | |
} | |
/** | |
* 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment