Last active
December 5, 2017 05:17
-
-
Save elimn/bfcda80db4711aafbb36d708e25fb4b6 to your computer and use it in GitHub Desktop.
MT | TEC | Custom Time of Day Filter
This file contains 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 | |
/** | |
* Customized version of the Time of Day filter that allows selecting by specific hour ranges in the afternoon | |
* New filter available in WP-Admin > Events > Settings > Filters | |
*/ | |
if ( class_exists( 'Tribe__Events__Filterbar__Filter' ) ) { | |
class Tribe__Events__Filterbar__Filters__Time_Of_Day_Custom extends Tribe__Events__Filterbar__Filter { | |
public $type = 'checkbox'; | |
protected function get_values() { | |
// The time-of-day filter. | |
$time_of_day_array = array( | |
'allday' => __( 'All Day', 'tribe-events-filter-view' ), | |
'13-14' => __( '1:00-2:00 pm', 'tribe-events-filter-view' ), | |
'14-15' => __( '2:00-3:00 pm', 'tribe-events-filter-view' ), | |
'15-16' => __( '3:00-4:00 pm', 'tribe-events-filter-view' ), | |
'16-17' => __( '4:00-5:00 pm', 'tribe-events-filter-view' ), | |
'17-18' => __( '5:00-6:00 pm', 'tribe-events-filter-view' ), | |
'18-19' => __( '6:00-7:00 pm', 'tribe-events-filter-view' ), | |
'19-20' => __( '7:00-8:00 pm', 'tribe-events-filter-view' ), | |
'20-21' => __( '8:00-9:00 pm', 'tribe-events-filter-view' ), | |
'21-22' => __( '9:00-10:00 pm', 'tribe-events-filter-view' ), | |
'22-23' => __( '10:00-11:00 pm', 'tribe-events-filter-view' ), | |
'23-24' => __( '11:00-12:00 pm', 'tribe-events-filter-view' ), | |
); | |
$time_of_day_values = array(); | |
foreach ( $time_of_day_array as $value => $name ) { | |
$time_of_day_values[] = array( | |
'name' => $name, | |
'value' => $value, | |
); | |
} | |
return $time_of_day_values; | |
} | |
protected function setup_join_clause() { | |
add_filter( 'posts_join', array( 'Tribe__Events__Query', 'posts_join' ), 10, 2 ); | |
global $wpdb; | |
$values = $this->currentValue; | |
$all_day_index = array_search( 'allday', $values ); | |
if ( $all_day_index !== false ) { | |
unset( $values[ $all_day_index ] ); | |
} | |
$this->joinClause .= " LEFT JOIN {$wpdb->postmeta} AS all_day ON ({$wpdb->posts}.ID = all_day.post_id AND all_day.meta_key = '_EventAllDay')"; | |
if ( ! empty( $values ) ) { // values other than allday | |
$this->joinClause .= " INNER JOIN {$wpdb->postmeta} AS tod_start_date ON ({$wpdb->posts}.ID = tod_start_date.post_id AND tod_start_date.meta_key = '_EventStartDate')"; | |
$this->joinClause .= " INNER JOIN {$wpdb->postmeta} AS tod_duration ON ({$wpdb->posts}.ID = tod_duration.post_id AND tod_duration.meta_key = '_EventDuration')"; | |
} | |
} | |
protected function setup_where_clause() { | |
global $wpdb; | |
$clauses = array(); | |
if ( in_array( 'allday', $this->currentValue ) ) $clauses[] = "(all_day.meta_value = 'yes')"; | |
else $this->whereClause = ' AND ( all_day.meta_id IS NULL ) '; | |
foreach ( $this->currentValue as $value ) { | |
if ( $value == 'allday' ) { | |
continue; // handled earlier | |
} | |
$value = explode( '-', $value ); | |
$abs[0] = $value[0]; | |
$abs[1] = $value[1]; | |
$value[0] = $value[0] . ':00:00'; | |
$value[1] = $value[1] . ':00:00'; | |
// If it spans the day marker... | |
if ( $abs[0] > $abs[1] ) { | |
$start_measure_date = new DateTime( '2011-01-01 ' . $value[0] ); | |
$end_measure_date = new DateTime( '2011-01-02 ' . $value[1] ); | |
$interval = date( $end_measure_date->format( 'U' ) - $start_measure_date->format( 'U' ) ); | |
$clauses[] = $wpdb->prepare( ' | |
( | |
( TIME(CAST(tod_start_date.meta_value as DATETIME)) < %s ) | |
OR ( TIME(CAST(tod_start_date.meta_value as DATETIME)) > %s ) | |
OR ( MOD(TIME_TO_SEC(TIMEDIFF(%s, TIME(CAST(tod_start_date.meta_value as DATETIME)))) + 86400, 86400) < tod_duration.meta_value ) | |
)', $value[1], $value[0], $value[1], $value[0], $interval ); | |
} else { | |
$clauses[] = $wpdb->prepare( ' | |
( | |
( TIME(CAST(tod_start_date.meta_value as DATETIME)) >= %s AND TIME(CAST(tod_start_date.meta_value as DATETIME)) < %s ) | |
OR ( MOD(TIME_TO_SEC(TIMEDIFF(%s, TIME(CAST(tod_start_date.meta_value as DATETIME)))) + 86400, 86400) < tod_duration.meta_value ) | |
)', $value[0], $value[1], $value[0] ); | |
} | |
} | |
$this->whereClause .= ' AND (' . implode( ' OR ', $clauses ) . ')'; | |
} | |
} | |
new Tribe__Events__Filterbar__Filters__Time_Of_Day_Custom( __( 'Time Custom', 'tribe-events-filter-view' ), 'timeofdaycustom' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment