From 260dcf4d20f14a2477f7482ec6822cae935fad04 Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Wed, 6 Dec 2023 15:57:44 -0800 Subject: [PATCH] Events: Normalize facet parameters to apply multiple facet types Sometimes the query-filter block will provide query var as an array, and sometimes as a string. --- .../src/event-list/index.php | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/public_html/wp-content/themes/wporg-events-2023/src/event-list/index.php b/public_html/wp-content/themes/wporg-events-2023/src/event-list/index.php index 4552590efc..2899b40417 100644 --- a/public_html/wp-content/themes/wporg-events-2023/src/event-list/index.php +++ b/public_html/wp-content/themes/wporg-events-2023/src/event-list/index.php @@ -38,14 +38,7 @@ function init() { * @return string Returns the block markup. */ function render( $attributes, $content, $block ) { - $facets = array( - 'search' => sanitize_text_field( get_query_var( 's' ) ?? '' ), - 'type' => sanitize_text_field( get_query_var( 'event_type' ) ?? '' ), - 'format' => sanitize_text_field( get_query_var( 'format_type' ) ?? '' ), - 'month' => absint( get_query_var( 'month' ) ?? 0 ), - 'country' => sanitize_text_field( get_query_var( 'country' ) ?? '' ), - ); - + $facets = get_clean_query_facets(); $events = get_events( $attributes['events'], 0, 0, $facets ); schedule_filter_cron( $attributes['events'], 0, 0, $facets ); @@ -75,6 +68,36 @@ function render( $attributes, $content, $block ) { ); } +/** + * Get the query var facts and sanitize them. + * + * The query-filters block will provide the values as strings in some cases, but arrays in others. + * + * This converts them to the keys that the Google Map block uses. + */ +function get_clean_query_facets(): array { + $search = (array) get_query_var( 's' ) ?? array(); + $search = sanitize_text_field( $search[0] ?? '' ); + + $type = (array) get_query_var( 'event_type' ) ?? array(); + $type = sanitize_text_field( $type[0] ?? '' ); + + $format = (array) get_query_var( 'format_type' ) ?? array(); + $format = sanitize_text_field( $format[0] ?? '' ); + + $month = (array) get_query_var( 'month' ) ?? array(); + $month = absint( $month[0] ?? 0 ); + + $country = (array) get_query_var( 'country' ) ?? array(); + $country = sanitize_text_field( $country[0] ?? '' ); + + $facets = compact( 'search', 'type', 'format', 'month', 'country' ); + + array_filter( $facets ); // Remove empty. + + return $facets; +} + /** * Get a list of the currently-applied filters. *