| 1 |
<?php |
| 2 |
/** |
| 3 |
* License: Manager |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.4.5 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core\License; |
| 13 |
|
| 14 |
/** |
| 15 |
* LicenseManager class. |
| 16 |
* |
| 17 |
* @since 4.4.5 |
| 18 |
*/ |
| 19 |
class LicenseManager { |
| 20 |
|
| 21 |
/** |
| 22 |
* Activate a license key. |
| 23 |
* |
| 24 |
* Note: This method does no permission or security checks. Those should be done by |
| 25 |
* the service or subscriber consuming this service. |
| 26 |
* |
| 27 |
* @since 4.4.5 |
| 28 |
* |
| 29 |
* @param string $license License key to activate. |
| 30 |
* @return bool|\SimplePay\Core\License\License False on failure, or License. |
| 31 |
*/ |
| 32 |
function activate( $license ) { |
| 33 |
// Retrieve license key from form field. |
| 34 |
$key = sanitize_key( trim( $license ) ); |
| 35 |
|
| 36 |
// Data to send in our API request. |
| 37 |
$api_params = array( |
| 38 |
'edd_action' => 'activate_license', |
| 39 |
'license' => $key, |
| 40 |
'item_id' => SIMPLE_PAY_ITEM_ID, // @phpstan-ignore-line |
| 41 |
'url' => home_url(), |
| 42 |
); |
| 43 |
|
| 44 |
// Call the custom API. |
| 45 |
$response = wp_remote_post( |
| 46 |
SIMPLE_PAY_STORE_URL, |
| 47 |
array( |
| 48 |
'timeout' => 15, |
| 49 |
'sslverify' => false, |
| 50 |
'body' => $api_params, |
| 51 |
) |
| 52 |
); |
| 53 |
|
| 54 |
// Make sure the response came back okay. |
| 55 |
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
// No error, so let's proceed. |
| 60 |
|
| 61 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 62 |
|
| 63 |
// Update saved license key & data. |
| 64 |
update_option( 'simpay_license_key', $key ); |
| 65 |
update_option( 'simpay_license_data', $license_data ); |
| 66 |
|
| 67 |
return new License( $key ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Deactivate a license by key. |
| 72 |
* |
| 73 |
* Note: This method does no permission or security checks. Those should be done by |
| 74 |
* the service or subscriber consuming this service. |
| 75 |
* |
| 76 |
* @since 4.4.5 |
| 77 |
* |
| 78 |
* @param string $license License key to deactivate. |
| 79 |
* @return bool|\SimplePay\Core\License\License False on failure, or License. |
| 80 |
*/ |
| 81 |
public function deactivate( $license ) { |
| 82 |
$key = sanitize_key( trim( $license ) ); |
| 83 |
|
| 84 |
// Data to send in our API request. |
| 85 |
$api_params = array( |
| 86 |
'edd_action' => 'deactivate_license', |
| 87 |
'license' => $key, |
| 88 |
'item_id' => SIMPLE_PAY_ITEM_ID, // @phpstan-ignore-line |
| 89 |
'url' => home_url(), |
| 90 |
); |
| 91 |
|
| 92 |
// Call the custom API. |
| 93 |
$response = wp_remote_post( |
| 94 |
SIMPLE_PAY_STORE_URL, |
| 95 |
array( |
| 96 |
'timeout' => 15, |
| 97 |
'sslverify' => false, |
| 98 |
'body' => $api_params, |
| 99 |
) |
| 100 |
); |
| 101 |
|
| 102 |
// Make sure the response came back okay. |
| 103 |
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
// No error, so let's proceed. |
| 108 |
|
| 109 |
// Decode the license data. |
| 110 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 111 |
|
| 112 |
/** @var \stdClass $license_data */ |
| 113 |
|
| 114 |
// $license_data->license will be either "deactivated" or "failed". |
| 115 |
if ( 'deactivated' === $license_data->license || 'failed' === $license_data->license ) { |
| 116 |
|
| 117 |
// Remove saved license data, key & next check options. |
| 118 |
delete_option( 'simpay_license_data' ); |
| 119 |
delete_option( 'simpay_license_key' ); |
| 120 |
delete_option( 'simpay_license_next_check' ); |
| 121 |
} |
| 122 |
|
| 123 |
return new License( '' ); |
| 124 |
} |
| 125 |
} |
| 126 |
|