From f254ee34d51dca784e44bb1687d894c929471d16 Mon Sep 17 00:00:00 2001
From: John James Jacoby
Date: Wed, 9 Mar 2022 02:37:17 -0600
Subject: [PATCH 1/2] Events: introduce sugar_calendar_is_display_type().
This change adds a new helper function to encapsulate the logic used to determine how an archive of Events is being displayed.
---
sugar-calendar/includes/events/functions.php | 41 +++++++++++++++++---
1 file changed, 35 insertions(+), 6 deletions(-)
diff --git a/sugar-calendar/includes/events/functions.php b/sugar-calendar/includes/events/functions.php
index 3516a6c..54001bd 100644
--- a/sugar-calendar/includes/events/functions.php
+++ b/sugar-calendar/includes/events/functions.php
@@ -628,7 +628,7 @@ function sugar_calendar_get_events_list( $args = array(), $query_args = array()
'cache_prefix' => 'sc_',
'order' => 'DESC',
'number' => 5,
- 'display' => 'upcoming',
+ 'display' => 'all',
'spread' => 'P100Y', // 100 years
'expires' => 'PT900S', // 15 minutes
'start_of_week' => sugar_calendar_get_user_preference( 'sc_start_of_week' ),
@@ -677,9 +677,9 @@ function sugar_calendar_get_events_list( $args = array(), $query_args = array()
if ( ! empty( $events ) ) {
// Types
- $past = ( false !== strpos( $r['display'], 'past' ) );
- $upcoming = ( false !== strpos( $r['display'], 'upcoming' ) );
- $in_progress = ( false !== strpos( $r['display'], 'in-progress' ) );
+ $past = sugar_calendar_is_display_type( 'past', $r['display'] );
+ $upcoming = sugar_calendar_is_display_type( 'upcoming', $r['display'] );
+ $in_progress = sugar_calendar_is_display_type( 'in-progress', $r['display'] );
// Default start/end
$after = clone( $r['round'] );
@@ -750,16 +750,24 @@ function sugar_calendar_get_events_list( $args = array(), $query_args = array()
? min( (int) $r['number'], 100 )
: 5;
- // Sort by order
+ // Sort descending
$list = wp_list_sort( $list, 'end', $r['order'] );
+ // Maybe invert
+ $flip = ( true === $upcoming )
+ ? 'DESC'
+ : 'ASC';
+
// Determine start of array slice, based on order
- $start = ( 'DESC' === $r['order'] )
+ $start = ( $flip === $r['order'] )
? ( - $max )
: 0;
// Slice list to get only the number needed
$retval = array_slice( $list, $start, $max );
+
+ // Sort by order
+ $list = wp_list_sort( $list, 'end', $r['order'] );
}
}
}
@@ -800,3 +808,24 @@ function sugar_calendar_clean_events_list_cache( $post_id = 0 ) {
// Delete the entire list cache
delete_option( 'sc_events_list_cache' );
}
+
+/**
+ * Helper function to determine if an Event display is of a specific type.
+ *
+ * @since 2.3.0
+ *
+ * @param string $type
+ * @param string $display
+ *
+ * @return boolean
+ */
+function sugar_calendar_is_display_type( $type = 'upcoming', $display = 'upcoming' ) {
+
+ // "all" is all types
+ if ( 'all' === $display ) {
+ return true;
+ }
+
+ // Check for existence of type in display
+ return ( false !== strpos( $display, $type ) );
+}
From 227c29f12044f20218f929de96b985a6de724982 Mon Sep 17 00:00:00 2001
From: John James Jacoby
Date: Wed, 9 Mar 2022 02:39:02 -0600
Subject: [PATCH 2/2] Events: use sugar_calendar_is_display_type() where
appropriate.
This change implements the usage of the function that allows for multiple display types in the various archives, widgets, etc...
---
.../includes/post/query-filters.php | 16 +++++-----
.../includes/themes/legacy/widgets.php | 31 +++++++++++++------
2 files changed, 30 insertions(+), 17 deletions(-)
diff --git a/sugar-calendar/includes/post/query-filters.php b/sugar-calendar/includes/post/query-filters.php
index a87572c..9f4fce3 100644
--- a/sugar-calendar/includes/post/query-filters.php
+++ b/sugar-calendar/includes/post/query-filters.php
@@ -127,16 +127,18 @@ function sc_modify_events_archive_where( $where = '', $query = false ) {
$time = $datetime->format( 'Y-m-d H:i:s' );
// In-Progress
- if ( 'in-progress' === $display ) {
+ if ( sugar_calendar_is_display_type( 'in-progress', $display ) ) {
$where .= " AND ( {$alias}.start <= '{$time}' AND {$alias}.end >= '{$time}' )";
+ }
- // Upcoming (includes in-progress)
- } elseif ( 'upcoming' === $display ) {
- $where .= " AND {$alias}.end >= '{$time}'";
+ // Past
+ if ( sugar_calendar_is_display_type( 'past', $display ) ) {
+ $where .= " AND {$alias}.start <= '{$time}'";
+ }
- // Past (excludes in-progress)
- } elseif ( 'past' === $display ) {
- $where .= " AND {$alias}.end <= '{$time}'";
+ // Upcoming
+ if ( sugar_calendar_is_display_type( 'upcoming', $display ) ) {
+ $where .= " AND {$alias}.end >= '{$time}'";
}
// Return new where
diff --git a/sugar-calendar/includes/themes/legacy/widgets.php b/sugar-calendar/includes/themes/legacy/widgets.php
index 57de627..5e6162e 100644
--- a/sugar-calendar/includes/themes/legacy/widgets.php
+++ b/sugar-calendar/includes/themes/legacy/widgets.php
@@ -221,11 +221,17 @@ public function __construct() {
public function widget( $args = array(), $instance = array() ) {
// Parse args
- $r = wp_parse_args( $args, array(
+ $a = wp_parse_args( $args, array(
'before_widget' => '',
'before_title' => '',
'after_title' => '',
'after_widget' => '',
+ 'widget_id' => '',
+ 'widget_name' => '',
+ ) );
+
+ // Parse instance
+ $i = wp_parse_args( $instance, array(
'title' => '',
'display' => '',
'order' => '',
@@ -237,6 +243,9 @@ public function widget( $args = array(), $instance = array() ) {
'show_categories' => null,
) );
+ // Combine arguments with instance
+ $r = wp_parse_args( $a, $i );
+
// Filter the title
$r['title'] = apply_filters( 'widget_title', $r['title'] );
@@ -355,6 +364,7 @@ public function form( $instance = array() ) {
@@ -380,9 +390,10 @@ public function form( $instance = array() ) {
?>
-
-
+
+
+
">
@@ -569,7 +580,7 @@ public function widget( $args = array(), $instance = array() ) {
// Filtering?
$display = ! empty( $_GET['event-display'] )
- ? sanitize_key( $_GET['event-display'] )
+ ? sanitize_text_field( $_GET['event-display'] )
: false;
// Defaults
@@ -582,18 +593,18 @@ public function widget( $args = array(), $instance = array() ) {
// Custom
} else {
- // Upcoming?
- $is_upcoming = ( 'upcoming' === $display )
+ // In-progress
+ $is_in_progress = sugar_calendar_is_display_type( 'in-progress', $display )
? $current
: '';
- // In-progress?
- $is_in_progress = ( 'in-progress' === $display )
+ // Past
+ $is_past = sugar_calendar_is_display_type( 'past', $display )
? $current
: '';
- // Past?
- $is_past = ( 'past' === $display )
+ // Upcoming
+ $is_upcoming = sugar_calendar_is_display_type( 'upcoming', $display )
? $current
: '';