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