Skip to content

Commit

Permalink
Merge pull request #162 from RRZE-Webteam/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rvdforst authored Aug 18, 2023
2 parents 1a818ab + b267f19 commit 176112c
Show file tree
Hide file tree
Showing 11 changed files with 957 additions and 991 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rrze/rrze-calendar",
"description": "RRZE WordPress Plugin",
"version": "2.0.1",
"version": "2.0.2",
"type": "wordpress-plugin",
"license": "GPL",
"require": {
Expand Down
79 changes: 55 additions & 24 deletions includes/CPT/CalendarEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,22 +589,46 @@ public static function getEventData($post_id)
$tsEndUTC = get_gmt_from_date(date('Y-m-d H:i', $tsEnd), 'U');
$startDay = date('Y-m-d', $tsStart);
$endDay = date('Y-m-d', $tsEnd);
if ($data['allDay'] == 'on' || $startDay != $endDay) {
$eventItemsFormatted = [
'date' => ($endDay == $startDay ? date_i18n(get_option('date_format'), $tsStart) : date_i18n(get_option('date_format'), $tsStart)
. ' – '
. date_i18n(get_option('date_format'), $tsEnd)),
'time' => '',
'startISO' => $startDay,
'endISO' => $endDay,
];
if ($startDay != $endDay) {
// multiday
if ($data['allDay'] == 'on') {
$eventItemsFormatted = [
'date' => date_i18n(get_option('date_format'), $tsStart)
. ' – '
. date_i18n(get_option('date_format'), $tsEnd),
'time' => __('All Day', 'rrze-calendar'),
'startISO' => $startDay,
'endISO' => $endDay,
];
} else {
$eventItemsFormatted = [
'date' => date_i18n(get_option('date_format') . ', ' . get_option('time_format'), $tsStart)
. ' – '
. date_i18n(get_option('date_format') . ', ' . get_option('time_format'), $tsEnd),
'time' => '',
'startISO' => date_i18n('c', $tsStartUTC, true),
'endISO' => date_i18n('c', $tsEndUTC),
];
}
} else {
$eventItemsFormatted = [
'date' => date_i18n(get_option('date_format'), $tsStart),
'time' => date_i18n(get_option('time_format'), $tsStart) . ' – ' . date_i18n(get_option('time_format'), $tsEnd),
'startISO' => date_i18n('c', $tsStartUTC, true),
'endISO' => date_i18n('c', $tsEndUTC),
];
// single day
if ($data['allDay'] == 'on') {
$eventItemsFormatted = [
'date' => ($endDay == $startDay ? date_i18n(get_option('date_format'), $tsStart) : date_i18n(get_option('date_format'), $tsStart)
. ' – '
. date_i18n(get_option('date_format'), $tsEnd)),
'time' => __('All day', 'rrze-calendar'),
'startISO' => $startDay,
'endISO' => $endDay,
];
} else {
$eventItemsFormatted = [
'date' => date_i18n(get_option('date_format'), $tsStart),
'time' => date_i18n(get_option('time_format'), $tsStart) . ' – ' . date_i18n(get_option('time_format'), $tsEnd),
'startISO' => date_i18n('c', $tsStartUTC, true),
'endISO' => date_i18n('c', $tsEndUTC),
];
}
}
$data['eventItemsFormatted'][] = $eventItemsFormatted;
if (!$nextOccurenceFound && $tsStart >= time()) {
Expand All @@ -622,10 +646,11 @@ public static function displayEventMain($data)
$numItems = count($data['eventItemsFormatted']);
echo '<div class="rrze-event-schedule">';
if (!empty($data['nextOccurrenceFormatted'])) {
echo '<p><span class="rrze-event-date"><span class="dashicons dashicons-calendar"></span><span class="sr-only">' . __('Date', 'rrze-calendar') . ': </span>' . $data['nextOccurrenceFormatted']['date'] . '</span>'
echo '<p><span class="rrze-event-date"><span class="dashicons dashicons-calendar"></span><span class="sr-only">' . __('Date', 'rrze-calendar') . ': </span>'
. $data['nextOccurrenceFormatted']['date'] . '</span>'
. '<meta itemprop="startDate" content="' . $data['nextOccurrenceFormatted']['startISO'] . '">'
. '<meta itemprop="endDate" content="' . $data['nextOccurrenceFormatted']['endISO'] . '">'
. (($data['allDay'] != 'on' && !strpos($data['nextOccurrenceFormatted']['date'], '&ndash;')) ? '<span class="rrze-event-time"><span class="dashicons dashicons-clock"></span><span class="sr-only">' . __('Time', 'rrze-calendar') . ': </span>' . $data['nextOccurrenceFormatted']['time'] . '</span>' : '')
. ($data['nextOccurrenceFormatted']['time'] != '' ? '<span class="rrze-event-time"><span class="dashicons dashicons-clock"></span><span class="sr-only">' . __('Time', 'rrze-calendar') . ': </span>' . $data['nextOccurrenceFormatted']['time'] . '</span>' : '')
. ($data['location'] != '' ? '<span class="rrze-event-location" itemprop="location" itemscope><span class="dashicons dashicons-location"></span><span class="sr-only">' . __('Location', 'rrze-calendar') . ': </span>' . $data['location'] . '</span>' : '')
. '</p>';
} else {
Expand Down Expand Up @@ -668,9 +693,7 @@ public static function displayEventDetails($data)
echo '<dt>' . __('Date', 'rrze-calendar') . ':</dt><dd>' . (!empty($data['nextOccurrenceFormatted']) ? $data['nextOccurrenceFormatted']['date'] : $data['eventItemsFormatted'][$i]['date']) . '</dd>';

// Time
if ($data['allDay'] != 'on' && !strpos($data['eventItemsFormatted'][0]['date'], '&ndash;')) {
echo '<dt>' . __('Time', 'rrze-calendar') . ':</dt><dd>' . (!empty($data['nextOccurrenceFormatted']) ? $data['nextOccurrenceFormatted']['time'] : $data['eventItemsFormatted'][$i]['time']) . '</dd>';
}
echo '<dt>' . __('Time', 'rrze-calendar') . ':</dt><dd>' . (!empty($data['nextOccurrenceFormatted']) ? $data['nextOccurrenceFormatted']['time'] : $data['eventItemsFormatted'][$i]['time']) . '</dd>';

// Location
if ($data['location'] != '') {
Expand Down Expand Up @@ -759,29 +782,37 @@ public static function listTableContent($column_name, $post_id)
{
$data = CalendarEvent::getEventData($post_id);
$meta = get_post_meta($post_id);
$feedID = get_post_meta($post_id, 'ics_feed_id', true);
switch ($column_name) {
case 'event_date':
echo ($data["eventItemsFormatted"][0]['date'] ?? '') . (isset($data["eventItemsFormatted"][0]['time']) ? ', ' . $data["eventItemsFormatted"][0]['time'] : '');
break;
case 'event_recurrence':
if ($data['repeat'] == 'on') {
$readable_rrule = '&mdash;';
if (!$feedID && $data['repeat'] == 'on') {
$rruleArgs = Utils::getMeta($meta, 'event-rrule-args');
if ($rruleArgs != '') {
$rruleArgs = json_decode($rruleArgs, TRUE);
$rule = new RRule($rruleArgs);
echo Utils::humanReadableRecurrence($rule);
$readable_rrule = Utils::humanReadableRecurrence($rule);
}
} elseif ($feedID && ($feedMeta = get_post_meta($post_id, 'ics_event_meta', true))) {
if (!empty($feedMeta['readable_rrule'])) {
$readable_rrule = $feedMeta['readable_rrule'];
}
}
echo $readable_rrule;
break;
case 'event_location':
echo ($data['location'] ?? '');
break;
case 'event-feed':
$feedID = Utils::getMeta($meta, 'ics_feed_id');
if ($feedID != '') {
if ($feedID) {
$feedURL = get_edit_post_link($feedID);
$feedName = get_the_title($feedID);
echo '<a href="' . esc_url($feedURL) . '">' . esc_attr($feedName) . '</a>';
} else {
echo '&mdash;';
}
break;
}
Expand Down
72 changes: 35 additions & 37 deletions includes/ICS/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

class Events
{
private static $pastDays = 365;

private static $limitDays = 365;

public static function updateFeedsItems()
{
$feeds = self::getFeeds();
Expand All @@ -20,7 +24,7 @@ public static function updateFeedsItems()
}
}

protected static function getFeeds(array $postIn = [])
private static function getFeeds(array $postIn = [])
{
$args = [
'numberposts' => -1,
Expand All @@ -33,11 +37,12 @@ protected static function getFeeds(array $postIn = [])
return get_posts($args);
}

public static function updateItems(int $postId, bool $cache = true)
public static function updateItems(int $postId, bool $cache = true, int $pastDays = 365, int $limitDays = 365)
{
$url = (string) get_post_meta($postId, CalendarFeed::FEED_URL, true);

$events = Import::getEvents($url, $cache);
$events = Import::getEvents($url, $cache, $pastDays, $limitDays);

$error = !$events ? __('No events found.', 'rrze-calendar') : '';
$items = !empty($events['events']) ? $events['events'] : [];
$meta = !empty($events['meta']) ? $events['meta'] : [];
Expand All @@ -48,18 +53,15 @@ public static function updateItems(int $postId, bool $cache = true)
update_post_meta($postId, CalendarFeed::FEED_EVENTS_META, $meta);
}

public static function getItems(int $postId): array
{
return self::processItems($postId);
}

/**
* processItems
* Get feed items
*
* @param integer $postId
* @return mixed
* @param integer $pastDays
* @param integer $limitDays
* @return void
*/
protected static function processItems($postId, int $pastDays = 0, int $limitDays = 365)
private static function getItems(int $postId, int $pastDays, int $limitDays)
{
$feedItems = [];
if (
Expand All @@ -68,19 +70,15 @@ protected static function processItems($postId, int $pastDays = 0, int $limitDay
) {
return $feedItems;
}

$feedItems['events'] = [];
$feedItems['tz'] = get_option('timezone_string');

// Set display date range.
$pastDays = $pastDays ? abs($pastDays) : 0;
$pastDays = abs($pastDays);
$limitDays = abs($limitDays);
$startDate = date('Ymd', current_time('timestamp'));
if ($pastDays) {
$firstDate = Utils::dateFormat('Ymd', $startDate, null, '-' . abs($pastDays) . ' days');
} else {
$firstDate = $startDate;
}
$limitDate = Utils::dateFormat('Ymd', $firstDate, null, '+' . intval($limitDays - 1) . ' days');
$firstDate = Utils::dateFormat('Ymd', $startDate, null, '-' . $pastDays + 30 . ' days');
$limitDate = Utils::dateFormat('Ymd', $startDate, null, '+' . ($limitDays + 7) . ' days');

// Set earliest and latest dates
$feedItems['earliest'] = substr($firstDate, 0, 6);
Expand All @@ -101,11 +99,10 @@ protected static function processItems($postId, int $pastDays = 0, int $limitDay

// Sort events and split into year/month/day groups
ksort($feedItems['events']);
foreach ((array)$feedItems['events'] as $date => $events) {

foreach ((array)$feedItems['events'] as $date => $events) {
// Only reorganize dates that are in the proper date range
if ($date >= $firstDate && $date <= $limitDate) {

// Get the date's events in order
ksort($events);

Expand All @@ -124,7 +121,7 @@ protected static function processItems($postId, int $pastDays = 0, int $limitDay
}

// Add empty event arrays
for ($i = substr($feedItems['earliest'], 0, 6); $i <= substr($feedItems['latest'], 0, 6); $i++) {
for ($i = substr($firstDate, 0, 6); $i <= substr($limitDate, 0, 6); $i++) {
$Y = substr($i, 0, 4);
$m = substr($i, 4, 2);
if (intval($m) < 1 || intval($m) > 12) {
Expand All @@ -144,7 +141,7 @@ protected static function processItems($postId, int $pastDays = 0, int $limitDay
return $feedItems;
}

protected static function assembleEvents($postId, $event, $eventKey, $urlTz, &$feedItems)
private static function assembleEvents($postId, $event, $eventKey, $urlTz, &$feedItems)
{
// Set start and end dates for event
$dtstartDate = wp_date('Ymd', $event->dtstart_array[2], $urlTz);
Expand Down Expand Up @@ -307,11 +304,10 @@ public static function getListTableData(string $searchTerm = ''): array
global $post;
if (get_post_type($post) === CalendarFeed::POST_TYPE) {
$postId = $post->ID;
$items = self::getItems($postId);
$items = self::getItems($postId, self::$pastDays, self::$limitDays);
}
}
}
//\RRZE\WP\Debug::log(self::getListData($postId, $items));
return count($items) ? self::getListData($postId, $items, $searchTerm) : $items;
}

Expand Down Expand Up @@ -347,8 +343,8 @@ public static function getListData(int $postId, array $items, string $searchTerm
}

if (isset($items['events'][$year][$month])) {
foreach ((array)$items['events'][$year][$month] as $day => $dayEvents) {

foreach ((array)$items['events'][$year][$month] as $day => $dayEvents) {
// Pull out multi-day events and list them separately first
foreach ((array)$dayEvents as $time => $events) {

Expand Down Expand Up @@ -383,18 +379,18 @@ public static function getListData(int $postId, array $items, string $searchTerm
$dtStart = Utils::dateFormat('Y-m-d', strtotime($event['multiday']['date_start']));
$dtEnd = Utils::dateFormat('Y-m-d', strtotime($event['multiday']['date_end']));
if ($time != 'all-day') {
$mdDtStart .= ' ' . Utils::timeFormat($event['multiday']['start_time']);
$mdDtEnd .= ' ' . Utils::timeFormat($event['multiday']['end_time']);
$dtStart .= ' ' . Utils::timeFormat($event['multiday']['start_time'], 'H:i:s');
$dtEnd .= ' ' . Utils::timeFormat($event['multiday']['end_time'], 'H:i:s');
$mdDtStart .= ', ' . Utils::timeFormat($event['multiday']['start_time']);
$mdDtEnd .= ', ' . Utils::timeFormat($event['multiday']['end_time']);
$dtStart .= ', ' . Utils::timeFormat($event['multiday']['start_time'], 'H:i:s');
$dtEnd .= ', ' . Utils::timeFormat($event['multiday']['end_time'], 'H:i:s');
} else {
$data[$i]['allday'] = true;
}

// Date/time
$data[$i]['dt_start'] = $dtStart;
$data[$i]['dt_end'] = $dtEnd;
$data[$i]['readable_date'] = $mdDtStart . ' &#8211; ' . $mdDtEnd;
$data[$i]['readable_date'] = $mdDtStart . ' &mdash; ' . $mdDtEnd;

// RRULE/FREQ
if ($event['rrule']) {
Expand Down Expand Up @@ -434,15 +430,16 @@ public static function getListData(int $postId, array $items, string $searchTerm
foreach ((array)$dayEvents as $time => $events) {

foreach ((array)$events as $event) {

if (!empty($event['multiday'])) {
continue;
}

// If it is not an all day event and current time > event end datetime then skip
// If it is not an all day event and limit date >= event end date then skip
if (
$time !== 'all-day'
&& !empty($event['end'])
&& current_time('Y-m-d H:i:s') > sprintf('%1$s-%2$s-%3$s %4$s', $year, $month, $day, $event['end'])
&& self::$limitDays >= $year . $month . $day
) {
continue;
}
Expand Down Expand Up @@ -470,7 +467,7 @@ public static function getListData(int $postId, array $items, string $searchTerm
$mtime = ' ' . $event['start'];
$dtStart = $dtStart . ' ' . $event['start'];
if (!empty($event['end']) && $event['end'] != $event['start']) {
$mtime .= ' &#8211; ' . $event['end'];
$mtime .= ' &mdash; ' . $event['end'];
$dtEnd = $dtEnd . ' ' . $event['end'];
} else {
$dtEnd = $dtStart;
Expand Down Expand Up @@ -538,7 +535,7 @@ public static function getListData(int $postId, array $items, string $searchTerm
* @param array $events
* @return array
*/
protected static function fixRecurrenceExceptions(array $events): array
private static function fixRecurrenceExceptions(array $events): array
{
$recurrenceExceptions = [];
foreach ($events as $time => $timeEvents) {
Expand Down Expand Up @@ -580,9 +577,10 @@ public static function insertData(int $postId)
$items = [];
$post = get_post($postId);
if (get_post_type($post) === CalendarFeed::POST_TYPE) {
$items = Events::getItems($postId);
$items = self::getItems($postId, self::$pastDays, self::$limitDays);
}
$items = count($items) ? Events::getListData($postId, $items) : $items;

$items = count($items) ? self::getListData($postId, $items) : $items;

if (count($items)) {
self::deleteData($postId);
Expand Down
2 changes: 1 addition & 1 deletion includes/ICS/EventsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function column_default($item, $column_name)
return $item['readable_date'];
break;
case 'readable_rrule':
return $item['readable_rrule'] ?? '';
return $item['readable_rrule'] ?? '&mdash;';
break;
case 'event_location':
return sanitize_text_field($item['location']);
Expand Down
Loading

0 comments on commit 176112c

Please sign in to comment.