Skip to content

Commit

Permalink
TMS-974: Recurring event functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
eebbi committed Nov 10, 2023
1 parent 848fd89 commit 5efc7c3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 29 deletions.
41 changes: 37 additions & 4 deletions lib/Eventz.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ public static function normalize_event( $event ) : array {
'date_title' => __( 'Dates', 'tms-theme-base' ),
'date' => static::get_event_date( $event ),
'dates' => static::get_event_dates( $event ),
'recurring' => ! empty( $event->event->dates ),
'recurring' => count( $event->event->dates ) > 1,
'time_title' => __( 'Time', 'tms-theme-base' ),
'time' => static::get_event_time( $event ),
// Include raw dates for possible sorting.
'start_date_raw' => static::get_as_datetime( $event->event->start ),
'end_date_raw' => static::get_as_datetime( $event->event->end ),
'start_date_raw' => static::get_as_datetime( $event->event->start ),
'end_date_raw' => static::get_as_datetime( $event->event->end ),
'location_title' => __( 'Location', 'tms-theme-base' ),
'location' => static::get_event_location( $event ),
'price_title' => __( 'Price', 'tms-theme-base' ),
'price' => static::get_event_price_info( $event, $lang_key ),
'provider_title' => __( 'Organizer', 'tms-theme-base' ),
'provider_title' => __( 'Organizer', 'tms-theme-base' ),
'area_title' => __( 'Area', 'tms-theme-base' ),
'areas' => static::get_area_info( $event ),
'target_title' => __( 'Target', 'tms-theme-base' ),
Expand Down Expand Up @@ -172,6 +172,24 @@ public static function get_event_date( $event ) {
$end_time = static::get_as_datetime( $event->event->end );
$date_format = get_option( 'date_format' );

// If date-parameter exists in url
if ( ! empty( $_GET['date'] ) ) {
list( $start_date, $end_date ) = explode( ' - ', $_GET['date'] );

$start_datetime = static::get_as_datetime( $start_date );
$end_datetime = ! is_null($end_date) ? static::get_as_datetime( $end_date ) : '';

if ( $start_datetime && $end_datetime && $start_datetime->diff( $end_datetime )->days >= 1 ) {
return sprintf(
'%s - %s',
$start_datetime->format( $date_format ),
$end_datetime->format( $date_format )
);
}

return $start_datetime->format( $date_format );
}

if ( $start_time && $end_time && $start_time->diff( $end_time )->days >= 1 ) {
return sprintf(
'%s - %s',
Expand All @@ -195,6 +213,21 @@ public static function get_event_time( $event ) {
return null;
}

// If time-parameter exists in url
if ( ! empty( $_GET['time'] ) ) {
list( $start_time, $end_time) = explode( ' - ', urldecode($_GET['time'] ) );

if ( $start_time && $end_time ) {
return sprintf(
'%s - %s',
$start_time,
$end_time
);
}

return $start_time;
}

$start_time = static::get_as_datetime( $event->event->start );
$end_time = static::get_as_datetime( $event->event->end );
$time_format = 'H.i';
Expand Down
8 changes: 7 additions & 1 deletion lib/Traits/Sharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ protected function get_share_links() : ?array {
$selected_channels = array_filter( $selected_channels, fn( $item ) => isset( $channels[ $item ] ) );
$current_post = get_queried_object();
$event_query_var = get_query_var( 'event-id' );
$event_date = $_GET['date'];
$event_time = $_GET['time'];
$overwrite_url = '';

if ( ! $current_post instanceof \WP_Post ) {
Expand All @@ -72,8 +74,12 @@ protected function get_share_links() : ?array {

// If the url has event-id url parameter set, use the whole url as share link
// instead of getting the permalink of the post.
// Add also date & time-parameters if they exist in event url.
if ( ! empty( $event_query_var ) ) {
$overwrite_url = get_permalink( $current_post->ID ) . '?event-id=' . $event_query_var;
$overwrite_url = get_permalink( $current_post->ID )
. '?event-id=' . $event_query_var
. ( ! empty( $event_date ) ? '&date=' . $event_date : '' )
. ( ! empty( $event_time ) ? '&time=' . urlencode( $event_time ) : '' );
}

return array_map( function ( $selected_channel ) use ( $channels, $current_post, $overwrite_url ) {
Expand Down
4 changes: 2 additions & 2 deletions models/page-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ protected function seo_image_generator() {
*/
protected function alter_desc( $description ) {
$event = $this->get_event();
$event = Eventz::normalize_event( $event );

if ( $event ) {
$event = Eventz::normalize_event( $event );
$description = $event['short_description'];
}

Expand Down Expand Up @@ -297,7 +297,7 @@ protected function set_event() : void {
* @return object|null
*/
protected function get_event() {
return $this->event;
return $this->event ?? null;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions models/page-events-calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ protected function get_events() : array {
}
}

if ( ! empty ( $response ) ) {
$response = $this->create_recurring_events( $response );
}

if ( ! empty( $response['events'] ) ) {

// Sort events.
Expand Down
27 changes: 16 additions & 11 deletions models/page-events-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,29 +314,34 @@ protected function set_pagination_data( int $event_count ) : void {
protected function create_recurring_events( $events ) {

$recurring_events = [];
foreach( $events['events'] as $event ) {
foreach ( $events['events'] as $event ) {
if ( count( $event['dates'] ) > 1 ) {
foreach( $event['dates'] as $date ) {
foreach ( $event['dates'] as $date ) {
$clone = $event;
// Split the string into date and time range
list($datePart, $timeRange) = explode(' ', $date['date'], 2);
list( $datePart, $timeRange ) = explode( ' ', $date['date'], 2 );

// Parse the date
$newDate = DateTime::createFromFormat('d.m.Y', $datePart);
$newDate = DateTime::createFromFormat( 'd.m.Y', $datePart );

// Split the time range into start and end times
list($startTime, $endTime) = explode(' - ', $timeRange);
list( $startTime, $endTime ) = explode( ' - ', $timeRange );

// Parse the start and end times
$startDateTime = DateTime::createFromFormat('H.i', $startTime);
$startDateTime->setDate( $newDate->format('Y'), $newDate->format('m'), $newDate->format('d') );
$endDateTime = DateTime::createFromFormat('H.i', $endTime);
$endDateTime->setDate( $newDate->format('Y'), $newDate->format('m'), $newDate->format('d') );
$startDateTime = DateTime::createFromFormat( 'H.i', $startTime );
$startDateTime->setDate( $newDate->format( 'Y' ), $newDate->format( 'm' ), $newDate->format( 'd' ) );
$endDateTime = DateTime::createFromFormat( 'H.i', $endTime );
$endDateTime->setDate( $newDate->format( 'Y' ), $newDate->format( 'm' ), $newDate->format( 'd' ) );

$clone['date'] = $newDate->format('d.m.Y');
$clone['date'] = $newDate->format( 'd.m.Y' );
$clone['start_date_raw'] = $startDateTime;
$clone['end_date_raw'] = $endDateTime;
$clone['url'] = $event['url'].'&date='.urlencode( $date['date'] );
$clone['url'] = $event['url'] . '&date=' . urlencode( $datePart ) . '&time=' . urlencode( $timeRange );
$clone['time'] = sprintf(
'%s - %s',
$startDateTime->format( 'H.i' ),
$endDateTime->format( 'H.i' )
);

$recurring_events[] = $clone;
}
Expand Down
6 changes: 3 additions & 3 deletions partials/page-events-calendar.dust
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{?description}
<div class="mt-5">
{description|s}
{description|kses}
</div>
{/description}
</div>
Expand All @@ -25,7 +25,7 @@
<a href="{url}"
class="is-flex is-align-items-center has-height-100 p-4 p-8-desktop has-background-secondary">
<span class="h6 pr-4 has-text-secondary-invert">
{title|s}
{title|html}
</span>

<span class="ml-auto">
Expand Down Expand Up @@ -58,7 +58,7 @@
{^events}
<div class="columns">
<div class="column is-6 is-offset-3 has-text-centered is-size-4">
{no_results|s}
{no_results|kses}
</div>
</div>
{/events}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

<div class="pt-3 pb-2">
{#event.normalized.dates}
<div class="info-group__description pr-8 pb-1 pl-8">
{date|attr}
</div>
{/event.normalized.dates}
<div class="info-group__description pr-8 pb-1 pl-8">
{date|attr}
</div>
{/event.normalized.dates}
</div>
</div>
{/event.normalized.date}
Expand Down

0 comments on commit 5efc7c3

Please sign in to comment.