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