PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.0.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.0.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
← All changes | includes/Utils/Http.php +78 -173 3.6.82.0.0 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 = Helper::is_dev_api();
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,87 +43,46 @@
40 43 *
41 44 * @return string
42 45 */
43 46 public function url() {
44 - if ( Helper::is_dev_api() ) {
47 + if( $this->dev_mode ) {
45 48 $this->url = 'https://app.templately.dev/api/plugin';
46 49 }
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 50 return $this->url;
57 51 }
58 52
59 53 /**
60 - * Generate Google OAuth authentication URL
61 - *
62 - * @param string $redirect_to Optional redirect path after authentication
63 - * @return string The Google auth URL with query parameters
64 - */
65 -public function google_auth_url($redirect_to = '', $current_url = '') {
66 - $base_url = $this->url();
67 - // Replace /api/plugin with /api/auth/plugin/google
68 - $auth_url = str_replace('/api/plugin', '/api/auth/plugin/google', $base_url);
69 -
70 - // Get the referer to return to the exact same page we initiated login from securely
71 - if ( ! empty( $current_url ) ) {
72 - $referer = esc_url_raw( $current_url );
73 - } else {
74 - $referer = isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '';
54 + * Preparing query arguments.
55 + * Unknown.
56 + *
57 + * @return string
58 + */
59 + public static function prepare( $query, ...$args ) {
60 + return sprintf( $query, ...$args );
75 61 }
76 62
77 - $return_url = wp_validate_redirect( $referer, '' );
78 -
79 - if ( empty( $return_url ) ) {
80 - $return_url = admin_url( 'admin.php?page=templately' );
81 - }
82 -
83 - $return_params = [
84 - 'templately_google_login' => '1',
85 - ];
86 -
87 - // Add redirect-to parameter if provided
88 - if (!empty($redirect_to)) {
89 - $return_params['redirect-to'] = $redirect_to;
90 - }
91 -
92 - $site_url_with_params = add_query_arg($return_params, $return_url);
93 -
94 - $query_params = [
95 - 'site_url' => urlencode($site_url_with_params),
96 - 'site_ip' => Helper::get_ip(),
97 - 'state' => wp_generate_password(32, false) // Add unique random state for cache busting
98 - ];
99 -
100 - return add_query_arg($query_params, $auth_url);
101 -}
102 -
103 -/**
63 + /**
64 + * Preparing query arguments
65 + *
104 66 * @param array $args
105 67 * @return string
106 68 */
107 69 protected function prepareArgs( $args ) {
108 70 $prepareArgs = "";
109 - foreach ( $args as $key => $value ) {
110 - switch ( true ) {
71 + foreach( $args as $key => $value ) {
72 + switch( true ) {
111 73 case is_int( $value ):
112 74 case is_bool( $value ):
113 - case is_string( $value ) && ( $value === 'true' || $value === 'false' ):
75 + case is_string( $value ) && ($value === 'true' || $value === 'false'):
114 76 $prepareArgs .= "$key:" . $value . ",";
115 77 break;
116 78 default:
117 - $prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ",";
79 + $prepareArgs .= "$key:" . '"' . $value . '"' . ",";
118 80 break;
119 81 }
120 82 }
121 83
122 - return rtrim( $prepareArgs, ',' );
84 + return rtrim($prepareArgs, ',');
123 85 }
124 86
125 87 /**
126 88 * Preparing query for the endpoint.
@@ -132,21 +94,21 @@
132 94 * @return Http
133 95 */
134 96 public function query( $query_name, $params, $funcArgs = [], ...$args ) {
135 97 $query = '{';
136 - $query .= $query_name;
137 - if ( is_array( $funcArgs ) && ! empty( $funcArgs ) ) {
138 - $query .= "(" . $this->prepareArgs( $funcArgs ) . ")";
139 - }
140 - if ( ! empty( $params ) ) {
141 - $query .= "{";
142 - $query .= $params;
143 - $query .= "}";
144 - }
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 + }
145 107 $query .= '}';
146 108
147 109 $this->endpoint = $query_name;
148 - $this->query = ! empty( $args ) ? sprintf( $query, ...$args ) : $query;
110 + $this->query = ! empty( $args ) ? sprintf($query, ...$args) : $query;
149 111 return $this;
150 112 }
151 113
152 114 /**
@@ -162,9 +124,9 @@
162 124 $this->query( $mutate, $params, $funcArgs, ...$args );
163 125 $mutation = 'mutation';
164 126 $mutation .= $this->query;
165 127 $this->endpoint = $mutate;
166 - $this->query = ! empty( $args ) ? sprintf( $mutation, ...$args ) : $mutation;
128 + $this->query = ! empty( $args ) ? sprintf($mutation, ...$args) : $mutation;
167 129 return $this;
168 130 }
169 131
170 132 /**
@@ -173,59 +135,47 @@
173 135 * @param string $query
174 136 * @param array $args
175 137 * @return mixed
176 138 */
177 - public function post( $args = [] ) {
178 - if ( empty( $query ) ) {
139 + public function post( $query = '', $args = array() ){
140 + if( empty( $query ) ) {
179 141 $query = $this->query;
180 142 }
181 143
182 144 $headers = [
183 - 'Content-Type' => 'application/json',
184 - 'Accept' => 'application/json',
185 - 'x-templately-ip' => Helper::get_ip(),
186 - 'x-templately-url' => home_url( '/' ),
187 - 'x-templately-version' => TEMPLATELY_VERSION,
145 + 'Content-Type' => 'application/json',
188 146 ];
189 147
190 - if ( ! empty( $args['headers'] ) ) {
148 + if( ! empty( $args['headers'] ) ) {
191 149 $headers = wp_parse_args( $args['headers'], $headers );
192 150 unset( $args['headers'] );
193 151 }
194 152
195 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
196 - Helper::log( 'URL: ' . $this->url() );
197 - Helper::log( 'QUERY: ' . $query );
153 + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
154 + Helper::log('URL: ' . $this->url());
155 + Helper::log('QUERY: ' . $query);
198 156 }
199 157
200 - $_default_args = [
201 - 'timeout' => $this->dev_mode ? 120 : 30,
158 + $response = wp_remote_post( $this->url(), [
159 + 'timeout' => $this->dev_mode ? 40 : 15,
202 160 'headers' => $headers,
203 - 'body' => wp_json_encode( [
161 + 'body' => wp_json_encode([
204 162 'query' => $query
205 - ] )
206 - ];
163 + ])
164 + ]);
207 165
208 - $retryCount = 0;
209 - $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3;
210 - $args = wp_parse_args( $args, $_default_args );
211 - do {
212 - $response = wp_remote_post( $this->url(), $args );
213 - $retryCount++;
214 - } while ( is_wp_error( $response ) && $retryCount < $maxRetries );
215 166
216 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
217 - Helper::log( 'Retry Count: ' . $retryCount );
218 - // Helper::log( 'RAW RESPONSE: ' );
219 - // Helper::log( $response );
220 - // Helper::log( 'END RAW RESPONSE' );
221 - }
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 + }
222 172
223 173 return $this->maybeErrors( $response, $args );
224 174 }
225 175
226 176 /**
227 - * Formatting the self::post() response
177 + * Formating the self::post() response
228 178 *
229 179 * @param mixed $response
230 180 * @param array $args
231 181 * @return mixed
@@ -230,100 +180,55 @@
230 180 * @param array $args
231 181 * @return mixed
232 182 */
233 183 private function maybeErrors( &$response, $args = [] ) {
234 - if ( $response instanceof WP_Error ) {
184 + if( $response instanceof WP_Error ) {
235 185 return $response; // Return WP_Error, if it is an error.
236 186 }
237 187
238 - // Check for verification header before processing response body
239 - Helper::check_verification_header( $response );
240 - Helper::check_site_disconnection( $response );
188 + $response_code = wp_remote_retrieve_response_code($response);
189 + $response_message = wp_remote_retrieve_response_message($response);
241 190
242 - $response_code = wp_remote_retrieve_response_code( $response );
243 - $response_message = wp_remote_retrieve_response_message( $response );
244 -
245 191 /**
246 - * Retrieve Data from Response Body.
192 + * Retrive Data from Response Body.
247 193 */
248 194 $response = json_decode( wp_remote_retrieve_body( $response ), true );
249 195 /**
250 196 * If the graphql hit with any error.
251 197 */
252 - if ( ! empty( $response['errors'] ) ) {
253 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
254 - Helper::log( 'ERROR: ' );
198 + if( ! empty( $response['errors'] ) ) {
199 + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
200 + Helper::log('ERROR: ');
255 201 Helper::log( $response['errors'] );
256 - Helper::log( 'END ERROR' );
202 + Helper::log('END ERROR');
257 203 }
258 - if ( is_array( $response['errors'] ) ) {
204 + if( is_array( $response['errors'] ) ) {
259 205 $wp_error = new WP_Error;
260 - array_walk( $response['errors'], function ( $error ) use ( &$wp_error ) {
261 - if ( isset( $error['message'] ) ) {
262 - if ( $error['message'] === 'validation' ) {
263 - array_walk( $error['extensions'], function ( $_error, $_error_key ) use ( &$wp_error ) {
264 - if ( $_error_key == 'validation' ) {
265 - array_walk( $_error, function ( $v_error, $key ) use ( &$wp_error ) {
266 - $wp_error->add( "{$key}_error", $v_error[0] );
267 - } );
268 - }
269 - } );
270 - } else {
271 - $error_data = [];
272 - if(!empty($error["extensions"]["statusText"])) {
273 - $error_data["statusText"] = $error["extensions"]["statusText"];
274 - }
275 - if ( isset( $error['debugMessage'] ) ) {
276 - $wp_error->add( 'templately_graphql_error', $error['debugMessage'] );
277 - } else {
278 - $wp_error->add( 'templately_graphql_error', $error['message'], $error_data );
279 - }
280 - }
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 + }
281 219 }
282 - } );
283 -
284 - if( $wp_error->get_error_code() === 'templately_graphql_error' ) {
285 - if( $wp_error->get_error_message() == 'Unauthorized' ) {
286 - $global_user = Login::get_instance()->delete();
287 -
288 - return [
289 - 'redirect' => true,
290 - 'url' => 'sign-in',
291 - 'user' => $global_user,
292 - ];
293 - }
294 - }
295 -
220 + });
296 221 return $wp_error;
297 222 }
298 - } elseif ( ! empty( $response['status'] ) && $response['status'] === 'error' ) {
299 -
300 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
301 - Helper::log( 'ERROR: ' );
302 - Helper::log( $response );
303 - Helper::log( 'END ERROR' );
304 - }
305 -
306 - $error_data = [];
307 - if ( ! empty( $response['statusText'] ) ) {
308 - $error_data['statusText'] = $response['statusText'];
309 - }
310 -
311 - $error_message = ! empty( $response['message'] ) ? $response['message'] : __( 'Unknown error occurred', 'templately' );
312 -
313 - return new WP_Error( 'templately_api_error', $error_message, $error_data );
314 223 }
315 224
316 - $_response = isset( $response['data'][$this->endpoint] ) ? $response['data'][$this->endpoint] : [];
317 - // {"data":{"connectWithApiKey":{"status":"error","message":"Invalid API key.","user":null}}}
318 - if ( ! empty( $response['status'] ) && $response['status'] === 'error' ) {
225 + $_response = ! empty( $response['data'][ $this->endpoint ] ) ? $response['data'][ $this->endpoint ] : [];
319 226
320 - }
321 -
322 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
323 - Helper::log( 'RESPONSE: ' );
227 + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
228 + Helper::log('RESPONSE: ');
324 229 Helper::log( $_response );
325 - Helper::log( 'END RESPONSE' );
230 + Helper::log('END RESPONSE');
326 231 }
327 232
328 233 return $_response;
329 234 }