Skip to content

Commit

Permalink
TMS-1089: Fix event fetching for events-component
Browse files Browse the repository at this point in the history
  • Loading branch information
eebbi committed Dec 2, 2024
1 parent 468d399 commit 1ef0380
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- TMS-1089: Fix event fetching for events-component

## [1.59.3] - 2024-11-25

- TMS-1059: Eventz changes:
Expand Down
27 changes: 13 additions & 14 deletions lib/Formatters/EventzFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public function format( array $layout ) : array {

$layout['events'] = $this->format_events( $events, $layout['show_images'] );
$layout['classes'] = [
'event_item_bg' => apply_filters( 'tms/theme/layout_events/item_bg_class', 'has-background-secondary' ),
'event_item_text' => apply_filters( 'tms/theme/layout_events/item_text_class', '' ),
'event_item_icon' => apply_filters( 'tms/theme/layout_events/item_icon_class', '' ),
'all_events_link' => apply_filters( 'tms/theme/layout_events/all_events_link', 'is-size-7' ),
'event_item_pill' => apply_filters( 'tms/theme/layout_events/event_item', 'is-primary-invert' ),
'event_item_bg' => \apply_filters( 'tms/theme/layout_events/item_bg_class', 'has-background-secondary' ),
'event_item_text' => \apply_filters( 'tms/theme/layout_events/item_text_class', '' ),
'event_item_icon' => \apply_filters( 'tms/theme/layout_events/item_icon_class', '' ),
'all_events_link' => \apply_filters( 'tms/theme/layout_events/all_events_link', 'is-size-7' ),
'event_item_pill' => \apply_filters( 'tms/theme/layout_events/event_item', 'is-primary-invert' ),
];

return $layout;
Expand All @@ -106,10 +106,10 @@ public function format( array $layout ) : array {
*
* @return void
*/
public static function create_recurring_events( $events, $query_params ) {
public static function create_recurring_events( $events, $query_params ) {

$recurring_events = [];
if( ! empty( $events['events'] ) ) {
if ( ! empty( $events['events'] ) ) {
foreach ( $events['events'] as $event ) {
$recurring_event_dates = [];

Expand Down Expand Up @@ -143,7 +143,7 @@ public static function create_recurring_events( $events, $query_params ) {
}

// Check if endPart includes date & time
if ( strpos($endPart, ' ') ) {
if ( strpos( $endPart, ' ' ) ) {
list( $endDate, $endTime ) = explode( ' ', $endPart, 2 );
}
else {
Expand Down Expand Up @@ -185,7 +185,8 @@ public static function create_recurring_events( $events, $query_params ) {

$recurring_events[] = $clone;
}
} else {
}
else {
$recurring_events[] = $event;
}
}
Expand Down Expand Up @@ -248,7 +249,6 @@ public function format_query_params( array $layout ) : array {
'sort' => null,
'size' => null,
'skip' => null,
'page_size' => null,
];

foreach ( $layout as $key => $value ) {
Expand Down Expand Up @@ -287,13 +287,12 @@ public function format_query_params( array $layout ) : array {
*
* @return array|null
*/
private function get_events( array $query_params ) : ?array {
private function get_events( array $query_params ): ?array {
// Force sort param
$query_params['sort'] = 'startDate';

if ( ! empty( $query_params['page_size'] ) ) {
$query_params['size'] = $query_params['page_size'];
}
// Get enough events to show correct events if they're recurring
$query_params['size'] = 30;

$client = new EventzClient( PIRKANMAA_EVENTZ_API_URL, PIRKANMAA_EVENTZ_API_KEY );

Expand Down
48 changes: 24 additions & 24 deletions models/page-events-calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PageEventsCalendar extends PageEventsSearch {
/**
* Description text
*/
public function description() : ?string {
public function description(): ?string {
return get_field( 'description' );
}

Expand All @@ -35,7 +35,7 @@ public function description() : ?string {
*
* @return string
*/
public function no_results() : string {
public function no_results(): string {
return __( 'No results', 'tms-theme-base' );
}

Expand All @@ -44,7 +44,7 @@ public function no_results() : string {
*
* @return bool
*/
public function is_grid() : bool {
public function is_grid(): bool {
$is_grid = get_field( 'layout' ) ?? 'grid';

return $is_grid === 'grid';
Expand All @@ -55,7 +55,7 @@ public function is_grid() : bool {
*
* @return string
*/
public function item_partial() : string {
public function item_partial(): string {
$part = $this->is_grid() ? 'grid' : 'list';

return 'views/page-events-calendar/page-events-calendar-item-' . $part;
Expand All @@ -64,7 +64,7 @@ public function item_partial() : string {
/**
* Get events
*/
public function events() : ?array {
public function events(): ?array {
try {
$response = $this->get_events();

Expand All @@ -82,28 +82,28 @@ public function events() : ?array {
*
* @return array
*/
protected function get_events() : array {
protected function get_events(): array {

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

if ( $paged > 1 ) {
$skip = ( $paged - 1 ) * get_option( 'posts_per_page' );
$skip = ( $paged - 1 ) * \get_option( 'posts_per_page' );
}

$params = [
'q' => get_field( 'text' ),
'start' => get_field( 'start' ),
'end' => get_field( 'end' ),
'category_id' => get_field( 'category' ),
'areas' => get_field( 'area' ),
'targets' => get_field( 'target' ),
'tags' => get_field( 'tag' ),
'q' => \get_field( 'text' ),
'start' => \get_field( 'start' ),
'end' => \get_field( 'end' ),
'category_id' => \get_field( 'category' ),
'areas' => \get_field( 'area' ),
'targets' => \get_field( 'target' ),
'tags' => \get_field( 'tag' ),
'sort' => 'startDate',
'show_images' => get_field( 'show_images' ),
'show_images' => \get_field( 'show_images' ),
];

if ( ! empty( get_field( 'starts_today' ) ) && true === get_field( 'starts_today' ) ) {
if ( ! empty( \get_field( 'starts_today' ) ) && true === \get_field( 'starts_today' ) ) {
$params['start'] = date( 'Y-m-d' );
}

Expand All @@ -116,15 +116,15 @@ protected function get_events() : array {
$params = $formatter->format_query_params( $params );

$cache_group = 'page-events-calendar';
$cache_key = md5( wp_json_encode( $params ) );
$response = wp_cache_get( $cache_key, $cache_group );
$cache_key = md5( \wp_json_encode( $params ) );
$response = \wp_cache_get( $cache_key, $cache_group );

if ( empty( $response ) ) {

$response = $this->do_get_events( $params );

if ( ! empty( $response ) ) {
wp_cache_set(
\wp_cache_set(
$cache_key,
$response,
$cache_group,
Expand All @@ -136,7 +136,7 @@ protected function get_events() : array {
if ( ! empty( $response['events'] ) ) {

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

Expand All @@ -153,7 +153,7 @@ protected function get_events() : array {
*
* @return array|null
*/
public function calendar_pages() : ?array {
public function calendar_pages(): ?array {
if ( ! Settings::get_setting( 'show_related_events_calendars' ) ) {
return null;
}
Expand All @@ -171,15 +171,15 @@ public function calendar_pages() : ?array {
return null;
}

$current_page = get_queried_object_id();
$current_page = \get_queried_object_id();

$pages = array_filter( $the_query->posts, function ( $item ) use ( $current_page ) {
return $item->ID !== $current_page;
} );

return array_map( function ( $item ) {
return [
'url' => get_the_permalink( $item->ID ),
'url' => \get_the_permalink( $item->ID ),
'title' => $item->post_title,
];
}, $pages );
Expand Down

0 comments on commit 1ef0380

Please sign in to comment.