PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.2
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 +106 -23 3.0.43.7.2 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,80 @@
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 + // Unique random state — doubles as cache busting and as the CSRF token the
84 + // callback validates. Only minted into a transient for a logged-in user:
85 + // this endpoint is public, and an anonymous caller could otherwise flood
86 + // wp_options with tokens that can never authorize anything.
87 + $state = wp_generate_password( 32, false );
88 + $state_owner = get_current_user_id();
89 +
90 + if ( $state_owner > 0 ) {
91 + Database::set_transient( 'google_state_' . $state, $state_owner, 15 * MINUTE_IN_SECONDS );
92 + }
93 +
94 + $return_params = [
95 + 'templately_google_login' => '1',
96 + 'templately_state' => $state,
97 + ];
98 +
99 + // Add redirect-to parameter if provided
100 + if (!empty($redirect_to)) {
101 + $return_params['redirect-to'] = $redirect_to;
102 + }
103 +
104 + $site_url_with_params = add_query_arg($return_params, $return_url);
105 +
106 + $query_params = [
107 + 'site_url' => urlencode($site_url_with_params),
108 + 'site_ip' => Helper::get_ip(),
109 + 'state' => $state,
110 + ];
111 +
112 + return add_query_arg($query_params, $auth_url);
113 +}
114 +
115 +/**
63 116 * @param array $args
64 117 * @return string
65 118 */
66 119 protected function prepareArgs( $args ) {
@@ -72,9 +125,9 @@
72 125 case is_string( $value ) && ( $value === 'true' || $value === 'false' ):
73 126 $prepareArgs .= "$key:" . $value . ",";
74 127 break;
75 128 default:
76 - $prepareArgs .= "$key:" . '"' . $value . '"' . ",";
129 + $prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ",";
77 130 break;
78 131 }
79 132 }
80 133
@@ -138,11 +191,13 @@
138 191 $query = $this->query;
139 192 }
140 193
141 194 $headers = [
142 - 'Content-Type' => 'application/json',
143 - 'x-templately-ip' => Helper::get_ip(),
144 - 'x-templately-url' => home_url( '/' )
195 + 'Content-Type' => 'application/json',
196 + 'Accept' => 'application/json',
197 + 'x-templately-ip' => Helper::get_ip(),
198 + 'x-templately-url' => home_url( '/' ),
199 + 'x-templately-version' => TEMPLATELY_VERSION,
145 200 ];
146 201
147 202 if ( ! empty( $args['headers'] ) ) {
148 203 $headers = wp_parse_args( $args['headers'], $headers );
@@ -154,9 +209,9 @@
154 209 Helper::log( 'QUERY: ' . $query );
155 210 }
156 211
157 212 $_default_args = [
158 - 'timeout' => $this->dev_mode ? 40 : 30,
213 + 'timeout' => $this->dev_mode ? 120 : 30,
159 214 'headers' => $headers,
160 215 'body' => wp_json_encode( [
161 216 'query' => $query
162 217 ] )
@@ -171,11 +226,11 @@
171 226 } while ( is_wp_error( $response ) && $retryCount < $maxRetries );
172 227
173 228 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
174 229 Helper::log( 'Retry Count: ' . $retryCount );
175 - Helper::log( 'RAW RESPONSE: ' );
176 - Helper::log( $response );
177 - Helper::log( 'END RAW RESPONSE' );
230 + // Helper::log( 'RAW RESPONSE: ' );
231 + // Helper::log( $response );
232 + // Helper::log( 'END RAW RESPONSE' );
178 233 }
179 234
180 235 return $this->maybeErrors( $response, $args );
181 236 }
@@ -180,9 +235,9 @@
180 235 return $this->maybeErrors( $response, $args );
181 236 }
182 237
183 238 /**
184 - * Formating the self::post() response
239 + * Formatting the self::post() response
185 240 *
186 241 * @param mixed $response
187 242 * @param array $args
188 243 * @return mixed
@@ -191,13 +246,17 @@
191 246 if ( $response instanceof WP_Error ) {
192 247 return $response; // Return WP_Error, if it is an error.
193 248 }
194 249
250 + // Check for verification header before processing response body
251 + Helper::check_verification_header( $response );
252 + Helper::check_site_disconnection( $response );
253 +
195 254 $response_code = wp_remote_retrieve_response_code( $response );
196 255 $response_message = wp_remote_retrieve_response_message( $response );
197 256
198 257 /**
199 - * Retrive Data from Response Body.
258 + * Retrieve Data from Response Body.
200 259 */
201 260 $response = json_decode( wp_remote_retrieve_body( $response ), true );
202 261 /**
203 262 * If the graphql hit with any error.
@@ -220,12 +279,16 @@
220 279 } );
221 280 }
222 281 } );
223 282 } else {
283 + $error_data = [];
284 + if(!empty($error["extensions"]["statusText"])) {
285 + $error_data["statusText"] = $error["extensions"]["statusText"];
286 + }
224 287 if ( isset( $error['debugMessage'] ) ) {
225 288 $wp_error->add( 'templately_graphql_error', $error['debugMessage'] );
226 289 } else {
227 - $wp_error->add( 'templately_graphql_error', $error['message'] );
290 + $wp_error->add( 'templately_graphql_error', $error['message'], $error_data );
228 291 }
229 292 }
230 293 }
231 294 } );
@@ -243,11 +306,31 @@
243 306 }
244 307
245 308 return $wp_error;
246 309 }
310 + } elseif ( ! empty( $response['status'] ) && $response['status'] === 'error' ) {
311 +
312 + if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
313 + Helper::log( 'ERROR: ' );
314 + Helper::log( $response );
315 + Helper::log( 'END ERROR' );
316 + }
317 +
318 + $error_data = [];
319 + if ( ! empty( $response['statusText'] ) ) {
320 + $error_data['statusText'] = $response['statusText'];
321 + }
322 +
323 + $error_message = ! empty( $response['message'] ) ? $response['message'] : __( 'Unknown error occurred', 'templately' );
324 +
325 + return new WP_Error( 'templately_api_error', $error_message, $error_data );
247 326 }
248 327
249 328 $_response = isset( $response['data'][$this->endpoint] ) ? $response['data'][$this->endpoint] : [];
329 + // {"data":{"connectWithApiKey":{"status":"error","message":"Invalid API key.","user":null}}}
330 + if ( ! empty( $response['status'] ) && $response['status'] === 'error' ) {
331 +
332 + }
250 333
251 334 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
252 335 Helper::log( 'RESPONSE: ' );
253 336 Helper::log( $_response );