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