| @@ -5,10 +5,8 @@ | ||
| 5 | 5 | * @package Timetics |
| 6 | 6 | */ |
| 7 | 7 | namespace Timetics\Core\Integrations\Google; |
| 8 | 8 | |
| 9 | -defined( 'ABSPATH' ) || exit; | |
| 10 | - | |
| 11 | 9 | use Exception; |
| 12 | 10 | use InvalidArgumentException; |
| 13 | 11 | |
| 14 | 12 | /** |
| @@ -88,23 +86,21 @@ | ||
| 88 | 86 | * Get auth url |
| 89 | 87 | * |
| 90 | 88 | * @return string |
| 91 | 89 | */ |
| 92 | - public function get_auth_url( $state = '' ) { | |
| 93 | - $params = array( | |
| 94 | - 'client_id' => $this->client_id, | |
| 95 | - 'scope' => urlencode_deep( $this->auth_scope ), | |
| 96 | - 'redirect_uri' => $this->redirect_uri, | |
| 97 | - 'response_type' => 'code', | |
| 98 | - 'access_type' => 'offline', | |
| 99 | - 'prompt' => 'consent', | |
| 90 | + public function get_auth_url() { | |
| 91 | + $auth_url = add_query_arg( | |
| 92 | + array( | |
| 93 | + 'client_id' => $this->client_id, | |
| 94 | + 'scope' => urlencode_deep( $this->auth_scope ), | |
| 95 | + 'redirect_uri' => $this->redirect_uri, | |
| 96 | + 'response_type' => 'code', | |
| 97 | + 'access_type' => 'offline', | |
| 98 | + | |
| 99 | + ), self::TIMETICS_AUTH_URL | |
| 100 | 100 | ); |
| 101 | 101 | |
| 102 | - if ( ! empty( $state ) ) { | |
| 103 | - $params['state'] = $state; | |
| 104 | - } | |
| 105 | - | |
| 106 | - return add_query_arg( $params, self::TIMETICS_AUTH_URL ); | |
| 102 | + return $auth_url; | |
| 107 | 103 | } |
| 108 | 104 | |
| 109 | 105 | /** |
| 110 | 106 | * Set google authentication configuration |
| @@ -147,11 +143,9 @@ | ||
| 147 | 143 | * Fetch access token |
| 148 | 144 | * |
| 149 | 145 | * @param string $code |
| 150 | 146 | * |
| 151 | - * @return array Token data on success. | |
| 152 | - * @throws \InvalidArgumentException When the code is empty. | |
| 153 | - * @throws \Exception On transport failure or a non-200 token response. | |
| 147 | + * @return void | |
| 154 | 148 | */ |
| 155 | 149 | public function fetch_access_token_with_auth_code( $code ) { |
| 156 | 150 | if ( strlen( $code ) === 0 ) { |
| 157 | 151 | throw new InvalidArgumentException( 'Invalid code' ); |
| @@ -166,21 +160,16 @@ | ||
| 166 | 160 | ); |
| 167 | 161 | |
| 168 | 162 | $response = wp_remote_post( self::TIMETICS_TOKEN_URI, array( 'body' => $args ) ); |
| 169 | 163 | |
| 170 | - if ( is_wp_error( $response ) ) { | |
| 171 | - throw new Exception( esc_html( $response->get_error_message() ) ); | |
| 164 | + $status_code = wp_remote_retrieve_response_code( $response ); | |
| 165 | + | |
| 166 | + if ( 200 != $status_code ) { | |
| 167 | + return false; | |
| 172 | 168 | } |
| 173 | 169 | |
| 174 | - $status_code = (int) wp_remote_retrieve_response_code( $response ); | |
| 175 | - $body = wp_remote_retrieve_body( $response ); | |
| 176 | - $data = json_decode( $body, true ); | |
| 170 | + $data = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 177 | 171 | |
| 178 | - if ( 200 !== $status_code || empty( $data['access_token'] ) ) { | |
| 179 | - $message = ! empty( $data['error_description'] ) ? $data['error_description'] : __( 'Failed to obtain Google access token.', 'timetics' ); | |
| 180 | - throw new Exception( esc_html( $message ) ); | |
| 181 | - } | |
| 182 | - | |
| 183 | 172 | return $data; |
| 184 | 173 | } |
| 185 | 174 | |
| 186 | 175 | /** |
| @@ -185,21 +174,11 @@ | ||
| 185 | 174 | |
| 186 | 175 | /** |
| 187 | 176 | * Fetch access token by using refresh token |
| 188 | 177 | * |
| 189 | - * @param string $refresh_token | |
| 190 | - * | |
| 191 | - * @return array|\WP_Error Token data on success, or a WP_Error whose error_data carries a boolean `transient` flag. | |
| 178 | + * @return void | |
| 192 | 179 | */ |
| 193 | 180 | public function fetch_access_token_with_refresh_token( $refresh_token ) { |
| 194 | - if ( empty( $refresh_token ) ) { | |
| 195 | - return new \WP_Error( | |
| 196 | - 'timetics_google_no_refresh_token', | |
| 197 | - 'No Google refresh token is available for this account.', | |
| 198 | - array( 'transient' => false ) | |
| 199 | - ); | |
| 200 | - } | |
| 201 | - | |
| 202 | 181 | $args = array( |
| 203 | 182 | 'client_id' => $this->client_id, |
| 204 | 183 | 'client_secret' => $this->client_secrete, |
| 205 | 184 | 'refresh_token' => $refresh_token, |
| @@ -208,47 +187,17 @@ | ||
| 208 | 187 | ); |
| 209 | 188 | |
| 210 | 189 | $response = wp_remote_post( self::TIMETICS_TOKEN_URI, array( 'body' => $args ) ); |
| 211 | 190 | |
| 212 | - // Transport-level failure (DNS, timeout, connection refused). Transient: | |
| 213 | - // the credential is fine, Google was just briefly unreachable. | |
| 214 | - if ( is_wp_error( $response ) ) { | |
| 215 | - return new \WP_Error( | |
| 216 | - 'timetics_google_refresh_http_error', | |
| 217 | - $response->get_error_message(), | |
| 218 | - array( 'transient' => true ) | |
| 219 | - ); | |
| 220 | - } | |
| 191 | + $status_code = wp_remote_retrieve_response_code( $response ); | |
| 221 | 192 | |
| 222 | - $status_code = (int) wp_remote_retrieve_response_code( $response ); | |
| 223 | - $body = wp_remote_retrieve_body( $response ); | |
| 224 | - $data = json_decode( $body, true ); | |
| 225 | - | |
| 226 | - if ( 200 === $status_code && ! empty( $data['access_token'] ) ) { | |
| 227 | - return $data; | |
| 193 | + if ( 200 != $status_code ) { | |
| 194 | + return false; | |
| 228 | 195 | } |
| 229 | 196 | |
| 230 | - $google_error = isset( $data['error'] ) ? $data['error'] : ''; | |
| 231 | - $transient = ( 'invalid_grant' !== $google_error ); | |
| 197 | + $data = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 232 | 198 | |
| 233 | - if ( 429 === $status_code || $status_code >= 500 || 0 === $status_code ) { | |
| 234 | - $transient = true; | |
| 235 | - } | |
| 236 | - | |
| 237 | - if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 238 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Debug logging is guarded by WP_DEBUG checks. | |
| 239 | - error_log( sprintf( 'Timetics Google token refresh failed (HTTP %d): %s', $status_code, $body ) ); | |
| 240 | - } | |
| 241 | - | |
| 242 | - return new \WP_Error( | |
| 243 | - 'timetics_google_refresh_failed', | |
| 244 | - ! empty( $data['error_description'] ) ? $data['error_description'] : 'Failed to refresh Google access token.', | |
| 245 | - [ | |
| 246 | - 'transient' => $transient, | |
| 247 | - 'status_code' => $status_code, | |
| 248 | - 'error' => $google_error, | |
| 249 | - ] | |
| 250 | - ); | |
| 199 | + return $data; | |
| 251 | 200 | } |
| 252 | 201 | |
| 253 | 202 | /** |
| 254 | 203 | * Revoke authorization |