Community Events: Trim events by Unix timestamp for accuracy.

The `date` and `end_date` fields are ''WP'' timestamps representing the venue's local time. As of meta:changeset:10270 (#meta4480), new `start_unix_timestamp` and `end_unix_timestamp` values are available, providing a proper ''Unix'' timestamp in the  UTC timezone. Using those is more precise, and removes the time window where the event has expired but still appears in the Events Widget.

To simplify the function, it now only accepts and returns the events themselves, rather than the entire response body.

See #51130
See #meta4480
Related: https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/

Built from https://develop.svn.wordpress.org/trunk@49145


git-svn-id: http://core.svn.wordpress.org/trunk@48907 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
iandunn 2020-10-14 18:19:10 +00:00
parent 542d13830b
commit 5d42748c18
2 changed files with 40 additions and 33 deletions

View File

@ -158,9 +158,13 @@ class WP_Community_Events {
$response_body['location']['description'] = $this->user_location['description'];
}
/*
* Store the raw response, because events will expire before the cache does.
* The response will need to be processed every page load.
*/
$this->cache_events( $response_body, $expiration );
$response_body = $this->trim_events( $response_body );
$response_body['events'] = $this->trim_events( $response_body['events'] );
$response_body = $this->format_event_data_time( $response_body );
return $response_body;
@ -346,7 +350,10 @@ class WP_Community_Events {
*/
public function get_cached_events() {
$cached_response = get_site_transient( $this->get_events_transient_key( $this->user_location ) );
$cached_response = $this->trim_events( $cached_response );
if ( isset( $cached_response['events'] ) ) {
$cached_response['events'] = $this->trim_events( $cached_response['events'] );
}
return $this->format_event_data_time( $cached_response );
}
@ -435,44 +442,44 @@ class WP_Community_Events {
*
* @since 4.8.0
* @since 4.9.7 Stick a WordCamp to the final list.
* @since 5.6.0 Accepts and returns only the events, rather than an entire HTTP response.
*
* @param array $response_body The response body which contains the events.
* @param array $events The events that will be prepared.
* @return array The response body with events trimmed.
*/
protected function trim_events( $response_body ) {
if ( isset( $response_body['events'] ) ) {
$wordcamps = array();
$today = current_time( 'Y-m-d' );
protected function trim_events( array $events ) {
$future_events = array();
foreach ( $response_body['events'] as $key => $event ) {
/*
* Skip WordCamps, because they might be multi-day events.
* Save a copy so they can be pinned later.
*/
if ( 'wordcamp' === $event['type'] ) {
$wordcamps[] = $event;
continue;
}
foreach ( $events as $event ) {
/*
* The API's `date` and `end_date` fields are in the _event's_ local timezone, but UTC is needed so
* it can be converted to the _user's_ local time.
*/
$end_time = (int) $event['end_unix_timestamp'];
// We don't get accurate time with timezone from API, so we only take the date part (Y-m-d).
$event_date = substr( $event['date'], 0, 10 );
if ( $today > $event_date ) {
unset( $response_body['events'][ $key ] );
}
}
$response_body['events'] = array_slice( $response_body['events'], 0, 3 );
$trimmed_event_types = wp_list_pluck( $response_body['events'], 'type' );
// Make sure the soonest upcoming WordCamp is pinned in the list.
if ( ! in_array( 'wordcamp', $trimmed_event_types, true ) && $wordcamps ) {
array_pop( $response_body['events'] );
array_push( $response_body['events'], $wordcamps[0] );
if ( time() < $end_time ) {
array_push( $future_events, $event );
}
}
return $response_body;
$future_wordcamps = array_filter(
$future_events,
function( $wordcamp ) {
return 'wordcamp' === $wordcamp['type'];
}
);
$future_wordcamps = array_values( $future_wordcamps ); // Remove gaps in indices.
$trimmed_events = array_slice( $future_events, 0, 3 );
$trimmed_event_types = wp_list_pluck( $trimmed_events, 'type' );
// Make sure the soonest upcoming WordCamp is pinned in the list.
if ( $future_wordcamps && ! in_array( 'wordcamp', $trimmed_event_types, true ) ) {
array_pop( $trimmed_events );
array_push( $trimmed_events, $future_wordcamps[0] );
}
return $trimmed_events;
}
/**

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.6-alpha-49144';
$wp_version = '5.6-alpha-49145';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.