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