Skip to content
Draft
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
21 changes: 20 additions & 1 deletion includes/calendars/admin/default-calendar-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function add_settings_meta_calendar_panel($post_id)
?>
</td>
</tr>
<tr class="simcal-panel-field simcal-default-calendar-list" style="display: none;">
<tr class="simcal-panel-field simcal-default-calendar-list test-class" style="display: none;">
<th><label for="_default_calendar_list_grouped_span"><?php _e('Span', 'google-calendar-events'); ?></label></th>
<td>
<?php
Expand Down Expand Up @@ -221,6 +221,25 @@ public function add_settings_meta_calendar_panel($post_id)
),
'value' => 'yes' == $compact ? 'yes' : 'no',
]);
?>
</td>
</tr>
<tr class="simcal-panel-field simcal-default-calendar-list" style="display: none;">
<th><label for="_default_calendar_reverse_chronological_order"><?php _e('Reverse Chronological Order', 'google-calendar-events'); ?></label></th>
<td>
<?php
$reverse_order = get_post_meta($post_id, '_default_calendar_reverse_chronological_order', true);

simcal_print_field([
'type' => 'checkbox',
'name' => '_default_calendar_reverse_chronological_order',
'id' => '_default_calendar_reverse_chronological_order',
'tooltip' => __(
'Display events in reverse chronological order (newest first) instead of chronological order (oldest first).',
'google-calendar-events'
),
'value' => 'yes' == $reverse_order ? 'yes' : 'no',
]);
?>
</td>
</tr>
Expand Down
36 changes: 31 additions & 5 deletions includes/calendars/views/default-calendar-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,13 @@ private function get_events($timestamp)
}
}

ksort($daily_events, SORT_NUMERIC);
// Sort daily events - reverse if reverse chronological order is enabled
$reverse_order = get_post_meta($calendar->id, '_default_calendar_reverse_chronological_order', true) == 'yes';
if ($reverse_order) {
krsort($daily_events, SORT_NUMERIC);
} else {
ksort($daily_events, SORT_NUMERIC);
}

if (!empty($paged_events)) {
$first_event = array_slice($paged_events, 0, 1, true);
Expand Down Expand Up @@ -514,18 +520,31 @@ private function get_heading()
* @since 3.1.28
* @access private
*/
private static function cmp($a, $b)
private static function cmp($a, $b, $reverse = false)
{
if ($a->start == $b->start) {
if ($a->title == $b->title) {
return 0;
}
return $a->title < $b->title ? -1 : 1;
$title_cmp = $a->title < $b->title ? -1 : 1;
return $reverse ? -$title_cmp : $title_cmp;
} else {
return $a->start < $b->start ? -1 : 1;
$time_cmp = $a->start < $b->start ? -1 : 1;
return $reverse ? -$time_cmp : $time_cmp;
}
}

/**
* Comparison function for reverse chronological order.
*
* @since 3.x.x
* @access private
*/
private function cmp_reverse($a, $b)
{
return self::cmp($a, $b, true);
}

/**
* Make a calendar list of events.
*
Expand All @@ -550,6 +569,9 @@ private function draw_list($timestamp, $id = 0)
}
}

// Check if reverse chronological order is enabled
$reverse_order = get_post_meta($calendar->id, '_default_calendar_reverse_chronological_order', true) == 'yes';

$now = $calendar->now;
$current_events = $this->get_events($timestamp);
$format = $calendar->date_format;
Expand Down Expand Up @@ -660,7 +682,11 @@ private function draw_list($timestamp, $id = 0)
$count = 0;

foreach ($events as $day_events):
usort($day_events, [$this, 'cmp']);
if ($reverse_order) {
usort($day_events, [$this, 'cmp_reverse']);
} else {
usort($day_events, [$this, 'cmp']);
}

foreach ($day_events as $event):
if ($event instanceof Event):
Expand Down
Loading