From fbfb59a79bae43a501bbf45fe0df9e70eb89311a Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Thu, 16 Nov 2023 11:07:19 -0800 Subject: [PATCH 1/3] Map: Modularize cleanup function for reuse --- .../google-map-event-filters.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php b/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php index 16c96fe97..5203ee3d8 100644 --- a/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php +++ b/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php @@ -108,6 +108,15 @@ function get_events_between_dates( int $start_timestamp, int $end_timestamp ) : $events = get_latin1_results_with_prepared_query( $query ); } + $events = prepare_events( $events ); + + return $events; +} + +/** + * Clean up events + */ +function prepare_events( array $events ): array { foreach ( $events as $event ) { // `capital_P_dangit()` won't work here because the current filter isn't `the_title` and there isn't a safelisted prefix before `$text`. $event->title = str_replace( 'Wordpress', 'WordPress', $event->title ); From 593d5e8cd4ebf9182636fe05e298a5152eebd3e3 Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Thu, 16 Nov 2023 11:08:59 -0800 Subject: [PATCH 2/3] Map: Add filter for all upcoming events --- .../blocks/google-map-event-filters/README.md | 2 + .../google-map-event-filters.php | 52 +++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/mu-plugins/blocks/google-map-event-filters/README.md b/mu-plugins/blocks/google-map-event-filters/README.md index 090313abd..0b73d3197 100644 --- a/mu-plugins/blocks/google-map-event-filters/README.md +++ b/mu-plugins/blocks/google-map-event-filters/README.md @@ -32,6 +32,8 @@ It uses the `wporg/google-map` block to display a searchable list and/or map of Alternatively, you could take that JSON and manually put it in the post source like this: ```html + + ``` diff --git a/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php b/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php index 5203ee3d8..559260963 100644 --- a/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php +++ b/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php @@ -70,15 +70,59 @@ function get_events( string $filter_slug, int $start_timestamp, int $end_timesta if ( $cached_events && ! $force_refresh ) { $events = $cached_events; + } else { - $potential_matches = get_events_between_dates( $start_timestamp, $end_timestamp ); - $filtered_events = filter_potential_events( $filter_slug, $potential_matches ); - $events = $filtered_events; + switch ( $filter_slug ) { + case 'all-upcoming': + $events = get_all_upcoming_events(); + break; + + case 'wp20': + case 'sotw': + $potential_matches = get_events_between_dates( $start_timestamp, $end_timestamp ); + $events = filter_potential_events( $filter_slug, $potential_matches ); + break; + + default: + return apply_filters( "google_map_event_filters_{$filter_slug}", array() ); + } // Store for a day to make sure it never expires before the priming cron job runs. - set_transient( $cache_key, $filtered_events, DAY_IN_SECONDS ); + set_transient( $cache_key, $events, DAY_IN_SECONDS ); + } + + return $events; +} + +/** + * Get a list of all upcoming events across all sites. + */ +function get_all_upcoming_events(): array { + global $wpdb; + + $query = ' + SELECT + id, `type`, title, url, meetup, location, latitude, longitude, date_utc, + date_utc_offset AS tz_offset + FROM `wporg_events` + WHERE + status = "scheduled" AND + ( + ( "wordcamp" = type AND date_utc BETWEEN NOW() AND ADDDATE( NOW(), 180 ) ) OR + ( "meetup" = type AND date_utc BETWEEN NOW() AND ADDDATE( NOW(), 30 ) ) + ) + ORDER BY date_utc ASC + LIMIT 400' + ; + + if ( 'latin1' === DB_CHARSET ) { + $events = $wpdb->get_results( $query ); + } else { + $events = get_latin1_results_with_prepared_query( $query ); } + $events = prepare_events( $events ); + return $events; } From ef8936be8d1e1c8500224344fb172f03677289e7 Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Thu, 16 Nov 2023 11:31:56 -0800 Subject: [PATCH 3/3] Map: Remove cron option from cache key to match live key Previously the events that were cached from a live run were using a separate key than the events that were cached from a cron run. They need to use the same key in order for the cron job to do what it's intended to do. --- .../google-map-event-filters/google-map-event-filters.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php b/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php index 559260963..31c85447a 100644 --- a/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php +++ b/mu-plugins/blocks/google-map-event-filters/google-map-event-filters.php @@ -65,7 +65,7 @@ function render( array $attributes, string $content, WP_Block $block ): string { */ function get_events( string $filter_slug, int $start_timestamp, int $end_timestamp, bool $force_refresh = false ) : array { $events = array(); - $cache_key = 'google-map-event-filters-' . md5( wp_json_encode( func_get_args() ) ); + $cache_key = 'google-map-event-filters-' . md5( wp_json_encode( $filter_slug . $start_timestamp . $end_timestamp ) ); $cached_events = get_transient( $cache_key ); if ( $cached_events && ! $force_refresh ) {