Changeset 9107
- Timestamp:
- 08/17/2019 12:35:04 AM (6 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup
- Files:
-
- 3 edited
-
class-api-client.php (modified) (5 diffs)
-
class-meetup-client.php (modified) (4 diffs)
-
class-meetup-oauth2-client.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-api-client.php
r9100 r9107 37 37 */ 38 38 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; 39 49 40 50 /** … … 86 96 } 87 97 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 88 102 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 ); 90 104 $response_code = wp_remote_retrieve_response_code( $response ); 91 105 … … 118 132 * @param int $max_attempts 119 133 */ 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 ); 121 144 122 145 if ( $attempt_count < $max_attempts ) { … … 129 152 } 130 153 } 154 155 // Reset current request. 156 $this->current_request_url = null; 157 $this->current_request_args = null; 131 158 132 159 if ( $attempt_count === $max_attempts && ( 200 !== $response_code || is_wp_error( $response ) ) ) { … … 225 252 226 253 /** 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 /** 227 276 * Outputs a message when the command is run from the PHP command line. 228 277 * -
sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-client.php
r9105 r9107 73 73 ); 74 74 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 75 81 $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(); 80 113 } 81 114 } … … 123 156 $request_url = $this->get_next_url( $response ); 124 157 } else { 125 $this->handle_error_response( $response );158 $this->handle_error_response( $response, $request_url ); 126 159 break; 127 160 } … … 168 201 } 169 202 } else { 170 $this->handle_error_response( $response );203 $this->handle_error_response( $response, $request_url ); 171 204 } 172 205 … … 295 328 ); 296 329 } 330 } elseif ( isset( $data['error'], $data['error_description'] ) ) { 331 $this->error->add( 332 $data['error'], 333 $data['error_description'] 334 ); 297 335 } elseif ( isset( $data['code'] ) && isset( $data['details'] ) ) { 298 336 $this->error->add( -
sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-oauth2-client.php
r9105 r9107 97 97 ), 98 98 ) ); 99 100 // Pre-cache the oauth token. 101 $this->get_oauth_token(); 99 102 } 100 103 … … 308 311 309 312 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(); 310 331 } 311 332
Note: See TracChangeset
for help on using the changeset viewer.