PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.0.1
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.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 +35 -111 3.6.53.0.1 View file →
@@ -31,9 +31,9 @@
31 31 /**
32 32 * Setting the development mode.
33 33 */
34 34 public function __construct() {
35 - $this->dev_mode = Helper::is_dev_api();
35 + $this->dev_mode = defined( 'TEMPLATELY_DEV' ) && TEMPLATELY_DEV;
36 36 }
37 37
38 38 /**
39 39 * Determining the endpoint URL based on the mode.
@@ -40,68 +40,27 @@
40 40 *
41 41 * @return string
42 42 */
43 43 public function url() {
44 - if ( Helper::is_dev_api() ) {
44 + if ( $this->dev_mode ) {
45 45 $this->url = 'https://app.templately.dev/api/plugin';
46 46 }
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 47 return $this->url;
57 48 }
58 49
59 50 /**
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'] ) ) : '';
51 + * Preparing query arguments.
52 + * Unknown.
53 + *
54 + * @return string
55 + */
56 + public static function prepare( $query, ...$args ) {
57 + return sprintf( $query, ...$args );
75 58 }
76 59
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 -/**
60 + /**
61 + * Preparing query arguments
62 + *
104 63 * @param array $args
105 64 * @return string
106 65 */
107 66 protected function prepareArgs( $args ) {
@@ -113,9 +72,9 @@
113 72 case is_string( $value ) && ( $value === 'true' || $value === 'false' ):
114 73 $prepareArgs .= "$key:" . $value . ",";
115 74 break;
116 75 default:
117 - $prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ",";
76 + $prepareArgs .= "$key:" . '"' . $value . '"' . ",";
118 77 break;
119 78 }
120 79 }
121 80
@@ -179,12 +138,11 @@
179 138 $query = $this->query;
180 139 }
181 140
182 141 $headers = [
183 - 'Content-Type' => 'application/json',
184 - 'x-templately-ip' => Helper::get_ip(),
185 - 'x-templately-url' => home_url( '/' ),
186 - 'x-templately-version' => TEMPLATELY_VERSION,
142 + 'Content-Type' => 'application/json',
143 + 'x-templately-ip' => Helper::get_ip(),
144 + 'x-templately-url' => home_url( '/' )
187 145 ];
188 146
189 147 if ( ! empty( $args['headers'] ) ) {
190 148 $headers = wp_parse_args( $args['headers'], $headers );
@@ -196,9 +154,9 @@
196 154 Helper::log( 'QUERY: ' . $query );
197 155 }
198 156
199 157 $_default_args = [
200 - 'timeout' => $this->dev_mode ? 120 : 30,
158 + 'timeout' => $this->dev_mode ? 40 : 15,
201 159 'headers' => $headers,
202 160 'body' => wp_json_encode( [
203 161 'query' => $query
204 162 ] )
@@ -203,21 +161,15 @@
203 161 'query' => $query
204 162 ] )
205 163 ];
206 164
207 - $retryCount = 0;
208 - $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3;
209 - $args = wp_parse_args( $args, $_default_args );
210 - do {
211 - $response = wp_remote_post( $this->url(), $args );
212 - $retryCount++;
213 - } while ( is_wp_error( $response ) && $retryCount < $maxRetries );
165 + $args = wp_parse_args( $args, $_default_args );
166 + $response = wp_remote_post( $this->url(), $args );
214 167
215 168 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
216 - Helper::log( 'Retry Count: ' . $retryCount );
217 - // Helper::log( 'RAW RESPONSE: ' );
218 - // Helper::log( $response );
219 - // Helper::log( 'END RAW RESPONSE' );
169 + Helper::log( 'RAW RESPONSE: ' );
170 + Helper::log( $response );
171 + Helper::log( 'END RAW RESPONSE' );
220 172 }
221 173
222 174 return $this->maybeErrors( $response, $args );
223 175 }
@@ -222,9 +174,9 @@
222 174 return $this->maybeErrors( $response, $args );
223 175 }
224 176
225 177 /**
226 - * Formatting the self::post() response
178 + * Formating the self::post() response
227 179 *
228 180 * @param mixed $response
229 181 * @param array $args
230 182 * @return mixed
@@ -233,17 +185,13 @@
233 185 if ( $response instanceof WP_Error ) {
234 186 return $response; // Return WP_Error, if it is an error.
235 187 }
236 188
237 - // Check for verification header before processing response body
238 - Helper::check_verification_header( $response );
239 - Helper::check_site_disconnection( $response );
240 -
241 189 $response_code = wp_remote_retrieve_response_code( $response );
242 190 $response_message = wp_remote_retrieve_response_message( $response );
243 191
244 192 /**
245 - * Retrieve Data from Response Body.
193 + * Retrive Data from Response Body.
246 194 */
247 195 $response = json_decode( wp_remote_retrieve_body( $response ), true );
248 196 /**
249 197 * If the graphql hit with any error.
@@ -266,58 +214,34 @@
266 214 } );
267 215 }
268 216 } );
269 217 } else {
270 - $error_data = [];
271 - if(!empty($error["extensions"]["statusText"])) {
272 - $error_data["statusText"] = $error["extensions"]["statusText"];
273 - }
274 218 if ( isset( $error['debugMessage'] ) ) {
275 219 $wp_error->add( 'templately_graphql_error', $error['debugMessage'] );
276 220 } else {
277 - $wp_error->add( 'templately_graphql_error', $error['message'], $error_data );
221 + $wp_error->add( 'templately_graphql_error', $error['message'] );
278 222 }
279 223 }
280 224 }
281 225 } );
282 226
283 - if( $wp_error->get_error_code() === 'templately_graphql_error' ) {
284 - if( $wp_error->get_error_message() == 'Unauthorized' ) {
285 - $global_user = Login::get_instance()->delete();
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();
286 230
287 - return [
288 - 'redirect' => true,
289 - 'url' => 'sign-in',
290 - 'user' => $global_user,
291 - ];
292 - }
293 - }
231 + return [
232 + 'redirect' => true,
233 + 'url' => 'sign-in',
234 + 'user' => $global_user,
235 + ];
236 + }
237 + }
294 238
295 239 return $wp_error;
296 240 }
297 - } elseif ( ! empty( $response['status'] ) && $response['status'] === 'error' ) {
298 -
299 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
300 - Helper::log( 'ERROR: ' );
301 - Helper::log( $response );
302 - Helper::log( 'END ERROR' );
303 - }
304 -
305 - $error_data = [];
306 - if ( ! empty( $response['statusText'] ) ) {
307 - $error_data['statusText'] = $response['statusText'];
308 - }
309 -
310 - $error_message = ! empty( $response['message'] ) ? $response['message'] : __( 'Unknown error occurred', 'templately' );
311 -
312 - return new WP_Error( 'templately_api_error', $error_message, $error_data );
313 241 }
314 242
315 243 $_response = isset( $response['data'][$this->endpoint] ) ? $response['data'][$this->endpoint] : [];
316 - // {"data":{"connectWithApiKey":{"status":"error","message":"Invalid API key.","user":null}}}
317 - if ( ! empty( $response['status'] ) && $response['status'] === 'error' ) {
318 -
319 - }
320 244
321 245 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
322 246 Helper::log( 'RESPONSE: ' );
323 247 Helper::log( $_response );