| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCartQuickWebP\Licensing; |
| 4 |
|
| 5 |
/** |
| 6 |
* License model |
| 7 |
*/ |
| 8 |
class License { |
| 9 |
/** |
| 10 |
* The endpoint for the licenses. |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected $endpoint = 'v1/public/licenses'; |
| 15 |
|
| 16 |
/** |
| 17 |
* SureCartQuickWebP\Licensing\Client |
| 18 |
* |
| 19 |
* @var object |
| 20 |
*/ |
| 21 |
protected $client; |
| 22 |
|
| 23 |
/** |
| 24 |
* Set value for valid licnese |
| 25 |
* |
| 26 |
* @var bool |
| 27 |
*/ |
| 28 |
private $is_valid_license = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* Initialize the class. |
| 32 |
* |
| 33 |
* @param SureCartQuickWebP\Licensing\Client $client The client. |
| 34 |
*/ |
| 35 |
public function __construct( Client $client ) { |
| 36 |
$this->client = $client; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Retrieve license information by key. |
| 41 |
* |
| 42 |
* @param string $license_key The license key. |
| 43 |
* |
| 44 |
* @return Object|\WP_Error |
| 45 |
*/ |
| 46 |
public function retrieve( $license_key ) { |
| 47 |
$route = trailingslashit( $this->endpoint ) . $license_key; |
| 48 |
return $this->client->send_request( 'GET', $route ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Activate a specific license key. |
| 53 |
* |
| 54 |
* @param string $key A license key. |
| 55 |
* |
| 56 |
* @return \WP_Error|Object |
| 57 |
* @throws \Exception If something goes wrong. |
| 58 |
*/ |
| 59 |
public function activate( $key = '' ) { |
| 60 |
try { |
| 61 |
// validate the license and store it. |
| 62 |
$license = $this->validate( $key, true ); |
| 63 |
// create the activation. |
| 64 |
$activation = $this->client->activation()->create( $license->id ); |
| 65 |
if ( is_wp_error( $activation ) ) { |
| 66 |
$exception_code = 0; |
| 67 |
if ( 'not_found' === $activation->get_error_code() ) { |
| 68 |
$exception_code = 404; |
| 69 |
} |
| 70 |
throw new \Exception( $activation->get_error_message(), $exception_code ); |
| 71 |
} |
| 72 |
$this->client->settings()->activation_id = $activation->id; |
| 73 |
// validate the release. |
| 74 |
$this->validate_release(); |
| 75 |
} catch ( \Exception $e ) { |
| 76 |
// undo activation. |
| 77 |
$activation = $this->client->activation()->get(); |
| 78 |
if ( $activation ) { |
| 79 |
$this->client->activation()->delete(); |
| 80 |
} |
| 81 |
|
| 82 |
// on error, clear options. |
| 83 |
if ( 404 == $e->getCode() ) { |
| 84 |
$this->client->settings()->clear_options(); |
| 85 |
} |
| 86 |
|
| 87 |
// return \WP_Error. |
| 88 |
return new \WP_Error( 'error', $e->getMessage() ); |
| 89 |
} |
| 90 |
|
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Deactivate a license. |
| 96 |
* |
| 97 |
* @param string $activation_id The activation id. |
| 98 |
* |
| 99 |
* @return \WP_Error|true |
| 100 |
*/ |
| 101 |
public function deactivate( $activation_id = '' ) { |
| 102 |
if ( ! $activation_id ) { |
| 103 |
$activation_id = $this->client->settings()->activation_id; |
| 104 |
} |
| 105 |
|
| 106 |
$deactivated = $this->client->activation()->delete( sanitize_text_field( $activation_id ) ); |
| 107 |
|
| 108 |
if ( is_wp_error( $deactivated ) ) { |
| 109 |
// it has been deleted remotely. |
| 110 |
if ( 'not_found' === $deactivated->get_error_code() ) { |
| 111 |
$this->client->settings()->clear_options(); |
| 112 |
return true; |
| 113 |
} |
| 114 |
return $deactivated; |
| 115 |
} |
| 116 |
|
| 117 |
$this->client->settings()->clear_options(); |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Ge the current release |
| 123 |
* |
| 124 |
* @param integer $expires_in The amount of time until it expires. |
| 125 |
* |
| 126 |
* @return Object|WP_Error |
| 127 |
*/ |
| 128 |
public function get_current_release( $expires_in = 900 ) { |
| 129 |
$key = $this->client->settings()->license_key; |
| 130 |
if ( empty( $key ) ) { |
| 131 |
return new \WP_Error( 'license_key_missing', __( 'Please enter a license key.', 'quickwebp' ) ); |
| 132 |
} |
| 133 |
|
| 134 |
$activation_id = $this->client->settings()->activation_id; |
| 135 |
if ( empty( $activation_id ) ) { |
| 136 |
return new \WP_Error( 'activation_id_missing', __( 'This license is not yet activated.', 'quickwebp' ) ); |
| 137 |
} |
| 138 |
|
| 139 |
// There is no release because there is no zip file in the product. |
| 140 |
$release_data = new \stdClass(); |
| 141 |
$release_json = new \stdClass(); |
| 142 |
$release_json->slug = $this->client->slug; |
| 143 |
$release_json->version = '1.0.0'; |
| 144 |
$release_data->release_json = $release_json; |
| 145 |
return $release_data; |
| 146 |
|
| 147 |
// $route = add_query_arg( |
| 148 |
// array( |
| 149 |
// 'activation_id' => $activation_id, |
| 150 |
// 'expose_for' => $expires_in, |
| 151 |
// ), |
| 152 |
// trailingslashit( $this->endpoint ) . $key . '/expose_current_release' |
| 153 |
// ); |
| 154 |
|
| 155 |
// return $this->client->send_request( 'GET', $route ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Validate a license key. |
| 160 |
* |
| 161 |
* @param string $key The license key. |
| 162 |
* @param boolean $store Should we store the key and id. |
| 163 |
* @return Object |
| 164 |
* @throws \Exception If the license is not valid. |
| 165 |
*/ |
| 166 |
public function validate( $key, $store = false ) { |
| 167 |
// get license. |
| 168 |
$license = $this->retrieve( sanitize_text_field( $key ) ); |
| 169 |
if ( is_wp_error( $license ) ) { |
| 170 |
if ( 'not_found' === $license->get_error_code() ) { |
| 171 |
throw new \Exception( esc_html__( 'This is not a valid license. Please double-check it and try again.', 'quickwebp' ), 404 ); |
| 172 |
} |
| 173 |
throw new \Exception( esc_html( $license->get_error_message() ) ); |
| 174 |
} |
| 175 |
if ( empty( $license->id ) ) { |
| 176 |
throw new \Exception( esc_html__( 'This is not a valid license. Please double-check it and try again.', 'quickwebp' ), 404 ); |
| 177 |
} |
| 178 |
if ( 'revoked' === ( isset( $license->status ) ? $license->status : 'revoked' ) ) { |
| 179 |
throw new \Exception( esc_html__( 'This license has been revoked. Please re-purchase to obtain a new license.', 'quickwebp' ), 404 ); |
| 180 |
} |
| 181 |
|
| 182 |
if ( $store ) { |
| 183 |
$this->client->settings()->license_key = $license->key; |
| 184 |
$this->client->settings()->license_id = $license->id; |
| 185 |
} |
| 186 |
|
| 187 |
return $license; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Validate the current release. |
| 192 |
* |
| 193 |
* @return Object |
| 194 |
* @throws \Exception If the release is not valid. |
| 195 |
*/ |
| 196 |
public function validate_release() { |
| 197 |
$current_release = $this->get_current_release(); |
| 198 |
if ( is_wp_error( $current_release ) ) { |
| 199 |
$exception_code = 0; |
| 200 |
if ( 'not_found' === $current_release->get_error_code() ) { |
| 201 |
$exception_code = 404; |
| 202 |
} |
| 203 |
throw new \Exception( esc_html( $current_release->get_error_message() ), esc_html( $exception_code ) ); |
| 204 |
} |
| 205 |
// if there is no slug or it does not match. |
| 206 |
if ( empty( $current_release->release_json->slug ) || $this->client->slug !== $current_release->release_json->slug ) { |
| 207 |
throw new \Exception( esc_html__( 'This license is not valid for this product.', 'quickwebp' ) ); |
| 208 |
} |
| 209 |
return $current_release; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Check this is a valid license. |
| 214 |
* |
| 215 |
* @param string $license_key The license key. |
| 216 |
* |
| 217 |
* @return boolean|\WP_Error |
| 218 |
*/ |
| 219 |
public function is_valid( $license_key = '' ) { |
| 220 |
// already set. |
| 221 |
if ( null !== $this->is_valid_license ) { |
| 222 |
return $this->is_valid_license; |
| 223 |
} |
| 224 |
|
| 225 |
// check to see if a license is saved. |
| 226 |
if ( empty( $license_key ) ) { |
| 227 |
$license_key = $this->client->settings()->license_key; |
| 228 |
if ( empty( $license_key ) ) { |
| 229 |
$this->is_valid_license = false; |
| 230 |
return $this->is_valid_license; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// get the license from the server. |
| 235 |
$license = $this->retrieve( $license_key ); |
| 236 |
|
| 237 |
// validate the license response. |
| 238 |
$this->is_valid_license = $this->validate_license( $license ); |
| 239 |
|
| 240 |
// return validity. |
| 241 |
return $this->is_valid_license; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Is this license active? |
| 246 |
* |
| 247 |
* @return boolean |
| 248 |
*/ |
| 249 |
public function is_active() { |
| 250 |
if ( empty( $this->client->settings()->activation_id ) ) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
$activation = $this->client->activation()->get( $this->client->settings()->activation_id ); |
| 255 |
|
| 256 |
return ! empty( $activation->id ); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Validate the license response |
| 261 |
* |
| 262 |
* @param Object|\WP_Error $license The license response. |
| 263 |
* |
| 264 |
* @return \WP_Error|boolean |
| 265 |
*/ |
| 266 |
public function validate_license( $license ) { |
| 267 |
if ( is_wp_error( $license ) ) { |
| 268 |
if ( $license->get_error_code( 'not_found' ) ) { |
| 269 |
return new \WP_Error( $license->get_error_code(), __( 'This license key is not valid. Please double check it and try again.', 'quickwebp' ) ); |
| 270 |
} |
| 271 |
return $license; |
| 272 |
} |
| 273 |
|
| 274 |
// if we have a key and the status is not revoked. |
| 275 |
if ( ! empty( $license->key ) && isset( $license->status ) && 'revoked' !== $license->status ) { |
| 276 |
return true; |
| 277 |
} |
| 278 |
|
| 279 |
return false; |
| 280 |
} |
| 281 |
} |
| 282 |
|