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

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