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