diff --git a/assets/js/admin/sportspress-admin.js b/assets/js/admin/sportspress-admin.js index 7c0b9c64..7f6fa66a 100644 --- a/assets/js/admin/sportspress-admin.js +++ b/assets/js/admin/sportspress-admin.js @@ -604,6 +604,21 @@ jQuery(document).ready(function($){ }); $(".sp-date-selector select").trigger("change"); + // Toggle date range selectors + $(".sp-date-relative input").change(function() { + $relative = $(this).closest(".sp-date-relative").siblings(".sp-date-range-relative").toggle(0, $(this).attr("checked")); + $absolute = $(this).closest(".sp-date-relative").siblings(".sp-date-range-absolute").toggle(0, $(this).attr("checked")); + + if ($(this).attr("checked")) { + $relative.show(); + $absolute.hide(); + } else { + $absolute.show(); + $relative.hide(); + } + }); + $(".sp-date-selector input").trigger("change"); + // Apply color scheme $(".sp-color-option").on("click", function() { colors = $(this).find("label").data("sp-colors").split(","); diff --git a/assets/js/admin/widgets.js b/assets/js/admin/widgets.js index ffbf1446..0aba986d 100644 --- a/assets/js/admin/widgets.js +++ b/assets/js/admin/widgets.js @@ -23,4 +23,19 @@ jQuery(document).ready(function($){ } }); $(".sp-date-selector select").trigger("change"); + + // Toggle date range selectors + $("body").on("change", ".sp-date-relative input", function() { + $relative = $(this).closest(".sp-date-relative").siblings(".sp-date-range-relative").toggle(0, $(this).attr("checked")); + $absolute = $(this).closest(".sp-date-relative").siblings(".sp-date-range-absolute").toggle(0, $(this).attr("checked")); + + if ($(this).attr("checked")) { + $relative.show(); + $absolute.hide(); + } else { + $absolute.show(); + $relative.hide(); + } + }); + $(".sp-date-selector input").trigger("change"); }); \ No newline at end of file diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-details.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-details.php index a28cce7b..081acffc 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-details.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-details.php @@ -25,6 +25,9 @@ class SP_Meta_Box_Calendar_Details { $date = get_post_meta( $post->ID, 'sp_date', true ); $date_from = get_post_meta( $post->ID, 'sp_date_from', true ); $date_to = get_post_meta( $post->ID, 'sp_date_to', true ); + $date_past = get_post_meta( $post->ID, 'sp_date_past', true ); + $date_future = get_post_meta( $post->ID, 'sp_date_future', true ); + $date_relative = get_post_meta( $post->ID, 'sp_date_relative', true ); $day = get_post_meta( $post->ID, 'sp_day', true ); $teams = get_post_meta( $post->ID, 'sp_team', false ); $table_id = get_post_meta( $post->ID, 'sp_table', true ); @@ -59,9 +62,27 @@ class SP_Meta_Box_Calendar_Details { ?>

- - : - +

+ + : + +

+ +

+ + + → + + + +

+ +

+ +

