| @@ -7,49 +7,50 @@ | ||
| 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 | - * The API url | |
| 18 | - * | |
| 19 | - * @var string | |
| 20 | - */ | |
| 18 | + * The API url | |
| 19 | + * | |
| 20 | + * @var string | |
| 21 | + */ | |
| 21 | 22 | public $url = ''; |
| 22 | 23 | |
| 23 | 24 | /** |
| 24 | - * @var int | |
| 25 | - */ | |
| 25 | + * @var int | |
| 26 | + */ | |
| 26 | 27 | protected $error_code = 0; |
| 27 | 28 | |
| 28 | 29 | /** |
| 29 | - * @var string | |
| 30 | - */ | |
| 30 | + * @var string | |
| 31 | + */ | |
| 31 | 32 | protected $error_message = ''; |
| 32 | 33 | |
| 33 | 34 | /** |
| 34 | - * @var | |
| 35 | - */ | |
| 35 | + * @var | |
| 36 | + */ | |
| 36 | 37 | protected $last_response; |
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | - * @param string $url | |
| 40 | - * @param License $license | |
| 41 | - */ | |
| 40 | + * @param string $url | |
| 41 | + * @param License $license | |
| 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 | /** |
| 48 | - * Gets license status | |
| 49 | - * | |
| 50 | - * @return object | |
| 51 | - */ | |
| 49 | + * Gets license status | |
| 50 | + * | |
| 51 | + * @return object | |
| 52 | + */ | |
| 52 | 53 | public function get_license() { |
| 53 | 54 | $endpoint = '/license'; |
| 54 | 55 | $response = $this->request( 'GET', $endpoint ); |
| 55 | 56 | return $response; |
| @@ -56,16 +57,16 @@ | ||
| 56 | 57 | } |
| 57 | 58 | |
| 58 | 59 | |
| 59 | 60 | /** |
| 60 | - * Logs the current site in to the remote API | |
| 61 | - * | |
| 62 | - * @return object | |
| 63 | - */ | |
| 61 | + * Logs the current site in to the remote API | |
| 62 | + * | |
| 63 | + * @return object | |
| 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 | } |
| @@ -70,12 +71,12 @@ | ||
| 70 | 71 | return $response; |
| 71 | 72 | } |
| 72 | 73 | |
| 73 | 74 | /** |
| 74 | - * Logs the current site out of the remote API | |
| 75 | - * | |
| 76 | - * @return object | |
| 77 | - */ | |
| 75 | + * Logs the current site out of the remote API | |
| 76 | + * | |
| 77 | + * @return object | |
| 78 | + */ | |
| 78 | 79 | public function deactivate_license() { |
| 79 | 80 | $endpoint = sprintf( '/license/activations/%s', $this->license->activation_key ); |
| 80 | 81 | $response = $this->request( 'DELETE', $endpoint ); |
| 81 | 82 | return $response; |
| @@ -81,11 +82,11 @@ | ||
| 81 | 82 | return $response; |
| 82 | 83 | } |
| 83 | 84 | |
| 84 | 85 | /** |
| 85 | - * @param Plugin $plugin | |
| 86 | - * @return object | |
| 87 | - */ | |
| 86 | + * @param Plugin $plugin | |
| 87 | + * @return object | |
| 88 | + */ | |
| 88 | 89 | public function get_plugin( Plugin $plugin ) { |
| 89 | 90 | $endpoint = sprintf( '/plugins/%s?format=wp', $plugin->id() ); |
| 90 | 91 | $response = $this->request( 'GET', $endpoint ); |
| 91 | 92 | return $response; |
| @@ -91,13 +92,12 @@ | ||
| 91 | 92 | return $response; |
| 92 | 93 | } |
| 93 | 94 | |
| 94 | 95 | /** |
| 95 | - * @param Plugin[] $plugins (optional) | |
| 96 | - * @return object | |
| 97 | - */ | |
| 96 | + * @param Plugin[] $plugins (optional) | |
| 97 | + * @return object | |
| 98 | + */ | |
| 98 | 99 | public function get_plugins( $plugins = null ) { |
| 99 | - | |
| 100 | 100 | $args = array( |
| 101 | 101 | 'format' => 'wp', |
| 102 | 102 | ); |
| 103 | 103 | |
| @@ -106,37 +106,36 @@ | ||
| 106 | 106 | return $response; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
| 110 | - * @param string $method | |
| 111 | - * @param string $endpoint | |
| 112 | - * @param array $data | |
| 113 | - * | |
| 114 | - * @return object|array | |
| 115 | - */ | |
| 110 | + * @param string $method | |
| 111 | + * @param string $endpoint | |
| 112 | + * @param array $data | |
| 113 | + * | |
| 114 | + * @return object|array | |
| 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 | - 'Content-Type' => 'application/json', | |
| 123 | - 'Accepts' => 'application/json', | |
| 124 | - ), | |
| 121 | + 'Content-Type' => 'application/json', | |
| 122 | + 'Accepts' => 'application/json', | |
| 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' ) ) ) { | |
| 134 | - $url = add_query_arg( $data, $url ); | |
| 135 | - } else { | |
| 136 | - $args['body'] = json_encode( $data ); | |
| 137 | - } | |
| 138 | - } | |
| 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 | + } | |
| 139 | 138 | |
| 140 | 139 | $response = wp_remote_request( $url, $args ); |
| 141 | 140 | return $this->parse_response( $response ); |
| 142 | 141 | } |
| @@ -141,37 +140,36 @@ | ||
| 141 | 140 | return $this->parse_response( $response ); |
| 142 | 141 | } |
| 143 | 142 | |
| 144 | 143 | /** |
| 145 | - * @param mixed $response | |
| 146 | - * | |
| 147 | - * @return object|null | |
| 148 | - * | |
| 149 | - * @throws API_Exception | |
| 150 | - */ | |
| 144 | + * @param mixed $response | |
| 145 | + * | |
| 146 | + * @return object|null | |
| 147 | + * | |
| 148 | + * @throws API_Exception | |
| 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) ) { | |
| 160 | - return null; | |
| 161 | - } | |
| 158 | + if ( empty( $body ) ) { | |
| 159 | + return null; | |
| 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 ) { | |
| 170 | - throw new API_Exception( $json->message, $json->code ); | |
| 171 | - } | |
| 168 | + if ( wp_remote_retrieve_response_code( $response ) >= 400 ) { | |
| 169 | + throw new API_Exception( $json->message, $json->code ); | |
| 170 | + } | |
| 172 | 171 | |
| 173 | 172 | // return actual response data |
| 174 | 173 | return $json; |
| 175 | 174 | } |
| 176 | - | |
| 177 | 175 | } |