Making WordPress.org

Changeset 4857


Ignore:
Timestamp:
02/03/2017 03:24:52 PM (8 years ago)
Author:
iandunn
Message:

Official WordPress Events: Pull events from database cache for performance

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php

    r4855 r4857  
    3535     * Shortcode
    3636     * ==============
    37      * Update shortcode to pull from cached DB entries instead of API directly. Probably remove caching from $this->remote_get()
    3837     * Make meetups and wordcamps cut off on the same date, so it doesn't look like there aren't any meetups later in the year
    3938     * Ability to feature a camp in a hero area
     
    6564        global $wpdb;
    6665
    67         $events = $this->get_all_events();
     66        $events = $this->fetch_upcoming_events();
    6867
    6968        foreach ( $events as $event ) {
     
    121120    public function render_events() {
    122121        $output = '';
    123         $events = $this->group_events_by_date( $this->get_all_events() );
     122        $events = $this->group_events_by_date( $this->get_cached_events() );
    124123
    125124        if ( $events ) {
     
    133132
    134133    /**
    135      * Get all official events
     134     * Get cached events from the local database
     135     *
     136     * @return array
     137     */
     138    protected function get_cached_events() {
     139        global $wpdb;
     140
     141        $cached_events = array();
     142
     143        // Include yesterday's events because server timezone may be ahead of user's timezone
     144        $raw_events = $wpdb->get_results( sprintf( "
     145            SELECT *
     146            FROM `%s`
     147            WHERE date_utc >= SUBDATE( CURRENT_DATE(), 1 )
     148            ORDER BY date_utc ASC
     149            LIMIT 300",
     150            self::EVENTS_TABLE
     151        ) );
     152
     153        foreach ( $raw_events as $event ) {
     154            $cached_events[] = new Official_WordPress_Event( array(
     155                'id'              => $event->id,
     156                'type'            => $event->type,
     157                'source_id'       => $event->source_id,
     158                'title'           => $event->title,
     159                'url'             => $event->url,
     160                'description'     => $event->description,
     161                'num_attendees'   => $event->attendees,
     162                'meetup_name'     => $event->meetup,
     163                'meetup_url'      => $event->meetup_url,
     164                'start_timestamp' => strtotime( $event->date_utc ),
     165                'end_timestamp'   => strtotime( $event->end_date ),
     166                'location'        => $event->location,
     167                'country_code'    => $event->country,
     168                'latitude'        => $event->latitude,
     169                'longitude'       => $event->longitude,
     170            ) );
     171        }
     172
     173        return $cached_events;
     174    }
     175
     176    /**
     177     * Fetch all upcoming official events from various external APIs
    136178     *
    137179     * @return array
    138180     */
    139     protected function get_all_events() {
     181    protected function fetch_upcoming_events() {
    140182        $events = array_merge( $this->get_wordcamp_events(), $this->get_meetup_events() );
    141         usort( $events, array( $this, 'sort_events' ) );
    142183
    143184        return $events;
    144     }
    145 
    146     /**
    147      * Sort events based on start timestamp
    148      *
    149      * This is a callback for usort()
    150      *
    151      * @param $a
    152      * @param $b
    153      * @return int
    154      */
    155     protected function sort_events( $a, $b ) {
    156         if ( $a->start_timestamp == $b->start_timestamp ) {
    157             return 0;
    158         } else {
    159             return $a->start_timestamp > $b->start_timestamp ? 1 : -1;
    160         }
    161185    }
    162186
     
    536560     * Wrapper for wp_remote_get()
    537561     *
    538      * This adds caching and error logging/notification.
    539      *
    540      * @todo It'd be better to always display cached data, but trigger an asynchronous refresh when you detect it's
    541      *       changed, so that the user is never waiting on it to refresh.
     562     * This adds error logging/notification.
    542563     *
    543564     * @param string $url
     
    550571
    551572        if ( $url ) {
    552             $transient_key = 'owe_' . wp_hash( $url . print_r( $args, true ) );
    553 
    554             if ( ! $response = get_transient( $transient_key ) ) {
    555573                $response = wp_remote_get( $url, $args );
    556574
     
    587605                        wp_mail( $to, sprintf( '%s error for %s', __METHOD__, parse_url( site_url(), PHP_URL_HOST ) ), sanitize_text_field( $error ) );
    588606                    }
    589                 } else {
    590                     set_transient( $transient_key, $response, HOUR_IN_SECONDS );
    591607                }
    592608
    593609                $this->maybe_pause( wp_remote_retrieve_headers( $response ) );
    594             }
    595610        }
    596611
Note: See TracChangeset for help on using the changeset viewer.