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