WordPress.org

Making WordPress.org

Changeset 9107


Ignore:
Timestamp:
08/17/2019 12:35:04 AM (5 weeks ago)
Author:
coreymckrill
Message:

Official WordPress Events: Tweak Meetup client to try to avoid auth token probs

This attempts to avoid the scenario where the client has somehow stored an
oauth token that is expired/invalid, and keeps using it for requests anyway.
Now it will try to detect when the token it has isn't working and refresh it
without breaking the current request cycle.

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

Legend:

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

    r9100 r9107  
    3737     */
    3838    protected $breaking_response_codes = [];
     39
     40    /**
     41     * @var string|null The URL for the current request being attempted.
     42     */
     43    protected $current_request_url = null;
     44
     45    /**
     46     * @var array|null The args for the current request being attempted.
     47     */
     48    protected $current_request_args = null;
    3949
    4050    /**
     
    8696        }
    8797
     98        // Set current request in state so it can be manipulated between request attempts if necessary.
     99        $this->current_request_url  = $url;
     100        $this->current_request_args = $args;
     101
    88102        while ( $attempt_count < $max_attempts ) {
    89             $response      = wp_remote_request( $url, $args );
     103            $response      = wp_remote_request( $this->current_request_url, $this->current_request_args );
    90104            $response_code = wp_remote_retrieve_response_code( $response );
    91105
     
    118132             * @param int   $max_attempts
    119133             */
    120             do_action( 'api_client_tenacious_remote_request_attempt', $response, compact( 'url', 'args' ), $attempt_count, $max_attempts );
     134            do_action(
     135                'api_client_tenacious_remote_request_attempt',
     136                $response,
     137                array(
     138                    'url'  => $this->current_request_url,
     139                    'args' => $this->current_request_args,
     140                ),
     141                $attempt_count,
     142                $max_attempts
     143            );
    121144
    122145            if ( $attempt_count < $max_attempts ) {
     
    129152            }
    130153        }
     154
     155        // Reset current request.
     156        $this->current_request_url  = null;
     157        $this->current_request_args = null;
    131158
    132159        if ( $attempt_count === $max_attempts && ( 200 !== $response_code || is_wp_error( $response ) ) ) {
     
    225252
    226253    /**
     254     * Merge two error objects into one, new error object.
     255     *
     256     * @param WP_Error $error1 An error object.
     257     * @param WP_Error $error2 An error object.
     258     *
     259     * @return WP_Error The combined errors of the two parameters.
     260     */
     261    protected function merge_errors( WP_Error $error1, WP_Error $error2 ) {
     262        $codes = $error2->get_error_codes();
     263
     264        foreach ( $codes as $code ) {
     265            $messages = $error2->get_error_messages( $code );
     266
     267            foreach ( $messages as $message ) {
     268                $error1->add( $code, $message );
     269            }
     270        }
     271
     272        return $error1;
     273    }
     274
     275    /**
    227276     * Outputs a message when the command is run from the PHP command line.
    228277     *
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-client.php

    r9105 r9107  
    7373        );
    7474
     75        $this->debug = $settings['debug'];
     76
     77        if ( $this->debug ) {
     78            self::cli_message( "Meetup Client debug is on. Results will be truncated." );
     79        }
     80
    7581        $this->oauth_client = new Meetup_OAuth2_Client;
    76         $this->debug        = $settings['debug'];
    77 
    78         if ( $this->debug ) {
    79             self::cli_message( "Meetup Client debug is ON. Results will be truncated." );
     82
     83        if ( ! empty( $this->oauth_client->error->get_error_messages() ) ) {
     84            $this->error = $this->merge_errors( $this->error, $this->oauth_client->error );
     85        }
     86
     87        add_action( 'api_client_tenacious_remote_request_attempt', array( $this, 'maybe_reset_oauth_token' ) );
     88    }
     89
     90    /**
     91     * Attempt to fix authorization errors before they permanently fail.
     92     *
     93     * Hooked to `api_client_tenacious_remote_request_attempt` so that a request that has failed due to an invalid
     94     * oauth token can be retried after resetting the token.
     95     *
     96     * @param array $response
     97     *
     98     * @return void
     99     */
     100    public function maybe_reset_oauth_token( $response ) {
     101        $code = wp_remote_retrieve_response_code( $response );
     102        $body = json_decode( wp_remote_retrieve_body( $response ), true );
     103
     104        if ( 400 === $code && ! empty( $body['error'] ) && 'invalid_grant' === $body['error'] ) {
     105            $this->oauth_client->reset_oauth_token();
     106
     107            if ( ! empty( $this->oauth_client->error->get_error_messages() ) ) {
     108                $this->error = $this->merge_errors( $this->error, $this->oauth_client->error );
     109            }
     110
     111            // Reset the request headers, so that they include the new oauth token.
     112            $this->current_request_args = $this->get_request_args();
    80113        }
    81114    }
     
    123156                $request_url = $this->get_next_url( $response );
    124157            } else {
    125                 $this->handle_error_response( $response );
     158                $this->handle_error_response( $response, $request_url );
    126159                break;
    127160            }
     
    168201            }
    169202        } else {
    170             $this->handle_error_response( $response );
     203            $this->handle_error_response( $response, $request_url );
    171204        }
    172205
     
    295328                );
    296329            }
     330        } elseif ( isset( $data['error'], $data['error_description'] ) ) {
     331            $this->error->add(
     332                $data['error'],
     333                $data['error_description']
     334            );
    297335        } elseif ( isset( $data['code'] ) && isset( $data['details'] ) ) {
    298336            $this->error->add(
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-oauth2-client.php

    r9105 r9107  
    9797            ),
    9898        ) );
     99
     100        // Pre-cache the oauth token.
     101        $this->get_oauth_token();
    99102    }
    100103
     
    308311
    309312        return $oauth_token;
     313    }
     314
     315    /**
     316     * Un-cache any existing oauth token and request a new one.
     317     *
     318     * This also resets the error property so that it has a clean slate when it attempts to request a new token.
     319     *
     320     * Useful if a token stops working during a batch of requests :/
     321     *
     322     * @return void
     323     */
     324    public function reset_oauth_token() {
     325        delete_site_option( self::SITE_OPTION_KEY_OAUTH );
     326
     327        $this->oauth_token = array();
     328        $this->error       = new WP_Error();
     329
     330        $this->get_oauth_token();
    310331    }
    311332
Note: See TracChangeset for help on using the changeset viewer.