PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.9
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.9
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 +21 -81 3.7.13.4.9 View file →
@@ -39,10 +39,10 @@
39 39 * Determining the endpoint URL based on the mode.
40 40 *
41 41 * @return string
42 42 */
43 - public function url() {
44 - if ( Helper::is_dev_api() ) {
43 + public function url($is_live_api = false) {
44 + if ( !$is_live_api && Helper::is_dev_api() ) {
45 45 $this->url = 'https://app.templately.dev/api/plugin';
46 46 }
47 47
48 48 /**
@@ -56,64 +56,20 @@
56 56 return $this->url;
57 57 }
58 58
59 59 /**
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'] ) ) : '';
60 + * Preparing query arguments.
61 + * Unknown.
62 + *
63 + * @return string
64 + */
65 + public static function prepare( $query, ...$args ) {
66 + return sprintf( $query, ...$args );
75 67 }
76 68
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 -/**
69 + /**
70 + * Preparing query arguments
71 + *
116 72 * @param array $args
117 73 * @return string
118 74 */
119 75 protected function prepareArgs( $args ) {
@@ -125,9 +81,9 @@
125 81 case is_string( $value ) && ( $value === 'true' || $value === 'false' ):
126 82 $prepareArgs .= "$key:" . $value . ",";
127 83 break;
128 84 default:
129 - $prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ",";
85 + $prepareArgs .= "$key:" . '"' . $value . '"' . ",";
130 86 break;
131 87 }
132 88 }
133 89
@@ -190,11 +146,11 @@
190 146 if ( empty( $query ) ) {
191 147 $query = $this->query;
192 148 }
193 149
150 + $is_live_api = null;
194 151 $headers = [
195 152 'Content-Type' => 'application/json',
196 - 'Accept' => 'application/json',
197 153 'x-templately-ip' => Helper::get_ip(),
198 154 'x-templately-url' => home_url( '/' ),
199 155 'x-templately-version' => TEMPLATELY_VERSION,
200 156 ];
@@ -203,8 +159,13 @@
203 159 $headers = wp_parse_args( $args['headers'], $headers );
204 160 unset( $args['headers'] );
205 161 }
206 162
163 + if ( ! empty( $args['is_live_api'] ) ) {
164 + $is_live_api = $args['is_live_api'];
165 + unset( $args['is_live_api'] );
166 + }
167 +
207 168 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
208 169 Helper::log( 'URL: ' . $this->url() );
209 170 Helper::log( 'QUERY: ' . $query );
210 171 }
@@ -209,9 +170,9 @@
209 170 Helper::log( 'QUERY: ' . $query );
210 171 }
211 172
212 173 $_default_args = [
213 - 'timeout' => $this->dev_mode ? 120 : 30,
174 + 'timeout' => $this->dev_mode ? 40 : 30,
214 175 'headers' => $headers,
215 176 'body' => wp_json_encode( [
216 177 'query' => $query
217 178 ] )
@@ -220,9 +181,9 @@
220 181 $retryCount = 0;
221 182 $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3;
222 183 $args = wp_parse_args( $args, $_default_args );
223 184 do {
224 - $response = wp_remote_post( $this->url(), $args );
185 + $response = wp_remote_post( $this->url($is_live_api), $args );
225 186 $retryCount++;
226 187 } while ( is_wp_error( $response ) && $retryCount < $maxRetries );
227 188
228 189 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
@@ -248,9 +209,8 @@
248 209 }
249 210
250 211 // Check for verification header before processing response body
251 212 Helper::check_verification_header( $response );
252 - Helper::check_site_disconnection( $response );
253 213
254 214 $response_code = wp_remote_retrieve_response_code( $response );
255 215 $response_message = wp_remote_retrieve_response_message( $response );
256 216
@@ -306,31 +266,11 @@
306 266 }
307 267
308 268 return $wp_error;
309 269 }
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 );
326 270 }
327 271
328 272 $_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 - }
333 273
334 274 if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
335 275 Helper::log( 'RESPONSE: ' );
336 276 Helper::log( $_response );