PluginProbe
YayMail – WooCommerce Email Customizer / 3.2.2
YayMail – WooCommerce Email Customizer v3.2.2
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 3.4.2 3.4.3 All 55 releases
yaymail / includes / License / RestAPI.php

RestAPI.php in YayMail – WooCommerce Email Customizer 3.2.2, at includes/License/RestAPI.php

114 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\License;
4
5 use YayMail\License\LicensingPlugin;
6 use YayMail\License\CorePlugin;
7
8 class RestAPI {
9 public function __construct() {
10 add_action( 'rest_api_init', array( $this, 'init_rest_api' ) );
11 }
12
13 public function init_rest_api() {
14 register_rest_route(
15 CorePlugin::get( 'slug' ) . '-license/v1',
16 '/license/activate',
17 array(
18 'methods' => array( \WP_REST_Server::CREATABLE ),
19 'callback' => array( $this, 'activate_license' ),
20 'permission_callback' => array( $this, 'permission_callback' ),
21 )
22 );
23 register_rest_route(
24 CorePlugin::get( 'slug' ) . '-license/v1',
25 '/license/update',
26 array(
27 'methods' => array( \WP_REST_Server::CREATABLE ),
28 'callback' => array( $this, 'update_license' ),
29 'permission_callback' => array( $this, 'permission_callback' ),
30 )
31 );
32 register_rest_route(
33 CorePlugin::get( 'slug' ) . '-license/v1',
34 '/license/delete',
35 array(
36 'methods' => array( \WP_REST_Server::CREATABLE ),
37 'callback' => array( $this, 'remove_license' ),
38 'permission_callback' => array( $this, 'permission_callback' ),
39 )
40 );
41
42 }
43
44 public function activate_license( $request_data ) {
45 $params = $request_data->get_params();
46 $plugin_slug = $params['plugin'];
47 $license_key = $params['license_key'];
48
49 $licensing_plugin = new LicensingPlugin( $plugin_slug );
50 $license = $licensing_plugin->get_license();
51 $activate_response = $license->activate( $license_key );
52
53 $return_result['success'] = $activate_response['success'];
54 $return_result['name'] = $licensing_plugin->get_option( 'name' );
55 $return_result['slug'] = $plugin_slug;
56 if ( $activate_response['success'] ) {
57 $return_result['formatted_license_key'] = $license->format_license_key();
58 if ( 'lifetime' === $activate_response['expires'] ) {
59 $return_result['expires'] = 'Lifetime';
60 } else {
61 $return_result['expires'] = gmdate( 'F j, Y H:i:s', strtotime( $activate_response['expires'] ) );
62 }
63
64 $return_result['plugin_url'] = $licensing_plugin->get_option( 'url' );
65 $return_result['renewal_url'] = $license->get_renewal_url();
66 } else {
67 $return_result['message'] = LicenseAPI::get_error_message( $activate_response['message'] );
68 }
69 return new \WP_REST_Response( $return_result );
70 }
71
72 public function update_license( $request_data ) {
73 $params = $request_data->get_params();
74 $plugin_slug = $params['plugin'];
75
76 $licensing_plugin = new LicensingPlugin( $plugin_slug );
77 $license = $licensing_plugin->get_license();
78 $update_response = $license->update();
79 $return_result['success'] = $update_response['success'];
80 $return_result['name'] = $licensing_plugin->get_option( 'name' );
81 $return_result['slug'] = $plugin_slug;
82 if ( $update_response['success'] ) {
83 $return_result['formatted_license_key'] = $license->format_license_key();
84 if ( 'lifetime' === $update_response['expires'] ) {
85 $return_result['expires'] = 'Lifetime';
86 } else {
87 $return_result['expires'] = gmdate( 'F j, Y H:i:s', strtotime( $update_response['expires'] ) );
88 }
89
90 $return_result['plugin_url'] = $licensing_plugin->get_option( 'url' );
91 $return_result['renewal_url'] = $license->get_renewal_url();
92 $return_result['is_expired'] = $license->is_expired();
93 }
94 return new \WP_REST_Response( $return_result );
95 }
96 public function remove_license( $request_data ) {
97 $params = $request_data->get_params();
98 $plugin_slug = $params['plugin'];
99
100 $licensing_plugin = new LicensingPlugin( $plugin_slug );
101 $license = $licensing_plugin->get_license();
102 $license->remove();
103 $return_result = array(
104 'slug' => $plugin_slug,
105 'name' => $licensing_plugin->get_option( 'name' ),
106 );
107 return new \WP_REST_Response( $return_result );
108 }
109
110 public function permission_callback() {
111 return true;
112 }
113 }
114