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