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