Skip to content

Commit

Permalink
Merge branch 'master' into TMS-935
Browse files Browse the repository at this point in the history
  • Loading branch information
eebbi committed Nov 20, 2023
2 parents fffc980 + cfb1d38 commit c854027
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 28 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [1.54.0] - 2023-11-21

### Added

- TMS-974: Add recurring events to lists as single item.

### Fixed

- TMS-935: Archive filter accessibility fixes

## [1.53.0] - 2023-11-07
Expand Down
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' => isset( $event->event->dates ) ? count( $event->event->dates ) > 1 : null,
'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
14 changes: 10 additions & 4 deletions models/page-events-calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ protected function get_events() : array {
'targets' => get_field( 'target' ),
'tags' => get_field( 'tag' ),
'sort' => 'startDate',
'size' => get_option( 'posts_per_page' ),
'skip' => $skip,
'show_images' => get_field( 'show_images' ),
];

Expand Down Expand Up @@ -135,8 +133,16 @@ protected function get_events() : array {
}
}

if ( ! empty( $response['meta'] ) ) {
$this->set_pagination_data( $response['meta']->total );
if ( ! empty( $response['events'] ) ) {

// Sort events.
usort( $response['events'], function( $a, $b ) {
return $a['start_date_raw'] <=> $b['start_date_raw'];
} );

$this->set_pagination_data( count( $response['events'] ) );

$response['events'] = array_slice( $response['events'], $skip, get_option( 'posts_per_page' ) );
}

return $response;
Expand Down
99 changes: 90 additions & 9 deletions models/page-events-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ protected function get_events() : array {

$paged = get_query_var( 'paged', 1 );
$skip = 0;
$count = 0;

if ( $paged > 1 ) {
$skip = ( $paged - 1 ) * get_option( 'posts_per_page' );
Expand All @@ -152,8 +153,6 @@ protected function get_events() : array {
'end' => $end_date,
'sort' => 'startDate',
'category_id' => get_field( 'category' ) ?? [],
'size' => get_option( 'posts_per_page' ),
'skip' => $skip,
];

$formatter = new EventzFormatter();
Expand All @@ -176,12 +175,22 @@ protected function get_events() : array {
}
}

if ( ! empty( $response['meta'] ) ) {
$this->set_pagination_data( $response['meta']->total );
if ( ! empty( $response['events'] ) ) {

// Sort and paginate events.
usort( $response['events'], function( $a, $b ) {
return $a['start_date_raw'] <=> $b['start_date_raw'];
} );

$this->set_pagination_data( count( $response['events'] ) );

$count = count( $response['events'] );

$response['events'] = array_slice( $response['events'], $skip, get_option( 'posts_per_page' ) );
}

return [
'summary' => $this->get_results_text( $response['meta']->total ?? 0 ),
'summary' => $this->get_results_text( $count ),
'posts' => $response['events'],
];
}
Expand Down Expand Up @@ -227,12 +236,12 @@ protected function get_results_text( $event_count ) : ?string {
protected function do_get_events( array $params ) : array {
$event_data = $this->do_api_call( $params );

if ( ! empty( $event_data['meta'] ) ) {
$this->set_pagination_data( $event_data['meta']->total );
}

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

$event_data = $this->create_recurring_events( $event_data );

$event_data['events'] = ( new EventzFormatter() )->format_events( $event_data['events'] );

$event_data['events'] = array_map( function ( $item ) {
$item['short_description'] = wp_trim_words( $item['short_description'], 30 );
$item['location_icon'] = $item['is_virtual_event']
Expand Down Expand Up @@ -294,4 +303,76 @@ protected function set_pagination_data( int $event_count ) : void {
$this->pagination->items = $event_count;
$this->pagination->max_page = (int) ceil( $event_count / $per_page );
}

/**
* Create recurring events as single item.
*
* @param array $events Events.
*
* @return void
*/
protected function create_recurring_events( $events ) {

$recurring_events = [];
foreach ( $events['events'] as $event ) {
if ( count( $event['dates'] ) > 1 ) {
foreach ( $event['dates'] as $date ) {
$clone = $event;

// Split the dates and times into parts
list( $startPart, $endPart ) = explode( ' - ', $date['date'], 2 );
list( $startDate, $startTime ) = explode( ' ', $startPart, 2 );

// Check if endPart includes date & time
if ( strpos($endPart, ' ') ) {
list( $endDate, $endTime ) = explode( ' ', $endPart, 2 );
}
else {
$endTime = $endPart;
}

// Parse the dates
$newStartDate = DateTime::createFromFormat( 'd.m.Y', $startDate );
$newEndDate = $endDate ? DateTime::createFromFormat( 'd.m.Y', $endDate ) : null;

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

// Create time & date-ranges
if ( $endTime ) {
$timeRange = $startTime . ' - ' . $endTime;
}
else {
$timeRange = $startTime;
}

if ( $newEndDate ) {
$dateRange = $newStartDate->format( 'd.m.Y' ) . ' - ' . $newEndDate->format( 'd.m.Y' );
}
else {
$dateRange = $newStartDate->format( 'd.m.Y' );
}

$clone['date'] = $dateRange;
$clone['start_date_raw'] = $startDateTime;
$clone['end_date_raw'] = $endDateTime;
$clone['url'] = $event['url'] . '&date=' . urlencode( $dateRange ) . '&time=' . urlencode( $timeRange );

$recurring_events[] = $clone;
}
} else {
$recurring_events[] = $event;
}
}

$events['events'] = $recurring_events;

return $events;

}
}
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
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Theme Name: TMS Theme Base
* Description: Tampere Multisite Base Theme
* Domain Path: /lang
* Version: 1.52.0
* Version: 1.53.0
* Author: Geniem
* Author URI: https://geniem.fi
* Text Domain: tms-theme-base
Expand Down

0 comments on commit c854027

Please sign in to comment.