| 1 |
<?php |
| 2 |
namespace Templately\Utils; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
|
| 6 |
use function wp_remote_retrieve_response_code; |
| 7 |
use function wp_remote_retrieve_response_message; |
| 8 |
use function wp_remote_retrieve_body; |
| 9 |
|
| 10 |
class Http extends Base { |
| 11 |
/** |
| 12 |
* API Endpoint |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private $url = 'https://app.templately.com/api/plugin'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Development Mode |
| 19 |
* @var boolean |
| 20 |
*/ |
| 21 |
private $dev_mode = false; |
| 22 |
|
| 23 |
/** |
| 24 |
* API Query |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public $query = null; |
| 28 |
/** |
| 29 |
* API Endpoint |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
public $endpoint = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Setting the development mode. |
| 36 |
*/ |
| 37 |
public function __construct(){ |
| 38 |
$this->dev_mode = defined('TEMPLATELY_DEV') && TEMPLATELY_DEV; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Determining the endpoint URL based on the mode. |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public function url() { |
| 47 |
if( $this->dev_mode ) { |
| 48 |
$this->url = 'https://app.templately.dev/api/plugin'; |
| 49 |
} |
| 50 |
return $this->url; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Preparing query arguments. |
| 55 |
* Unknown. |
| 56 |
* |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
public static function prepare( $query, ...$args ) { |
| 60 |
return sprintf( $query, ...$args ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Preparing query arguments |
| 65 |
* |
| 66 |
* @param array $args |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
protected function prepareArgs( $args ) { |
| 70 |
$prepareArgs = ""; |
| 71 |
foreach( $args as $key => $value ) { |
| 72 |
switch( true ) { |
| 73 |
case is_int( $value ): |
| 74 |
case is_bool( $value ): |
| 75 |
case is_string( $value ) && ($value === 'true' || $value === 'false'): |
| 76 |
$prepareArgs .= "$key:" . $value . ","; |
| 77 |
break; |
| 78 |
default: |
| 79 |
$prepareArgs .= "$key:" . '"' . $value . '"' . ","; |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return rtrim($prepareArgs, ','); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Preparing query for the endpoint. |
| 89 |
* |
| 90 |
* @param string $query_name |
| 91 |
* @param string $params |
| 92 |
* @param array $funcArgs |
| 93 |
* @param array ...$args |
| 94 |
* @return Http |
| 95 |
*/ |
| 96 |
public function query( $query_name, $params, $funcArgs = [], ...$args ) { |
| 97 |
$query = '{'; |
| 98 |
$query .= $query_name; |
| 99 |
if( is_array( $funcArgs ) && ! empty( $funcArgs ) ) { |
| 100 |
$query .= "(". $this->prepareArgs( $funcArgs ) .")"; |
| 101 |
} |
| 102 |
if( ! empty( $params ) ) { |
| 103 |
$query .= "{"; |
| 104 |
$query .= $params; |
| 105 |
$query .= "}"; |
| 106 |
} |
| 107 |
$query .= '}'; |
| 108 |
|
| 109 |
$this->endpoint = $query_name; |
| 110 |
$this->query = ! empty( $args ) ? sprintf($query, ...$args) : $query; |
| 111 |
return $this; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Preparing mutation for the endpoint. |
| 116 |
* |
| 117 |
* @param string $mutate |
| 118 |
* @param string $params |
| 119 |
* @param array $funcArgs |
| 120 |
* @param array ...$args |
| 121 |
* @return Http |
| 122 |
*/ |
| 123 |
public function mutation( $mutate, $params, $funcArgs = [], ...$args ) { |
| 124 |
$this->query( $mutate, $params, $funcArgs, ...$args ); |
| 125 |
$mutation = 'mutation'; |
| 126 |
$mutation .= $this->query; |
| 127 |
$this->endpoint = $mutate; |
| 128 |
$this->query = ! empty( $args ) ? sprintf($mutation, ...$args) : $mutation; |
| 129 |
return $this; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* This function is responsible for Remote HTTP POST |
| 134 |
* |
| 135 |
* @param string $query |
| 136 |
* @param array $args |
| 137 |
* @return mixed |
| 138 |
*/ |
| 139 |
public function post( $query = '', $args = array() ){ |
| 140 |
if( empty( $query ) ) { |
| 141 |
$query = $this->query; |
| 142 |
} |
| 143 |
|
| 144 |
$headers = [ |
| 145 |
'Content-Type' => 'application/json', |
| 146 |
]; |
| 147 |
|
| 148 |
if( ! empty( $args['headers'] ) ) { |
| 149 |
$headers = wp_parse_args( $args['headers'], $headers ); |
| 150 |
unset( $args['headers'] ); |
| 151 |
} |
| 152 |
|
| 153 |
if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 154 |
Helper::log('URL: ' . $this->url()); |
| 155 |
Helper::log('QUERY: ' . $query); |
| 156 |
} |
| 157 |
|
| 158 |
$response = wp_remote_post( $this->url(), [ |
| 159 |
'timeout' => $this->dev_mode ? 40 : 15, |
| 160 |
'headers' => $headers, |
| 161 |
'body' => wp_json_encode([ |
| 162 |
'query' => $query |
| 163 |
]) |
| 164 |
]); |
| 165 |
|
| 166 |
|
| 167 |
if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 168 |
Helper::log('RAW RESPONSE: '); |
| 169 |
Helper::log( $response ); |
| 170 |
Helper::log('END RAW RESPONSE'); |
| 171 |
} |
| 172 |
|
| 173 |
return $this->maybeErrors( $response, $args ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Formating the self::post() response |
| 178 |
* |
| 179 |
* @param mixed $response |
| 180 |
* @param array $args |
| 181 |
* @return mixed |
| 182 |
*/ |
| 183 |
private function maybeErrors( &$response, $args = [] ) { |
| 184 |
if( $response instanceof WP_Error ) { |
| 185 |
return $response; // Return WP_Error, if it is an error. |
| 186 |
} |
| 187 |
|
| 188 |
$response_code = wp_remote_retrieve_response_code($response); |
| 189 |
$response_message = wp_remote_retrieve_response_message($response); |
| 190 |
|
| 191 |
/** |
| 192 |
* Retrive Data from Response Body. |
| 193 |
*/ |
| 194 |
$response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 195 |
/** |
| 196 |
* If the graphql hit with any error. |
| 197 |
*/ |
| 198 |
if( ! empty( $response['errors'] ) ) { |
| 199 |
if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 200 |
Helper::log('ERROR: '); |
| 201 |
Helper::log( $response['errors'] ); |
| 202 |
Helper::log('END ERROR'); |
| 203 |
} |
| 204 |
if( is_array( $response['errors'] ) ) { |
| 205 |
$wp_error = new WP_Error; |
| 206 |
array_walk( $response['errors'], function( $error ) use( &$wp_error ) { |
| 207 |
if( isset( $error['message'] ) ) { |
| 208 |
if( $error['message'] === 'validation' ) { |
| 209 |
array_walk( $error['extensions'], function( $_error, $_error_key ) use( &$wp_error ) { |
| 210 |
if( $_error_key == 'validation' ) { |
| 211 |
array_walk( $_error, function( $v_error, $key ) use( &$wp_error ) { |
| 212 |
$wp_error->add( "{$key}_error", ucwords($v_error[0]) ); |
| 213 |
}); |
| 214 |
} |
| 215 |
}); |
| 216 |
} else { |
| 217 |
$wp_error->add( 'templately_graphql_error', $error['message'] ); |
| 218 |
} |
| 219 |
} |
| 220 |
}); |
| 221 |
return $wp_error; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
$_response = isset( $response['data'][ $this->endpoint ] ) ? $response['data'][ $this->endpoint ] : []; |
| 226 |
|
| 227 |
if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 228 |
Helper::log('RESPONSE: '); |
| 229 |
Helper::log( $_response ); |
| 230 |
Helper::log('END RESPONSE'); |
| 231 |
} |
| 232 |
|
| 233 |
return $_response; |
| 234 |
} |
| 235 |
} |
| 236 |
|