class-onpage-request.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * This class will fetch a new status from Ryte and if it's necessary it will
  9. * notify the site admin by email and remove the current meta value to hide the
  10. * notice for all admin users.
  11. */
  12. class WPSEO_OnPage_Request {
  13. /**
  14. * The endpoint where the request will be send to.
  15. *
  16. * @var string
  17. */
  18. private $onpage_endpoint = 'https://indexability.yoast.onpage.org/';
  19. /**
  20. * Doing the remote get and returns the body.
  21. *
  22. * @param string $target_url The home url.
  23. * @param array $parameters Array of extra parameters to send to Ryte.
  24. *
  25. * @return array
  26. * @throws Exception The error message that can be used to show to the user.
  27. */
  28. protected function get_remote( $target_url, $parameters = [] ) {
  29. $defaults = [
  30. 'url' => $target_url,
  31. 'wp_version' => $GLOBALS['wp_version'],
  32. 'yseo_version' => WPSEO_VERSION,
  33. ];
  34. $parameters = array_merge( $defaults, $parameters );
  35. $url = add_query_arg( $parameters, $this->onpage_endpoint );
  36. $response = wp_remote_get( $url );
  37. $response_code = wp_remote_retrieve_response_code( $response );
  38. // When the request is successful, the response code will be 200.
  39. if ( $response_code === 200 ) {
  40. $response_body = wp_remote_retrieve_body( $response );
  41. return json_decode( $response_body, true );
  42. }
  43. }
  44. /**
  45. * Sending a request to Ryte to check if the $home_url is indexable.
  46. *
  47. * @param string $target_url The URL that will be send to the API.
  48. * @param array $parameters Array of extra parameters to send to Ryte.
  49. *
  50. * @return array
  51. */
  52. public function do_request( $target_url, $parameters = [] ) {
  53. $json_body = $this->get_remote( $target_url, $parameters );
  54. // Ryte recognized a redirect, fetch the data of that URL by calling this method with the value from Ryte.
  55. if ( ! empty( $json_body['passes_juice_to'] ) ) {
  56. return $this->do_request( $json_body['passes_juice_to'], $parameters );
  57. }
  58. return $json_body;
  59. }
  60. }