V5000.php
2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace ACP\Plugin\Update;
use AC\Plugin\Update;
use ACP\Entity\License;
use ACP\LicenseKeyRepository;
use ACP\LicenseRepository;
use ACP\Type\License\ExpiryDate;
use ACP\Type\License\Key;
use ACP\Type\License\RenewalDiscount;
use ACP\Type\License\RenewalMethod;
use ACP\Type\License\Status;
use DateTime;
use DateTimeZone;
use Exception;
/**
* Migrate license settings per site
*/
class V5000 extends Update {
const LICENSE_OPTION_KEY = 'cpupdate_cac-pro';
/**
* @throws Exception
*/
public function apply_update() {
$this->migrate_license();
}
protected function set_version() {
$this->version = '5.0.0';
}
private function migrate_license() {
$license_key_value = defined( 'ACP_LICENCE' ) && ACP_LICENCE
? ACP_LICENCE
: (string) $this->get_license_option();
if ( ! Key::is_valid( $license_key_value ) ) {
return;
}
$status = $this->get_license_option( '_sts' );
if ( ! Status::is_valid( $status ) ) {
return;
}
$expiry_date = $this->get_license_option( '_expiry_date' )
? DateTime::createFromFormat( 'U', $this->get_license_option( '_expiry_date' ), new DateTimeZone( 'Europe/Amsterdam' ) )
: null;
if ( $expiry_date === false ) {
return;
}
$renewal_method = $this->get_license_option( '_renewal_method' );
if ( ! RenewalMethod::is_valid( $renewal_method ) ) {
$renewal_method = RenewalMethod::METHOD_MANUAL;
}
$discount = (int) $this->get_license_option( '_renewal_discount' );
if ( ! RenewalDiscount::is_valid( $discount ) ) {
$discount = 0;
}
$license = new License(
new Key( $license_key_value ),
new Status( $status ),
new RenewalDiscount( $discount ),
new RenewalMethod( $renewal_method ),
new ExpiryDate( $expiry_date )
);
$this->save_license( $license );
}
protected function save_license( License $license ) {
( new LicenseRepository() )->save( $license );
( new LicenseKeyRepository() )->save( $license->get_key() );
}
protected function get_license_option( $key = '' ) {
return get_option( self::LICENSE_OPTION_KEY . $key );
}
}