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