| @@ -11,69 +11,83 @@ | ||
| 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 | { |
| 25 | 27 | } |
| 26 | - public function get_prefix() : string | |
| 28 | + public function get_prefix(): string | |
| 27 | 29 | { |
| 28 | 30 | return 'admin/settings/license'; |
| 29 | 31 | } |
| 30 | - public function register_custom_endpoints() : void | |
| 32 | + public function register_custom_endpoints(): void | |
| 31 | 33 | { |
| 32 | - \register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/index', ['methods' => WP_REST_Server::READABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->index($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); | |
| 33 | - \register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/activate', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->activate($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); | |
| 34 | - \register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/deactivate', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->deactivate($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); | |
| 34 | + register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/index', ['methods' => WP_REST_Server::READABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->index($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); | |
| 35 | + register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/activate', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->activate($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); | |
| 36 | + register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/deactivate', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->deactivate($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); | |
| 35 | 37 | } |
| 36 | - public function index(WP_REST_Request $wprestRequest) : WP_REST_Response | |
| 38 | + public function index(WP_REST_Request $wprestRequest): WP_REST_Response | |
| 37 | 39 | { |
| 38 | 40 | return new WP_REST_Response(['license' => $this->get_license()]); |
| 39 | 41 | } |
| 40 | - public function activate(WP_REST_Request $wprestRequest) : WP_REST_Response | |
| 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 | - public function deactivate(WP_REST_Request $wprestRequest) : WP_REST_Response | |
| 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(); | |
| 65 | - \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()]); | |
| 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 | + } | |
| 70 | + update_option(WIND_PRESS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]); | |
| 71 | + return new WP_REST_Response(['message' => __('Plugin license key deactivated successfully.', 'windpress'), 'license' => $this->get_license()]); | |
| 67 | 72 | } |
| 68 | - private function get_license() : array | |
| 73 | + private function get_license(): array | |
| 69 | 74 | { |
| 70 | - $license = \get_option(WIND_PRESS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]); | |
| 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 | } |