| @@ -2,181 +2,176 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Boxzilla\Licensing; |
| 4 | 4 | |
| 5 | 5 | use Boxzilla\Plugin; |
| 6 | +use Exception; | |
| 6 | 7 | use WP_Error; |
| 7 | 8 | |
| 8 | -class API | |
| 9 | -{ | |
| 10 | - /** | |
| 11 | - * @var License | |
| 12 | - */ | |
| 13 | - protected $license; | |
| 9 | +class API { | |
| 14 | 10 | |
| 15 | - /** | |
| 16 | - * The API url | |
| 17 | - * | |
| 18 | - * @var string | |
| 19 | - */ | |
| 20 | - public $url = ''; | |
| 11 | + /** | |
| 12 | + * @var License | |
| 13 | + */ | |
| 14 | + protected $license; | |
| 21 | 15 | |
| 22 | - /** | |
| 23 | - * @var int | |
| 24 | - */ | |
| 25 | - protected $error_code = 0; | |
| 16 | + /** | |
| 17 | + * The API url | |
| 18 | + * | |
| 19 | + * @var string | |
| 20 | + */ | |
| 21 | + public $url = ''; | |
| 26 | 22 | |
| 27 | - /** | |
| 28 | - * @var string | |
| 29 | - */ | |
| 30 | - protected $error_message = ''; | |
| 23 | + /** | |
| 24 | + * @var int | |
| 25 | + */ | |
| 26 | + protected $error_code = 0; | |
| 31 | 27 | |
| 32 | - /** | |
| 33 | - * @var object|array | |
| 34 | - */ | |
| 35 | - protected $last_response; | |
| 28 | + /** | |
| 29 | + * @var string | |
| 30 | + */ | |
| 31 | + protected $error_message = ''; | |
| 36 | 32 | |
| 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 | - } | |
| 33 | + /** | |
| 34 | + * @var | |
| 35 | + */ | |
| 36 | + protected $last_response; | |
| 46 | 37 | |
| 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 | - } | |
| 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 | + } | |
| 58 | 46 | |
| 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 | + } | |
| 59 | 57 | |
| 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 | - } | |
| 74 | 58 | |
| 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 | - } | |
| 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 | + } | |
| 86 | 72 | |
| 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 | - } | |
| 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 | + } | |
| 97 | 83 | |
| 98 | - /** | |
| 99 | - * @param Plugin[] $plugins (optional) | |
| 100 | - * @return object | |
| 101 | - */ | |
| 102 | - public function get_plugins($plugins = null) | |
| 103 | - { | |
| 104 | - $args = [ | |
| 105 | - 'format' => 'wp', | |
| 106 | - ]; | |
| 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 | + } | |
| 107 | 93 | |
| 108 | - $endpoint = add_query_arg($args, '/plugins'); | |
| 109 | - $response = $this->request('GET', $endpoint); | |
| 110 | - return $response; | |
| 111 | - } | |
| 94 | + /** | |
| 95 | + * @param Plugin[] $plugins (optional) | |
| 96 | + * @return object | |
| 97 | + */ | |
| 98 | + public function get_plugins( $plugins = null ) { | |
| 112 | 99 | |
| 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 | - ]; | |
| 100 | + $args = array( | |
| 101 | + 'format' => 'wp', | |
| 102 | + ); | |
| 131 | 103 | |
| 132 | - // add license key to headers if set | |
| 133 | - if (! empty($this->license->key)) { | |
| 134 | - $args['headers']['Authorization'] = 'Bearer ' . urlencode($this->license->key); | |
| 135 | - } | |
| 104 | + $endpoint = add_query_arg( $args, '/plugins' ); | |
| 105 | + $response = $this->request( 'GET', $endpoint ); | |
| 106 | + return $response; | |
| 107 | + } | |
| 136 | 108 | |
| 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 | - } | |
| 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() ) { | |
| 144 | 117 | |
| 145 | - $response = wp_remote_request($url, $args); | |
| 146 | - return $this->parse_response($response); | |
| 147 | - } | |
| 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 | + ); | |
| 148 | 126 | |
| 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 | - } | |
| 127 | + // add license key to headers if set | |
| 128 | + if( ! empty( $this->license->key ) ) { | |
| 129 | + $args['headers']['Authorization'] = 'Bearer ' . urlencode( $this->license->key ); | |
| 130 | + } | |
| 162 | 131 | |
| 163 | - // retrieve response body | |
| 164 | - $body = wp_remote_retrieve_body($response); | |
| 165 | - if (empty($body)) { | |
| 166 | - return null; | |
| 167 | - } | |
| 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 | + } | |
| 168 | 139 | |
| 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 | - } | |
| 140 | + $response = wp_remote_request( $url, $args ); | |
| 141 | + return $this->parse_response( $response ); | |
| 142 | + } | |
| 173 | 143 | |
| 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 | - } | |
| 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 | + } | |
| 178 | 156 | |
| 179 | - // return actual response data | |
| 180 | - return $json; | |
| 181 | - } | |
| 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 | + | |
| 182 | 177 | } |