PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.0.1
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.0.1
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
← All changes | includes/Utils/Http.php +65 -89 3.0.42.0.1 View file →
@@ -1,10 +1,13 @@
1 1 <?php
2 2 namespace Templately\Utils;
3 3
4 -use Templately\API\Login;
5 4 use WP_Error;
6 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 +
7 10 class Http extends Base {
8 11 /**
9 12 * API Endpoint
10 13 * @var string
@@ -30,10 +33,10 @@
30 33
31 34 /**
32 35 * Setting the development mode.
33 36 */
34 - public function __construct() {
35 - $this->dev_mode = defined( 'TEMPLATELY_DEV' ) && TEMPLATELY_DEV;
37 + public function __construct(){
38 + $this->dev_mode = defined('TEMPLATELY_DEV') && TEMPLATELY_DEV;
36 39 }
37 40
38 41 /**
39 42 * Determining the endpoint URL based on the mode.
@@ -40,9 +43,9 @@
40 43 *
41 44 * @return string
42 45 */
43 46 public function url() {
44 - if ( $this->dev_mode ) {
47 + if( $this->dev_mode ) {
45 48 $this->url = 'https://app.templately.dev/api/plugin';
46 49 }
47 50 return $this->url;
48 51 }
@@ -64,13 +67,13 @@
64 67 * @return string
65 68 */
66 69 protected function prepareArgs( $args ) {
67 70 $prepareArgs = "";
68 - foreach ( $args as $key => $value ) {
69 - switch ( true ) {
71 + foreach( $args as $key => $value ) {
72 + switch( true ) {
70 73 case is_int( $value ):
71 74 case is_bool( $value ):
72 - case is_string( $value ) && ( $value === 'true' || $value === 'false' ):
75 + case is_string( $value ) && ($value === 'true' || $value === 'false'):
73 76 $prepareArgs .= "$key:" . $value . ",";
74 77 break;
75 78 default:
76 79 $prepareArgs .= "$key:" . '"' . $value . '"' . ",";
@@ -77,9 +80,9 @@
77 80 break;
78 81 }
79 82 }
80 83
81 - return rtrim( $prepareArgs, ',' );
84 + return rtrim($prepareArgs, ',');
82 85 }
83 86
84 87 /**
85 88 * Preparing query for the endpoint.
@@ -91,21 +94,21 @@
91 94 * @return Http
92 95 */
93 96 public function query( $query_name, $params, $funcArgs = [], ...$args ) {
94 97 $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 - }
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 + }
104 107 $query .= '}';
105 108
106 109 $this->endpoint = $query_name;
107 - $this->query = ! empty( $args ) ? sprintf( $query, ...$args ) : $query;
110 + $this->query = ! empty( $args ) ? sprintf($query, ...$args) : $query;
108 111 return $this;
109 112 }
110 113
111 114 /**
@@ -121,9 +124,9 @@
121 124 $this->query( $mutate, $params, $funcArgs, ...$args );
122 125 $mutation = 'mutation';
123 126 $mutation .= $this->query;
124 127 $this->endpoint = $mutate;
125 - $this->query = ! empty( $args ) ? sprintf( $mutation, ...$args ) : $mutation;
128 + $this->query = ! empty( $args ) ? sprintf($mutation, ...$args) : $mutation;
126 129 return $this;
127 130 }
128 131
129 132 /**
@@ -132,51 +135,41 @@
132 135 * @param string $query
133 136 * @param array $args
134 137 * @return mixed
135 138 */
136 - public function post( $args = [] ) {
137 - if ( empty( $query ) ) {
139 + public function post( $query = '', $args = array() ){
140 + if( empty( $query ) ) {
138 141 $query = $this->query;
139 142 }
140 143
141 144 $headers = [
142 - 'Content-Type' => 'application/json',
143 - 'x-templately-ip' => Helper::get_ip(),
144 - 'x-templately-url' => home_url( '/' )
145 + 'Content-Type' => 'application/json',
145 146 ];
146 147
147 - if ( ! empty( $args['headers'] ) ) {
148 + if( ! empty( $args['headers'] ) ) {
148 149 $headers = wp_parse_args( $args['headers'], $headers );
149 150 unset( $args['headers'] );
150 151 }
151 152
152 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
153 - Helper::log( 'URL: ' . $this->url() );
154 - Helper::log( 'QUERY: ' . $query );
153 + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
154 + Helper::log('URL: ' . $this->url());
155 + Helper::log('QUERY: ' . $query);
155 156 }
156 157
157 - $_default_args = [
158 - 'timeout' => $this->dev_mode ? 40 : 30,
158 + $response = wp_remote_post( $this->url(), [
159 + 'timeout' => $this->dev_mode ? 40 : 15,
159 160 'headers' => $headers,
160 - 'body' => wp_json_encode( [
161 + 'body' => wp_json_encode([
161 162 'query' => $query
162 - ] )
163 - ];
163 + ])
164 + ]);
164 165
165 - $retryCount = 0;
166 - $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3;
167 - $args = wp_parse_args( $args, $_default_args );
168 - do {
169 - $response = wp_remote_post( $this->url(), $args );
170 - $retryCount++;
171 - } while ( is_wp_error( $response ) && $retryCount < $maxRetries );
172 166
173 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
174 - Helper::log( 'Retry Count: ' . $retryCount );
175 - Helper::log( 'RAW RESPONSE: ' );
176 - Helper::log( $response );
177 - Helper::log( 'END RAW RESPONSE' );
178 - }
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 + }
179 172
180 173 return $this->maybeErrors( $response, $args );
181 174 }
182 175
@@ -187,14 +180,14 @@
187 180 * @param array $args
188 181 * @return mixed
189 182 */
190 183 private function maybeErrors( &$response, $args = [] ) {
191 - if ( $response instanceof WP_Error ) {
184 + if( $response instanceof WP_Error ) {
192 185 return $response; // Return WP_Error, if it is an error.
193 186 }
194 187
195 - $response_code = wp_remote_retrieve_response_code( $response );
196 - $response_message = wp_remote_retrieve_response_message( $response );
188 + $response_code = wp_remote_retrieve_response_code($response);
189 + $response_message = wp_remote_retrieve_response_message($response);
197 190
198 191 /**
199 192 * Retrive Data from Response Body.
200 193 */
@@ -201,58 +194,41 @@
201 194 $response = json_decode( wp_remote_retrieve_body( $response ), true );
202 195 /**
203 196 * If the graphql hit with any error.
204 197 */
205 - if ( ! empty( $response['errors'] ) ) {
206 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
207 - Helper::log( 'ERROR: ' );
198 + if( ! empty( $response['errors'] ) ) {
199 + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
200 + Helper::log('ERROR: ');
208 201 Helper::log( $response['errors'] );
209 - Helper::log( 'END ERROR' );
202 + Helper::log('END ERROR');
210 203 }
211 - if ( is_array( $response['errors'] ) ) {
204 + if( is_array( $response['errors'] ) ) {
212 205 $wp_error = new WP_Error;
213 - array_walk( $response['errors'], function ( $error ) use ( &$wp_error ) {
214 - if ( isset( $error['message'] ) ) {
215 - if ( $error['message'] === 'validation' ) {
216 - array_walk( $error['extensions'], function ( $_error, $_error_key ) use ( &$wp_error ) {
217 - if ( $_error_key == 'validation' ) {
218 - array_walk( $_error, function ( $v_error, $key ) use ( &$wp_error ) {
219 - $wp_error->add( "{$key}_error", $v_error[0] );
220 - } );
221 - }
222 - } );
223 - } else {
224 - if ( isset( $error['debugMessage'] ) ) {
225 - $wp_error->add( 'templately_graphql_error', $error['debugMessage'] );
226 - } else {
227 - $wp_error->add( 'templately_graphql_error', $error['message'] );
228 - }
229 - }
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 + }
230 219 }
231 - } );
232 -
233 - if( $wp_error->get_error_code() === 'templately_graphql_error' ) {
234 - if( $wp_error->get_error_message() == 'Unauthorized' ) {
235 - $global_user = Login::get_instance()->delete();
236 -
237 - return [
238 - 'redirect' => true,
239 - 'url' => 'sign-in',
240 - 'user' => $global_user,
241 - ];
242 - }
243 - }
244 -
220 + });
245 221 return $wp_error;
246 222 }
247 223 }
248 224
249 - $_response = isset( $response['data'][$this->endpoint] ) ? $response['data'][$this->endpoint] : [];
225 + $_response = isset( $response['data'][ $this->endpoint ] ) ? $response['data'][ $this->endpoint ] : [];
250 226
251 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
252 - Helper::log( 'RESPONSE: ' );
227 + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
228 + Helper::log('RESPONSE: ');
253 229 Helper::log( $_response );
254 - Helper::log( 'END RESPONSE' );
230 + Helper::log('END RESPONSE');
255 231 }
256 232
257 233 return $_response;
258 234 }