PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.2.81
WindPress – Tailwind CSS integration for WordPress v3.2.81
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.81, at src/Api/Admin/Settings/License.php

80 lines 4.2 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\Plugin;
18 use WP_REST_Request;
19 use WP_REST_Response;
20 use WP_REST_Server;
21 class License extends AbstractApi implements ApiInterface
22 {
23 public function __construct()
24 {
25 }
26 public function get_prefix(): string
27 {
28 return 'admin/settings/license';
29 }
30 public function register_custom_endpoints(): void
31 {
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)]);
35 }
36 public function index(WP_REST_Request $wprestRequest): WP_REST_Response
37 {
38 return new WP_REST_Response(['license' => $this->get_license()]);
39 }
40 public function activate(WP_REST_Request $wprestRequest): WP_REST_Response
41 {
42 $payload = $wprestRequest->get_json_params();
43 $new_license_key = sanitize_text_field($payload['license']);
44 if ($new_license_key === '') {
45 return new WP_REST_Response(['message' => __('License key is empty', 'windpress')], 400);
46 }
47 $plugin_updater = Plugin::get_instance()->plugin_updater;
48 $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.', 'windpress')], 500);
51 }
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', '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 $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', 'windpress'), 'license' => $this->get_license()]);
67 }
68 private function get_license(): array
69 {
70 $license = get_option(WIND_PRESS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]);
71 try {
72 $license['is_activated'] = Plugin::get_instance()->plugin_updater->is_activated();
73 } catch (\Throwable $throwable) {
74 //throw $th;
75 $license['is_activated'] = \false;
76 }
77 return $license;
78 }
79 }
80