| @@ -100,8 +100,14 @@ | ||
| 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 | + | |
| 104 | 110 | $endpoint = add_query_arg( $args, '/plugins' ); |
| 105 | 111 | $response = $this->request( 'GET', $endpoint ); |
| 106 | 112 | return $response; |
| 107 | 113 | } |
| @@ -117,12 +123,9 @@ | ||
| 117 | 123 | |
| 118 | 124 | $url = $this->url . $endpoint; |
| 119 | 125 | $args = array( |
| 120 | 126 | 'method' => $method, |
| 121 | - 'headers' => array( | |
| 122 | - 'Content-Type' => 'application/json', | |
| 123 | - 'Accepts' => 'application/json', | |
| 124 | - ), | |
| 127 | + 'headers' => array(), | |
| 125 | 128 | ); |
| 126 | 129 | |
| 127 | 130 | // add license key to headers if set |
| 128 | 131 | if( ! empty( $this->license->key ) ) { |
| @@ -128,15 +131,13 @@ | ||
| 128 | 131 | if( ! empty( $this->license->key ) ) { |
| 129 | 132 | $args['headers']['Authorization'] = 'Bearer ' . urlencode( $this->license->key ); |
| 130 | 133 | } |
| 131 | 134 | |
| 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 | - } | |
| 135 | + if( in_array( $method, array( 'GET', 'DELETE' ) ) ) { | |
| 136 | + $url = add_query_arg( $data, $url ); | |
| 137 | + } else { | |
| 138 | + $args['body'] = $data; | |
| 139 | + } | |
| 139 | 140 | |
| 140 | 141 | $response = wp_remote_request( $url, $args ); |
| 141 | 142 | return $this->parse_response( $response ); |
| 142 | 143 | } |
| @@ -148,30 +149,26 @@ | ||
| 148 | 149 | * |
| 149 | 150 | * @throws API_Exception |
| 150 | 151 | */ |
| 151 | 152 | public function parse_response( $response ) { |
| 152 | - // test for wp errors (request failures) | |
| 153 | - if( $response instanceof WP_Error) { | |
| 153 | + // test for wp errors | |
| 154 | + if( $response instanceof WP_Error) { | |
| 154 | 155 | throw new API_Exception( $response->get_error_message() ); |
| 155 | 156 | } |
| 156 | 157 | |
| 157 | 158 | // retrieve response body |
| 158 | 159 | $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 ) ) { | |
| 160 | + $json = json_decode( $body ); | |
| 161 | + if( ! is_object( $json ) ) { | |
| 165 | 162 | throw new API_Exception( __( "The Boxzilla server returned an invalid response.", 'boxzilla' ) ); |
| 166 | 163 | } |
| 167 | 164 | |
| 168 | 165 | // 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 | - } | |
| 166 | + if( isset( $json->error ) ) { | |
| 167 | + throw new API_Exception( $json->error->message, $json->error->code ); | |
| 168 | + } | |
| 172 | 169 | |
| 173 | 170 | // return actual response data |
| 174 | - return $json; | |
| 171 | + return $json->data; | |
| 175 | 172 | } |
| 176 | 173 | |
| 177 | -} | |
| 174 | +} | |