PluginProbe
YayMail – WooCommerce Email Customizer / 4.3.3
YayMail – WooCommerce Email Customizer v4.3.3
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 / src / License / RestAPI.php

RestAPI.php in YayMail – WooCommerce Email Customizer 4.3.3, at src/License/RestAPI.php

146 lines 5.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 /**
6 * RestAPI
7 *
8 * @package RestAPI
9 */
10 class RestAPI {
11 public function __construct() {
12 add_action( 'rest_api_init', [ $this, 'init_rest_api' ] );
13 }
14
15 public function init_rest_api() {
16 register_rest_route(
17 CorePlugin::get( 'slug' ) . '-license/v1',
18 '/license/activate',
19 [
20 'methods' => [ \WP_REST_Server::CREATABLE ],
21 'callback' => [ $this, 'activate_license' ],
22 'permission_callback' => [ $this, 'permission_callback' ],
23 ]
24 );
25 register_rest_route(
26 CorePlugin::get( 'slug' ) . '-license/v1',
27 '/license/update',
28 [
29 'methods' => [ \WP_REST_Server::CREATABLE ],
30 'callback' => [ $this, 'update_license' ],
31 'permission_callback' => [ $this, 'permission_callback' ],
32 ]
33 );
34 register_rest_route(
35 CorePlugin::get( 'slug' ) . '-license/v1',
36 '/license/delete',
37 [
38 'methods' => [ \WP_REST_Server::CREATABLE ],
39 'callback' => [ $this, 'remove_license' ],
40 'permission_callback' => [ $this, 'permission_callback' ],
41 ]
42 );
43 }
44
45 public function activate_license( $request_data ) {
46 $nonce = $request_data->get_header( 'x_wp_nonce' );
47 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
48 return new \WP_REST_Response(
49 [
50 'success' => false,
51 'message' => 'Nonce is invalid',
52 ]
53 );
54 }
55 $plugin_slug = sanitize_text_field( $request_data->get_param( 'plugin' ) );
56 $license_key = sanitize_text_field( $request_data->get_param( 'license_key' ) );
57 $licensing_plugin = new LicensingPlugin( $plugin_slug );
58 $license = $licensing_plugin->get_license();
59 $activate_response = $license->activate( $license_key );
60 $return_result['success'] = $activate_response['success'];
61 $return_result['name'] = $licensing_plugin->get_option( 'name' );
62 $return_result['slug'] = $plugin_slug;
63 if ( $activate_response['success'] ) {
64 $_plugin = [
65 'slug' => $plugin_slug,
66 'name' => $licensing_plugin->get_option( 'name' ),
67 ];
68 ob_start();
69 include plugin_dir_path( __FILE__ ) . 'views/information-card.php';
70 $html = ob_get_contents();
71 ob_end_clean();
72
73 $return_result['html'] = $html;
74 $return_result['core_plugin_licese_inactive'] = LicenseHandler::is_any_core_license_inactive();
75 } else {
76 $return_result['message'] = LicenseAPI::get_error_message( $activate_response['message'] );
77 }
78 return new \WP_REST_Response( $return_result );
79 }
80
81 public function update_license( $request_data ) {
82 $nonce = $request_data->get_header( 'x_wp_nonce' );
83 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
84 return new \WP_REST_Response(
85 [
86 'success' => false,
87 'message' => 'Nonce is invalid',
88 ]
89 );
90 }
91 $plugin_slug = sanitize_text_field( $request_data->get_param( 'plugin' ) );
92 $licensing_plugin = new LicensingPlugin( $plugin_slug );
93 $license = $licensing_plugin->get_license();
94 $update_response = $license->update();
95 $return_result['success'] = $update_response['success'];
96 $return_result['name'] = $licensing_plugin->get_option( 'name' );
97 $return_result['slug'] = $plugin_slug;
98 $_plugin = $return_result;
99 if ( $update_response['success'] || ! empty( $update_response['is_server_error'] ) ) {
100 ob_start();
101 include plugin_dir_path( __FILE__ ) . 'views/information-card.php';
102 $html = ob_get_contents();
103 ob_end_clean();
104 } else {
105 ob_start();
106 include plugin_dir_path( __FILE__ ) . 'views/activate-card.php';
107 $html = ob_get_contents();
108 ob_end_clean();
109 }
110 $return_result['html'] = $html;
111 $return_result['core_plugin_licese_inactive'] = LicenseHandler::is_any_core_license_inactive();
112 return new \WP_REST_Response( $return_result );
113 }
114 public function remove_license( $request_data ) {
115 $nonce = $request_data->get_header( 'x_wp_nonce' );
116 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
117 return new \WP_REST_Response(
118 [
119 'success' => false,
120 'message' => 'Nonce is invalid',
121 ]
122 );
123 }
124 $plugin_slug = sanitize_text_field( $request_data->get_param( 'plugin' ) );
125 $licensing_plugin = new LicensingPlugin( $plugin_slug );
126 $license = $licensing_plugin->get_license();
127 $license->remove();
128 $return_result = [
129 'slug' => $plugin_slug,
130 'name' => $licensing_plugin->get_option( 'name' ),
131 ];
132 $_plugin = $return_result;
133 ob_start();
134 include plugin_dir_path( __FILE__ ) . 'views/activate-card.php';
135 $html = ob_get_contents();
136 ob_end_clean();
137 $return_result['html'] = $html;
138 $return_result['core_plugin_licese_inactive'] = LicenseHandler::is_any_core_license_inactive();
139 return new \WP_REST_Response( $return_result );
140 }
141
142 public function permission_callback() {
143 return current_user_can( 'manage_options' );
144 }
145 }
146