class-remote-request.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * This class handles a post request being send to a given endpoint.
  9. */
  10. class WPSEO_Remote_Request {
  11. const METHOD_POST = 'post';
  12. const METHOD_GET = 'get';
  13. /** @var string */
  14. protected $endpoint = '';
  15. /** @var array */
  16. protected $args = array(
  17. 'blocking' => false,
  18. 'sslverify' => false,
  19. 'timeout' => 2,
  20. );
  21. /** @var WP_Error|null */
  22. protected $response_error;
  23. /** @var mixed */
  24. protected $response_body;
  25. /**
  26. * Sets the endpoint and arguments.
  27. *
  28. * @param string $endpoint The endpoint to send the request to.
  29. * @param array $args The arguments to use in this request.
  30. */
  31. public function __construct( $endpoint, array $args = array() ) {
  32. $this->endpoint = $endpoint;
  33. $this->args = wp_parse_args( $this->args, $args );
  34. }
  35. /**
  36. * Sets the request body.
  37. *
  38. * @param mixed $body The body to set.
  39. */
  40. public function set_body( $body ) {
  41. $this->args['body'] = $body;
  42. }
  43. /**
  44. * Sends the data to the given endpoint.
  45. *
  46. * @param string $method The type of request to send.
  47. *
  48. * @return bool True when sending data has been successful.
  49. */
  50. public function send( $method = self::METHOD_POST ) {
  51. switch ( $method ) {
  52. case self::METHOD_POST:
  53. $response = $this->post();
  54. break;
  55. case self::METHOD_GET:
  56. $response = $this->get();
  57. break;
  58. default:
  59. /* translators: %1$s expands to the request method */
  60. $response = new WP_Error( 1, sprintf( __( 'Request method %1$s is not valid.', 'wordpress-seo' ), $method ) );
  61. break;
  62. }
  63. return $this->process_response( $response );
  64. }
  65. /**
  66. * Returns the value of the response error.
  67. *
  68. * @return null|WP_Error The response error.
  69. */
  70. public function get_response_error() {
  71. return $this->response_error;
  72. }
  73. /**
  74. * Returns the response body.
  75. *
  76. * @return mixed The response body.
  77. */
  78. public function get_response_body() {
  79. return $this->response_body;
  80. }
  81. /**
  82. * Processes the given response.
  83. *
  84. * @param mixed $response The response to process.
  85. *
  86. * @return bool True when response is valid.
  87. */
  88. protected function process_response( $response ) {
  89. if ( $response instanceof WP_Error ) {
  90. $this->response_error = $response;
  91. return false;
  92. }
  93. $this->response_body = wp_remote_retrieve_body( $response );
  94. return ( wp_remote_retrieve_response_code( $response ) === 200 );
  95. }
  96. /**
  97. * Performs a post request to the specified endpoint with set arguments.
  98. *
  99. * @return WP_Error|array The response or WP_Error on failure.
  100. */
  101. protected function post() {
  102. return wp_remote_post( $this->endpoint, $this->args );
  103. }
  104. /**
  105. * Performs a post request to the specified endpoint with set arguments.
  106. *
  107. * @return WP_Error|array The response or WP_Error on failure.
  108. */
  109. protected function get() {
  110. return wp_remote_get( $this->endpoint, $this->args );
  111. }
  112. }