Contracts
4 months ago
EDD_SL_Plugin_Updater.php
4 months ago
License.php
4 months ago
LicenseAPI.php
4 months ago
LicenseHandler.php
3 months ago
PluginInfoFactory.php
4 months ago
RestAPI.php
4 months ago
RestAPI.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WhatsappScoped\YayCommerce\AdminShell\License; |
| 4 | |
| 5 | use WhatsappScoped\YayCommerce\AdminShell\License\Contracts\LicenseConfigAdapter; |
| 6 | use WhatsappScoped\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 |