PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.5.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.5.3
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 +91 -21 3.0.83.5.3 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 = defined( 'TEMPLATELY_DEV' ) && TEMPLATELY_DEV;
35 + $this->dev_mode = Helper::is_dev_api();
36 36 }
37 37
38 38 /**
39 39 * Determining the endpoint URL based on the mode.
@@ -40,27 +40,68 @@
40 40 *
41 41 * @return string
42 42 */
43 43 public function url() {
44 - if ( $this->dev_mode ) {
44 + if ( Helper::is_dev_api() ) {
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 +
47 56 return $this->url;
48 57 }
49 58
50 59 /**
51 - * Preparing query arguments.
52 - * Unknown.
53 - *
54 - * @return string
55 - */
56 - public static function prepare( $query, ...$args ) {
57 - return sprintf( $query, ...$args );
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'] ) ) : '';
58 75 }
59 76
60 - /**
61 - * Preparing query arguments
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 104 * @param array $args
64 105 * @return string
65 106 */
66 107 protected function prepareArgs( $args ) {
@@ -138,11 +179,12 @@
138 179 $query = $this->query;
139 180 }
140 181
141 182 $headers = [
142 - 'Content-Type' => 'application/json',
143 - 'x-templately-ip' => Helper::get_ip(),
144 - 'x-templately-url' => home_url( '/' )
183 + 'Content-Type' => 'application/json',
184 + 'x-templately-ip' => Helper::get_ip(),
185 + 'x-templately-url' => home_url( '/' ),
186 + 'x-templately-version' => TEMPLATELY_VERSION,
145 187 ];
146 188
147 189 if ( ! empty( $args['headers'] ) ) {
148 190 $headers = wp_parse_args( $args['headers'], $headers );
@@ -171,11 +213,11 @@
171 213 } while ( is_wp_error( $response ) && $retryCount < $maxRetries );
172 214
173 215 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
174 216 Helper::log( 'Retry Count: ' . $retryCount );
175 - Helper::log( 'RAW RESPONSE: ' );
176 - Helper::log( $response );
177 - Helper::log( 'END RAW RESPONSE' );
217 + // Helper::log( 'RAW RESPONSE: ' );
218 + // Helper::log( $response );
219 + // Helper::log( 'END RAW RESPONSE' );
178 220 }
179 221
180 222 return $this->maybeErrors( $response, $args );
181 223 }
@@ -180,9 +222,9 @@
180 222 return $this->maybeErrors( $response, $args );
181 223 }
182 224
183 225 /**
184 - * Formating the self::post() response
226 + * Formatting the self::post() response
185 227 *
186 228 * @param mixed $response
187 229 * @param array $args
188 230 * @return mixed
@@ -191,13 +233,17 @@
191 233 if ( $response instanceof WP_Error ) {
192 234 return $response; // Return WP_Error, if it is an error.
193 235 }
194 236
237 + // Check for verification header before processing response body
238 + Helper::check_verification_header( $response );
239 + Helper::check_site_disconnection( $response );
240 +
195 241 $response_code = wp_remote_retrieve_response_code( $response );
196 242 $response_message = wp_remote_retrieve_response_message( $response );
197 243
198 244 /**
199 - * Retrive Data from Response Body.
245 + * Retrieve Data from Response Body.
200 246 */
201 247 $response = json_decode( wp_remote_retrieve_body( $response ), true );
202 248 /**
203 249 * If the graphql hit with any error.
@@ -220,12 +266,16 @@
220 266 } );
221 267 }
222 268 } );
223 269 } else {
270 + $error_data = [];
271 + if(!empty($error["extensions"]["statusText"])) {
272 + $error_data["statusText"] = $error["extensions"]["statusText"];
273 + }
224 274 if ( isset( $error['debugMessage'] ) ) {
225 275 $wp_error->add( 'templately_graphql_error', $error['debugMessage'] );
226 276 } else {
227 - $wp_error->add( 'templately_graphql_error', $error['message'] );
277 + $wp_error->add( 'templately_graphql_error', $error['message'], $error_data );
228 278 }
229 279 }
230 280 }
231 281 } );
@@ -243,11 +293,31 @@
243 293 }
244 294
245 295 return $wp_error;
246 296 }
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 );
247 313 }
248 314
249 315 $_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 + }
250 320
251 321 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
252 322 Helper::log( 'RESPONSE: ' );
253 323 Helper::log( $_response );