diff --git a/wp-admin/includes/class-wp-community-events.php b/wp-admin/includes/class-wp-community-events.php index 486739349e..37c71be412 100644 --- a/wp-admin/includes/class-wp-community-events.php +++ b/wp-admin/includes/class-wp-community-events.php @@ -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; } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index bffd69a9fc..bac267d646 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.