Add relative date range selector to calendars and widgets
This commit is contained in:
@@ -34,6 +34,15 @@ class SP_Calendar extends SP_Custom_Post {
|
||||
/** @var string The date to range to. */
|
||||
public $to;
|
||||
|
||||
/** @var string The number of days to query in the past. */
|
||||
public $past;
|
||||
|
||||
/** @var string The number of days to query in the future. */
|
||||
public $future;
|
||||
|
||||
/** @var boolean Determines whether the date range is relative. */
|
||||
public $relative;
|
||||
|
||||
/** @var string The match day. */
|
||||
public $day;
|
||||
|
||||
@@ -88,11 +97,29 @@ class SP_Calendar extends SP_Custom_Post {
|
||||
if ( ! $this->orderby )
|
||||
$this->orderby = 'post_date';
|
||||
|
||||
if ( ! $this->from )
|
||||
$this->from = get_post_meta( $this->ID, 'sp_date_from', true );
|
||||
if ( 'range' == $this->date ) {
|
||||
|
||||
if ( ! $this->to )
|
||||
$this->to = get_post_meta( $this->ID, 'sp_date_to', true );
|
||||
$this->relative = get_post_meta( $this->ID, 'sp_date_relative', true );
|
||||
|
||||
if ( $this->relative ) {
|
||||
|
||||
if ( ! $this->past )
|
||||
$this->past = get_post_meta( $this->ID, 'sp_date_past', true );
|
||||
|
||||
if ( ! $this->future )
|
||||
$this->future = get_post_meta( $this->ID, 'sp_date_future', true );
|
||||
|
||||
} else {
|
||||
|
||||
if ( ! $this->from )
|
||||
$this->from = get_post_meta( $this->ID, 'sp_date_from', true );
|
||||
|
||||
if ( ! $this->to )
|
||||
$this->to = get_post_meta( $this->ID, 'sp_date_to', true );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( ! $this->day )
|
||||
$this->day = get_post_meta( $this->ID, 'sp_day', true );
|
||||
@@ -133,7 +160,11 @@ class SP_Calendar extends SP_Custom_Post {
|
||||
$args['day'] = date_i18n('j');
|
||||
$args['monthnum'] = date_i18n('n');
|
||||
elseif ( $this->date == 'range' ):
|
||||
add_filter( 'posts_where', array( $this, 'range' ) );
|
||||
if ( $this->relative ):
|
||||
add_filter( 'posts_where', array( $this, 'relative' ) );
|
||||
else:
|
||||
add_filter( 'posts_where', array( $this, 'range' ) );
|
||||
endif;
|
||||
endif;
|
||||
endif;
|
||||
|
||||
@@ -240,19 +271,19 @@ class SP_Calendar extends SP_Custom_Post {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
if ( 'auto' === $this->date && 'any' === $this->status ) {
|
||||
$args['post_status'] = 'publish';
|
||||
$args['order'] = 'DESC';
|
||||
$args['posts_per_page'] = ceil( $this->number / 2 );
|
||||
$results = get_posts( $args );
|
||||
$results = array_reverse( $results, true );
|
||||
|
||||
|
||||
$args['post_status'] = 'future';
|
||||
$args['order'] = 'ASC';
|
||||
$args['posts_per_page'] = floor( $this->number / 2 );
|
||||
$fixtures = get_posts( $args );
|
||||
|
||||
|
||||
$events = array_merge_recursive( $results, $fixtures );
|
||||
} else {
|
||||
$events = get_posts( $args );
|
||||
@@ -262,15 +293,33 @@ class SP_Calendar extends SP_Custom_Post {
|
||||
$events = null;
|
||||
endif;
|
||||
|
||||
// Remove any calendar selection filters
|
||||
remove_filter( 'posts_where', array( $this, 'range' ) );
|
||||
remove_filter( 'posts_where', array( $this, 'relative' ) );
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
public function range( $where = '' ) {
|
||||
$to = new DateTime( $this->to, new DateTimeZone( get_option( 'timezone_string' ) ) );
|
||||
public function range( $where = '', $format = 'Y-m-d' ) {
|
||||
$from = new DateTime( $this->from, new DateTimeZone( get_option( 'timezone_string' ) ) );
|
||||
$where .= " AND post_date BETWEEN '" . $from->format( 'Y-m-d' ) . "' AND '" . $to->format( 'Y-m-d' ) . "'";
|
||||
$to = new DateTime( $this->to, new DateTimeZone( get_option( 'timezone_string' ) ) );
|
||||
$to->modify( '+1 day' );
|
||||
$where .= " AND post_date BETWEEN '" . $from->format( $format ) . "' AND '" . $to->format( $format ) . "'";
|
||||
return $where;
|
||||
}
|
||||
|
||||
public function relative( $where = '', $format = 'Y-m-d' ) {
|
||||
$datetimezone = new DateTimeZone( get_option( 'timezone_string' ) );
|
||||
$from = new DateTime( 'now', $datetimezone );
|
||||
$to = new DateTime( 'now', $datetimezone );
|
||||
|
||||
$from->modify( '-' . abs( (int) $this->past ) . ' day' );
|
||||
$to->modify( '+' . abs( (int) $this->future ) . ' day' );
|
||||
|
||||
$to->modify( '+1 day' );
|
||||
|
||||
$where .= " AND post_date BETWEEN '" . $from->format( $format ) . "' AND '" . $to->format( $format ) . "'";
|
||||
|
||||
return $where;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user