PluginProbe
Boxzilla – WordPress Popup Builder / 3.4.0
Boxzilla – WordPress Popup Builder v3.4.0
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.4.0, at src/licensing/class-api.php

177 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 /**
13 * @var License
14 */
15 protected $license;
16
17 /**
18 * The API url
19 *
20 * @var string
21 */
22 public $url = '';
23
24 /**
25 * @var int
26 */
27 protected $error_code = 0;
28
29 /**
30 * @var string
31 */
32 protected $error_message = '';
33
34 /**
35 * @var
36 */
37 protected $last_response;
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 }
47
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 }
58
59
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 }
73
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 }
84
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 }
94
95 /**
96 * @param Plugin[] $plugins (optional)
97 * @return object
98 */
99 public function get_plugins( $plugins = null ) {
100 $args = array(
101 'format' => 'wp',
102 );
103
104 $endpoint = add_query_arg( $args, '/plugins' );
105 $response = $this->request( 'GET', $endpoint );
106 return $response;
107 }
108
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 'timeout' => 10,
125 );
126
127 // add license key to headers if set
128 if ( ! empty( $this->license->key ) ) {
129 $args['headers']['Authorization'] = 'Bearer ' . urlencode( $this->license->key );
130 }
131
132 if ( ! empty( $data ) ) {
133 if ( in_array( $method, array( 'GET', 'DELETE' ), true ) ) {
134 $url = add_query_arg( $data, $url );
135 } else {
136 $args['body'] = json_encode( $data );
137 }
138 }
139
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() );
155 }
156
157 // retrieve response body
158 $body = wp_remote_retrieve_body( $response );
159 if ( empty( $body ) ) {
160 return null;
161 }
162
163 $json = json_decode( $body, false );
164 if ( is_null( $json ) ) {
165 throw new API_Exception( esc_html__( 'The Boxzilla server returned an invalid response.', 'boxzilla' ) );
166 }
167
168 // did request return an error response?
169 if ( wp_remote_retrieve_response_code( $response ) >= 400 ) {
170 throw new API_Exception( $json->message, $json->code );
171 }
172
173 // return actual response data
174 return $json;
175 }
176 }
177