@@ -120,6 +141,9 @@ class SP_Meta_Box_Calendar_Details { update_post_meta( $post_id, 'sp_date', sp_array_value( $_POST, 'sp_date', 0 ) ); update_post_meta( $post_id, 'sp_date_from', sp_array_value( $_POST, 'sp_date_from', null ) ); update_post_meta( $post_id, 'sp_date_to', sp_array_value( $_POST, 'sp_date_to', null ) ); + update_post_meta( $post_id, 'sp_date_past', sp_array_value( $_POST, 'sp_date_past', 0 ) ); + update_post_meta( $post_id, 'sp_date_future', sp_array_value( $_POST, 'sp_date_future', 0 ) ); + update_post_meta( $post_id, 'sp_date_relative', sp_array_value( $_POST, 'sp_date_relative', 0 ) ); update_post_meta( $post_id, 'sp_day', sp_array_value( $_POST, 'sp_day', null ) ); update_post_meta( $post_id, 'sp_orderby', sp_array_value( $_POST, 'sp_orderby', null ) ); update_post_meta( $post_id, 'sp_order', sp_array_value( $_POST, 'sp_order', null ) ); diff --git a/includes/class-sp-calendar.php b/includes/class-sp-calendar.php index 75fb88a5..cdc99432 100644 --- a/includes/class-sp-calendar.php +++ b/includes/class-sp-calendar.php @@ -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; } } diff --git a/includes/widgets/class-sp-widget-event-blocks.php b/includes/widgets/class-sp-widget-event-blocks.php index 20621396..2b3d496e 100644 --- a/includes/widgets/class-sp-widget-event-blocks.php +++ b/includes/widgets/class-sp-widget-event-blocks.php @@ -21,6 +21,9 @@ class SP_Widget_Event_Blocks extends WP_Widget { $date = empty($instance['date']) ? 'default' : $instance['date']; $date_from = empty($instance['date_from']) ? 'default' : $instance['date_from']; $date_to = empty($instance['date_to']) ? 'default' : $instance['date_to']; + $date_past = empty($instance['date_past']) ? 'default' : $instance['date_past']; + $date_future = empty($instance['date_future']) ? 'default' : $instance['date_future']; + $date_relative = empty($instance['date_relative']) ? 'default' : $instance['date_relative']; $day = empty($instance['day']) ? 'default' : $instance['day']; $number = empty($instance['number']) ? null : $instance['number']; $order = empty($instance['order']) ? 'default' : $instance['order']; @@ -35,7 +38,7 @@ class SP_Widget_Event_Blocks extends WP_Widget { // Action to hook into do_action( 'sportspress_before_widget_template', $args, $instance, 'event-blocks' ); - sp_get_template( 'event-blocks.php', array( 'id' => $id, 'title' => $caption, 'status' => $status, 'date' => $date, 'date_from' => $date_from, 'date_to' => $date_to, 'day' => $day, 'number' => $number, 'order' => $order, 'show_all_events_link' => $show_all_events_link ) ); + sp_get_template( 'event-blocks.php', array( 'id' => $id, 'title' => $caption, 'status' => $status, 'date' => $date, 'date_from' => $date_from, 'date_to' => $date_to, 'date_past' => $date_past, 'date_future' => $date_future, 'date_relative' => $date_relative, 'day' => $day, 'number' => $number, 'order' => $order, 'show_all_events_link' => $show_all_events_link ) ); // Action to hook into do_action( 'sportspress_after_widget_template', $args, $instance, 'event-blocks' ); @@ -53,6 +56,9 @@ class SP_Widget_Event_Blocks extends WP_Widget { $instance['date'] = $new_instance['date']; $instance['date_from'] = $new_instance['date_from']; $instance['date_to'] = $new_instance['date_to']; + $instance['date_past'] = $new_instance['date_past']; + $instance['date_future'] = $new_instance['date_future']; + $instance['date_relative'] = $new_instance['date_relative']; $instance['day'] = $new_instance['day']; $instance['number'] = intval($new_instance['number']); $instance['order'] = strip_tags($new_instance['order']); @@ -65,7 +71,7 @@ class SP_Widget_Event_Blocks extends WP_Widget { } function form( $instance ) { - $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'id' => null, 'caption' => '', 'status' => 'default', 'date' => 'default', 'date_from' => date_i18n( 'Y-m-d' ), 'date_to' => date_i18n( 'Y-m-d' ), 'day' => '', 'number' => 5, 'order' => 'default', 'show_all_events_link' => true ) ); + $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'id' => null, 'caption' => '', 'status' => 'default', 'date' => 'default', 'date_from' => date_i18n( 'Y-m-d' ), 'date_to' => date_i18n( 'Y-m-d' ), 'date_past' => 7, 'date_future' => 7, 'date_relative' => false, 'day' => '', 'number' => 5, 'order' => 'default', 'show_all_events_link' => true ) ); $title = strip_tags($instance['title']); $id = intval($instance['id']); $caption = strip_tags($instance['caption']); @@ -73,6 +79,9 @@ class SP_Widget_Event_Blocks extends WP_Widget { $date = $instance['date']; $date_from = $instance['date_from']; $date_to = $instance['date_to']; + $date_past = $instance['date_past']; + $date_future = $instance['date_future']; + $date_relative = $instance['date_relative']; $day = $instance['day']; $number = intval($instance['number']); $order = strip_tags($instance['order']); @@ -130,11 +139,29 @@ class SP_Widget_Event_Blocks extends WP_Widget { sp_dropdown_dates( $args ); ?>

-

- - : - -

+
+

+ + : + +

+ +

+ + + → + + + +

+ +

+ +

+

diff --git a/includes/widgets/class-sp-widget-event-calendar.php b/includes/widgets/class-sp-widget-event-calendar.php index 369f26b3..995709db 100644 --- a/includes/widgets/class-sp-widget-event-calendar.php +++ b/includes/widgets/class-sp-widget-event-calendar.php @@ -20,6 +20,9 @@ class SP_Widget_Event_Calendar extends WP_Widget { $date = empty($instance['date']) ? 'default' : $instance['date']; $date_from = empty($instance['date_from']) ? 'default' : $instance['date_from']; $date_to = empty($instance['date_to']) ? 'default' : $instance['date_to']; + $date_past = empty($instance['date_past']) ? 'default' : $instance['date_past']; + $date_future = empty($instance['date_future']) ? 'default' : $instance['date_future']; + $date_relative = empty($instance['date_relative']) ? 'default' : $instance['date_relative']; $day = empty($instance['day']) ? 'default' : $instance['day']; $show_all_events_link = empty($instance['show_all_events_link']) ? false : $instance['show_all_events_link']; @@ -32,7 +35,7 @@ class SP_Widget_Event_Calendar extends WP_Widget { // Action to hook into do_action( 'sportspress_before_widget_template', $args, $instance, 'event-calendar' ); - sp_get_template( 'event-calendar.php', array( 'id' => $id, 'status' => $status, 'date' => $date, 'date_from' => $date_from, 'date_to' => $date_to, 'caption_tag' => 'caption', 'day' => $day, 'show_all_events_link' => $show_all_events_link ) ); + sp_get_template( 'event-calendar.php', array( 'id' => $id, 'status' => $status, 'date' => $date, 'date_from' => $date_from, 'date_to' => $date_to, 'date_past' => $date_past, 'date_future' => $date_future, 'date_relative' => $date_relative, 'caption_tag' => 'caption', 'day' => $day, 'show_all_events_link' => $show_all_events_link ) ); // Action to hook into do_action( 'sportspress_after_widget_template', $args, $instance, 'event-calendar' ); @@ -49,6 +52,9 @@ class SP_Widget_Event_Calendar extends WP_Widget { $instance['date'] = $new_instance['date']; $instance['date_from'] = $new_instance['date_from']; $instance['date_to'] = $new_instance['date_to']; + $instance['date_past'] = $new_instance['date_past']; + $instance['date_future'] = $new_instance['date_future']; + $instance['date_relative'] = $new_instance['date_relative']; $instance['day'] = $new_instance['day']; $instance['show_all_events_link'] = $new_instance['show_all_events_link']; @@ -59,13 +65,16 @@ class SP_Widget_Event_Calendar extends WP_Widget { } function form( $instance ) { - $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'id' => null, 'status' => 'default', 'date' => 'default', 'date_from' => date_i18n( 'Y-m-d' ), 'date_to' => date_i18n( 'Y-m-d' ), 'day' => '', 'show_all_events_link' => false ) ); + $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'id' => null, 'status' => 'default', 'date' => 'default', 'date_from' => date_i18n( 'Y-m-d' ), 'date_to' => date_i18n( 'Y-m-d' ), 'date_past' => 7, 'date_future' => 7, 'date_relative' => false, 'day' => '', 'show_all_events_link' => false ) ); $title = strip_tags($instance['title']); $id = intval($instance['id']); $status = $instance['status']; $date = $instance['date']; $date_from = $instance['date_from']; $date_to = $instance['date_to']; + $date_past = $instance['date_past']; + $date_future = $instance['date_future']; + $date_relative = $instance['date_relative']; $day = $instance['day']; $show_all_events_link = $instance['show_all_events_link']; @@ -118,11 +127,29 @@ class SP_Widget_Event_Calendar extends WP_Widget { sp_dropdown_dates( $args ); ?>

-

- - : - -

+
+

+ + : + +

+ +

+ + + → + + + +

+ +

+ +

+

diff --git a/includes/widgets/class-sp-widget-event-list.php b/includes/widgets/class-sp-widget-event-list.php index 0e9c7e61..4c646af9 100644 --- a/includes/widgets/class-sp-widget-event-list.php +++ b/includes/widgets/class-sp-widget-event-list.php @@ -21,6 +21,9 @@ class SP_Widget_Event_List extends WP_Widget { $date = empty($instance['date']) ? 'default' : $instance['date']; $date_from = empty($instance['date_from']) ? 'default' : $instance['date_from']; $date_to = empty($instance['date_to']) ? 'default' : $instance['date_to']; + $date_past = empty($instance['date_past']) ? 'default' : $instance['date_past']; + $date_future = empty($instance['date_future']) ? 'default' : $instance['date_future']; + $date_relative = empty($instance['date_relative']) ? 'default' : $instance['date_relative']; $day = empty($instance['day']) ? 'default' : $instance['day']; $number = empty($instance['number']) ? null : $instance['number']; $columns = empty($instance['columns']) ? null : $instance['columns']; @@ -36,7 +39,7 @@ class SP_Widget_Event_List extends WP_Widget { // Action to hook into do_action( 'sportspress_before_widget_template', $args, $instance, 'event-list' ); - sp_get_template( 'event-list.php', array( 'id' => $id, 'title' => $caption, 'status' => $status, 'date' => $date, 'date_from' => $date_from, 'date_to' => $date_to, 'day' => $day, 'number' => $number, 'columns' => $columns, 'order' => $order, 'show_all_events_link' => $show_all_events_link ) ); + sp_get_template( 'event-list.php', array( 'id' => $id, 'title' => $caption, 'status' => $status, 'date' => $date, 'date_from' => $date_from, 'date_to' => $date_to, 'date_past' => $date_past, 'date_future' => $date_future, 'date_relative' => $date_relative, 'day' => $day, 'number' => $number, 'columns' => $columns, 'order' => $order, 'show_all_events_link' => $show_all_events_link ) ); // Action to hook into do_action( 'sportspress_after_widget_template', $args, $instance, 'event-list' ); @@ -54,6 +57,9 @@ class SP_Widget_Event_List extends WP_Widget { $instance['date'] = $new_instance['date']; $instance['date_from'] = $new_instance['date_from']; $instance['date_to'] = $new_instance['date_to']; + $instance['date_past'] = $new_instance['date_past']; + $instance['date_future'] = $new_instance['date_future']; + $instance['date_relative'] = $new_instance['date_relative']; $instance['day'] = $new_instance['day']; $instance['number'] = intval($new_instance['number']); $instance['columns'] = (array)$new_instance['columns']; @@ -67,7 +73,7 @@ class SP_Widget_Event_List extends WP_Widget { } function form( $instance ) { - $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'id' => null, 'caption' => '', 'status' => 'default', 'date' => 'default', 'date_from' => date_i18n( 'Y-m-d' ), 'date_to' => date_i18n( 'Y-m-d' ), 'day' => '', 'number' => 5, 'columns' => null, 'order' => 'default', 'show_all_events_link' => true ) ); + $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'id' => null, 'caption' => '', 'status' => 'default', 'date' => 'default', 'date_from' => date_i18n( 'Y-m-d' ), 'date_to' => date_i18n( 'Y-m-d' ), 'date_past' => 7, 'date_future' => 7, 'date_relative' => false, 'day' => '', 'number' => 5, 'columns' => null, 'order' => 'default', 'show_all_events_link' => true ) ); $title = strip_tags($instance['title']); $id = intval($instance['id']); $caption = strip_tags($instance['caption']); @@ -75,6 +81,9 @@ class SP_Widget_Event_List extends WP_Widget { $date = $instance['date']; $date_from = $instance['date_from']; $date_to = $instance['date_to']; + $date_past = $instance['date_past']; + $date_future = $instance['date_future']; + $date_relative = $instance['date_relative']; $day = $instance['day']; $number = intval($instance['number']); $columns = $instance['columns']; @@ -135,11 +144,29 @@ class SP_Widget_Event_List extends WP_Widget { sp_dropdown_dates( $args ); ?>

-

- - : - -

+
+

+ + : + +

+ +

+ + + → + + + +

+ +

+ +

+

diff --git a/templates/event-blocks.php b/templates/event-blocks.php index 5c5a12d1..232fe701 100644 --- a/templates/event-blocks.php +++ b/templates/event-blocks.php @@ -16,6 +16,9 @@ $defaults = array( 'date' => 'default', 'date_from' => 'default', 'date_to' => 'default', + 'date_past' => 'default', + 'date_future' => 'default', + 'date_relative' => 'default', 'day' => 'default', 'league' => null, 'season' => null, @@ -49,6 +52,12 @@ if ( $date_from != 'default' ) $calendar->from = $date_from; if ( $date_to != 'default' ) $calendar->to = $date_to; +if ( $date_past != 'default' ) + $calendar->past = $date_past; +if ( $date_future != 'default' ) + $calendar->future = $date_future; +if ( $date_relative != 'default' ) + $calendar->relative = $date_relative; if ( $league ) $calendar->league = $league; if ( $season ) diff --git a/templates/event-calendar.php b/templates/event-calendar.php index 5230e7e8..2a764652 100644 --- a/templates/event-calendar.php +++ b/templates/event-calendar.php @@ -17,6 +17,9 @@ $defaults = array( 'date' => 'default', 'date_from' => 'default', 'date_to' => 'default', + 'date_past' => 'default', + 'date_future' => 'default', + 'date_relative' => 'default', 'day' => 'default', 'league' => null, 'season' => null, @@ -40,6 +43,12 @@ if ( $date_from != 'default' ) $calendar->from = $date_from; if ( $date_to != 'default' ) $calendar->to = $date_to; +if ( $date_past != 'default' ) + $calendar->past = $date_past; +if ( $date_future != 'default' ) + $calendar->future = $date_future; +if ( $date_relative != 'default' ) + $calendar->relative = $date_relative; if ( $day != 'default' ) $calendar->day = $day; if ( $league ) diff --git a/templates/event-list.php b/templates/event-list.php index 964f77ff..ee86d556 100644 --- a/templates/event-list.php +++ b/templates/event-list.php @@ -16,6 +16,9 @@ $defaults = array( 'date' => 'default', 'date_from' => 'default', 'date_to' => 'default', + 'date_past' => 'default', + 'date_future' => 'default', + 'date_relative' => 'default', 'day' => 'default', 'league' => null, 'season' => null, @@ -51,6 +54,12 @@ if ( $date_from != 'default' ) $calendar->from = $date_from; if ( $date_to != 'default' ) $calendar->to = $date_to; +if ( $date_past != 'default' ) + $calendar->past = $date_past; +if ( $date_future != 'default' ) + $calendar->future = $date_future; +if ( $date_relative != 'default' ) + $calendar->relative = $date_relative; if ( $league ) $calendar->league = $league; if ( $season )