PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.2.89
WindPress – Tailwind CSS integration for WordPress v3.2.89
3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 143 releases
windpress / src / Api / Admin / Settings / License.php

License.php in WindPress – Tailwind CSS integration for WordPress 3.2.89, at src/Api/Admin/Settings/License.php

94 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the WindPress package.
5 *
6 * (c) Joshua Gugun Siagian <suabahasa@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 declare (strict_types=1);
12 namespace WindPress\WindPress\Api\Admin\Settings;
13
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;
20 use WP_REST_Request;
21 use WP_REST_Response;
22 use WP_REST_Server;
23 class License extends AbstractApi implements ApiInterface
24 {
25 public function __construct()
26 {
27 }
28 public function get_prefix(): string
29 {
30 return 'admin/settings/license';
31 }
32 public function register_custom_endpoints(): void
33 {
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)]);
37 }
38 public function index(WP_REST_Request $wprestRequest): WP_REST_Response
39 {
40 return new WP_REST_Response(['license' => $this->get_license()]);
41 }
42 public function activate(WP_REST_Request $wprestRequest): WP_REST_Response
43 {
44 $payload = $wprestRequest->get_json_params();
45 $new_license_key = sanitize_text_field((string) ($payload['license'] ?? ''));
46 if ($new_license_key === '') {
47 return new WP_REST_Response(['message' => __('License key is empty', 'windpress')], 400);
48 }
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 }
53 $response = $plugin_updater->activate($new_license_key);
54 if (is_wp_error($response)) {
55 return $this->error_response($response);
56 }
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 }
60 public function deactivate(WP_REST_Request $wprestRequest): WP_REST_Response
61 {
62 $plugin_updater = Plugin::get_instance()->plugin_updater;
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()]);
72 }
73 private function get_license(): array
74 {
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 }
81 try {
82 $license['is_activated'] = $plugin_updater instanceof LicenseManager && $plugin_updater->is_activated();
83 } catch (\Throwable $throwable) {
84 $license['is_activated'] = \false;
85 }
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);
92 }
93 }
94