class-remote-request.php 3.1 KB

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