class-onpage-request.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. * @var string The endpoint where the request will be send to.
  15. */
  16. private $onpage_endpoint = 'https://indexability.yoast.onpage.org/';
  17. /**
  18. * Doing the remote get and returns the body
  19. *
  20. * @param string $target_url The home url.
  21. * @param array $parameters Array of extra parameters to send to Ryte.
  22. *
  23. * @return array
  24. * @throws Exception The error message that can be used to show to the user.
  25. */
  26. protected function get_remote( $target_url, $parameters = array() ) {
  27. $parameters = array_merge( array(
  28. 'url' => $target_url,
  29. 'wp_version' => $GLOBALS['wp_version'],
  30. 'yseo_version' => WPSEO_VERSION,
  31. ), $parameters );
  32. $url = add_query_arg( $parameters, $this->onpage_endpoint );
  33. $response = wp_remote_get( $url );
  34. $response_code = wp_remote_retrieve_response_code( $response );
  35. // When the request is successful, the response code will be 200.
  36. if ( $response_code === 200 ) {
  37. $response_body = wp_remote_retrieve_body( $response );
  38. return json_decode( $response_body, true );
  39. }
  40. }
  41. /**
  42. * Sending a request to Ryte to check if the $home_url is indexable.
  43. *
  44. * @param string $target_url The URL that will be send to the API.
  45. * @param array $parameters Array of extra parameters to send to Ryte.
  46. *
  47. * @return array
  48. */
  49. public function do_request( $target_url, $parameters = array() ) {
  50. $json_body = $this->get_remote( $target_url, $parameters );
  51. // Ryte recognized a redirect, fetch the data of that URL by calling this method with the value from Ryte.
  52. if ( ! empty( $json_body['passes_juice_to'] ) ) {
  53. return $this->do_request( $json_body['passes_juice_to'], $parameters );
  54. }
  55. return $json_body;
  56. }
  57. }