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