Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/240 - Allows multiple display values #254

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions sugar-calendar/includes/events/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down Expand Up @@ -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'] );
Expand Down Expand Up @@ -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'] );
}
}
}
Expand Down Expand Up @@ -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 ) );
}
16 changes: 9 additions & 7 deletions sugar-calendar/includes/post/query-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 21 additions & 10 deletions sugar-calendar/includes/themes/legacy/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
Expand All @@ -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'] );

Expand Down Expand Up @@ -355,6 +364,7 @@ public function form( $instance = array() ) {
<label for="<?php echo $this->get_field_id( 'display' ); ?>"><?php esc_html_e( 'Time Period:', 'sugar-calendar' ); ?></label>
<select class="widefat <?php echo $this->get_field_name( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>" id="<?php echo $this->get_field_id( 'display' ); ?>">
<option value="all" <?php selected( 'all', $display ); ?>><?php esc_html_e( 'All', 'sugar-calendar' ); ?></option>
<option value="in-progress" <?php selected( 'in-progress', $display ); ?>><?php esc_html_e( 'In Progress', 'sugar-calendar' ); ?></option>
<option value="upcoming" <?php selected( 'upcoming', $display ); ?>><?php esc_html_e( 'Upcoming', 'sugar-calendar' ); ?></option>
<option value="past" <?php selected( 'past', $display ); ?>><?php esc_html_e( 'Past', 'sugar-calendar' ); ?></option>
</select>
Expand All @@ -380,9 +390,10 @@ public function form( $instance = array() ) {
?></select>
</p>
<p>
<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'number' ); ?>" style="width: 40px;" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr( $number ); ?>">
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php esc_html_e( 'Number to show', 'sugar-calendar' ); ?></label>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php esc_html_e( 'Number to show:', 'sugar-calendar' ); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr( $number ); ?>">
</p>

<p>
<input class="checkbox" type="checkbox" id="<?php echo $this->get_field_id( 'show_title' ); ?>" name="<?php echo $this->get_field_name( 'show_title' ); ?>" <?php echo checked( $show_title, 1 ); ?>">
<label for="<?php echo $this->get_field_id( 'show_title' ); ?>"><?php esc_html_e( 'Show widget title', 'sugar-calendar' ); ?></label>
Expand Down Expand Up @@ -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
Expand All @@ -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
: '';

Expand Down