| 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 = defined( 'TEMPLATELY_DEV' ) && TEMPLATELY_DEV; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Determining the endpoint URL based on the mode. |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public function url() { |
| 44 |
if ( $this->dev_mode ) { |
| 45 |
$this->url = 'https://app.templately.dev/api/plugin'; |
| 46 |
} |
| 47 |
return $this->url; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Preparing query arguments. |
| 52 |
* Unknown. |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public static function prepare( $query, ...$args ) { |
| 57 |
return sprintf( $query, ...$args ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Preparing query arguments |
| 62 |
* |
| 63 |
* @param array $args |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
protected function prepareArgs( $args ) { |
| 67 |
$prepareArgs = ""; |
| 68 |
foreach ( $args as $key => $value ) { |
| 69 |
switch ( true ) { |
| 70 |
case is_int( $value ): |
| 71 |
case is_bool( $value ): |
| 72 |
case is_string( $value ) && ( $value === 'true' || $value === 'false' ): |
| 73 |
$prepareArgs .= "$key:" . $value . ","; |
| 74 |
break; |
| 75 |
default: |
| 76 |
$prepareArgs .= "$key:" . '"' . $value . '"' . ","; |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return rtrim( $prepareArgs, ',' ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Preparing query for the endpoint. |
| 86 |
* |
| 87 |
* @param string $query_name |
| 88 |
* @param string $params |
| 89 |
* @param array $funcArgs |
| 90 |
* @param array ...$args |
| 91 |
* @return Http |
| 92 |
*/ |
| 93 |
public function query( $query_name, $params, $funcArgs = [], ...$args ) { |
| 94 |
$query = '{'; |
| 95 |
$query .= $query_name; |
| 96 |
if ( is_array( $funcArgs ) && ! empty( $funcArgs ) ) { |
| 97 |
$query .= "(" . $this->prepareArgs( $funcArgs ) . ")"; |
| 98 |
} |
| 99 |
if ( ! empty( $params ) ) { |
| 100 |
$query .= "{"; |
| 101 |
$query .= $params; |
| 102 |
$query .= "}"; |
| 103 |
} |
| 104 |
$query .= '}'; |
| 105 |
|
| 106 |
$this->endpoint = $query_name; |
| 107 |
$this->query = ! empty( $args ) ? sprintf( $query, ...$args ) : $query; |
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Preparing mutation for the endpoint. |
| 113 |
* |
| 114 |
* @param string $mutate |
| 115 |
* @param string $params |
| 116 |
* @param array $funcArgs |
| 117 |
* @param array ...$args |
| 118 |
* @return Http |
| 119 |
*/ |
| 120 |
public function mutation( $mutate, $params, $funcArgs = [], ...$args ) { |
| 121 |
$this->query( $mutate, $params, $funcArgs, ...$args ); |
| 122 |
$mutation = 'mutation'; |
| 123 |
$mutation .= $this->query; |
| 124 |
$this->endpoint = $mutate; |
| 125 |
$this->query = ! empty( $args ) ? sprintf( $mutation, ...$args ) : $mutation; |
| 126 |
return $this; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* This function is responsible for Remote HTTP POST |
| 131 |
* |
| 132 |
* @param string $query |
| 133 |
* @param array $args |
| 134 |
* @return mixed |
| 135 |
*/ |
| 136 |
public function post( $args = [] ) { |
| 137 |
if ( empty( $query ) ) { |
| 138 |
$query = $this->query; |
| 139 |
} |
| 140 |
|
| 141 |
$headers = [ |
| 142 |
'Content-Type' => 'application/json', |
| 143 |
'x-templately-ip' => Helper::get_ip(), |
| 144 |
'x-templately-url' => home_url( '/' ) |
| 145 |
]; |
| 146 |
|
| 147 |
if ( ! empty( $args['headers'] ) ) { |
| 148 |
$headers = wp_parse_args( $args['headers'], $headers ); |
| 149 |
unset( $args['headers'] ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 153 |
Helper::log( 'URL: ' . $this->url() ); |
| 154 |
Helper::log( 'QUERY: ' . $query ); |
| 155 |
} |
| 156 |
|
| 157 |
$_default_args = [ |
| 158 |
'timeout' => $this->dev_mode ? 40 : 30, |
| 159 |
'headers' => $headers, |
| 160 |
'body' => wp_json_encode( [ |
| 161 |
'query' => $query |
| 162 |
] ) |
| 163 |
]; |
| 164 |
|
| 165 |
$retryCount = 0; |
| 166 |
$maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3; |
| 167 |
$args = wp_parse_args( $args, $_default_args ); |
| 168 |
do { |
| 169 |
$response = wp_remote_post( $this->url(), $args ); |
| 170 |
$retryCount++; |
| 171 |
} while ( is_wp_error( $response ) && $retryCount < $maxRetries ); |
| 172 |
|
| 173 |
if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 174 |
Helper::log( 'Retry Count: ' . $retryCount ); |
| 175 |
Helper::log( 'RAW RESPONSE: ' ); |
| 176 |
Helper::log( $response ); |
| 177 |
Helper::log( 'END RAW RESPONSE' ); |
| 178 |
} |
| 179 |
|
| 180 |
return $this->maybeErrors( $response, $args ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Formating the self::post() response |
| 185 |
* |
| 186 |
* @param mixed $response |
| 187 |
* @param array $args |
| 188 |
* @return mixed |
| 189 |
*/ |
| 190 |
private function maybeErrors( &$response, $args = [] ) { |
| 191 |
if ( $response instanceof WP_Error ) { |
| 192 |
return $response; // Return WP_Error, if it is an error. |
| 193 |
} |
| 194 |
|
| 195 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 196 |
$response_message = wp_remote_retrieve_response_message( $response ); |
| 197 |
|
| 198 |
/** |
| 199 |
* Retrive Data from Response Body. |
| 200 |
*/ |
| 201 |
$response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 202 |
/** |
| 203 |
* If the graphql hit with any error. |
| 204 |
*/ |
| 205 |
if ( ! empty( $response['errors'] ) ) { |
| 206 |
if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 207 |
Helper::log( 'ERROR: ' ); |
| 208 |
Helper::log( $response['errors'] ); |
| 209 |
Helper::log( 'END ERROR' ); |
| 210 |
} |
| 211 |
if ( is_array( $response['errors'] ) ) { |
| 212 |
$wp_error = new WP_Error; |
| 213 |
array_walk( $response['errors'], function ( $error ) use ( &$wp_error ) { |
| 214 |
if ( isset( $error['message'] ) ) { |
| 215 |
if ( $error['message'] === 'validation' ) { |
| 216 |
array_walk( $error['extensions'], function ( $_error, $_error_key ) use ( &$wp_error ) { |
| 217 |
if ( $_error_key == 'validation' ) { |
| 218 |
array_walk( $_error, function ( $v_error, $key ) use ( &$wp_error ) { |
| 219 |
$wp_error->add( "{$key}_error", $v_error[0] ); |
| 220 |
} ); |
| 221 |
} |
| 222 |
} ); |
| 223 |
} else { |
| 224 |
if ( isset( $error['debugMessage'] ) ) { |
| 225 |
$wp_error->add( 'templately_graphql_error', $error['debugMessage'] ); |
| 226 |
} else { |
| 227 |
$wp_error->add( 'templately_graphql_error', $error['message'] ); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} ); |
| 232 |
|
| 233 |
if( $wp_error->get_error_code() === 'templately_graphql_error' ) { |
| 234 |
if( $wp_error->get_error_message() == 'Unauthorized' ) { |
| 235 |
$global_user = Login::get_instance()->delete(); |
| 236 |
|
| 237 |
return [ |
| 238 |
'redirect' => true, |
| 239 |
'url' => 'sign-in', |
| 240 |
'user' => $global_user, |
| 241 |
]; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
return $wp_error; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
$_response = isset( $response['data'][$this->endpoint] ) ? $response['data'][$this->endpoint] : []; |
| 250 |
|
| 251 |
if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 252 |
Helper::log( 'RESPONSE: ' ); |
| 253 |
Helper::log( $_response ); |
| 254 |
Helper::log( 'END RESPONSE' ); |
| 255 |
} |
| 256 |
|
| 257 |
return $_response; |
| 258 |
} |
| 259 |
} |
| 260 |
|