class-api-request.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. if ( ! class_exists( "Yoast_API_Request", false ) ) {
  3. /**
  4. * Handles requests to the Yoast EDD API
  5. */
  6. class Yoast_API_Request {
  7. /**
  8. * @var string Request URL
  9. */
  10. private $url = '';
  11. /**
  12. * @var array Request parameters
  13. */
  14. private $args = array(
  15. 'method' => 'GET',
  16. 'timeout' => 10,
  17. 'sslverify' => false,
  18. 'headers' => array(
  19. 'Accept-Encoding' => '*',
  20. 'X-Yoast-EDD' => '1'
  21. )
  22. );
  23. /**
  24. * @var boolean
  25. */
  26. private $success = false;
  27. /**
  28. * @var mixed
  29. */
  30. private $response;
  31. /**
  32. * @var string
  33. */
  34. private $error_message = '';
  35. /**
  36. * Constructor
  37. *
  38. * @param string url
  39. * @param array $args
  40. */
  41. public function __construct( $url, array $args = array() ) {
  42. // set api url
  43. $this->url = $url;
  44. // set request args (merge with defaults)
  45. $this->args = wp_parse_args( $args, $this->args );
  46. // fire the request
  47. $this->success = $this->fire();
  48. }
  49. /**
  50. * Fires the request, automatically called from constructor
  51. *
  52. * @return boolean
  53. */
  54. private function fire() {
  55. // fire request to shop
  56. $response = wp_remote_request( $this->url, $this->args );
  57. // validate raw response
  58. if( $this->validate_raw_response( $response ) === false ) {
  59. return false;
  60. }
  61. // decode the response
  62. $this->response = json_decode( wp_remote_retrieve_body( $response ) );
  63. // response should be an object
  64. if( ! is_object( $this->response ) ) {
  65. $this->error_message = 'No JSON object was returned.';
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * @param object $response
  72. * @return boolean
  73. */
  74. private function validate_raw_response( $response ) {
  75. // make sure response came back okay
  76. if( is_wp_error( $response ) ) {
  77. $this->error_message = $response->get_error_message();
  78. return false;
  79. }
  80. // check response code, should be 200
  81. $response_code = wp_remote_retrieve_response_code( $response );
  82. if( false === strstr( $response_code, '200' ) ) {
  83. $response_message = wp_remote_retrieve_response_message( $response );
  84. $this->error_message = "{$response_code} {$response_message}";
  85. return false;
  86. }
  87. return true;
  88. }
  89. /**
  90. * Was a valid response returned?
  91. *
  92. * @return boolean
  93. */
  94. public function is_valid() {
  95. return ( $this->success === true );
  96. }
  97. /**
  98. * @return string
  99. */
  100. public function get_error_message() {
  101. return $this->error_message;
  102. }
  103. /**
  104. * @return object
  105. */
  106. public function get_response() {
  107. return $this->response;
  108. }
  109. }
  110. }