Making WordPress.org

Changeset 7751


Ignore:
Timestamp:
10/19/2018 09:04:34 PM (8 years ago)
Author:
iandunn
Message:

Official WordPress Events: Use Meetup Client to fetch events.

The client is a more robust iteration on the previous code, and should avoid the HTTP 400 status errors that have been occurring recently, by retrying the requests.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events
Files:
2 edited

Legend:

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

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

    r7749 r7751  
    77Author:      WordPress.org Meta Team
    88*/
     9
     10use WordCamp\Utilities\Meetup_Client;
    911
    1012class Official_WordPress_Events {
     
    318320                $events = array();
    319321
    320                 if ( ! defined( 'MEETUP_API_KEY' ) || ! MEETUP_API_KEY || ! $groups = $this->get_meetup_group_ids() ) {
     322                require_once( __DIR__ . '/class-meetup-client.php' );
     323
     324                $client = new Meetup_Client();
     325                if ( ! empty( $client->error->errors ) ) {
     326                        $this->log( 'Failed to instantiate meetup client: ' . wp_json_encode( $client->error ), true );
    321327                        return $events;
    322328                }
    323329
    324                 // Meetup API sometimes throws an error with chunk size larger than 50.
    325                 $groups = array_chunk( $groups, 50, true );
    326 
    327                 foreach ( $groups as $group_batch ) {
    328                         $request_url = add_query_arg(
    329                                 array(
    330                                         'group_id' => implode( ',', $group_batch ),
    331                                         'time'     => '0,3m',
    332                                         'page'     => 200,
    333                                         'status'   => 'upcoming,cancelled',
    334                                 ),
    335                                 self::MEETUP_API_BASE_URL . '2/events'
    336                         );
    337 
    338                         while ( ! empty( $request_url ) ) {
    339                                 // The "next" URLs returned by the API need to be re-signed.
    340                                 $request_url = add_query_arg(
    341                                         array(
    342                                                 'sign' => true,
    343                                                 'key'  => MEETUP_API_KEY,
    344                                         ),
    345                                         $request_url
    346                                 );
    347 
    348                                 $this->log( 'fetching more events from: ' . var_export( $request_url, true ) );
    349 
    350                                 $response = $this->remote_get( $request_url );
    351                                 $body     = json_decode( wp_remote_retrieve_body( $response ) );
    352 
    353                                 $this->log( 'pruned response - ' . print_r( $this->prune_response_for_log( $response ), true ) );
    354 
    355                                 if ( ! empty ( $body->results ) ) {
    356                                         $meetups = $body->results;
     330                $groups = $client->get_groups();
     331                if ( ! empty( $client->error->errors ) ) {
     332                        $this->log( 'Failed to fetch groups: ' . wp_json_encode( $client->error ), true );
     333                        return $events;
     334                }
     335
     336                $meetups = $client->get_events( wp_list_pluck( $groups, 'id' ) );
     337                if ( ! empty( $client->error->errors ) ) {
     338                        $this->log( 'Failed to fetch meetups: ' . wp_json_encode( $client->error ), true );
     339                        return $events;
     340                }
    357341
    358342                                        foreach ( $meetups as $meetup ) {
    359                                                 $start_timestamp = ( $meetup->time / 1000 ) + ( $meetup->utc_offset / 1000 );    // convert to seconds
    360 
    361                                                 if ( isset( $meetup->venue ) ) {
    362                                                         $location = $this->format_meetup_venue_location( $meetup->venue );
     343                                                if ( empty( $meetup['id'] ) || empty( $meetup['name'] ) ) {
     344                                                        $this->log( 'Malformed meetup: ' . wp_json_encode( $meetup ) );
     345                                                        continue;
     346                                                }
     347
     348                                                $start_timestamp = ( $meetup['time'] / 1000 ) + ( $meetup['utc_offset'] / 1000 );    // convert to seconds
     349
     350                                                if ( isset( $meetup['venue'] ) ) {
     351                                                        $location = $this->format_meetup_venue_location( $meetup['venue'] );
    363352                                                } else {
    364                                                         $geocoded_location = $this->reverse_geocode( $meetup->group->group_lat, $meetup->group->group_lon );
     353                                                        $geocoded_location = $this->reverse_geocode( $meetup['group']['group_lat'], $meetup['group']['group_lon'] );
    365354                                                        $location_parts    = $this->parse_reverse_geocode_address( $geocoded_location );
    366355                                                        $location          = sprintf(
    367356                                                                '%s%s%s',
    368                                                                 $location_parts['city'],
     357                                                                $location_parts['city'] ?? '',
    369358                                                                empty( $location_parts['state'] )        ? '' : ', ' . $location_parts['state'],
    370359                                                                empty( $location_parts['country_name'] ) ? '' : ', ' . $location_parts['country_name']
     
    373362                                                }
    374363
    375                                                 if ( ! empty( $meetup->venue->country ) ) {
    376                                                         $country_code = $meetup->venue->country;
     364                                                if ( ! empty( $meetup['venue']['country'] ) ) {
     365                                                        $country_code = $meetup['venue']['country'];
    377366                                                } elseif ( ! empty( $location_parts['country_code'] ) ) {
    378367                                                        $country_code = $location_parts['country_code'];
     
    383372                                                $events[] = new Official_WordPress_Event( array(
    384373                                                        'type'            => 'meetup',
    385                                                         'source_id'       => $meetup->id,
    386                                                         'status'          => 'upcoming' === $meetup->status ? 'scheduled' : 'cancelled',
    387                                                         'title'           => $meetup->name,
    388                                                         'url'             => $meetup->event_url,
    389                                                         'meetup_name'     => $meetup->group->name,
    390                                                         'meetup_url'      => sprintf( 'https://www.meetup.com/%s/', $meetup->group->urlname ),
    391                                                         'description'     => $meetup->description,
    392                                                         'num_attendees'   => $meetup->yes_rsvp_count,
     374                                                        'source_id'       => $meetup['id'],
     375                                                        'status'          => 'upcoming' === $meetup['status'] ? 'scheduled' : 'cancelled',
     376                                                        'title'           => $meetup['name'],
     377                                                        'url'             => $meetup['event_url'],
     378                                                        'meetup_name'     => $meetup['group']['name'],
     379                                                        'meetup_url'      => sprintf( 'https://www.meetup.com/%s/', $meetup['group']['urlname'] ),
     380                                                        'description'     => $meetup['description'] ?? '',
     381                                                        'num_attendees'   => $meetup['yes_rsvp_count'],
    393382                                                        'start_timestamp' => $start_timestamp,
    394                                                         'end_timestamp'   => ( empty ( $meetup->duration ) ? $start_timestamp : $start_timestamp + ( $meetup->duration / 1000 ) ), // convert to seconds
     383                                                        'end_timestamp'   => ( empty ( $meetup['duration'] ) ? $start_timestamp : $start_timestamp + ( $meetup['duration'] / 1000 ) ), // convert to seconds
    395384                                                        'location'        => $location,
    396385                                                        'country_code'    => $country_code,
    397                                                         'latitude'        => empty( $meetup->venue->lat ) ? $meetup->group->group_lat : $meetup->venue->lat,
    398                                                         'longitude'       => empty( $meetup->venue->lon ) ? $meetup->group->group_lon : $meetup->venue->lon,
     386                                                        'latitude'        => empty( $meetup['venue']['lat'] ) ? $meetup['group']['group_lat'] : $meetup['venue']['lat'],
     387                                                        'longitude'       => empty( $meetup['venue']['lon'] ) ? $meetup['group']['group_lon'] : $meetup['venue']['lon'],
    399388                                                ) );
    400389                                        }
    401                                 }
    402 
    403                                 $request_url = isset( $body->meta->next ) ? esc_url_raw( $body->meta->next ) : null;
    404                         }
    405                 }
    406390
    407391                $this->log( sprintf( 'returning %d events', count( $events ) ) );
    408392
    409393                return $events;
    410         }
    411 
    412         /*
    413          * Gets the IDs of all of the meetup groups associated
    414          *
    415          * @return array
    416          */
    417         protected function get_meetup_group_ids() {
    418                 $group_ids = array();
    419 
    420                 if ( ! defined( 'MEETUP_API_KEY' ) || ! MEETUP_API_KEY ) {
    421                         return $group_ids;
    422                 }
    423 
    424                 $request_url = sprintf(
    425                         '%s2/profiles?&member_id=%d&key=%s',
    426                         self::MEETUP_API_BASE_URL,
    427                         self::MEETUP_MEMBER_ID,
    428                         MEETUP_API_KEY
    429                 );
    430 
    431                 while ( ! empty( $request_url ) ) {
    432                         $this->log( 'fetching more groups from: ' . var_export( $request_url, true ) );
    433 
    434                         $response = $this->remote_get( $request_url );
    435                         $body     = json_decode( wp_remote_retrieve_body( $response ) );
    436 
    437                         $this->log( 'pruned response - ' . print_r( $this->prune_response_for_log( $response ), true ) );
    438 
    439                         if ( ! empty ( $body->results ) ) {
    440                                 foreach ( $body->results as $profile ) {
    441                                         if ( ! isset( $profile->group->id, $profile->role ) || 'Organizer' !== $profile->role ) {
    442                                                 continue;
    443                                         }
    444 
    445                                         $group_ids[] = $profile->group->id;
    446                                 }
    447                         }
    448 
    449                         $request_url = isset( $body->meta->next ) ? $body->meta->next : null;
    450                 }
    451 
    452                 $this->log( sprintf( 'returning %d groups', count( $group_ids ) ) );
    453 
    454                 return $group_ids;
    455394        }
    456395
     
    539478
    540479                foreach ( array( 'city', 'state', 'localized_country_name' ) as $part ) {
    541                         if ( ! empty( $venue->$part ) ) {
     480                        if ( ! empty( $venue[ $part ] ) ) {
    542481                                if ( in_array( $part, array( 'state' ) ) ) {
    543                                         $location[] = strtoupper( $venue->$part );
     482                                        $location[] = strtoupper( $venue[ $part ] );
    544483                                } else {
    545                                         $location[] = $venue->$part;
     484                                        $location[] = $venue[ $part ];
    546485                                }
    547486                        }
     
    734673         *
    735674         * @param string $message
    736          */
    737         protected function log( $message ) {
     675         * @param bool   $write_to_disk If true, writes the message to the standard error log in addition to the
     676         *                              `owpe_log` option.
     677         */
     678        protected function log( $message, $write_to_disk = false ) {
    738679                $limit = 500;
     680                $api_keys = array( MEETUP_API_KEY, OFFICIAL_WP_EVENTS_GOOGLE_MAPS_API_KEY );
     681
     682                if ( $write_to_disk ) {
     683                        error_log( sprintf(
     684                                /*
     685                                 * Use the folder name as a prefix so that Slack searches/highlights will match this and log
     686                                 * entries generated by PHP itself.
     687                                 */
     688                                'official-wordpress-events: %s',
     689                                str_replace( $api_keys, '[redacted]', $message )
     690                        ) );
     691                }
    739692
    740693                if ( ! isset( $this->log ) ) {
Note: See TracChangeset for help on using the changeset viewer.