| 1 |
<?php |
| 2 |
namespace Templately\Utils; |
| 3 |
|
| 4 |
use Templately\API\Login; |
| 5 |
use WP_Error; |
| 6 |
|
| 7 |
class Http extends Base { |
| 8 |
/** |
| 9 |
* API Endpoint |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
private $url = 'https://app.templately.com/api/plugin'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Development Mode |
| 16 |
* @var boolean |
| 17 |
*/ |
| 18 |
private $dev_mode = false; |
| 19 |
|
| 20 |
/** |
| 21 |
* API Query |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $query = null; |
| 25 |
/** |
| 26 |
* API Endpoint |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $endpoint = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Setting the development mode. |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
$this->dev_mode = Helper::is_dev_api(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Determining the endpoint URL based on the mode. |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public function url() { |
| 44 |
if ( Helper::is_dev_api() ) { |
| 45 |
$this->url = 'https://app.templately.dev/api/plugin'; |
| 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 |
return $this->url; |
| 57 |
} |
| 58 |
|
| 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'] ) ) : ''; |
| 75 |
} |
| 76 |
|
| 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 |
/** |
| 104 |
* @param array $args |
| 105 |
* @return string |
| 106 |
*/ |
| 107 |
protected function prepareArgs( $args ) { |
| 108 |
$prepareArgs = ""; |
| 109 |
foreach ( $args as $key => $value ) { |
| 110 |
switch ( true ) { |
| 111 |
case is_int( $value ): |
| 112 |
case is_bool( $value ): |
| 113 |
case is_string( $value ) && ( $value === 'true' || $value === 'false' ): |
| 114 |
$prepareArgs .= "$key:" . $value . ","; |
| 115 |
break; |
| 116 |
default: |
| 117 |
$prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ","; |
| 118 |
break; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return rtrim( $prepareArgs, ',' ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Preparing query for the endpoint. |
| 127 |
* |
| 128 |
* @param string $query_name |
| 129 |
* @param string $params |
| 130 |
* @param array $funcArgs |
| 131 |
* @param array ...$args |
| 132 |
* @return Http |
| 133 |
*/ |
| 134 |
public function query( $query_name, $params, $funcArgs = [], ...$args ) { |
| 135 |
$query = '{'; |
| 136 |
$query .= $query_name; |
| 137 |
if ( is_array( $funcArgs ) && ! empty( $funcArgs ) ) { |
| 138 |
$query .= "(" . $this->prepareArgs( $funcArgs ) . ")"; |
| 139 |
} |
| 140 |
if ( ! empty( $params ) ) { |
| 141 |
$query .= "{"; |
| 142 |
$query .= $params; |
| 143 |
$query .= "}"; |
| 144 |
} |
| 145 |
$query .= '}'; |
| 146 |
|
| 147 |
$this->endpoint = $query_name; |
| 148 |
$this->query = ! empty( $args ) ? sprintf( $query, ...$args ) : $query; |
| 149 |
return $this; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Preparing mutation for the endpoint. |
| 154 |
* |
| 155 |
* @param string $mutate |
| 156 |
* @param string $params |
| 157 |
* @param array $funcArgs |
| 158 |
* @param array ...$args |
| 159 |
* @return Http |
| 160 |
*/ |
| 161 |
public function mutation( $mutate, $params, $funcArgs = [], ...$args ) { |
| 162 |
$this->query( $mutate, $params, $funcArgs, ...$args ); |
| 163 |
$mutation = 'mutation'; |
| 164 |
$mutation .= $this->query; |
| 165 |
$this->endpoint = $mutate; |
| 166 |
$this->query = ! empty( $args ) ? sprintf( $mutation, ...$args ) : $mutation; |
| 167 |
return $this; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* This function is responsible for Remote HTTP POST |
| 172 |
* |
| 173 |
* @param string $query |
| 174 |
* @param array $args |
| 175 |
* @return mixed |
| 176 |
*/ |
| 177 |
public function post( $args = [] ) { |
| 178 |
if ( empty( $query ) ) { |
| 179 |
$query = $this->query; |
| 180 |
} |
| 181 |
|
| 182 |
$headers = [ |
| 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, |
| 188 |
]; |
| 189 |
|
| 190 |
if ( ! empty( $args['headers'] ) ) { |
| 191 |
$headers = wp_parse_args( $args['headers'], $headers ); |
| 192 |
unset( $args['headers'] ); |
| 193 |
} |
| 194 |
|
| 195 |
if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 196 |
Helper::log( 'URL: ' . $this->url() ); |
| 197 |
Helper::log( 'QUERY: ' . $query ); |
| 198 |
} |
| 199 |
|
| 200 |
$_default_args = [ |
| 201 |
'timeout' => $this->dev_mode ? 120 : 30, |
| 202 |
'headers' => $headers, |
| 203 |
'body' => wp_json_encode( [ |
| 204 |
'query' => $query |
| 205 |
] ) |
| 206 |
]; |
| 207 |
|
| 208 |
$retryCount = 0; |
| 209 |
$maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3; |
| 210 |
$args = wp_parse_args( $args, $_default_args ); |
| 211 |
do { |
| 212 |
$response = wp_remote_post( $this->url(), $args ); |
| 213 |
$retryCount++; |
| 214 |
} while ( is_wp_error( $response ) && $retryCount < $maxRetries ); |
| 215 |
|
| 216 |
if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 217 |
Helper::log( 'Retry Count: ' . $retryCount ); |
| 218 |
// Helper::log( 'RAW RESPONSE: ' ); |
| 219 |
// Helper::log( $response ); |
| 220 |
// Helper::log( 'END RAW RESPONSE' ); |
| 221 |
} |
| 222 |
|
| 223 |
return $this->maybeErrors( $response, $args ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Formatting the self::post() response |
| 228 |
* |
| 229 |
* @param mixed $response |
| 230 |
* @param array $args |
| 231 |
* @return mixed |
| 232 |
*/ |
| 233 |
private function maybeErrors( &$response, $args = [] ) { |
| 234 |
if ( $response instanceof WP_Error ) { |
| 235 |
return $response; // Return WP_Error, if it is an error. |
| 236 |
} |
| 237 |
|
| 238 |
// Check for verification header before processing response body |
| 239 |
Helper::check_verification_header( $response ); |
| 240 |
Helper::check_site_disconnection( $response ); |
| 241 |
|
| 242 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 243 |
$response_message = wp_remote_retrieve_response_message( $response ); |
| 244 |
|
| 245 |
/** |
| 246 |
* Retrieve Data from Response Body. |
| 247 |
*/ |
| 248 |
$response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 249 |
/** |
| 250 |
* If the graphql hit with any error. |
| 251 |
*/ |
| 252 |
if ( ! empty( $response['errors'] ) ) { |
| 253 |
if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 254 |
Helper::log( 'ERROR: ' ); |
| 255 |
Helper::log( $response['errors'] ); |
| 256 |
Helper::log( 'END ERROR' ); |
| 257 |
} |
| 258 |
if ( is_array( $response['errors'] ) ) { |
| 259 |
$wp_error = new WP_Error; |
| 260 |
array_walk( $response['errors'], function ( $error ) use ( &$wp_error ) { |
| 261 |
if ( isset( $error['message'] ) ) { |
| 262 |
if ( $error['message'] === 'validation' ) { |
| 263 |
array_walk( $error['extensions'], function ( $_error, $_error_key ) use ( &$wp_error ) { |
| 264 |
if ( $_error_key == 'validation' ) { |
| 265 |
array_walk( $_error, function ( $v_error, $key ) use ( &$wp_error ) { |
| 266 |
$wp_error->add( "{$key}_error", $v_error[0] ); |
| 267 |
} ); |
| 268 |
} |
| 269 |
} ); |
| 270 |
} else { |
| 271 |
$error_data = []; |
| 272 |
if(!empty($error["extensions"]["statusText"])) { |
| 273 |
$error_data["statusText"] = $error["extensions"]["statusText"]; |
| 274 |
} |
| 275 |
if ( isset( $error['debugMessage'] ) ) { |
| 276 |
$wp_error->add( 'templately_graphql_error', $error['debugMessage'] ); |
| 277 |
} else { |
| 278 |
$wp_error->add( 'templately_graphql_error', $error['message'], $error_data ); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} ); |
| 283 |
|
| 284 |
if( $wp_error->get_error_code() === 'templately_graphql_error' ) { |
| 285 |
if( $wp_error->get_error_message() == 'Unauthorized' ) { |
| 286 |
$global_user = Login::get_instance()->delete(); |
| 287 |
|
| 288 |
return [ |
| 289 |
'redirect' => true, |
| 290 |
'url' => 'sign-in', |
| 291 |
'user' => $global_user, |
| 292 |
]; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return $wp_error; |
| 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 ); |
| 314 |
} |
| 315 |
|
| 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 |
} |
| 321 |
|
| 322 |
if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 323 |
Helper::log( 'RESPONSE: ' ); |
| 324 |
Helper::log( $_response ); |
| 325 |
Helper::log( 'END RESPONSE' ); |
| 326 |
} |
| 327 |
|
| 328 |
return $_response; |
| 329 |
} |
| 330 |
} |
| 331 |
|