MI_API_Request

Handles requests to the Yoast EDD API.

Defined (1)

The class is defined in the following location(s).

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