| @@ -39,10 +39,10 @@ | ||
| 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 | 47 | |
| 48 | 48 | /** |
| @@ -56,52 +56,20 @@ | ||
| 56 | 56 | return $this->url; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| 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'] ) ) : ''; | |
| 60 | + * Preparing query arguments. | |
| 61 | + * Unknown. | |
| 62 | + * | |
| 63 | + * @return string | |
| 64 | + */ | |
| 65 | + public static function prepare( $query, ...$args ) { | |
| 66 | + return sprintf( $query, ...$args ); | |
| 75 | 67 | } |
| 76 | 68 | |
| 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 | -/** | |
| 69 | + /** | |
| 70 | + * Preparing query arguments | |
| 71 | + * | |
| 104 | 72 | * @param array $args |
| 105 | 73 | * @return string |
| 106 | 74 | */ |
| 107 | 75 | protected function prepareArgs( $args ) { |
| @@ -113,9 +81,9 @@ | ||
| 113 | 81 | case is_string( $value ) && ( $value === 'true' || $value === 'false' ): |
| 114 | 82 | $prepareArgs .= "$key:" . $value . ","; |
| 115 | 83 | break; |
| 116 | 84 | default: |
| 117 | - $prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ","; | |
| 85 | + $prepareArgs .= "$key:" . '"' . $value . '"' . ","; | |
| 118 | 86 | break; |
| 119 | 87 | } |
| 120 | 88 | } |
| 121 | 89 | |
| @@ -178,11 +146,11 @@ | ||
| 178 | 146 | if ( empty( $query ) ) { |
| 179 | 147 | $query = $this->query; |
| 180 | 148 | } |
| 181 | 149 | |
| 150 | + $is_live_api = null; | |
| 182 | 151 | $headers = [ |
| 183 | 152 | 'Content-Type' => 'application/json', |
| 184 | - 'Accept' => 'application/json', | |
| 185 | 153 | 'x-templately-ip' => Helper::get_ip(), |
| 186 | 154 | 'x-templately-url' => home_url( '/' ), |
| 187 | 155 | 'x-templately-version' => TEMPLATELY_VERSION, |
| 188 | 156 | ]; |
| @@ -191,8 +159,13 @@ | ||
| 191 | 159 | $headers = wp_parse_args( $args['headers'], $headers ); |
| 192 | 160 | unset( $args['headers'] ); |
| 193 | 161 | } |
| 194 | 162 | |
| 163 | + if ( ! empty( $args['is_live_api'] ) ) { | |
| 164 | + $is_live_api = $args['is_live_api']; | |
| 165 | + unset( $args['is_live_api'] ); | |
| 166 | + } | |
| 167 | + | |
| 195 | 168 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 196 | 169 | Helper::log( 'URL: ' . $this->url() ); |
| 197 | 170 | Helper::log( 'QUERY: ' . $query ); |
| 198 | 171 | } |
| @@ -197,9 +170,9 @@ | ||
| 197 | 170 | Helper::log( 'QUERY: ' . $query ); |
| 198 | 171 | } |
| 199 | 172 | |
| 200 | 173 | $_default_args = [ |
| 201 | - 'timeout' => $this->dev_mode ? 120 : 30, | |
| 174 | + 'timeout' => $this->dev_mode ? 40 : 30, | |
| 202 | 175 | 'headers' => $headers, |
| 203 | 176 | 'body' => wp_json_encode( [ |
| 204 | 177 | 'query' => $query |
| 205 | 178 | ] ) |
| @@ -208,9 +181,9 @@ | ||
| 208 | 181 | $retryCount = 0; |
| 209 | 182 | $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3; |
| 210 | 183 | $args = wp_parse_args( $args, $_default_args ); |
| 211 | 184 | do { |
| 212 | - $response = wp_remote_post( $this->url(), $args ); | |
| 185 | + $response = wp_remote_post( $this->url($is_live_api), $args ); | |
| 213 | 186 | $retryCount++; |
| 214 | 187 | } while ( is_wp_error( $response ) && $retryCount < $maxRetries ); |
| 215 | 188 | |
| 216 | 189 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| @@ -236,9 +209,8 @@ | ||
| 236 | 209 | } |
| 237 | 210 | |
| 238 | 211 | // Check for verification header before processing response body |
| 239 | 212 | Helper::check_verification_header( $response ); |
| 240 | - Helper::check_site_disconnection( $response ); | |
| 241 | 213 | |
| 242 | 214 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 243 | 215 | $response_message = wp_remote_retrieve_response_message( $response ); |
| 244 | 216 | |
| @@ -294,31 +266,11 @@ | ||
| 294 | 266 | } |
| 295 | 267 | |
| 296 | 268 | return $wp_error; |
| 297 | 269 | } |
| 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 | 270 | } |
| 315 | 271 | |
| 316 | 272 | $_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 | 273 | |
| 322 | 274 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 323 | 275 | Helper::log( 'RESPONSE: ' ); |
| 324 | 276 | Helper::log( $_response ); |