PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.3
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.4.3, at includes/Utils/Http.php

270 lines 8.2 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 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 $is_live_api = null;
142 $headers = [
143 'Content-Type' => 'application/json',
144 'x-templately-ip' => Helper::get_ip(),
145 'x-templately-url' => home_url( '/' ),
146 'x-templately-version' => TEMPLATELY_VERSION,
147 ];
148
149 if ( ! empty( $args['headers'] ) ) {
150 $headers = wp_parse_args( $args['headers'], $headers );
151 unset( $args['headers'] );
152 }
153
154 if ( ! empty( $args['is_live_api'] ) ) {
155 $is_live_api = $args['is_live_api'];
156 unset( $args['is_live_api'] );
157 }
158
159 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
160 Helper::log( 'URL: ' . $this->url() );
161 Helper::log( 'QUERY: ' . $query );
162 }
163
164 $_default_args = [
165 'timeout' => $this->dev_mode ? 40 : 30,
166 'headers' => $headers,
167 'body' => wp_json_encode( [
168 'query' => $query
169 ] )
170 ];
171
172 $retryCount = 0;
173 $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3;
174 $args = wp_parse_args( $args, $_default_args );
175 do {
176 $response = wp_remote_post( $this->url($is_live_api), $args );
177 $retryCount++;
178 } while ( is_wp_error( $response ) && $retryCount < $maxRetries );
179
180 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
181 Helper::log( 'Retry Count: ' . $retryCount );
182 Helper::log( 'RAW RESPONSE: ' );
183 Helper::log( $response );
184 Helper::log( 'END RAW RESPONSE' );
185 }
186
187 return $this->maybeErrors( $response, $args );
188 }
189
190 /**
191 * Formatting the self::post() response
192 *
193 * @param mixed $response
194 * @param array $args
195 * @return mixed
196 */
197 private function maybeErrors( &$response, $args = [] ) {
198 if ( $response instanceof WP_Error ) {
199 return $response; // Return WP_Error, if it is an error.
200 }
201
202 // Check for verification header before processing response body
203 Helper::check_verification_header( $response );
204
205 $response_code = wp_remote_retrieve_response_code( $response );
206 $response_message = wp_remote_retrieve_response_message( $response );
207
208 /**
209 * Retrieve Data from Response Body.
210 */
211 $response = json_decode( wp_remote_retrieve_body( $response ), true );
212 /**
213 * If the graphql hit with any error.
214 */
215 if ( ! empty( $response['errors'] ) ) {
216 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
217 Helper::log( 'ERROR: ' );
218 Helper::log( $response['errors'] );
219 Helper::log( 'END ERROR' );
220 }
221 if ( is_array( $response['errors'] ) ) {
222 $wp_error = new WP_Error;
223 array_walk( $response['errors'], function ( $error ) use ( &$wp_error ) {
224 if ( isset( $error['message'] ) ) {
225 if ( $error['message'] === 'validation' ) {
226 array_walk( $error['extensions'], function ( $_error, $_error_key ) use ( &$wp_error ) {
227 if ( $_error_key == 'validation' ) {
228 array_walk( $_error, function ( $v_error, $key ) use ( &$wp_error ) {
229 $wp_error->add( "{$key}_error", $v_error[0] );
230 } );
231 }
232 } );
233 } else {
234 if ( isset( $error['debugMessage'] ) ) {
235 $wp_error->add( 'templately_graphql_error', $error['debugMessage'] );
236 } else {
237 $wp_error->add( 'templately_graphql_error', $error['message'] );
238 }
239 }
240 }
241 } );
242
243 if( $wp_error->get_error_code() === 'templately_graphql_error' ) {
244 if( $wp_error->get_error_message() == 'Unauthorized' ) {
245 $global_user = Login::get_instance()->delete();
246
247 return [
248 'redirect' => true,
249 'url' => 'sign-in',
250 'user' => $global_user,
251 ];
252 }
253 }
254
255 return $wp_error;
256 }
257 }
258
259 $_response = isset( $response['data'][$this->endpoint] ) ? $response['data'][$this->endpoint] : [];
260
261 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
262 Helper::log( 'RESPONSE: ' );
263 Helper::log( $_response );
264 Helper::log( 'END RESPONSE' );
265 }
266
267 return $_response;
268 }
269 }
270