PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.0.1
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.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 +78 -185 3.7.32.0.1 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,99 +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 - // 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 + /**
64 + * Preparing query arguments
65 + *
116 66 * @param array $args
117 67 * @return string
118 68 */
119 69 protected function prepareArgs( $args ) {
120 70 $prepareArgs = "";
121 - foreach ( $args as $key => $value ) {
122 - switch ( true ) {
71 + foreach( $args as $key => $value ) {
72 + switch( true ) {
123 73 case is_int( $value ):
124 74 case is_bool( $value ):
125 - case is_string( $value ) && ( $value === 'true' || $value === 'false' ):
75 + case is_string( $value ) && ($value === 'true' || $value === 'false'):
126 76 $prepareArgs .= "$key:" . $value . ",";
127 77 break;
128 78 default:
129 - $prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ",";
79 + $prepareArgs .= "$key:" . '"' . $value . '"' . ",";
130 80 break;
131 81 }
132 82 }
133 83
134 - return rtrim( $prepareArgs, ',' );
84 + return rtrim($prepareArgs, ',');
135 85 }
136 86
137 87 /**
138 88 * Preparing query for the endpoint.
@@ -144,21 +94,21 @@
144 94 * @return Http
145 95 */
146 96 public function query( $query_name, $params, $funcArgs = [], ...$args ) {
147 97 $query = '{';
148 - $query .= $query_name;
149 - if ( is_array( $funcArgs ) && ! empty( $funcArgs ) ) {
150 - $query .= "(" . $this->prepareArgs( $funcArgs ) . ")";
151 - }
152 - if ( ! empty( $params ) ) {
153 - $query .= "{";
154 - $query .= $params;
155 - $query .= "}";
156 - }
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 + }
157 107 $query .= '}';
158 108
159 109 $this->endpoint = $query_name;
160 - $this->query = ! empty( $args ) ? sprintf( $query, ...$args ) : $query;
110 + $this->query = ! empty( $args ) ? sprintf($query, ...$args) : $query;
161 111 return $this;
162 112 }
163 113
164 114 /**
@@ -174,9 +124,9 @@
174 124 $this->query( $mutate, $params, $funcArgs, ...$args );
175 125 $mutation = 'mutation';
176 126 $mutation .= $this->query;
177 127 $this->endpoint = $mutate;
178 - $this->query = ! empty( $args ) ? sprintf( $mutation, ...$args ) : $mutation;
128 + $this->query = ! empty( $args ) ? sprintf($mutation, ...$args) : $mutation;
179 129 return $this;
180 130 }
181 131
182 132 /**
@@ -185,59 +135,47 @@
185 135 * @param string $query
186 136 * @param array $args
187 137 * @return mixed
188 138 */
189 - public function post( $args = [] ) {
190 - if ( empty( $query ) ) {
139 + public function post( $query = '', $args = array() ){
140 + if( empty( $query ) ) {
191 141 $query = $this->query;
192 142 }
193 143
194 144 $headers = [
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 + 'Content-Type' => 'application/json',
200 146 ];
201 147
202 - if ( ! empty( $args['headers'] ) ) {
148 + if( ! empty( $args['headers'] ) ) {
203 149 $headers = wp_parse_args( $args['headers'], $headers );
204 150 unset( $args['headers'] );
205 151 }
206 152
207 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
208 - Helper::log( 'URL: ' . $this->url() );
209 - Helper::log( 'QUERY: ' . $query );
153 + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
154 + Helper::log('URL: ' . $this->url());
155 + Helper::log('QUERY: ' . $query);
210 156 }
211 157
212 - $_default_args = [
213 - 'timeout' => $this->dev_mode ? 120 : 30,
158 + $response = wp_remote_post( $this->url(), [
159 + 'timeout' => $this->dev_mode ? 40 : 15,
214 160 'headers' => $headers,
215 - 'body' => wp_json_encode( [
161 + 'body' => wp_json_encode([
216 162 'query' => $query
217 - ] )
218 - ];
163 + ])
164 + ]);
219 165
220 - $retryCount = 0;
221 - $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3;
222 - $args = wp_parse_args( $args, $_default_args );
223 - do {
224 - $response = wp_remote_post( $this->url(), $args );
225 - $retryCount++;
226 - } while ( is_wp_error( $response ) && $retryCount < $maxRetries );
227 166
228 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
229 - Helper::log( 'Retry Count: ' . $retryCount );
230 - // Helper::log( 'RAW RESPONSE: ' );
231 - // Helper::log( $response );
232 - // Helper::log( 'END RAW RESPONSE' );
233 - }
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 + }
234 172
235 173 return $this->maybeErrors( $response, $args );
236 174 }
237 175
238 176 /**
239 - * Formatting the self::post() response
177 + * Formating the self::post() response
240 178 *
241 179 * @param mixed $response
242 180 * @param array $args
243 181 * @return mixed
@@ -242,100 +180,55 @@
242 180 * @param array $args
243 181 * @return mixed
244 182 */
245 183 private function maybeErrors( &$response, $args = [] ) {
246 - if ( $response instanceof WP_Error ) {
184 + if( $response instanceof WP_Error ) {
247 185 return $response; // Return WP_Error, if it is an error.
248 186 }
249 187
250 - // Check for verification header before processing response body
251 - Helper::check_verification_header( $response );
252 - Helper::check_site_disconnection( $response );
188 + $response_code = wp_remote_retrieve_response_code($response);
189 + $response_message = wp_remote_retrieve_response_message($response);
253 190
254 - $response_code = wp_remote_retrieve_response_code( $response );
255 - $response_message = wp_remote_retrieve_response_message( $response );
256 -
257 191 /**
258 - * Retrieve Data from Response Body.
192 + * Retrive Data from Response Body.
259 193 */
260 194 $response = json_decode( wp_remote_retrieve_body( $response ), true );
261 195 /**
262 196 * If the graphql hit with any error.
263 197 */
264 - if ( ! empty( $response['errors'] ) ) {
265 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
266 - Helper::log( 'ERROR: ' );
198 + if( ! empty( $response['errors'] ) ) {
199 + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
200 + Helper::log('ERROR: ');
267 201 Helper::log( $response['errors'] );
268 - Helper::log( 'END ERROR' );
202 + Helper::log('END ERROR');
269 203 }
270 - if ( is_array( $response['errors'] ) ) {
204 + if( is_array( $response['errors'] ) ) {
271 205 $wp_error = new WP_Error;
272 - array_walk( $response['errors'], function ( $error ) use ( &$wp_error ) {
273 - if ( isset( $error['message'] ) ) {
274 - if ( $error['message'] === 'validation' ) {
275 - array_walk( $error['extensions'], function ( $_error, $_error_key ) use ( &$wp_error ) {
276 - if ( $_error_key == 'validation' ) {
277 - array_walk( $_error, function ( $v_error, $key ) use ( &$wp_error ) {
278 - $wp_error->add( "{$key}_error", $v_error[0] );
279 - } );
280 - }
281 - } );
282 - } else {
283 - $error_data = [];
284 - if(!empty($error["extensions"]["statusText"])) {
285 - $error_data["statusText"] = $error["extensions"]["statusText"];
286 - }
287 - if ( isset( $error['debugMessage'] ) ) {
288 - $wp_error->add( 'templately_graphql_error', $error['debugMessage'] );
289 - } else {
290 - $wp_error->add( 'templately_graphql_error', $error['message'], $error_data );
291 - }
292 - }
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 + }
293 219 }
294 - } );
295 -
296 - if( $wp_error->get_error_code() === 'templately_graphql_error' ) {
297 - if( $wp_error->get_error_message() == 'Unauthorized' ) {
298 - $global_user = Login::get_instance()->delete();
299 -
300 - return [
301 - 'redirect' => true,
302 - 'url' => 'sign-in',
303 - 'user' => $global_user,
304 - ];
305 - }
306 - }
307 -
220 + });
308 221 return $wp_error;
309 222 }
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 223 }
327 224
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' ) {
225 + $_response = isset( $response['data'][ $this->endpoint ] ) ? $response['data'][ $this->endpoint ] : [];
331 226
332 - }
333 -
334 - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
335 - Helper::log( 'RESPONSE: ' );
227 + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) {
228 + Helper::log('RESPONSE: ');
336 229 Helper::log( $_response );
337 - Helper::log( 'END RESPONSE' );
230 + Helper::log('END RESPONSE');
338 231 }
339 232
340 233 return $_response;
341 234 }