Conditions | 37 |
Paths | > 20000 |
Total Lines | 205 |
Code Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
224 | public function request( $extra_params = [] ) { |
||
225 | // Make sure we're not blocked |
||
226 | $blocked = $this->is_blocked( $this->url ); |
||
227 | if ( $blocked || is_wp_error( $blocked ) ) { |
||
228 | if ( is_wp_error( $blocked ) ) { |
||
229 | // Translators: Placeholder gets replaced with the error message. |
||
230 | return new WP_Error( 'api-error', sprintf( __( 'The firewall of your server is blocking outbound calls. Please contact your hosting provider to fix this issue. %s', 'google-analytics-for-wordpress' ), $blocked->get_error_message() ) ); |
||
231 | } else { |
||
232 | return new WP_Error( 'api-error', __( 'The firewall of your server is blocking outbound calls. Please contact your hosting provider to fix this issue.', 'google-analytics-for-wordpress' ) ); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | // Build the body of the request. |
||
237 | $body = array(); |
||
238 | |||
239 | if ( ! empty( $this->token ) ) { |
||
240 | $body['token'] = $this->token; |
||
241 | } |
||
242 | |||
243 | if ( ! empty( $this->key ) ) { |
||
244 | $body['key'] = $this->key; |
||
245 | } |
||
246 | |||
247 | if ( ! empty( $this->tt ) ) { |
||
248 | $body['tt'] = $this->tt; |
||
249 | } |
||
250 | |||
251 | if ( ! empty( $this->return ) ) { |
||
252 | $body['return'] = $this->return; |
||
253 | } |
||
254 | |||
255 | if ( monsterinsights_is_pro_version() && ! empty( $this->license ) ) { |
||
256 | $body['license'] = $this->license; |
||
257 | } |
||
258 | |||
259 | if ( ! empty( $this->start ) ) { |
||
260 | $body['start'] = $this->start; |
||
261 | } |
||
262 | |||
263 | if ( ! empty( $this->end ) ) { |
||
264 | $body['end'] = $this->end; |
||
265 | } |
||
266 | |||
267 | if ( ! empty( $this->compare_start ) ) { |
||
268 | $body['compare_start'] = $this->compare_start; |
||
269 | } |
||
270 | |||
271 | if ( ! empty( $this->compare_end ) ) { |
||
272 | $body['compare_end'] = $this->compare_end; |
||
273 | } |
||
274 | |||
275 | if ( ! empty( $this->sitei ) ) { |
||
276 | $body['sitei'] = $this->sitei; |
||
277 | } |
||
278 | |||
279 | $body['siteurl'] = $this->site_url; |
||
280 | $body['miversion'] = $this->miversion; |
||
281 | |||
282 | // If a plugin API request, add the data. |
||
283 | if ( 'info' == $this->route || 'update' == $this->route ) { |
||
284 | $body['miapi-plugin'] = $this->plugin; |
||
285 | } |
||
286 | |||
287 | // Add in additional data if needed. |
||
288 | if ( ! empty( $this->additional_data ) ) { |
||
289 | $body['miapi-data'] = maybe_serialize( $this->additional_data ); |
||
290 | } |
||
291 | |||
292 | if ( 'GET' == $this->method ) { |
||
293 | $body['time'] = time(); // just to avoid caching |
||
294 | } |
||
295 | |||
296 | $body['wp_timezone'] = wp_timezone_string(); // Timezone from WP Settings. |
||
297 | |||
298 | $body['timezone'] = date( 'e' ); |
||
299 | |||
300 | $body['network'] = $this->network ? 'network' : 'site'; |
||
301 | |||
302 | $body['ip'] = ! empty( $_SERVER['SERVER_ADDR'] ) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_ADDR'])) : ''; |
||
303 | |||
304 | // This filter will be removed in the future. |
||
305 | $body = apply_filters( 'monsterinsights_api_request_body', $body ); |
||
306 | |||
307 | $body = array_merge($body, $extra_params); |
||
308 | |||
309 | $string = http_build_query( $body, '', '&' ); |
||
310 | |||
311 | // Build the headers of the request. |
||
312 | $headers = array( |
||
313 | 'Content-Type' => 'application/x-www-form-urlencoded', |
||
314 | 'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0', |
||
315 | 'Pragma' => 'no-cache', |
||
316 | 'Expires' => 0, |
||
317 | 'MIAPI-Referer' => is_network_admin() ? network_admin_url() : site_url(), |
||
318 | 'MIAPI-Sender' => 'WordPress', |
||
319 | ); |
||
320 | |||
321 | // if ( $this->apikey ) { |
||
322 | // $headers['X-MonsterInsights-ApiKey'] = $this->apikey; |
||
323 | // } |
||
324 | |||
325 | // Setup data to be sent to the API. |
||
326 | $data = array( |
||
327 | 'headers' => $headers, |
||
328 | 'body' => $body, |
||
329 | 'timeout' => 3000, |
||
330 | 'user-agent' => 'MI/' . MONSTERINSIGHTS_VERSION . '; ' . $this->site_url, |
||
331 | 'sslverify' => false, |
||
332 | ); |
||
333 | |||
334 | // Perform the query and retrieve the response. |
||
335 | $response = 'GET' == $this->method ? wp_remote_get( esc_url_raw( $this->url ) . '?' . $string, $data ) : wp_remote_post( esc_url_raw( $this->url ), $data ); |
||
336 | |||
337 | // return new WP_Error( 'debug', '<pre>' . var_export( $response, true ) . '</pre>' ); |
||
338 | |||
339 | if ( is_wp_error( $response ) ) { |
||
340 | return $response; |
||
341 | } |
||
342 | |||
343 | $response_code = wp_remote_retrieve_response_code( $response ); |
||
344 | $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
||
345 | // return new WP_Error( 'debug', '<pre>' . var_export( $response_body, true ) . '</pre>' ); |
||
346 | // var_dump( $response_body ); |
||
347 | // Bail out early if there are any errors. |
||
348 | if ( is_wp_error( $response_body ) ) { |
||
349 | return $response_body; |
||
350 | } |
||
351 | |||
352 | // If not a 200 status header, send back error. |
||
353 | if ( 200 != $response_code && 204 != $response_code) { |
||
354 | $type = ! empty( $response_body['type'] ) ? $response_body['type'] : 'api-error'; |
||
355 | |||
356 | if ( empty( $response_code ) ) { |
||
357 | // Translators: Support link tag starts with url and Support link tag ends. |
||
358 | $message = sprintf( |
||
359 | esc_html__( 'Oops! We encountered an error. Please wait a few minutes and try again. If the issue persists, please %1$scontact our support%2$s team.', 'google-analytics-for-wordpress' ), |
||
360 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'unknown-api-error', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
361 | '</a>' |
||
362 | ); |
||
363 | |||
364 | return new WP_Error( $type, $message ); |
||
365 | } |
||
366 | |||
367 | if ( empty( $response_body ) || ( empty( $response_body['message'] ) && empty( $response_body['error'] ) ) ) { |
||
368 | // Translators: Support link tag starts with url, Support link tag ends and placeholder adds the response code. |
||
369 | $message = sprintf( |
||
370 | esc_html__( 'Oops! We ran into a problem. Please try again in a few minutes. If the issue persists please %1$scontact our support%2$s team. Error: API returned a %3$s%4$s%5$s response.', 'google-analytics-for-wordpress' ), |
||
371 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'unknown-api-error', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
372 | '</a>', |
||
373 | '<strong>', |
||
374 | $response_code, |
||
375 | '</strong>' |
||
376 | ); |
||
377 | |||
378 | return new WP_Error( $type, $message ); |
||
379 | } |
||
380 | |||
381 | if ( ! empty( $response_body['message'] ) ) { |
||
382 | // Translators: Support link tag starts with url, Support link tag ends, placeholder adds the response code and response message. |
||
383 | $message = sprintf( |
||
384 | esc_html__( 'Oops! We ran into a problem. Please try again in a few minutes. If the issue persists please %1$scontact our support%2$s team. Error: API returned a %3$s%4$d: %5$s%6$s', 'google-analytics-for-wordpress' ), |
||
385 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'unknown-api-error', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
386 | '</a>', |
||
387 | '<strong>', |
||
388 | $response_code, |
||
389 | stripslashes( $response_body['message'] ), |
||
390 | '</strong>' |
||
391 | ); |
||
392 | |||
393 | return new WP_Error( $type, $message ); |
||
394 | } |
||
395 | |||
396 | if ( ! empty( $response_body['error'] ) ) { |
||
397 | // Translators: Support link tag starts with url, Support link tag ends, placeholder adds the response code and response message. |
||
398 | $message = sprintf( |
||
399 | esc_html__( 'Oops! We ran into a problem. Please try again in a few minutes. If the issue persists please %1$scontact our support%2$s team. Error: API returned a %3$s%4$d: %5$s%6$s', 'google-analytics-for-wordpress' ), |
||
400 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'unknown-api-error', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
401 | '</a>', |
||
402 | '<strong>', |
||
403 | $response_code, |
||
404 | stripslashes( $response_body['error'] ), |
||
405 | '</strong>' |
||
406 | ); |
||
407 | |||
408 | return new WP_Error( $type, $message ); |
||
409 | } |
||
410 | } |
||
411 | |||
412 | // If TT required |
||
413 | if ( ! empty( $this->tt ) ) { |
||
414 | if ( empty( $response_body['tt'] ) || ! hash_equals( $this->tt, $response_body['tt'] ) ) { |
||
415 | // TT isn't set on return or doesn't match |
||
416 | // Translators: Support link tag starts with url and Support link tag ends. |
||
417 | $message = sprintf( |
||
418 | esc_html__( 'Oops! We ran into a problem. Please try again in a few minutes. If the issue persists please %1$scontact our support%2$s team. Error: Improper API Request.', 'google-analytics-for-wordpress' ), |
||
419 | '<a target="_blank" href="' . monsterinsights_get_url( 'notice', 'cannot-verify-license', 'https://www.monsterinsights.com/my-account/support/' ) . '">', |
||
420 | '</a>' |
||
421 | ); |
||
422 | |||
423 | return new WP_Error( 'validation-error', $message ); |
||
424 | } |
||
425 | } |
||
426 | |||
427 | // Return the json decoded content. |
||
428 | return $response_body; |
||
429 | } |
||
538 |