| @@ -11,14 +11,16 @@ | ||
| 11 | 11 | declare (strict_types=1); |
| 12 | 12 | namespace WindPress\WindPress\Api\Admin\Settings; |
| 13 | 13 | |
| 14 | 14 | use WIND_PRESS; |
| 15 | +use WindPress\WindPress\Api\AbstractApi; | |
| 16 | +use WindPress\WindPress\Api\ApiInterface; | |
| 17 | +use WindPress\WindPress\Licensing\Manager as LicenseManager; | |
| 18 | +use WindPress\WindPress\Plugin; | |
| 19 | +use WP_Error; | |
| 15 | 20 | use WP_REST_Request; |
| 16 | 21 | use WP_REST_Response; |
| 17 | 22 | use WP_REST_Server; |
| 18 | -use WindPress\WindPress\Api\AbstractApi; | |
| 19 | -use WindPress\WindPress\Api\ApiInterface; | |
| 20 | -use WindPress\WindPress\Plugin; | |
| 21 | 23 | class License extends AbstractApi implements ApiInterface |
| 22 | 24 | { |
| 23 | 25 | public function __construct() |
| 24 | 26 | { |
| @@ -39,41 +41,53 @@ | ||
| 39 | 41 | } |
| 40 | 42 | public function activate(WP_REST_Request $wprestRequest): WP_REST_Response |
| 41 | 43 | { |
| 42 | 44 | $payload = $wprestRequest->get_json_params(); |
| 43 | - $new_license_key = sanitize_text_field($payload['license']); | |
| 45 | + $new_license_key = sanitize_text_field((string) ($payload['license'] ?? '')); | |
| 44 | 46 | if ($new_license_key === '') { |
| 45 | - return new WP_REST_Response(['message' => 'License key is empty'], 400); | |
| 47 | + return new WP_REST_Response(['message' => __('License key is empty', 'windpress')], 400); | |
| 46 | 48 | } |
| 47 | 49 | $plugin_updater = Plugin::get_instance()->plugin_updater; |
| 50 | + if (!$plugin_updater instanceof LicenseManager) { | |
| 51 | + return new WP_REST_Response(['message' => __('License management is unavailable in this edition.', 'windpress'), 'license' => $this->get_license()], 404); | |
| 52 | + } | |
| 48 | 53 | $response = $plugin_updater->activate($new_license_key); |
| 49 | - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { | |
| 50 | - return new WP_REST_Response(['message' => is_wp_error($response) ? $response->get_error_message() : 'An error occurred, please try again.'], 500); | |
| 54 | + if (is_wp_error($response)) { | |
| 55 | + return $this->error_response($response); | |
| 51 | 56 | } |
| 52 | - $license_data = json_decode(wp_remote_retrieve_body($response), null, 512, \JSON_THROW_ON_ERROR); | |
| 53 | - if ($license_data->license !== 'valid') { | |
| 54 | - return new WP_REST_Response(['message' => $plugin_updater->error_message($license_data->error)], 400); | |
| 55 | - } | |
| 56 | - update_option(WIND_PRESS::WP_OPTION . '_license', ['key' => $new_license_key, 'opt_in_pre_release' => \false]); | |
| 57 | - $plugin_updater->drop_update_cache(); | |
| 58 | - return new WP_REST_Response(['message' => 'Plugin license key activated successfully', 'license' => $this->get_license()]); | |
| 57 | + update_option(WIND_PRESS::WP_OPTION . '_license', ['key' => $plugin_updater->get_license_key(), 'opt_in_pre_release' => (bool) $this->get_license()['opt_in_pre_release']]); | |
| 58 | + return new WP_REST_Response(['message' => __('Plugin license key activated successfully.', 'windpress'), 'license' => $this->get_license()]); | |
| 59 | 59 | } |
| 60 | 60 | public function deactivate(WP_REST_Request $wprestRequest): WP_REST_Response |
| 61 | 61 | { |
| 62 | 62 | $plugin_updater = Plugin::get_instance()->plugin_updater; |
| 63 | - $plugin_updater->deactivate(); | |
| 64 | - $plugin_updater->drop_update_cache(); | |
| 63 | + if (!$plugin_updater instanceof LicenseManager) { | |
| 64 | + return new WP_REST_Response(['message' => __('License management is unavailable in this edition.', 'windpress'), 'license' => $this->get_license()], 404); | |
| 65 | + } | |
| 66 | + $response = $plugin_updater->deactivate(); | |
| 67 | + if (is_wp_error($response)) { | |
| 68 | + return $this->error_response($response); | |
| 69 | + } | |
| 65 | 70 | update_option(WIND_PRESS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]); |
| 66 | - return new WP_REST_Response(['message' => 'Plugin license key de-activated successfully', 'license' => $this->get_license()]); | |
| 71 | + return new WP_REST_Response(['message' => __('Plugin license key deactivated successfully.', 'windpress'), 'license' => $this->get_license()]); | |
| 67 | 72 | } |
| 68 | 73 | private function get_license(): array |
| 69 | 74 | { |
| 70 | 75 | $license = get_option(WIND_PRESS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]); |
| 76 | + $license = wp_parse_args(is_array($license) ? $license : [], ['key' => '', 'opt_in_pre_release' => \false]); | |
| 77 | + $plugin_updater = Plugin::get_instance()->plugin_updater; | |
| 78 | + if ($plugin_updater instanceof LicenseManager) { | |
| 79 | + $license['key'] = $plugin_updater->get_license_key(); | |
| 80 | + } | |
| 71 | 81 | try { |
| 72 | - $license['is_activated'] = Plugin::get_instance()->plugin_updater->is_activated(); | |
| 82 | + $license['is_activated'] = $plugin_updater instanceof LicenseManager && $plugin_updater->is_activated(); | |
| 73 | 83 | } catch (\Throwable $throwable) { |
| 74 | - //throw $th; | |
| 75 | 84 | $license['is_activated'] = \false; |
| 76 | 85 | } |
| 77 | 86 | return $license; |
| 87 | + } | |
| 88 | + private function error_response(WP_Error $error): WP_REST_Response | |
| 89 | + { | |
| 90 | + $status = $error->get_error_code() === 'license_server_unavailable' ? 502 : 422; | |
| 91 | + return new WP_REST_Response(['message' => $error->get_error_message(), 'license' => $this->get_license()], $status); | |
| 78 | 92 | } |
| 79 | 93 | } |