| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* This class will fetch a new status from Ryte and if it's necessary it will |
| 10 |
* notify the site admin by email and remove the current meta value to hide the |
| 11 |
* notice for all admin users. |
| 12 |
*/ |
| 13 |
class WPSEO_Ryte_Request { |
| 14 |
|
| 15 |
/** |
| 16 |
* The endpoint where the request will be send to. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
private $ryte_endpoint = 'https://indexability.yoast.onpage.org/'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Gets the response from the Ryte API and returns the body. |
| 24 |
* |
| 25 |
* @param string $target_url The URL to check indexability for. |
| 26 |
* @param array $parameters Array of extra parameters to send to the Ryte API. |
| 27 |
* |
| 28 |
* @return array The successful response or the error details. |
| 29 |
*/ |
| 30 |
protected function get_remote( $target_url, $parameters = [] ) { |
| 31 |
$defaults = [ |
| 32 |
'url' => $target_url, |
| 33 |
'wp_version' => $GLOBALS['wp_version'], |
| 34 |
'yseo_version' => WPSEO_VERSION, |
| 35 |
]; |
| 36 |
|
| 37 |
$parameters = array_merge( $defaults, $parameters ); |
| 38 |
$url = add_query_arg( $parameters, $this->ryte_endpoint ); |
| 39 |
|
| 40 |
// Decrease a chance of "cURL 28 (timeout)" errors by increasing the timeout to 10 seconds. |
| 41 |
$args = [ 'timeout' => 10 ]; |
| 42 |
$response = wp_remote_get( $url, $args ); |
| 43 |
|
| 44 |
return $this->process_response( $response ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Sends a request to the Ryte API to check whether a URL is indexable. |
| 49 |
* |
| 50 |
* @param string $target_url The URL to check indexability for. |
| 51 |
* @param array $parameters Array of extra parameters to send to the Ryte API. |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public function do_request( $target_url, $parameters = [] ) { |
| 56 |
$json_body = $this->get_remote( $target_url, $parameters ); |
| 57 |
|
| 58 |
// Ryte recognized a redirect, fetch the data of that URL by calling this method with the value from Ryte. |
| 59 |
if ( ! empty( $json_body['passes_juice_to'] ) ) { |
| 60 |
return $this->do_request( $json_body['passes_juice_to'], $parameters ); |
| 61 |
} |
| 62 |
|
| 63 |
return $json_body; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Processes the given Ryte response. |
| 68 |
* |
| 69 |
* @param array|WP_Error $response The response or WP_Error to process. |
| 70 |
* |
| 71 |
* @return array The response body or the error detaiils on failure. |
| 72 |
*/ |
| 73 |
protected function process_response( $response ) { |
| 74 |
// Most of the potential errors are WP_Error(s). |
| 75 |
if ( is_wp_error( $response ) ) { |
| 76 |
return [ |
| 77 |
'is_error' => true, |
| 78 |
'raw_error_code' => '', |
| 79 |
// WP_Error codes aren't that helpful for users, let's display them in a less prominent way. |
| 80 |
'wp_error_code' => '(' . $response->get_error_code() . ')', |
| 81 |
'message' => $response->get_error_message(), |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* As of February 2020 the Ryte API returns an error 500 for non-reachable |
| 87 |
* sites. There's also the need to handle any potential raw HTTP error. |
| 88 |
*/ |
| 89 |
if ( wp_remote_retrieve_response_code( $response ) !== 200 ) { |
| 90 |
// Not all HTTP errors may have a response message. Let's provide a default one. |
| 91 |
$response_message = wp_remote_retrieve_response_message( $response ); |
| 92 |
$message = ( $response_message ) ? $response_message : __( 'The request to Ryte returned an error.', 'wordpress-seo' ); |
| 93 |
|
| 94 |
return [ |
| 95 |
'is_error' => true, |
| 96 |
'raw_error_code' => wp_remote_retrieve_response_code( $response ), |
| 97 |
'wp_error_code' => '', |
| 98 |
'message' => $message, |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
// When the request is successful, the response code will be 200. |
| 103 |
$response_body = wp_remote_retrieve_body( $response ); |
| 104 |
|
| 105 |
return json_decode( $response_body, true ); |
| 106 |
} |
| 107 |
} |
| 108 |
|