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

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