| @@ -2,176 +2,181 @@ | ||
| 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 | -class API { | |
| 8 | +class API | |
| 9 | +{ | |
| 10 | + /** | |
| 11 | + * @var License | |
| 12 | + */ | |
| 13 | + protected $license; | |
| 10 | 14 | |
| 11 | - /** | |
| 12 | - * @var License | |
| 13 | - */ | |
| 14 | - protected $license; | |
| 15 | + /** | |
| 16 | + * The API url | |
| 17 | + * | |
| 18 | + * @var string | |
| 19 | + */ | |
| 20 | + public $url = ''; | |
| 15 | 21 | |
| 16 | - /** | |
| 17 | - * The API url | |
| 18 | - * | |
| 19 | - * @var string | |
| 20 | - */ | |
| 21 | - public $url = ''; | |
| 22 | + /** | |
| 23 | + * @var int | |
| 24 | + */ | |
| 25 | + protected $error_code = 0; | |
| 22 | 26 | |
| 23 | - /** | |
| 24 | - * @var int | |
| 25 | - */ | |
| 26 | - protected $error_code = 0; | |
| 27 | + /** | |
| 28 | + * @var string | |
| 29 | + */ | |
| 30 | + protected $error_message = ''; | |
| 27 | 31 | |
| 28 | - /** | |
| 29 | - * @var string | |
| 30 | - */ | |
| 31 | - protected $error_message = ''; | |
| 32 | + /** | |
| 33 | + * @var object|array | |
| 34 | + */ | |
| 35 | + protected $last_response; | |
| 32 | 36 | |
| 33 | - /** | |
| 34 | - * @var | |
| 35 | - */ | |
| 36 | - protected $last_response; | |
| 37 | + /** | |
| 38 | + * @param string $url | |
| 39 | + * @param License $license | |
| 40 | + */ | |
| 41 | + public function __construct($url, License $license) | |
| 42 | + { | |
| 43 | + $this->url = $url; | |
| 44 | + $this->license = $license; | |
| 45 | + } | |
| 37 | 46 | |
| 38 | - /** | |
| 39 | - * @param string $url | |
| 40 | - * @param License $license | |
| 41 | - */ | |
| 42 | - public function __construct( $url, License $license ) { | |
| 43 | - $this->url = $url; | |
| 44 | - $this->license = $license; | |
| 45 | - } | |
| 47 | + /** | |
| 48 | + * Gets license status | |
| 49 | + * | |
| 50 | + * @return object | |
| 51 | + */ | |
| 52 | + public function get_license() | |
| 53 | + { | |
| 54 | + $endpoint = '/license'; | |
| 55 | + $response = $this->request('GET', $endpoint); | |
| 56 | + return $response; | |
| 57 | + } | |
| 46 | 58 | |
| 47 | - /** | |
| 48 | - * Gets license status | |
| 49 | - * | |
| 50 | - * @return object | |
| 51 | - */ | |
| 52 | - public function get_license() { | |
| 53 | - $endpoint = '/license'; | |
| 54 | - $response = $this->request( 'GET', $endpoint ); | |
| 55 | - return $response; | |
| 56 | - } | |
| 57 | 59 | |
| 60 | + /** | |
| 61 | + * Logs the current site in to the remote API | |
| 62 | + * | |
| 63 | + * @return object | |
| 64 | + */ | |
| 65 | + public function activate_license() | |
| 66 | + { | |
| 67 | + $endpoint = '/license/activations'; | |
| 68 | + $args = [ | |
| 69 | + 'site_url' => get_option('siteurl'), | |
| 70 | + ]; | |
| 71 | + $response = $this->request('POST', $endpoint, $args); | |
| 72 | + return $response; | |
| 73 | + } | |
| 58 | 74 | |
| 59 | - /** | |
| 60 | - * Logs the current site in to the remote API | |
| 61 | - * | |
| 62 | - * @return object | |
| 63 | - */ | |
| 64 | - public function activate_license() { | |
| 65 | - $endpoint = '/license/activations'; | |
| 66 | - $args = array( | |
| 67 | - 'site_url' => get_option( 'siteurl' ) | |
| 68 | - ); | |
| 69 | - $response = $this->request( 'POST', $endpoint, $args ); | |
| 70 | - return $response; | |
| 71 | - } | |
| 75 | + /** | |
| 76 | + * Logs the current site out of the remote API | |
| 77 | + * | |
| 78 | + * @return object | |
| 79 | + */ | |
| 80 | + public function deactivate_license() | |
| 81 | + { | |
| 82 | + $endpoint = sprintf('/license/activations/%s', $this->license->activation_key); | |
| 83 | + $response = $this->request('DELETE', $endpoint); | |
| 84 | + return $response; | |
| 85 | + } | |
| 72 | 86 | |
| 73 | - /** | |
| 74 | - * Logs the current site out of the remote API | |
| 75 | - * | |
| 76 | - * @return object | |
| 77 | - */ | |
| 78 | - public function deactivate_license() { | |
| 79 | - $endpoint = sprintf( '/license/activations/%s', $this->license->activation_key ); | |
| 80 | - $response = $this->request( 'DELETE', $endpoint ); | |
| 81 | - return $response; | |
| 82 | - } | |
| 87 | + /** | |
| 88 | + * @param Plugin $plugin | |
| 89 | + * @return object | |
| 90 | + */ | |
| 91 | + public function get_plugin(Plugin $plugin) | |
| 92 | + { | |
| 93 | + $endpoint = sprintf('/plugins/%s?format=wp', $plugin->id()); | |
| 94 | + $response = $this->request('GET', $endpoint); | |
| 95 | + return $response; | |
| 96 | + } | |
| 83 | 97 | |
| 84 | - /** | |
| 85 | - * @param Plugin $plugin | |
| 86 | - * @return object | |
| 87 | - */ | |
| 88 | - public function get_plugin( Plugin $plugin ) { | |
| 89 | - $endpoint = sprintf( '/plugins/%s?format=wp', $plugin->id() ); | |
| 90 | - $response = $this->request( 'GET', $endpoint ); | |
| 91 | - return $response; | |
| 92 | - } | |
| 98 | + /** | |
| 99 | + * @param Plugin[] $plugins (optional) | |
| 100 | + * @return object | |
| 101 | + */ | |
| 102 | + public function get_plugins($plugins = null) | |
| 103 | + { | |
| 104 | + $args = [ | |
| 105 | + 'format' => 'wp', | |
| 106 | + ]; | |
| 93 | 107 | |
| 94 | - /** | |
| 95 | - * @param Plugin[] $plugins (optional) | |
| 96 | - * @return object | |
| 97 | - */ | |
| 98 | - public function get_plugins( $plugins = null ) { | |
| 108 | + $endpoint = add_query_arg($args, '/plugins'); | |
| 109 | + $response = $this->request('GET', $endpoint); | |
| 110 | + return $response; | |
| 111 | + } | |
| 99 | 112 | |
| 100 | - $args = array( | |
| 101 | - 'format' => 'wp', | |
| 102 | - ); | |
| 113 | + /** | |
| 114 | + * @param string $method | |
| 115 | + * @param string $endpoint | |
| 116 | + * @param array $data | |
| 117 | + * | |
| 118 | + * @return object|array | |
| 119 | + */ | |
| 120 | + public function request($method, $endpoint, $data = []) | |
| 121 | + { | |
| 122 | + $url = $this->url . $endpoint; | |
| 123 | + $args = [ | |
| 124 | + 'method' => $method, | |
| 125 | + 'headers' => [ | |
| 126 | + 'Content-Type' => 'application/json', | |
| 127 | + 'Accept' => 'application/json', | |
| 128 | + ], | |
| 129 | + 'timeout' => 10, | |
| 130 | + ]; | |
| 103 | 131 | |
| 104 | - $endpoint = add_query_arg( $args, '/plugins' ); | |
| 105 | - $response = $this->request( 'GET', $endpoint ); | |
| 106 | - return $response; | |
| 107 | - } | |
| 132 | + // add license key to headers if set | |
| 133 | + if (! empty($this->license->key)) { | |
| 134 | + $args['headers']['Authorization'] = 'Bearer ' . urlencode($this->license->key); | |
| 135 | + } | |
| 108 | 136 | |
| 109 | - /** | |
| 110 | - * @param string $method | |
| 111 | - * @param string $endpoint | |
| 112 | - * @param array $data | |
| 113 | - * | |
| 114 | - * @return object|array | |
| 115 | - */ | |
| 116 | - public function request( $method, $endpoint, $data = array() ) { | |
| 137 | + if (! empty($data)) { | |
| 138 | + if (in_array($method, [ 'GET', 'DELETE' ], true)) { | |
| 139 | + $url = add_query_arg($data, $url); | |
| 140 | + } else { | |
| 141 | + $args['body'] = json_encode($data); | |
| 142 | + } | |
| 143 | + } | |
| 117 | 144 | |
| 118 | - $url = $this->url . $endpoint; | |
| 119 | - $args = array( | |
| 120 | - 'method' => $method, | |
| 121 | - 'headers' => array( | |
| 122 | - 'Content-Type' => 'application/json', | |
| 123 | - 'Accepts' => 'application/json', | |
| 124 | - ), | |
| 125 | - ); | |
| 145 | + $response = wp_remote_request($url, $args); | |
| 146 | + return $this->parse_response($response); | |
| 147 | + } | |
| 126 | 148 | |
| 127 | - // add license key to headers if set | |
| 128 | - if( ! empty( $this->license->key ) ) { | |
| 129 | - $args['headers']['Authorization'] = 'Bearer ' . urlencode( $this->license->key ); | |
| 130 | - } | |
| 149 | + /** | |
| 150 | + * @param mixed $response | |
| 151 | + * | |
| 152 | + * @return object|null | |
| 153 | + * | |
| 154 | + * @throws API_Exception | |
| 155 | + */ | |
| 156 | + public function parse_response($response) | |
| 157 | + { | |
| 158 | + // test for wp errors (request failures) | |
| 159 | + if ($response instanceof WP_Error) { | |
| 160 | + throw new API_Exception(esc_html($response->get_error_message())); | |
| 161 | + } | |
| 131 | 162 | |
| 132 | - if( ! empty( $data ) ) { | |
| 133 | - if( in_array( $method, array( 'GET', 'DELETE' ) ) ) { | |
| 134 | - $url = add_query_arg( $data, $url ); | |
| 135 | - } else { | |
| 136 | - $args['body'] = json_encode( $data ); | |
| 137 | - } | |
| 138 | - } | |
| 163 | + // retrieve response body | |
| 164 | + $body = wp_remote_retrieve_body($response); | |
| 165 | + if (empty($body)) { | |
| 166 | + return null; | |
| 167 | + } | |
| 139 | 168 | |
| 140 | - $response = wp_remote_request( $url, $args ); | |
| 141 | - return $this->parse_response( $response ); | |
| 142 | - } | |
| 169 | + $json = json_decode($body, false); | |
| 170 | + if ($json === null) { | |
| 171 | + throw new API_Exception(esc_html__('The Boxzilla server returned an invalid response.', 'boxzilla')); | |
| 172 | + } | |
| 143 | 173 | |
| 144 | - /** | |
| 145 | - * @param mixed $response | |
| 146 | - * | |
| 147 | - * @return object|null | |
| 148 | - * | |
| 149 | - * @throws API_Exception | |
| 150 | - */ | |
| 151 | - public function parse_response( $response ) { | |
| 152 | - // test for wp errors (request failures) | |
| 153 | - if( $response instanceof WP_Error) { | |
| 154 | - throw new API_Exception( $response->get_error_message() ); | |
| 155 | - } | |
| 174 | + // did request return an error response? | |
| 175 | + if (wp_remote_retrieve_response_code($response) >= 400) { | |
| 176 | + throw new API_Exception(esc_html((string) $json->message), absint($json->code)); | |
| 177 | + } | |
| 156 | 178 | |
| 157 | - // retrieve response body | |
| 158 | - $body = wp_remote_retrieve_body( $response ); | |
| 159 | - if( empty( $body) ) { | |
| 160 | - return null; | |
| 161 | - } | |
| 162 | - | |
| 163 | - $json = json_decode( $body, false ); | |
| 164 | - if( is_null( $json ) ) { | |
| 165 | - throw new API_Exception( __( "The Boxzilla server returned an invalid response.", 'boxzilla' ) ); | |
| 166 | - } | |
| 167 | - | |
| 168 | - // did request return an error response? | |
| 169 | - if( wp_remote_retrieve_response_code( $response ) >= 400 ) { | |
| 170 | - throw new API_Exception( $json->message, $json->code ); | |
| 171 | - } | |
| 172 | - | |
| 173 | - // return actual response data | |
| 174 | - return $json; | |
| 175 | - } | |
| 176 | - | |
| 179 | + // return actual response data | |
| 180 | + return $json; | |
| 181 | + } | |
| 177 | 182 | } |