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