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:
parent
542d13830b
commit
5d42748c18
|
@ -158,9 +158,13 @@ class WP_Community_Events {
|
||||||
$response_body['location']['description'] = $this->user_location['description'];
|
$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 );
|
$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 );
|
$response_body = $this->format_event_data_time( $response_body );
|
||||||
|
|
||||||
return $response_body;
|
return $response_body;
|
||||||
|
@ -346,7 +350,10 @@ class WP_Community_Events {
|
||||||
*/
|
*/
|
||||||
public function get_cached_events() {
|
public function get_cached_events() {
|
||||||
$cached_response = get_site_transient( $this->get_events_transient_key( $this->user_location ) );
|
$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 );
|
return $this->format_event_data_time( $cached_response );
|
||||||
}
|
}
|
||||||
|
@ -435,44 +442,44 @@ class WP_Community_Events {
|
||||||
*
|
*
|
||||||
* @since 4.8.0
|
* @since 4.8.0
|
||||||
* @since 4.9.7 Stick a WordCamp to the final list.
|
* @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.
|
* @return array The response body with events trimmed.
|
||||||
*/
|
*/
|
||||||
protected function trim_events( $response_body ) {
|
protected function trim_events( array $events ) {
|
||||||
if ( isset( $response_body['events'] ) ) {
|
$future_events = array();
|
||||||
$wordcamps = array();
|
|
||||||
$today = current_time( 'Y-m-d' );
|
|
||||||
|
|
||||||
foreach ( $response_body['events'] as $key => $event ) {
|
foreach ( $events as $event ) {
|
||||||
/*
|
/*
|
||||||
* Skip WordCamps, because they might be multi-day events.
|
* The API's `date` and `end_date` fields are in the _event's_ local timezone, but UTC is needed so
|
||||||
* Save a copy so they can be pinned later.
|
* it can be converted to the _user's_ local time.
|
||||||
*/
|
*/
|
||||||
if ( 'wordcamp' === $event['type'] ) {
|
$end_time = (int) $event['end_unix_timestamp'];
|
||||||
$wordcamps[] = $event;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't get accurate time with timezone from API, so we only take the date part (Y-m-d).
|
if ( time() < $end_time ) {
|
||||||
$event_date = substr( $event['date'], 0, 10 );
|
array_push( $future_events, $event );
|
||||||
|
|
||||||
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] );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @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.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue