| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax plugin check licensing |
| 4 |
* @author Webcraftic <wordpress.webraftic@gmail.com> |
| 5 |
* @copyright (c) 2017 Webraftic Ltd |
| 6 |
* @version 1.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined('ABSPATH') ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Обработчик ajax запросов для проверки, активации, деактивации лицензионного ключа |
| 16 |
* |
| 17 |
* @since 1.4.0 |
| 18 |
*/ |
| 19 |
function winp_check_license() |
| 20 |
{ |
| 21 |
check_admin_referer('license'); |
| 22 |
|
| 23 |
$action = WINP_Plugin::app()->request->post('license_action', false, true); |
| 24 |
$license_key = WINP_Plugin::app()->request->post('licensekey', null); |
| 25 |
|
| 26 |
if( empty($action) || !in_array($action, array('activate', 'deactivate', 'sync', 'unsubscribe')) ) { |
| 27 |
wp_send_json_error(array('error_message' => __('Licensing action not passed or this action is prohibited!', 'insert-php'))); |
| 28 |
die(); |
| 29 |
} |
| 30 |
|
| 31 |
$licensing = WINP_Plugin::app()->premium; |
| 32 |
|
| 33 |
$result = null; |
| 34 |
$success_message = ''; |
| 35 |
|
| 36 |
switch( $action ) { |
| 37 |
case 'activate': |
| 38 |
if( empty($license_key) || strlen($license_key) > 32 ) { |
| 39 |
wp_send_json_error(array('error_message' => __('License key is empty or license key too long (license key is 32 characters long)', 'insert-php'))); |
| 40 |
} else { |
| 41 |
$result = $licensing->activate($license_key); |
| 42 |
$success_message = __('Your license has been successfully activated', 'insert-php'); |
| 43 |
} |
| 44 |
break; |
| 45 |
case 'deactivate': |
| 46 |
$result = $licensing->deactivate(); |
| 47 |
$success_message = __('The license is deactivated', 'insert-php'); |
| 48 |
break; |
| 49 |
case 'sync': |
| 50 |
$result = $licensing->sync(); |
| 51 |
$success_message = __('The license has been updated', 'insert-php'); |
| 52 |
break; |
| 53 |
case 'unsubscribe': |
| 54 |
$result = $licensing->cancel_paid_subscription(); |
| 55 |
$success_message = __('Subscription success cancelled', 'insert-php'); |
| 56 |
break; |
| 57 |
} |
| 58 |
|
| 59 |
if( is_wp_error($result) ) { |
| 60 |
|
| 61 |
/** |
| 62 |
* Экшен выполняет, когда проверка лицензии вернула ошибку |
| 63 |
* @param string $action |
| 64 |
* @param string $license_key |
| 65 |
* @since 1.4.0 |
| 66 |
*/ |
| 67 |
add_action('wbcr/inp/check_license_error', $action, $license_key); |
| 68 |
|
| 69 |
wp_send_json_error(array('error_message' => $result->get_error_message())); |
| 70 |
die(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Экшен выполняет, когда проверка лицензии успешно завершена |
| 75 |
* @param string $action |
| 76 |
* @param string $license_key |
| 77 |
* @since 1.4.0 |
| 78 |
*/ |
| 79 |
add_action('wbcr/inp/check_license_success', $action, $license_key); |
| 80 |
|
| 81 |
wp_send_json_success(array('message' => $success_message)); |
| 82 |
|
| 83 |
die(); |
| 84 |
} |
| 85 |
|
| 86 |
add_action('wp_ajax_winp_check_license', 'winp_check_license'); |
| 87 |
|
| 88 |
|