| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\License; |
| 4 |
|
| 5 |
use YayMail\License\LicenseHandler; |
| 6 |
use YayMail\License\LicenseAPI; |
| 7 |
use YayMail\License\License; |
| 8 |
|
| 9 |
/** |
| 10 |
* LicensingPlugin |
| 11 |
* |
| 12 |
* @package LicensingPlugin |
| 13 |
*/ |
| 14 |
class LicensingPlugin { |
| 15 |
|
| 16 |
public $slug = null; |
| 17 |
|
| 18 |
protected $plugin_info = null; |
| 19 |
|
| 20 |
protected $license = null; |
| 21 |
|
| 22 |
public function __construct( $slug ) { |
| 23 |
$this->slug = $slug; |
| 24 |
$licensing_plugins = LicenseHandler::get_licensing_plugins(); |
| 25 |
$matching_position = array_search( $this->slug, array_column( $licensing_plugins, 'slug' ) ); |
| 26 |
if ( false !== $matching_position ) { |
| 27 |
$this->plugin_info = $licensing_plugins[ $matching_position ]; |
| 28 |
} |
| 29 |
$this->license = new License( $slug ); |
| 30 |
} |
| 31 |
|
| 32 |
public function get_option( $key ) { |
| 33 |
if ( $this->plugin_info ) { |
| 34 |
return $this->plugin_info[ $key ]; |
| 35 |
} |
| 36 |
return null; |
| 37 |
} |
| 38 |
|
| 39 |
public function get_license() { |
| 40 |
return $this->license; |
| 41 |
} |
| 42 |
|
| 43 |
public function get_version_info() { |
| 44 |
$info = get_option( $this->slug . '_version_info' ); |
| 45 |
$info = is_string( $info ) ? \json_decode( $info, true ) : $info; |
| 46 |
return $info; |
| 47 |
} |
| 48 |
|
| 49 |
public function set_version_info( $data ) { |
| 50 |
update_option( $this->slug . '_version_info', $data ); |
| 51 |
} |
| 52 |
|
| 53 |
public function update_version_info() { |
| 54 |
$license = $this->license; |
| 55 |
if ( $license instanceof License ) { |
| 56 |
$license_key = $license->get_license_key(); |
| 57 |
$item_id = $this->get_option( 'item_id' ); |
| 58 |
$response = LicenseAPI::get_version( $item_id, $license_key ); |
| 59 |
if ( $response ) { |
| 60 |
$this->set_version_info( $response ); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|