Changeset 4857
- Timestamp:
- 02/03/2017 03:24:52 PM (8 years ago)
- 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 35 35 * Shortcode 36 36 * ============== 37 * Update shortcode to pull from cached DB entries instead of API directly. Probably remove caching from $this->remote_get()38 37 * 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 39 38 * Ability to feature a camp in a hero area … … 65 64 global $wpdb; 66 65 67 $events = $this-> get_all_events();66 $events = $this->fetch_upcoming_events(); 68 67 69 68 foreach ( $events as $event ) { … … 121 120 public function render_events() { 122 121 $output = ''; 123 $events = $this->group_events_by_date( $this->get_ all_events() );122 $events = $this->group_events_by_date( $this->get_cached_events() ); 124 123 125 124 if ( $events ) { … … 133 132 134 133 /** 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 136 178 * 137 179 * @return array 138 180 */ 139 protected function get_all_events() {181 protected function fetch_upcoming_events() { 140 182 $events = array_merge( $this->get_wordcamp_events(), $this->get_meetup_events() ); 141 usort( $events, array( $this, 'sort_events' ) );142 183 143 184 return $events; 144 }145 146 /**147 * Sort events based on start timestamp148 *149 * This is a callback for usort()150 *151 * @param $a152 * @param $b153 * @return int154 */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 }161 185 } 162 186 … … 536 560 * Wrapper for wp_remote_get() 537 561 * 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. 542 563 * 543 564 * @param string $url … … 550 571 551 572 if ( $url ) { 552 $transient_key = 'owe_' . wp_hash( $url . print_r( $args, true ) );553 554 if ( ! $response = get_transient( $transient_key ) ) {555 573 $response = wp_remote_get( $url, $args ); 556 574 … … 587 605 wp_mail( $to, sprintf( '%s error for %s', __METHOD__, parse_url( site_url(), PHP_URL_HOST ) ), sanitize_text_field( $error ) ); 588 606 } 589 } else {590 set_transient( $transient_key, $response, HOUR_IN_SECONDS );591 607 } 592 608 593 609 $this->maybe_pause( wp_remote_retrieve_headers( $response ) ); 594 }595 610 } 596 611
Note: See TracChangeset
for help on using the changeset viewer.