PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.2
YayMail – WooCommerce Email Customizer v4.4.2
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / vendor-prefixed / src / License / RestAPI.php

RestAPI.php in YayMail – WooCommerce Email Customizer 4.4.2, at vendor-prefixed/src/License/RestAPI.php

91 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMailScoped\YayCommerce\AdminShell\License;
4
5 use YayMailScoped\YayCommerce\AdminShell\License\Contracts\LicenseConfigAdapter;
6 use YayMailScoped\YayCommerce\AdminShell\Support\Slug;
7 \defined('ABSPATH') || exit;
8 /**
9 * Per-plugin REST routes for license management.
10 *
11 * REST namespace: {slug}/v1 (derived from adapter slug).
12 * Ported from YayMail Pro RestAPI.php — CorePlugin::get() replaced with adapter.
13 * @internal
14 */
15 class RestAPI
16 {
17 protected LicenseConfigAdapter $adapter;
18 public function __construct(LicenseConfigAdapter $adapter)
19 {
20 $this->adapter = $adapter;
21 add_action('rest_api_init', [$this, 'init_rest_api']);
22 }
23 public function init_rest_api() : void
24 {
25 $namespace = Slug::to_var_name($this->adapter->get_plugin_slug()) . '/v1';
26 register_rest_route($namespace, '/license/activate', ['methods' => [\WP_REST_Server::CREATABLE], 'callback' => [$this, 'activate_license'], 'permission_callback' => [$this, 'permission_callback']]);
27 register_rest_route($namespace, '/license/update', ['methods' => [\WP_REST_Server::CREATABLE], 'callback' => [$this, 'update_license'], 'permission_callback' => [$this, 'permission_callback']]);
28 register_rest_route($namespace, '/license/delete', ['methods' => [\WP_REST_Server::CREATABLE], 'callback' => [$this, 'remove_license'], 'permission_callback' => [$this, 'permission_callback']]);
29 }
30 public function activate_license(\WP_REST_Request $request_data) : \WP_REST_Response
31 {
32 $nonce = $request_data->get_header('x_wp_nonce');
33 if (!wp_verify_nonce($nonce, 'wp_rest')) {
34 return new \WP_REST_Response(['success' => \false, 'message' => 'Nonce is invalid'], 403);
35 }
36 $license_key = \sanitize_text_field($request_data->get_param('license_key'));
37 $license = new License($this->adapter);
38 $activate_response = $license->activate($license_key);
39 $return = ['success' => $activate_response['success'], 'name' => $this->adapter->get_plugin_name(), 'slug' => $this->adapter->get_plugin_slug()];
40 if ($activate_response['success']) {
41 $_plugin = ['slug' => $this->adapter->get_plugin_slug(), 'name' => $this->adapter->get_plugin_name()];
42 \ob_start();
43 include __DIR__ . '/../../views/license-card.php';
44 $return['html'] = \ob_get_clean();
45 } else {
46 $return['message'] = LicenseAPI::get_error_message($activate_response['message'] ?? '');
47 }
48 return new \WP_REST_Response($return);
49 }
50 public function update_license(\WP_REST_Request $request_data) : \WP_REST_Response
51 {
52 $nonce = $request_data->get_header('x_wp_nonce');
53 if (!wp_verify_nonce($nonce, 'wp_rest')) {
54 return new \WP_REST_Response(['success' => \false, 'message' => 'Nonce is invalid'], 403);
55 }
56 $license = new License($this->adapter);
57 $update_response = $license->update();
58 $return = ['success' => $update_response['success'], 'name' => $this->adapter->get_plugin_name(), 'slug' => $this->adapter->get_plugin_slug()];
59 $_plugin = ['slug' => $this->adapter->get_plugin_slug(), 'name' => $this->adapter->get_plugin_name()];
60 if ($update_response['success'] || !empty($update_response['is_server_error'])) {
61 \ob_start();
62 include __DIR__ . '/../../views/license-card.php';
63 $return['html'] = \ob_get_clean();
64 } else {
65 \ob_start();
66 include __DIR__ . '/../../views/license-activate-card.php';
67 $return['html'] = \ob_get_clean();
68 }
69 return new \WP_REST_Response($return);
70 }
71 public function remove_license(\WP_REST_Request $request_data) : \WP_REST_Response
72 {
73 $nonce = $request_data->get_header('x_wp_nonce');
74 if (!wp_verify_nonce($nonce, 'wp_rest')) {
75 return new \WP_REST_Response(['success' => \false, 'message' => 'Nonce is invalid'], 403);
76 }
77 $license = new License($this->adapter);
78 $license->remove();
79 $return = ['success' => \true, 'name' => $this->adapter->get_plugin_name(), 'slug' => $this->adapter->get_plugin_slug()];
80 $_plugin = $return;
81 \ob_start();
82 include __DIR__ . '/../../views/license-activate-card.php';
83 $return['html'] = \ob_get_clean();
84 return new \WP_REST_Response($return);
85 }
86 public function permission_callback() : bool
87 {
88 return \current_user_can($this->adapter->get_capability());
89 }
90 }
91