| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla\Licensing; |
| 4 |
|
| 5 |
class Poller |
| 6 |
{ |
| 7 |
/** |
| 8 |
* @var API |
| 9 |
*/ |
| 10 |
protected $api; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var License |
| 14 |
*/ |
| 15 |
protected $license; |
| 16 |
|
| 17 |
/** |
| 18 |
* Poller constructor. |
| 19 |
* |
| 20 |
* @param API $api |
| 21 |
* @param License $license |
| 22 |
*/ |
| 23 |
public function __construct(API $api, License $license) |
| 24 |
{ |
| 25 |
$this->api = $api; |
| 26 |
$this->license = $license; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Add hooks. |
| 31 |
*/ |
| 32 |
public function init() |
| 33 |
{ |
| 34 |
if (! wp_next_scheduled('boxzilla_check_license_status')) { |
| 35 |
wp_schedule_event(time(), 'daily', 'boxzilla_check_license_status'); |
| 36 |
} |
| 37 |
|
| 38 |
add_action('boxzilla_check_license_status', [ $this, 'run' ]); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Run! |
| 43 |
*/ |
| 44 |
public function run() |
| 45 |
{ |
| 46 |
// don't run if license not active |
| 47 |
if (! $this->license->activated) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// assume valid by default, in case of server errors on our side. |
| 52 |
$license_still_valid = true; |
| 53 |
|
| 54 |
try { |
| 55 |
$remote_license = $this->api->get_license(); |
| 56 |
$license_still_valid = $remote_license->valid; |
| 57 |
} catch (API_Exception $e) { |
| 58 |
// license key wasn't found or expired |
| 59 |
if (in_array($e->getApiCode(), [ 'license_invalid', 'license_expired' ], true)) { |
| 60 |
$license_still_valid = false; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
if (! $license_still_valid) { |
| 65 |
$this->license->activated = false; |
| 66 |
$this->license->save(); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|