PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.5.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.5.0
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Utils / Http.php

Http.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.5.0, at includes/Utils/Http.php

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