| @@ -2,9 +2,8 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Boxzilla\Licensing; |
| 4 | 4 | |
| 5 | 5 | use Boxzilla\Plugin; |
| 6 | -use Exception; | |
| 7 | 6 | use WP_Error; |
| 8 | 7 | |
| 9 | 8 | class API |
| 10 | 9 | { |
| @@ -30,9 +29,9 @@ | ||
| 30 | 29 | */ |
| 31 | 30 | protected $error_message = ''; |
| 32 | 31 | |
| 33 | 32 | /** |
| 34 | - * @var | |
| 33 | + * @var object|array | |
| 35 | 34 | */ |
| 36 | 35 | protected $last_response; |
| 37 | 36 | |
| 38 | 37 | /** |
| @@ -124,9 +123,9 @@ | ||
| 124 | 123 | $args = [ |
| 125 | 124 | 'method' => $method, |
| 126 | 125 | 'headers' => [ |
| 127 | 126 | 'Content-Type' => 'application/json', |
| 128 | - 'Accepts' => 'application/json', | |
| 127 | + 'Accept' => 'application/json', | |
| 129 | 128 | ], |
| 130 | 129 | 'timeout' => 10, |
| 131 | 130 | ]; |
| 132 | 131 | |
| @@ -157,9 +156,9 @@ | ||
| 157 | 156 | public function parse_response($response) |
| 158 | 157 | { |
| 159 | 158 | // test for wp errors (request failures) |
| 160 | 159 | if ($response instanceof WP_Error) { |
| 161 | - throw new API_Exception($response->get_error_message()); | |
| 160 | + throw new API_Exception(esc_html($response->get_error_message())); | |
| 162 | 161 | } |
| 163 | 162 | |
| 164 | 163 | // retrieve response body |
| 165 | 164 | $body = wp_remote_retrieve_body($response); |
| @@ -167,15 +166,15 @@ | ||
| 167 | 166 | return null; |
| 168 | 167 | } |
| 169 | 168 | |
| 170 | 169 | $json = json_decode($body, false); |
| 171 | - if (is_null($json)) { | |
| 170 | + if ($json === null) { | |
| 172 | 171 | throw new API_Exception(esc_html__('The Boxzilla server returned an invalid response.', 'boxzilla')); |
| 173 | 172 | } |
| 174 | 173 | |
| 175 | 174 | // did request return an error response? |
| 176 | 175 | if (wp_remote_retrieve_response_code($response) >= 400) { |
| 177 | - throw new API_Exception($json->message, $json->code); | |
| 176 | + throw new API_Exception(esc_html((string) $json->message), absint($json->code)); | |
| 178 | 177 | } |
| 179 | 178 | |
| 180 | 179 | // return actual response data |
| 181 | 180 | return $json; |