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