Changeset 4871
- Timestamp:
- 02/03/2017 09:51:02 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
r4868 r4871 1 1 <?php 2 2 3 /* 3 4 Plugin Name: Official WordPress Events … … 37 38 */ 38 39 public function __construct() { 39 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 40 add_shortcode( 'official_wordpress_events', array( $this, 'render_events' ) ); 41 42 add_action( 'owpe_prime_events_cache', array( $this, 'prime_events_cache' ) ); 40 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 41 add_action( 'owpe_prime_events_cache', array( $this, 'prime_events_cache' ) ); 42 add_shortcode( 'official_wordpress_events', array( $this, 'render_events' ) ); 43 43 44 44 if ( ! wp_next_scheduled( 'owpe_prime_events_cache' ) ) { 45 wp_schedule_event( time(), 'hourly', 'owpe_prime_events_cache' );45 wp_schedule_event( time(), 'hourly', 'owpe_prime_events_cache' ); 46 46 } 47 47 } … … 168 168 /** 169 169 * Fetch all upcoming official events from various external APIs 170 * 170 * 171 171 * @return array 172 172 */ … … 208 208 default : 209 209 $request_params = array( 210 'type' 210 'type' => 'wordcamp', 211 211 ); 212 212 $endpoint = add_query_arg( $request_params, self::WORDCAMP_API_BASE_URL . 'posts' ); … … 232 232 */ 233 233 protected function get_wordcamp_events() { 234 $endpoint 235 $response 234 $endpoint = $this->get_wordcamp_events_endpoint( self::WORDCAMP_API_VERSION ); 235 $response = $this->remote_get( esc_url_raw( $endpoint ) ); 236 236 237 237 switch ( self::WORDCAMP_API_VERSION ) { … … 319 319 foreach ( $wordcamps as $wordcamp ) { 320 320 $event = array( 321 'source_id' => $wordcamp->id,322 'type' => 'wordcamp',323 'title' => $wordcamp->title->rendered,321 'source_id' => $wordcamp->id, 322 'type' => 'wordcamp', 323 'title' => $wordcamp->title->rendered, 324 324 'description' => $wordcamp->content->rendered, 325 325 ); … … 337 337 338 338 case 'End Date (YYYY-mm-dd)': 339 $value = absint( $value );339 $value = absint( $value ); 340 340 $event['end_timestamp'] = $value; 341 341 break; … … 394 394 395 395 $groups = array_chunk( $groups, 200, true ); 396 foreach( $groups as $group_batch ) { 396 397 foreach ( $groups as $group_batch ) { 397 398 $request_url = sprintf( 398 399 '%s2/events?group_id=%s&time=0,3m&page=%d&key=%s', 399 400 self::MEETUP_API_BASE_URL, 400 implode( ',', $group_batch ),401 implode( ',', $group_batch ), 401 402 200, 402 403 MEETUP_API_KEY 403 404 ); 404 405 405 while( '' !== $request_url ) { 406 $response = $this->remote_get( $request_url ); 407 $body = json_decode( wp_remote_retrieve_body( $response ) ); 408 409 if ( ! empty ( $body->results ) ) { 410 $meetups = $body->results; 411 412 foreach ( $meetups as $meetup ) { 413 $start_timestamp = ( $meetup->time / 1000 ) + ( $meetup->utc_offset / 1000 ); // convert to seconds 414 415 if ( isset( $meetup->venue ) ) { 416 $location = $this->format_meetup_venue_location( $meetup->venue ); 417 } else { 418 $geocoded_location = $this->reverse_geocode( $meetup->group->group_lat, $meetup->group->group_lon ); 419 $location_parts = $this->parse_reverse_geocode_address( $geocoded_location->address_components ); 420 $location = sprintf( 421 '%s%s%s', 422 $location_parts['city'], 423 empty( $location_parts['state'] ) ? '' : ', ' . $location_parts['state'], 424 empty( $location_parts['country_name'] ) ? '' : ', ' . $location_parts['country_name'] 425 ); 406 while ( '' !== $request_url ) { 407 $response = $this->remote_get( $request_url ); 408 $body = json_decode( wp_remote_retrieve_body( $response ) ); 409 410 if ( ! empty ( $body->results ) ) { 411 $meetups = $body->results; 412 413 foreach ( $meetups as $meetup ) { 414 $start_timestamp = ( $meetup->time / 1000 ) + ( $meetup->utc_offset / 1000 ); // convert to seconds 415 416 if ( isset( $meetup->venue ) ) { 417 $location = $this->format_meetup_venue_location( $meetup->venue ); 418 } else { 419 $geocoded_location = $this->reverse_geocode( $meetup->group->group_lat, $meetup->group->group_lon ); 420 $location_parts = $this->parse_reverse_geocode_address( $geocoded_location->address_components ); 421 $location = sprintf( 422 '%s%s%s', 423 $location_parts['city'], 424 empty( $location_parts['state'] ) ? '' : ', ' . $location_parts['state'], 425 empty( $location_parts['country_name'] ) ? '' : ', ' . $location_parts['country_name'] 426 ); 427 } 428 429 if ( ! empty( $meetup->venue->country ) ) { 430 $country_code = $meetup->venue->country; 431 } elseif ( ! empty( $location_parts['country_code'] ) ) { 432 $country_code = $location_parts['country_code']; 433 } else { 434 $country_code = ''; 435 } 436 437 $events[] = new Official_WordPress_Event( array( 438 'type' => 'meetup', 439 'source_id' => $meetup->id, 440 'title' => $meetup->name, 441 'url' => $meetup->event_url, 442 'meetup_name' => $meetup->group->name, 443 'meetup_url' => sprintf( 'https://www.meetup.com/%s/', $meetup->group->urlname ), 444 'description' => $meetup->description, 445 'num_attendees' => $meetup->yes_rsvp_count, 446 'start_timestamp' => $start_timestamp, 447 'end_timestamp' => ( empty ( $meetup->duration ) ? $start_timestamp : $start_timestamp + ( $meetup->duration / 1000 ) ), // convert to seconds 448 'location' => $location, 449 'country_code' => $country_code, 450 'latitude' => empty( $meetup->venue->lat ) ? $meetup->group->group_lat : $meetup->venue->lat, 451 'longitude' => empty( $meetup->venue->lon ) ? $meetup->group->group_lon : $meetup->venue->lon, 452 ) ); 426 453 } 427 428 if ( ! empty( $meetup->venue->country ) ) { 429 $country_code = $meetup->venue->country; 430 } elseif ( ! empty( $location_parts['country_code'] ) ) { 431 $country_code = $location_parts['country_code']; 432 } else { 433 $country_code = ''; 434 } 435 436 $events[] = new Official_WordPress_Event( array( 437 'type' => 'meetup', 438 'source_id' => $meetup->id, 439 'title' => $meetup->name, 440 'url' => $meetup->event_url, 441 'meetup_name' => $meetup->group->name, 442 'meetup_url' => sprintf( 'https://www.meetup.com/%s/', $meetup->group->urlname ), 443 'description' => $meetup->description, 444 'num_attendees' => $meetup->yes_rsvp_count, 445 'start_timestamp' => $start_timestamp, 446 'end_timestamp' => ( empty ( $meetup->duration ) ? $start_timestamp : $start_timestamp + ( $meetup->duration / 1000 ) ), // convert to seconds 447 'location' => $location, 448 'country_code' => $country_code, 449 'latitude' => empty( $meetup->venue->lat ) ? $meetup->group->group_lat : $meetup->venue->lat, 450 'longitude' => empty( $meetup->venue->lon ) ? $meetup->group->group_lon : $meetup->venue->lon, 451 ) ); 452 } 453 } 454 455 $request_url = $body->meta->next; 456 } 454 } 455 456 $request_url = $body->meta->next; 457 } 457 458 } 458 459 … … 504 505 protected function reverse_geocode( $latitude, $longitude ) { 505 506 $address = false; 506 $response = $this->remote_get( sprintf( 'https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false', $latitude, $longitude ) ); 507 $response = $this->remote_get( sprintf( 508 'https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false', 509 $latitude, 510 $longitude 511 ) ); 507 512 508 513 if ( ! is_wp_error( $response ) ) { … … 534 539 if ( 'locality' == $component->types[0] ) { 535 540 $address['city'] = $component->short_name; 541 536 542 } elseif ( 'administrative_area_level_1' == $component->types[0] ) { 537 543 $address['state'] = $component->short_name; 544 538 545 } elseif ( 'country' == $component->types[0] ) { 539 546 $address['country_code'] = strtoupper( $component->short_name ); … … 582 589 583 590 if ( $url ) { 584 $args['timeout'] = 30; 585 $response = wp_remote_get( $url, $args ); 586 587 if ( is_wp_error( $response ) ) { 588 $error_messages = implode( ', ', $response->get_error_messages() ); 589 590 if ( false === strpos( $error_messages, 'Operation timed out' ) ) { 591 $error = sprintf( 592 'Received WP_Error message: %s; Request was to %s; Arguments were: %s', 593 $error_messages, 594 $url, 595 print_r( $args, true ) 596 ); 597 } 598 } elseif ( 200 != $response['response']['code'] ) { 599 // trigger_error() has a message limit of 1024 bytes, so we truncate $response['body'] to make sure that $body doesn't get truncated. 600 591 $args['timeout'] = 30; 592 $response = wp_remote_get( $url, $args ); 593 594 if ( is_wp_error( $response ) ) { 595 $error_messages = implode( ', ', $response->get_error_messages() ); 596 597 if ( false === strpos( $error_messages, 'Operation timed out' ) ) { 601 598 $error = sprintf( 602 'Received HTTP code: %s and body: %s. Request was to: %s; Arguments were: %s', 603 $response['response']['code'], 604 substr( sanitize_text_field( $response['body'] ), 0, 500 ), 599 'Received WP_Error message: %s; Request was to %s; Arguments were: %s', 600 $error_messages, 605 601 $url, 606 602 print_r( $args, true ) 607 603 ); 608 609 $response = new WP_Error( 'woe_invalid_http_response', 'Invalid HTTP response code', $response ); 610 } 611 612 if ( $error ) { 613 $error = preg_replace( '/&key=[a-z0-9]+/i', '&key=[redacted]', $error ); 614 trigger_error( sprintf( '%s error for %s: %s', __METHOD__, parse_url( site_url(), PHP_URL_HOST ), sanitize_text_field( $error ) ), E_USER_WARNING ); 615 616 if ( $to = apply_filters( 'owe_error_email_addresses', array() ) ) { 617 wp_mail( $to, sprintf( '%s error for %s', __METHOD__, parse_url( site_url(), PHP_URL_HOST ) ), sanitize_text_field( $error ) ); 618 } 619 } 620 621 $this->maybe_pause( wp_remote_retrieve_headers( $response ) ); 604 } 605 } elseif ( 200 != $response['response']['code'] ) { 606 // trigger_error() has a message limit of 1024 bytes, so we truncate $response['body'] to make sure that $body doesn't get truncated. 607 608 $error = sprintf( 609 'Received HTTP code: %s and body: %s. Request was to: %s; Arguments were: %s', 610 $response['response']['code'], 611 substr( sanitize_text_field( $response['body'] ), 0, 500 ), 612 $url, 613 print_r( $args, true ) 614 ); 615 616 $response = new WP_Error( 'woe_invalid_http_response', 'Invalid HTTP response code', $response ); 617 } 618 619 if ( $error ) { 620 $error = preg_replace( '/&key=[a-z0-9]+/i', '&key=[redacted]', $error ); 621 trigger_error( sprintf( '%s error for %s: %s', __METHOD__, parse_url( site_url(), PHP_URL_HOST ), sanitize_text_field( $error ) ), E_USER_WARNING ); 622 623 if ( $to = apply_filters( 'owe_error_email_addresses', array() ) ) { 624 wp_mail( $to, sprintf( '%s error for %s', __METHOD__, parse_url( site_url(), PHP_URL_HOST ) ), sanitize_text_field( $error ) ); 625 } 626 } 627 628 $this->maybe_pause( wp_remote_retrieve_headers( $response ) ); 622 629 } 623 630
Note: See TracChangeset
for help on using the changeset viewer.