Make WordPress Core


Ignore:
Timestamp:
10/22/2020 04:03:25 AM (5 years ago)
Author:
tellyworth
Message:

Community Events: Display dates and times in the user's time zone.

Fixes #51130
Merges [49145], [49146], [49147], [49152], and [49201] to the 5.5 branch.
Props sippis, hlashbrooke, audrasjb, Rarst, iandunn

Location:
branches/5.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.5

  • branches/5.5/src/wp-admin/includes/class-wp-community-events.php

    r48110 r49275  
    7878     *
    7979     * @since 4.8.0
     80     * @since 5.5.2 Response no longer contains formatted date field. They're added
     81     *              in `wp.communityEvents.populateDynamicEventFields()` now.
    8082     *
    8183     * @param string $location_search Optional. City name to help determine the location.
     
    159161            }
    160162
     163            /*
     164             * Store the raw response, because events will expire before the cache does.
     165             * The response will need to be processed every page load.
     166             */
    161167            $this->cache_events( $response_body, $expiration );
    162168
    163             $response_body = $this->trim_events( $response_body );
    164             $response_body = $this->format_event_data_time( $response_body );
     169            $response_body['events'] = $this->trim_events( $response_body['events'] );
    165170
    166171            return $response_body;
     
    341346     *
    342347     * @since 4.8.0
     348     * @since 5.5.2 Response no longer contains formatted date field. They're added
     349     *              in `wp.communityEvents.populateDynamicEventFields()` now.
    343350     *
    344351     * @return array|false An array containing `location` and `events` items
     
    347354    public function get_cached_events() {
    348355        $cached_response = get_site_transient( $this->get_events_transient_key( $this->user_location ) );
    349         $cached_response = $this->trim_events( $cached_response );
    350 
    351         return $this->format_event_data_time( $cached_response );
     356
     357        if ( isset( $cached_response['events'] ) ) {
     358            $cached_response['events'] = $this->trim_events( $cached_response['events'] );
     359        }
     360
     361        return $cached_response;
    352362    }
    353363
     
    361371     *
    362372     * @since 4.8.0
     373     * @deprecated 5.6.0 No longer used in core.
    363374     *
    364375     * @param array $response_body The response which contains the events.
     
    366377     */
    367378    protected function format_event_data_time( $response_body ) {
     379        _deprecated_function(
     380            __METHOD__,
     381            '5.5.2',
     382            'This is no longer used by core, and only kept for backward compatibility.'
     383        );
     384
    368385        if ( isset( $response_body['events'] ) ) {
    369386            foreach ( $response_body['events'] as $key => $event ) {
     
    436453     * @since 4.8.0
    437454     * @since 4.9.7 Stick a WordCamp to the final list.
    438      *
    439      * @param array $response_body The response body which contains the events.
     455     * @since 5.5.2 Accepts and returns only the events, rather than an entire HTTP response.
     456     *
     457     * @param array $events The events that will be prepared.
    440458     * @return array The response body with events trimmed.
    441459     */
    442     protected function trim_events( $response_body ) {
    443         if ( isset( $response_body['events'] ) ) {
    444             $wordcamps = array();
    445             $today     = current_time( 'Y-m-d' );
    446 
    447             foreach ( $response_body['events'] as $key => $event ) {
    448                 /*
    449                  * Skip WordCamps, because they might be multi-day events.
    450                  * Save a copy so they can be pinned later.
    451                  */
    452                 if ( 'wordcamp' === $event['type'] ) {
    453                     $wordcamps[] = $event;
    454                     continue;
    455                 }
    456 
    457                 // We don't get accurate time with timezone from API, so we only take the date part (Y-m-d).
    458                 $event_date = substr( $event['date'], 0, 10 );
    459 
    460                 if ( $today > $event_date ) {
    461                     unset( $response_body['events'][ $key ] );
    462                 }
    463             }
    464 
    465             $response_body['events'] = array_slice( $response_body['events'], 0, 3 );
    466             $trimmed_event_types     = wp_list_pluck( $response_body['events'], 'type' );
    467 
    468             // Make sure the soonest upcoming WordCamp is pinned in the list.
    469             if ( ! in_array( 'wordcamp', $trimmed_event_types, true ) && $wordcamps ) {
    470                 array_pop( $response_body['events'] );
    471                 array_push( $response_body['events'], $wordcamps[0] );
    472             }
    473         }
    474 
    475         return $response_body;
     460    protected function trim_events( array $events ) {
     461        $future_events = array();
     462
     463        foreach ( $events as $event ) {
     464            /*
     465             * The API's `date` and `end_date` fields are in the _event's_ local timezone, but UTC is needed so
     466             * it can be converted to the _user's_ local time.
     467             */
     468            $end_time = (int) $event['end_unix_timestamp'];
     469
     470            if ( time() < $end_time ) {
     471                array_push( $future_events, $event );
     472            }
     473        }
     474
     475        $future_wordcamps = array_filter(
     476            $future_events,
     477            function( $wordcamp ) {
     478                return 'wordcamp' === $wordcamp['type'];
     479            }
     480        );
     481
     482        $future_wordcamps    = array_values( $future_wordcamps ); // Remove gaps in indices.
     483        $trimmed_events      = array_slice( $future_events, 0, 3 );
     484        $trimmed_event_types = wp_list_pluck( $trimmed_events, 'type' );
     485
     486        // Make sure the soonest upcoming WordCamp is pinned in the list.
     487        if ( $future_wordcamps && ! in_array( 'wordcamp', $trimmed_event_types, true ) ) {
     488            array_pop( $trimmed_events );
     489            array_push( $trimmed_events, $future_wordcamps[0] );
     490        }
     491
     492        return $trimmed_events;
    476493    }
    477494
Note: See TracChangeset for help on using the changeset viewer.