Skip to content

Commit

Permalink
Map: Add filter for all past events.
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Dec 4, 2023
1 parent 1da43b8 commit 1352b4b
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions mu-plugins/blocks/google-map/inc/event-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ function get_events( string $filter_slug, int $start_timestamp, int $end_timesta
$cacheable = true;
$events = array();
$facets = array_merge( array( 'search' => '' ), $_GET );
$page = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;

array_walk( $facets, 'sanitize_text_field' );

if ( ! empty( $facets['search'] ) || count( $facets ) > 1 ) {
if ( ! empty( $facets['search'] ) || count( $facets ) > 1 || $page !== 1 ) {
// Search terms vary so much that caching them probably wouldn't result in a significant degree of
// cache hits, but it would generate a lot of extra transients. With memcached, that could push
// more useful values out of the cache.
// more useful values out of the cache. Old pages are similar.
$cacheable = false;
}

Expand All @@ -60,6 +61,10 @@ function get_events( string $filter_slug, int $start_timestamp, int $end_timesta
$events = get_all_upcoming_events( $facets );
break;

case 'all-past':
$events = get_all_past_events( $page );
break;

case 'wp20':
case 'sotw':
$potential_matches = get_events_between_dates( $start_timestamp, $end_timestamp );
Expand Down Expand Up @@ -140,6 +145,66 @@ function get_all_upcoming_events( array $facets = array() ): array {
return $events;
}

/**
* Get a list of all upcoming events across all sites.
*/
function get_all_past_events( $page ): array {
global $wpdb;

$limit = 50;
$offset = ( $page - 1 ) * $limit;

// wporg_events.status doesn't have a separate value for "completed", it's just scheduled events that have
// a date in the past.
$query = $wpdb->prepare( '
SELECT
id, `type`, title, url, meetup, location, latitude, longitude, date_utc,
date_utc_offset AS tz_offset
FROM `wporg_events`
WHERE
status = "scheduled" AND
date_utc < NOW()
ORDER BY date_utc DESC
LIMIT %d, %d',
$offset,
$limit
);

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 the total number of past events.
*/
function get_all_past_events_count(): int {
global $wpdb;

$transient_key = 'google_map_event_filters_past_events_count';
$count = get_transient( $transient_key );

if ( ! $count ) {
$count = $wpdb->get_var( '
SELECT COUNT( id ) as found_events
FROM `wporg_events`
WHERE
status = "scheduled" AND
date_utc < NOW()'
);

set_transient( $transient_key, $count, HOUR_IN_SECONDS );
}

return $count;
}

/**
* Get a list of all events during a given timeframe.
*/
Expand Down

0 comments on commit 1352b4b

Please sign in to comment.