| @@ -31,9 +31,9 @@ | ||
| 31 | 31 | /** |
| 32 | 32 | * Setting the development mode. |
| 33 | 33 | */ |
| 34 | 34 | public function __construct() { |
| 35 | - $this->dev_mode = defined( 'TEMPLATELY_DEV' ) && TEMPLATELY_DEV; | |
| 35 | + $this->dev_mode = Helper::is_dev_api(); | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * Determining the endpoint URL based on the mode. |
| @@ -40,27 +40,57 @@ | ||
| 40 | 40 | * |
| 41 | 41 | * @return string |
| 42 | 42 | */ |
| 43 | 43 | public function url() { |
| 44 | - if ( $this->dev_mode ) { | |
| 44 | + if ( Helper::is_dev_api() ) { | |
| 45 | 45 | $this->url = 'https://app.templately.dev/api/plugin'; |
| 46 | 46 | } |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Filter the API endpoint URL | |
| 50 | + * | |
| 51 | + * @since 3.5.0 | |
| 52 | + * @param string $url The endpoint URL | |
| 53 | + */ | |
| 54 | + $this->url = apply_filters('templately_dev_api_endpoint_url', $this->url); | |
| 55 | + | |
| 47 | 56 | return $this->url; |
| 48 | 57 | } |
| 49 | 58 | |
| 50 | 59 | /** |
| 51 | - * Preparing query arguments. | |
| 52 | - * Unknown. | |
| 53 | - * | |
| 54 | - * @return string | |
| 55 | - */ | |
| 56 | - public static function prepare( $query, ...$args ) { | |
| 57 | - return sprintf( $query, ...$args ); | |
| 60 | + * Generate Google OAuth authentication URL | |
| 61 | + * | |
| 62 | + * @param string $redirect_to Optional redirect path after authentication | |
| 63 | + * @return string The Google auth URL with query parameters | |
| 64 | + */ | |
| 65 | +public function google_auth_url($redirect_to = '') { | |
| 66 | + $base_url = $this->url(); | |
| 67 | + // Replace /api/plugin with /api/auth/plugin/google | |
| 68 | + $auth_url = str_replace('/api/plugin', '/api/auth/plugin/google', $base_url); | |
| 69 | + | |
| 70 | + // Build the site_url with admin-ajax path | |
| 71 | + $admin_url = admin_url('admin-ajax.php'); | |
| 72 | + $admin_params = [ | |
| 73 | + 'action' => 'templately_google_login', | |
| 74 | + ]; | |
| 75 | + | |
| 76 | + // Add redirect-to parameter if provided | |
| 77 | + if (!empty($redirect_to)) { | |
| 78 | + $admin_params['redirect-to'] = $redirect_to; | |
| 58 | 79 | } |
| 59 | 80 | |
| 60 | - /** | |
| 61 | - * Preparing query arguments | |
| 62 | - * | |
| 81 | + $site_url_with_params = add_query_arg($admin_params, $admin_url); | |
| 82 | + | |
| 83 | + $query_params = [ | |
| 84 | + 'site_url' => urlencode($site_url_with_params), | |
| 85 | + 'site_ip' => Helper::get_ip(), | |
| 86 | + 'state' => wp_generate_password(32, false) // Add unique random state for cache busting | |
| 87 | + ]; | |
| 88 | + | |
| 89 | + return add_query_arg($query_params, $auth_url); | |
| 90 | +} | |
| 91 | + | |
| 92 | +/** | |
| 63 | 93 | * @param array $args |
| 64 | 94 | * @return string |
| 65 | 95 | */ |
| 66 | 96 | protected function prepareArgs( $args ) { |
| @@ -138,11 +168,12 @@ | ||
| 138 | 168 | $query = $this->query; |
| 139 | 169 | } |
| 140 | 170 | |
| 141 | 171 | $headers = [ |
| 142 | - 'Content-Type' => 'application/json', | |
| 143 | - 'x-templately-ip' => Helper::get_ip(), | |
| 144 | - 'x-templately-url' => home_url( '/' ) | |
| 172 | + 'Content-Type' => 'application/json', | |
| 173 | + 'x-templately-ip' => Helper::get_ip(), | |
| 174 | + 'x-templately-url' => home_url( '/' ), | |
| 175 | + 'x-templately-version' => TEMPLATELY_VERSION, | |
| 145 | 176 | ]; |
| 146 | 177 | |
| 147 | 178 | if ( ! empty( $args['headers'] ) ) { |
| 148 | 179 | $headers = wp_parse_args( $args['headers'], $headers ); |
| @@ -171,11 +202,11 @@ | ||
| 171 | 202 | } while ( is_wp_error( $response ) && $retryCount < $maxRetries ); |
| 172 | 203 | |
| 173 | 204 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 174 | 205 | Helper::log( 'Retry Count: ' . $retryCount ); |
| 175 | - Helper::log( 'RAW RESPONSE: ' ); | |
| 176 | - Helper::log( $response ); | |
| 177 | - Helper::log( 'END RAW RESPONSE' ); | |
| 206 | + // Helper::log( 'RAW RESPONSE: ' ); | |
| 207 | + // Helper::log( $response ); | |
| 208 | + // Helper::log( 'END RAW RESPONSE' ); | |
| 178 | 209 | } |
| 179 | 210 | |
| 180 | 211 | return $this->maybeErrors( $response, $args ); |
| 181 | 212 | } |
| @@ -180,9 +211,9 @@ | ||
| 180 | 211 | return $this->maybeErrors( $response, $args ); |
| 181 | 212 | } |
| 182 | 213 | |
| 183 | 214 | /** |
| 184 | - * Formating the self::post() response | |
| 215 | + * Formatting the self::post() response | |
| 185 | 216 | * |
| 186 | 217 | * @param mixed $response |
| 187 | 218 | * @param array $args |
| 188 | 219 | * @return mixed |
| @@ -191,13 +222,17 @@ | ||
| 191 | 222 | if ( $response instanceof WP_Error ) { |
| 192 | 223 | return $response; // Return WP_Error, if it is an error. |
| 193 | 224 | } |
| 194 | 225 | |
| 226 | + // Check for verification header before processing response body | |
| 227 | + Helper::check_verification_header( $response ); | |
| 228 | + Helper::check_site_disconnection( $response ); | |
| 229 | + | |
| 195 | 230 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 196 | 231 | $response_message = wp_remote_retrieve_response_message( $response ); |
| 197 | 232 | |
| 198 | 233 | /** |
| 199 | - * Retrive Data from Response Body. | |
| 234 | + * Retrieve Data from Response Body. | |
| 200 | 235 | */ |
| 201 | 236 | $response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 202 | 237 | /** |
| 203 | 238 | * If the graphql hit with any error. |
| @@ -220,12 +255,16 @@ | ||
| 220 | 255 | } ); |
| 221 | 256 | } |
| 222 | 257 | } ); |
| 223 | 258 | } else { |
| 259 | + $error_data = []; | |
| 260 | + if(!empty($error["extensions"]["statusText"])) { | |
| 261 | + $error_data["statusText"] = $error["extensions"]["statusText"]; | |
| 262 | + } | |
| 224 | 263 | if ( isset( $error['debugMessage'] ) ) { |
| 225 | 264 | $wp_error->add( 'templately_graphql_error', $error['debugMessage'] ); |
| 226 | 265 | } else { |
| 227 | - $wp_error->add( 'templately_graphql_error', $error['message'] ); | |
| 266 | + $wp_error->add( 'templately_graphql_error', $error['message'], $error_data ); | |
| 228 | 267 | } |
| 229 | 268 | } |
| 230 | 269 | } |
| 231 | 270 | } ); |
| @@ -243,11 +282,31 @@ | ||
| 243 | 282 | } |
| 244 | 283 | |
| 245 | 284 | return $wp_error; |
| 246 | 285 | } |
| 286 | + } elseif ( ! empty( $response['status'] ) && $response['status'] === 'error' ) { | |
| 287 | + | |
| 288 | + if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 289 | + Helper::log( 'ERROR: ' ); | |
| 290 | + Helper::log( $response ); | |
| 291 | + Helper::log( 'END ERROR' ); | |
| 292 | + } | |
| 293 | + | |
| 294 | + $error_data = []; | |
| 295 | + if ( ! empty( $response['statusText'] ) ) { | |
| 296 | + $error_data['statusText'] = $response['statusText']; | |
| 297 | + } | |
| 298 | + | |
| 299 | + $error_message = ! empty( $response['message'] ) ? $response['message'] : __( 'Unknown error occurred', 'templately' ); | |
| 300 | + | |
| 301 | + return new WP_Error( 'templately_api_error', $error_message, $error_data ); | |
| 247 | 302 | } |
| 248 | 303 | |
| 249 | 304 | $_response = isset( $response['data'][$this->endpoint] ) ? $response['data'][$this->endpoint] : []; |
| 305 | + // {"data":{"connectWithApiKey":{"status":"error","message":"Invalid API key.","user":null}}} | |
| 306 | + if ( ! empty( $response['status'] ) && $response['status'] === 'error' ) { | |
| 307 | + | |
| 308 | + } | |
| 250 | 309 | |
| 251 | 310 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 252 | 311 | Helper::log( 'RESPONSE: ' ); |
| 253 | 312 | Helper::log( $_response ); |