| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | use WP_Error; |
| 8 | 8 | |
| 9 | 9 | class API { |
| 10 | 10 | |
| 11 | + | |
| 11 | 12 | /** |
| 12 | 13 | * @var License |
| 13 | 14 | */ |
| 14 | 15 | protected $license; |
| @@ -36,12 +37,12 @@ | ||
| 36 | 37 | protected $last_response; |
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | 40 | * @param string $url |
| 40 | - * @param License $license | |
| 41 | + * @param License $license | |
| 41 | 42 | */ |
| 42 | 43 | public function __construct( $url, License $license ) { |
| 43 | - $this->url = $url; | |
| 44 | + $this->url = $url; | |
| 44 | 45 | $this->license = $license; |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | 48 | /** |
| @@ -62,10 +63,10 @@ | ||
| 62 | 63 | * @return object |
| 63 | 64 | */ |
| 64 | 65 | public function activate_license() { |
| 65 | 66 | $endpoint = '/license/activations'; |
| 66 | - $args = array( | |
| 67 | - 'site_url' => get_option( 'siteurl' ) | |
| 67 | + $args = array( | |
| 68 | + 'site_url' => get_option( 'siteurl' ), | |
| 68 | 69 | ); |
| 69 | 70 | $response = $this->request( 'POST', $endpoint, $args ); |
| 70 | 71 | return $response; |
| 71 | 72 | } |
| @@ -91,13 +92,12 @@ | ||
| 91 | 92 | return $response; |
| 92 | 93 | } |
| 93 | 94 | |
| 94 | 95 | /** |
| 95 | - * @param Plugin[] $plugins (optional) | |
| 96 | + * @param Plugin[] $plugins (optional) | |
| 96 | 97 | * @return object |
| 97 | 98 | */ |
| 98 | 99 | public function get_plugins( $plugins = null ) { |
| 99 | - | |
| 100 | 100 | $args = array( |
| 101 | 101 | 'format' => 'wp', |
| 102 | 102 | ); |
| 103 | 103 | |
| @@ -113,25 +113,24 @@ | ||
| 113 | 113 | * |
| 114 | 114 | * @return object|array |
| 115 | 115 | */ |
| 116 | 116 | public function request( $method, $endpoint, $data = array() ) { |
| 117 | - | |
| 118 | - $url = $this->url . $endpoint; | |
| 117 | + $url = $this->url . $endpoint; | |
| 119 | 118 | $args = array( |
| 120 | - 'method' => $method, | |
| 119 | + 'method' => $method, | |
| 121 | 120 | 'headers' => array( |
| 122 | 121 | 'Content-Type' => 'application/json', |
| 123 | - 'Accepts' => 'application/json', | |
| 122 | + 'Accepts' => 'application/json', | |
| 124 | 123 | ), |
| 125 | 124 | ); |
| 126 | 125 | |
| 127 | 126 | // add license key to headers if set |
| 128 | - if( ! empty( $this->license->key ) ) { | |
| 127 | + if ( ! empty( $this->license->key ) ) { | |
| 129 | 128 | $args['headers']['Authorization'] = 'Bearer ' . urlencode( $this->license->key ); |
| 130 | 129 | } |
| 131 | 130 | |
| 132 | - if( ! empty( $data ) ) { | |
| 133 | - if( in_array( $method, array( 'GET', 'DELETE' ) ) ) { | |
| 131 | + if ( ! empty( $data ) ) { | |
| 132 | + if ( in_array( $method, array( 'GET', 'DELETE' ), true ) ) { | |
| 134 | 133 | $url = add_query_arg( $data, $url ); |
| 135 | 134 | } else { |
| 136 | 135 | $args['body'] = json_encode( $data ); |
| 137 | 136 | } |
| @@ -149,25 +148,25 @@ | ||
| 149 | 148 | * @throws API_Exception |
| 150 | 149 | */ |
| 151 | 150 | public function parse_response( $response ) { |
| 152 | 151 | // test for wp errors (request failures) |
| 153 | - if( $response instanceof WP_Error) { | |
| 152 | + if ( $response instanceof WP_Error ) { | |
| 154 | 153 | throw new API_Exception( $response->get_error_message() ); |
| 155 | 154 | } |
| 156 | 155 | |
| 157 | 156 | // retrieve response body |
| 158 | 157 | $body = wp_remote_retrieve_body( $response ); |
| 159 | - if( empty( $body) ) { | |
| 158 | + if ( empty( $body ) ) { | |
| 160 | 159 | return null; |
| 161 | 160 | } |
| 162 | 161 | |
| 163 | 162 | $json = json_decode( $body, false ); |
| 164 | - if( is_null( $json ) ) { | |
| 165 | - throw new API_Exception( __( "The Boxzilla server returned an invalid response.", 'boxzilla' ) ); | |
| 163 | + if ( is_null( $json ) ) { | |
| 164 | + throw new API_Exception( __( 'The Boxzilla server returned an invalid response.', 'boxzilla' ) ); | |
| 166 | 165 | } |
| 167 | 166 | |
| 168 | 167 | // did request return an error response? |
| 169 | - if( wp_remote_retrieve_response_code( $response ) >= 400 ) { | |
| 168 | + if ( wp_remote_retrieve_response_code( $response ) >= 400 ) { | |
| 170 | 169 | throw new API_Exception( $json->message, $json->code ); |
| 171 | 170 | } |
| 172 | 171 | |
| 173 | 172 | // return actual response data |
| @@ -172,6 +171,5 @@ | ||
| 172 | 171 | |
| 173 | 172 | // return actual response data |
| 174 | 173 | return $json; |
| 175 | 174 | } |
| 176 | - | |
| 177 | 175 | } |