WordPress.org

Making WordPress.org


Ignore:
Timestamp:
08/17/2019 12:35:04 AM (8 months 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.

File:
1 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     *
Note: See TracChangeset for help on using the changeset viewer.