Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add map filter for all upcoming events #507

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mu-plugins/blocks/google-map-event-filters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<!-- wp:wporg/google-map-event-filters {"filterSlug":"sotw","startDate":"December 10, 2023","endDate":"January 12, 2024","googleMapBlockAttributes":{"id":"sotw-2023","apiKey":"WORDCAMP_DEV_GOOGLE_MAPS_API_KEY"}} /-->

<!-- wp:wporg/google-map-event-filters {"filterSlug":"wp20","startDate":"April 21, 2023","endDate":"May 30, 2023","googleMapBlockAttributes":{"id":"wp20","apiKey":"WORDCAMP_DEV_GOOGLE_MAPS_API_KEY"}} /-->
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,67 @@ 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 ) {
$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;
}

/**
* Get a list of all events during a given timeframe.
*/
Expand All @@ -108,6 +152,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 );
Expand Down
Loading