| @@ -39,69 +39,28 @@ | ||
| 39 | 39 | * Determining the endpoint URL based on the mode. |
| 40 | 40 | * |
| 41 | 41 | * @return string |
| 42 | 42 | */ |
| 43 | - public function url() { | |
| 44 | - if ( Helper::is_dev_api() ) { | |
| 43 | + public function url($is_live_api = false) { | |
| 44 | + if ( !$is_live_api && 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 | - | |
| 56 | 47 | return $this->url; |
| 57 | 48 | } |
| 58 | 49 | |
| 59 | 50 | /** |
| 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'] ) ) : ''; | |
| 51 | + * Preparing query arguments. | |
| 52 | + * Unknown. | |
| 53 | + * | |
| 54 | + * @return string | |
| 55 | + */ | |
| 56 | + public static function prepare( $query, ...$args ) { | |
| 57 | + return sprintf( $query, ...$args ); | |
| 75 | 58 | } |
| 76 | 59 | |
| 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 | -/** | |
| 60 | + /** | |
| 61 | + * Preparing query arguments | |
| 62 | + * | |
| 104 | 63 | * @param array $args |
| 105 | 64 | * @return string |
| 106 | 65 | */ |
| 107 | 66 | protected function prepareArgs( $args ) { |
| @@ -113,9 +72,9 @@ | ||
| 113 | 72 | case is_string( $value ) && ( $value === 'true' || $value === 'false' ): |
| 114 | 73 | $prepareArgs .= "$key:" . $value . ","; |
| 115 | 74 | break; |
| 116 | 75 | default: |
| 117 | - $prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ","; | |
| 76 | + $prepareArgs .= "$key:" . '"' . $value . '"' . ","; | |
| 118 | 77 | break; |
| 119 | 78 | } |
| 120 | 79 | } |
| 121 | 80 | |
| @@ -178,11 +137,11 @@ | ||
| 178 | 137 | if ( empty( $query ) ) { |
| 179 | 138 | $query = $this->query; |
| 180 | 139 | } |
| 181 | 140 | |
| 141 | + $is_live_api = null; | |
| 182 | 142 | $headers = [ |
| 183 | 143 | 'Content-Type' => 'application/json', |
| 184 | - 'Accept' => 'application/json', | |
| 185 | 144 | 'x-templately-ip' => Helper::get_ip(), |
| 186 | 145 | 'x-templately-url' => home_url( '/' ), |
| 187 | 146 | 'x-templately-version' => TEMPLATELY_VERSION, |
| 188 | 147 | ]; |
| @@ -191,8 +150,13 @@ | ||
| 191 | 150 | $headers = wp_parse_args( $args['headers'], $headers ); |
| 192 | 151 | unset( $args['headers'] ); |
| 193 | 152 | } |
| 194 | 153 | |
| 154 | + if ( ! empty( $args['is_live_api'] ) ) { | |
| 155 | + $is_live_api = $args['is_live_api']; | |
| 156 | + unset( $args['is_live_api'] ); | |
| 157 | + } | |
| 158 | + | |
| 195 | 159 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 196 | 160 | Helper::log( 'URL: ' . $this->url() ); |
| 197 | 161 | Helper::log( 'QUERY: ' . $query ); |
| 198 | 162 | } |
| @@ -197,9 +161,9 @@ | ||
| 197 | 161 | Helper::log( 'QUERY: ' . $query ); |
| 198 | 162 | } |
| 199 | 163 | |
| 200 | 164 | $_default_args = [ |
| 201 | - 'timeout' => $this->dev_mode ? 120 : 30, | |
| 165 | + 'timeout' => $this->dev_mode ? 40 : 30, | |
| 202 | 166 | 'headers' => $headers, |
| 203 | 167 | 'body' => wp_json_encode( [ |
| 204 | 168 | 'query' => $query |
| 205 | 169 | ] ) |
| @@ -208,17 +172,17 @@ | ||
| 208 | 172 | $retryCount = 0; |
| 209 | 173 | $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3; |
| 210 | 174 | $args = wp_parse_args( $args, $_default_args ); |
| 211 | 175 | do { |
| 212 | - $response = wp_remote_post( $this->url(), $args ); | |
| 176 | + $response = wp_remote_post( $this->url($is_live_api), $args ); | |
| 213 | 177 | $retryCount++; |
| 214 | 178 | } while ( is_wp_error( $response ) && $retryCount < $maxRetries ); |
| 215 | 179 | |
| 216 | 180 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 217 | 181 | Helper::log( 'Retry Count: ' . $retryCount ); |
| 218 | - // Helper::log( 'RAW RESPONSE: ' ); | |
| 219 | - // Helper::log( $response ); | |
| 220 | - // Helper::log( 'END RAW RESPONSE' ); | |
| 182 | + Helper::log( 'RAW RESPONSE: ' ); | |
| 183 | + Helper::log( $response ); | |
| 184 | + Helper::log( 'END RAW RESPONSE' ); | |
| 221 | 185 | } |
| 222 | 186 | |
| 223 | 187 | return $this->maybeErrors( $response, $args ); |
| 224 | 188 | } |
| @@ -236,9 +200,8 @@ | ||
| 236 | 200 | } |
| 237 | 201 | |
| 238 | 202 | // Check for verification header before processing response body |
| 239 | 203 | Helper::check_verification_header( $response ); |
| 240 | - Helper::check_site_disconnection( $response ); | |
| 241 | 204 | |
| 242 | 205 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 243 | 206 | $response_message = wp_remote_retrieve_response_message( $response ); |
| 244 | 207 | |
| @@ -267,16 +230,12 @@ | ||
| 267 | 230 | } ); |
| 268 | 231 | } |
| 269 | 232 | } ); |
| 270 | 233 | } else { |
| 271 | - $error_data = []; | |
| 272 | - if(!empty($error["extensions"]["statusText"])) { | |
| 273 | - $error_data["statusText"] = $error["extensions"]["statusText"]; | |
| 274 | - } | |
| 275 | 234 | if ( isset( $error['debugMessage'] ) ) { |
| 276 | 235 | $wp_error->add( 'templately_graphql_error', $error['debugMessage'] ); |
| 277 | 236 | } else { |
| 278 | - $wp_error->add( 'templately_graphql_error', $error['message'], $error_data ); | |
| 237 | + $wp_error->add( 'templately_graphql_error', $error['message'] ); | |
| 279 | 238 | } |
| 280 | 239 | } |
| 281 | 240 | } |
| 282 | 241 | } ); |
| @@ -294,31 +253,11 @@ | ||
| 294 | 253 | } |
| 295 | 254 | |
| 296 | 255 | return $wp_error; |
| 297 | 256 | } |
| 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 ); | |
| 314 | 257 | } |
| 315 | 258 | |
| 316 | 259 | $_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 | - } | |
| 321 | 260 | |
| 322 | 261 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 323 | 262 | Helper::log( 'RESPONSE: ' ); |
| 324 | 263 | Helper::log( $_response ); |