PluginProbe
Boxzilla – WordPress Popup Builder / 3.2.13
Boxzilla – WordPress Popup Builder v3.2.13
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
boxzilla / src / licensing / class-api.php

class-api.php in Boxzilla – WordPress Popup Builder 3.2.13, at src/licensing/class-api.php

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