Skip to content

Commit

Permalink
TMS-968: Add recurring manual events to event-component
Browse files Browse the repository at this point in the history
  • Loading branch information
eebbi committed Feb 12, 2024
1 parent c5cd0ec commit ed404f9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
67 changes: 65 additions & 2 deletions lib/Formatters/EventzFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ public function format( array $layout ) : array {

$manual_events = [];
if ( ! empty( $layout['manual_event_categories'] ) ) {
$manual_events = self::get_manual_events( $layout['manual_event_categories'] );
$manual_events = self::get_manual_events( $layout['manual_event_categories'] );
$recurring_manual_events = self::get_recurring_manual_events( $layout['manual_event_categories'] );
}

$events = array_merge( $events['events'] ?? [], $manual_events );
$events = array_merge( $events['events'] ?? [], $manual_events, $recurring_manual_events );

if ( empty( $events ) ) {
return $layout;
Expand Down Expand Up @@ -343,4 +344,66 @@ public static function get_manual_events( array $category_ids = null ) : array {

return $events;
}

/**
* Get recurring manual events.
*
* @return array
*/
protected function get_recurring_manual_events( array $category_ids = null ) : array {
$args = [
'post_type' => PostType\ManualEvent::SLUG,
'posts_per_page' => 200, // phpcs:ignore
'meta_query' => [
[
'key' => 'recurring_event',
'value' => 1,
],
],
];

if ( ! empty( $category_ids ) ) {
$args['tax_query'] = [
[
'taxonomy' => Taxonomy\ManualEventCategory::SLUG,
'field' => 'term_id',
'terms' => array_values( $category_ids ),
'operator' => 'IN',
],
];
}

$query = new \WP_Query( $args );

if ( empty( $query->posts ) ) {
return [];
}

// Loop through events
$recurring_events = array_map( function ( $e ) {
$id = $e->ID;
$event = (object) \get_fields( $id );

foreach ( $event->dates as $date ) {
date_default_timezone_set( 'Europe/Helsinki' );
$time_now = \current_datetime()->getTimestamp();
$event_start = strtotime( $date['start'] );
$event_end = strtotime( $date['end'] );

// Return only ongoing or next upcoming event
if ( ( $time_now > $event_start && $time_now < $event_end ) || $time_now < $event_start ) {
$event->id = $id;
$event->title = \get_the_title( $id );
$event->url = \get_permalink( $id );
$event->image = \has_post_thumbnail( $id ) ? \get_the_post_thumbnail_url( $id, 'medium_large' ) : null;
$event->start_datetime = $date['start'];
$event->end_datetime = $date['end'];

return PostType\ManualEvent::normalize_event( $event );
}
}
}, $query->posts );

return $recurring_events;
}
}
2 changes: 1 addition & 1 deletion models/page-events-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected function do_get_events( array $params ) : array {

$event_data['events'] = array_map( function ( $item ) {
$item['short_description'] = wp_trim_words( $item['short_description'], 30 );
$item['location_icon'] = $item['is_virtual_event']
$item['location_icon'] = isset( $item['is_virtual_event'] )
? 'globe'
: 'location';

Expand Down

0 comments on commit ed404f9

Please sign in to comment.