PluginProbe
Boxzilla – WordPress Popup Builder / 3.0.3
Boxzilla – WordPress Popup Builder v3.0.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
boxzilla / src / licensing / class-api.php

class-api.php in Boxzilla – WordPress Popup Builder 3.0.3, at src/licensing/class-api.php

174 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Boxzilla\Licensing;
4
5 use Boxzilla\Plugin;
6 use Exception;
7 use WP_Error;
8
9 class API {
10
11 /**
12 * @var License
13 */
14 protected $license;
15
16 /**
17 * The API url
18 *
19 * @var string
20 */
21 public $url = '';
22
23 /**
24 * @var int
25 */
26 protected $error_code = 0;
27
28 /**
29 * @var string
30 */
31 protected $error_message = '';
32
33 /**
34 * @var
35 */
36 protected $last_response;
37
38 /**
39 * @param string $url
40 * @param License $license
41 */
42 public function __construct( $url, License $license ) {
43 $this->url = $url;
44 $this->license = $license;
45 }
46
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 /**
60 * Logs the current site in to the remote API
61 *
62 * @return object
63 */
64 public function activate_license() {
65 $endpoint = '/license/activations';
66 $args = array(
67 'site_url' => get_option( 'siteurl' )
68 );
69 $response = $this->request( 'POST', $endpoint, $args );
70 return $response;
71 }
72
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;
82 }
83
84 /**
85 * @param Plugin $plugin
86 * @return object
87 */
88 public function get_plugin( Plugin $plugin ) {
89 $endpoint = sprintf( '/plugins/%s?format=wp', $plugin->id() );
90 $response = $this->request( 'GET', $endpoint );
91 return $response;
92 }
93
94 /**
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 // 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;
113 }
114
115 /**
116 * @param string $method
117 * @param string $endpoint
118 * @param array $data
119 *
120 * @return object|array
121 */
122 public function request( $method, $endpoint, $data = array() ) {
123
124 $url = $this->url . $endpoint;
125 $args = array(
126 'method' => $method,
127 'headers' => array(),
128 );
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
135 if( in_array( $method, array( 'GET', 'DELETE' ) ) ) {
136 $url = add_query_arg( $data, $url );
137 } else {
138 $args['body'] = $data;
139 }
140
141 $response = wp_remote_request( $url, $args );
142 return $this->parse_response( $response );
143 }
144
145 /**
146 * @param mixed $response
147 *
148 * @return object|null
149 *
150 * @throws API_Exception
151 */
152 public function parse_response( $response ) {
153 // test for wp errors
154 if( $response instanceof WP_Error) {
155 throw new API_Exception( $response->get_error_message() );
156 }
157
158 // retrieve response body
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' ) );
163 }
164
165 // did request return an error response?
166 if( isset( $json->error ) ) {
167 throw new API_Exception( $json->error->message, $json->error->code );
168 }
169
170 // return actual response data
171 return $json->data;
172 }
173
174 }