License.php
9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* RestAPI Global Settings Endpoint.
*
* @copyright (c) 2021, Code Atlantic LLC.
* @package ContentControl
*/
namespace ContentControl\RestAPI;
use WP_REST_Controller, WP_REST_Response, WP_REST_Server, WP_Error, WP_REST_Request;
use function ContentControl\plugin;
defined( 'ABSPATH' ) || exit;
/**
* Rest API Licensing Controller Class.
*/
class License extends WP_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'content-control/v2';
/**
* Route base.
*
* @var string
*/
protected $base = 'license';
/**
* Register API endpoint routes.
*
* @return void
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->base,
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_license' ],
'permission_callback' => [ $this, 'manage_license_permissions' ],
],
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'update_license_key' ],
'permission_callback' => [ $this, 'manage_license_permissions' ],
'args' => [
'licenseKey' => [
'required' => true,
'validate_callback' => function ( $param, $request, $key ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
return is_string( $param );
},
],
],
],
[
'methods' => WP_REST_Server::DELETABLE,
'callback' => [ $this, 'remove_license' ],
'permission_callback' => [ $this, 'manage_license_permissions' ],
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->base . '/activate',
[
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'activate_license' ],
'permission_callback' => [ $this, 'manage_license_permissions' ],
'args' => [
'licenseKey' => [
'required' => false,
'validate_callback' => function ( $param, $request, $key ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
return is_string( $param );
},
],
],
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->base . '/deactivate',
[
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'deactivate_license' ],
'permission_callback' => [ $this, 'manage_license_permissions' ],
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->base . '/status',
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_license_status' ],
'permission_callback' => [ $this, 'manage_license_permissions' ],
],
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'refresh_license_status' ],
'permission_callback' => [ $this, 'manage_license_permissions' ],
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->base . '/activate-pro',
[
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'activate_pro' ],
'permission_callback' => [ $this, 'manage_license_permissions' ],
],
],
);
}
/**
* Clean private or unnecessary data from license data before returning it.
*
* @param array{key:string,status:array<string,mixed>} $license_data License data.
* @return array{key:string,status:array<string,mixed>}
*/
public function clean_license_data( $license_data ) {
$license_data['status'] = $this->clean_license_status( $license_data['status'] );
return $license_data;
}
/**
* Clean license status.
*
* @param array{key:string,status:array<string,mixed>} $license_status License status.
*
* @return (array|string)[]
*
* @psalm-return array<'key'|'status', array<string, mixed>|string>
* @phpstan-return array{key: string, status: array<string, mixed>|string}
*/
public function clean_license_status( $license_status ) {
// Remove customer_email, customer_name, payment_id, ..., checksum keys from status array.
$license_status = array_diff_key(
$license_status,
array_flip(
[
'customer_email',
'customer_name',
'payment_id',
'checksum',
'item_id',
'item_name',
]
)
);
return $license_status;
}
/**
* Get plugin license.
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_license() {
$license_data = plugin( 'license' )->get_license_data();
if ( $license_data ) {
return new WP_REST_Response( $this->clean_license_data( $license_data ), 200 );
} else {
return new WP_Error( '404', __( 'Something went wrong.', 'content-control' ), [ 'status' => 404 ] );
}
}
/**
* Update plugin license key.
*
* @param \WP_REST_Request<array<string,mixed>> $request Request object.
*
* @return \WP_Error|\WP_REST_Response
*/
public function update_license_key( $request ) {
$license_key = sanitize_text_field( $request->get_param( 'licenseKey' ) );
try {
$old_key = plugin( 'license' )->get_license_key();
if ( $old_key === $license_key ) {
$license_status = plugin( 'license' )->get_license_status();
return new WP_REST_Response( $this->clean_license_status( $license_status ), 200 );
}
if ( plugin( 'license' )->is_license_active() ) {
plugin( 'license' )->deactivate_license();
}
plugin( 'license' )->update_license_key( $license_key );
$license_status = plugin( 'license' )->activate_license( $license_key );
$response = [
'status' => $this->clean_license_status( $license_status ),
];
if ( ! plugin()->is_pro_installed() ) {
$response['connectInfo'] = plugin( 'connect' )->get_connect_info( $license_key );
}
return new WP_REST_Response( $response, 200 );
} catch ( \Exception $e ) {
$message = __( 'Something went wrong, the license key could not be saved.', 'content-control' );
if ( '' !== $e->getMessage() ) {
$message = $e->getMessage();
}
return new WP_Error( '404', $message, [ 'status' => 404 ] );
}
}
/**
* Remove plugin license key.
*
* @return \WP_Error|\WP_REST_Response
*/
public function remove_license() {
plugin( 'license' )->remove_license();
if ( '' === plugin( 'license' )->get_license_key() ) {
return new WP_REST_Response( true, 200 );
} else {
return new WP_Error( '404', __( 'Something went wrong.', 'content-control' ), [ 'status' => 404 ] );
}
}
/**
* Activate plugin license.
*
* @param WP_REST_Request<array<string,mixed>> $request Request object.
*
* @return \WP_Error|\WP_REST_Response
*/
public function activate_license( $request ) {
$license_key = sanitize_text_field( $request->get_param( 'licenseKey' ) );
try {
$license_status = plugin( 'license' )->activate_license( $license_key );
$response = [
'status' => $this->clean_license_status( $license_status ),
];
if ( ! plugin()->is_pro_installed() ) {
$response['connectInfo'] = plugin( 'connect' )->get_connect_info( plugin( 'license' )->get_license_key() );
}
return new WP_REST_Response( $response, 200 );
} catch ( \Exception $e ) {
$message = __( 'Something went wrong, the license could not be activated.', 'content-control' );
if ( '' !== $e->getMessage() ) {
$message = $e->getMessage();
}
return new WP_Error( '404', $message, [ 'status' => 404 ] );
}
}
/**
* Activate pro plugin.
*
* @return \WP_Error|\WP_REST_Response
*/
public function activate_pro() {
// Check if its installed.
if ( ! plugin()->is_pro_installed() ) {
return new WP_Error( '404', __( 'Pro plugin is not installed.', 'content-control' ), [ 'status' => 404 ] );
}
// Check if its active.
if ( plugin()->is_pro_active() ) {
return new WP_Error( '404', __( 'Pro plugin is already active.', 'content-control' ), [ 'status' => 404 ] );
}
// // Activate pro plugin.
if ( ! function_exists( 'activate_plugin' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$activate = \activate_plugin( 'content-control-pro/content-control-pro.php', '', false, true );
if ( is_wp_error( $activate ) ) {
return new WP_Error( '404', __( 'Something went wrong, the pro plugin could not be activated.', 'content-control' ), [ 'status' => 404 ] );
}
return new WP_REST_Response( true, 200 );
}
/**
* Deactivate plugin license.
*
* @return \WP_Error|\WP_REST_Response
*/
public function deactivate_license() {
try {
$license_status = plugin( 'license' )->deactivate_license();
return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 );
} catch ( \Exception $e ) {
$message = __( 'Something went wrong, the license could not be deactivated.', 'content-control' );
if ( '' !== $e->getMessage() ) {
$message = $e->getMessage();
}
return new WP_Error( '404', $message, [ 'status' => 404 ] );
}
}
/**
* Get plugin license status.
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_status() {
$license_status = plugin( 'license' )->get_license_status();
if ( $license_status ) {
return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 );
} else {
return new WP_Error( '404', __( 'Something went wrong.', 'content-control' ), [ 'status' => 404 ] );
}
}
/**
* Refresh plugin license status.
*
* @return \WP_Error|\WP_REST_Response
*/
public function refresh_license_status() {
$license_status = plugin( 'license' )->get_license_status( true );
if ( $license_status ) {
return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 );
} else {
return new WP_Error( '404', __( 'Something went wrong.', 'content-control' ), [ 'status' => 404 ] );
}
}
/**
* Check update settings permissions.
*
* @return bool
*/
public function manage_license_permissions() {
return current_user_can( 'manage_options' ) || current_user_can( 'activate_plugins' );
}
}