| @@ -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,68 @@ | ||
| 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 = '', $current_url = '') { | |
| 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 | + // Get the referer to return to the exact same page we initiated login from securely | |
| 71 | + if ( ! empty( $current_url ) ) { | |
| 72 | + $referer = esc_url_raw( $current_url ); | |
| 73 | + } else { | |
| 74 | + $referer = isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; | |
| 58 | 75 | } |
| 59 | 76 | |
| 60 | - /** | |
| 61 | - * Preparing query arguments | |
| 62 | - * | |
| 77 | + $return_url = wp_validate_redirect( $referer, '' ); | |
| 78 | + | |
| 79 | + if ( empty( $return_url ) ) { | |
| 80 | + $return_url = admin_url( 'admin.php?page=templately' ); | |
| 81 | + } | |
| 82 | + | |
| 83 | + $return_params = [ | |
| 84 | + 'templately_google_login' => '1', | |
| 85 | + ]; | |
| 86 | + | |
| 87 | + // Add redirect-to parameter if provided | |
| 88 | + if (!empty($redirect_to)) { | |
| 89 | + $return_params['redirect-to'] = $redirect_to; | |
| 90 | + } | |
| 91 | + | |
| 92 | + $site_url_with_params = add_query_arg($return_params, $return_url); | |
| 93 | + | |
| 94 | + $query_params = [ | |
| 95 | + 'site_url' => urlencode($site_url_with_params), | |
| 96 | + 'site_ip' => Helper::get_ip(), | |
| 97 | + 'state' => wp_generate_password(32, false) // Add unique random state for cache busting | |
| 98 | + ]; | |
| 99 | + | |
| 100 | + return add_query_arg($query_params, $auth_url); | |
| 101 | +} | |
| 102 | + | |
| 103 | +/** | |
| 63 | 104 | * @param array $args |
| 64 | 105 | * @return string |
| 65 | 106 | */ |
| 66 | 107 | protected function prepareArgs( $args ) { |
| @@ -72,9 +113,9 @@ | ||
| 72 | 113 | case is_string( $value ) && ( $value === 'true' || $value === 'false' ): |
| 73 | 114 | $prepareArgs .= "$key:" . $value . ","; |
| 74 | 115 | break; |
| 75 | 116 | default: |
| 76 | - $prepareArgs .= "$key:" . '"' . $value . '"' . ","; | |
| 117 | + $prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ","; | |
| 77 | 118 | break; |
| 78 | 119 | } |
| 79 | 120 | } |
| 80 | 121 | |
| @@ -138,11 +179,13 @@ | ||
| 138 | 179 | $query = $this->query; |
| 139 | 180 | } |
| 140 | 181 | |
| 141 | 182 | $headers = [ |
| 142 | - 'Content-Type' => 'application/json', | |
| 143 | - 'x-templately-ip' => Helper::get_ip(), | |
| 144 | - 'x-templately-url' => home_url( '/' ) | |
| 183 | + 'Content-Type' => 'application/json', | |
| 184 | + 'Accept' => 'application/json', | |
| 185 | + 'x-templately-ip' => Helper::get_ip(), | |
| 186 | + 'x-templately-url' => home_url( '/' ), | |
| 187 | + 'x-templately-version' => TEMPLATELY_VERSION, | |
| 145 | 188 | ]; |
| 146 | 189 | |
| 147 | 190 | if ( ! empty( $args['headers'] ) ) { |
| 148 | 191 | $headers = wp_parse_args( $args['headers'], $headers ); |
| @@ -154,9 +197,9 @@ | ||
| 154 | 197 | Helper::log( 'QUERY: ' . $query ); |
| 155 | 198 | } |
| 156 | 199 | |
| 157 | 200 | $_default_args = [ |
| 158 | - 'timeout' => $this->dev_mode ? 40 : 30, | |
| 201 | + 'timeout' => $this->dev_mode ? 120 : 30, | |
| 159 | 202 | 'headers' => $headers, |
| 160 | 203 | 'body' => wp_json_encode( [ |
| 161 | 204 | 'query' => $query |
| 162 | 205 | ] ) |
| @@ -171,11 +214,11 @@ | ||
| 171 | 214 | } while ( is_wp_error( $response ) && $retryCount < $maxRetries ); |
| 172 | 215 | |
| 173 | 216 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 174 | 217 | Helper::log( 'Retry Count: ' . $retryCount ); |
| 175 | - Helper::log( 'RAW RESPONSE: ' ); | |
| 176 | - Helper::log( $response ); | |
| 177 | - Helper::log( 'END RAW RESPONSE' ); | |
| 218 | + // Helper::log( 'RAW RESPONSE: ' ); | |
| 219 | + // Helper::log( $response ); | |
| 220 | + // Helper::log( 'END RAW RESPONSE' ); | |
| 178 | 221 | } |
| 179 | 222 | |
| 180 | 223 | return $this->maybeErrors( $response, $args ); |
| 181 | 224 | } |
| @@ -180,9 +223,9 @@ | ||
| 180 | 223 | return $this->maybeErrors( $response, $args ); |
| 181 | 224 | } |
| 182 | 225 | |
| 183 | 226 | /** |
| 184 | - * Formating the self::post() response | |
| 227 | + * Formatting the self::post() response | |
| 185 | 228 | * |
| 186 | 229 | * @param mixed $response |
| 187 | 230 | * @param array $args |
| 188 | 231 | * @return mixed |
| @@ -191,13 +234,17 @@ | ||
| 191 | 234 | if ( $response instanceof WP_Error ) { |
| 192 | 235 | return $response; // Return WP_Error, if it is an error. |
| 193 | 236 | } |
| 194 | 237 | |
| 238 | + // Check for verification header before processing response body | |
| 239 | + Helper::check_verification_header( $response ); | |
| 240 | + Helper::check_site_disconnection( $response ); | |
| 241 | + | |
| 195 | 242 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 196 | 243 | $response_message = wp_remote_retrieve_response_message( $response ); |
| 197 | 244 | |
| 198 | 245 | /** |
| 199 | - * Retrive Data from Response Body. | |
| 246 | + * Retrieve Data from Response Body. | |
| 200 | 247 | */ |
| 201 | 248 | $response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 202 | 249 | /** |
| 203 | 250 | * If the graphql hit with any error. |
| @@ -220,12 +267,16 @@ | ||
| 220 | 267 | } ); |
| 221 | 268 | } |
| 222 | 269 | } ); |
| 223 | 270 | } else { |
| 271 | + $error_data = []; | |
| 272 | + if(!empty($error["extensions"]["statusText"])) { | |
| 273 | + $error_data["statusText"] = $error["extensions"]["statusText"]; | |
| 274 | + } | |
| 224 | 275 | if ( isset( $error['debugMessage'] ) ) { |
| 225 | 276 | $wp_error->add( 'templately_graphql_error', $error['debugMessage'] ); |
| 226 | 277 | } else { |
| 227 | - $wp_error->add( 'templately_graphql_error', $error['message'] ); | |
| 278 | + $wp_error->add( 'templately_graphql_error', $error['message'], $error_data ); | |
| 228 | 279 | } |
| 229 | 280 | } |
| 230 | 281 | } |
| 231 | 282 | } ); |
| @@ -243,11 +294,31 @@ | ||
| 243 | 294 | } |
| 244 | 295 | |
| 245 | 296 | return $wp_error; |
| 246 | 297 | } |
| 298 | + } elseif ( ! empty( $response['status'] ) && $response['status'] === 'error' ) { | |
| 299 | + | |
| 300 | + if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 301 | + Helper::log( 'ERROR: ' ); | |
| 302 | + Helper::log( $response ); | |
| 303 | + Helper::log( 'END ERROR' ); | |
| 304 | + } | |
| 305 | + | |
| 306 | + $error_data = []; | |
| 307 | + if ( ! empty( $response['statusText'] ) ) { | |
| 308 | + $error_data['statusText'] = $response['statusText']; | |
| 309 | + } | |
| 310 | + | |
| 311 | + $error_message = ! empty( $response['message'] ) ? $response['message'] : __( 'Unknown error occurred', 'templately' ); | |
| 312 | + | |
| 313 | + return new WP_Error( 'templately_api_error', $error_message, $error_data ); | |
| 247 | 314 | } |
| 248 | 315 | |
| 249 | 316 | $_response = isset( $response['data'][$this->endpoint] ) ? $response['data'][$this->endpoint] : []; |
| 317 | + // {"data":{"connectWithApiKey":{"status":"error","message":"Invalid API key.","user":null}}} | |
| 318 | + if ( ! empty( $response['status'] ) && $response['status'] === 'error' ) { | |
| 319 | + | |
| 320 | + } | |
| 250 | 321 | |
| 251 | 322 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 252 | 323 | Helper::log( 'RESPONSE: ' ); |
| 253 | 324 | Helper::log( $_response ); |