class-otgs-installer-subscription.php
4.05 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
class OTGS_Installer_Subscription {
const SUBSCRIPTION_STATUS_INACTIVE = 0;
const SUBSCRIPTION_STATUS_ACTIVE = 1;
const SUBSCRIPTION_STATUS_EXPIRED = 2;
const SUBSCRIPTION_STATUS_INACTIVE_UPGRADED = 3;
const SUBSCRIPTION_STATUS_ACTIVE_NO_EXPIRATION = 4;
const SITE_KEY_TYPE_PRODUCTION = 0;
const SITE_KEY_TYPE_DEVELOPMENT = 1;
const SUBSCRIPTION_STATUS_TEXT_EXPIRED = 'expired';
const SUBSCRIPTION_STATUS_TEXT_VALID = 'valid';
const SUBSCRIPTION_STATUS_TEXT_REFUNDED = 'refunded';
const SUBSCRIPTION_STATUS_TEXT_MISSING = 'missing';
private $status;
private $expires;
private $site_key;
private $site_key_type;
private $site_url;
private $type;
private $registered_by;
private $data;
private $notes;
/**
* WPML_Installer_Subscription constructor.
*
* @param array|null $subscription
*/
public function __construct( $subscription = array() ) {
if ( $subscription ) {
if ( isset( $subscription['data'] ) ) {
$this->data = $subscription['data'];
}
if ( isset( $subscription['data']->status ) ) {
$this->status = (int) $subscription['data']->status;
}
if ( isset( $subscription['data']->expires ) ) {
$this->expires = $subscription['data']->expires;
}
if ( isset( $subscription['data']->notes ) ) {
$this->notes = $subscription['data']->notes;
}
if ( isset( $subscription['key'] ) ) {
$this->site_key = $subscription['key'];
}
if ( isset( $subscription['site_url'] ) ) {
$this->site_url = $subscription['site_url'];
}
if ( isset( $subscription['registered_by'] ) ) {
$this->registered_by = $subscription['registered_by'];
}
if ( isset( $subscription['data']->subscription_type ) ) {
$this->type = $subscription['data']->subscription_type;
}
$this->site_key_type = isset($subscription['key_type'])
? $subscription['key_type'] : self::SITE_KEY_TYPE_PRODUCTION;
}
}
public function get_subscription_status_text() {
if ( $this->is_expired() ) {
return self::SUBSCRIPTION_STATUS_TEXT_EXPIRED;
}
if ( $this->is_valid() ) {
return self::SUBSCRIPTION_STATUS_TEXT_VALID;
}
if ( $this->is_refunded() ) {
return self::SUBSCRIPTION_STATUS_TEXT_REFUNDED;
}
return self::SUBSCRIPTION_STATUS_TEXT_MISSING;
}
/**
* @param int $expiredForPeriod
* @return bool
*/
private function is_expired( $expiredForPeriod = 0 ) {
return ! $this->is_lifetime()
&& (
self::SUBSCRIPTION_STATUS_EXPIRED === $this->get_status()
|| ( $this->get_expiration() && strtotime( $this->get_expiration() ) <= time() - $expiredForPeriod )
);
}
/**
* @return bool
*/
private function is_lifetime() {
return $this->get_status() === self::SUBSCRIPTION_STATUS_ACTIVE_NO_EXPIRATION;
}
private function get_status() {
return $this->status;
}
private function get_expiration() {
return $this->expires;
}
public function get_site_key() {
return $this->site_key;
}
public function get_site_url() {
return $this->site_url;
}
public function get_type() {
return $this->type;
}
public function get_site_key_type() {
return $this->site_key_type;
}
public function get_registered_by() {
return $this->registered_by;
}
public function get_data() {
return $this->data;
}
/**
* @param int $expiredForPeriod
* @return bool
*/
public function is_valid( $expiredForPeriod = 0 ) {
return ( $this->is_lifetime()
|| ( $this->get_status() === self::SUBSCRIPTION_STATUS_ACTIVE && ! $this->is_expired( $expiredForPeriod ) ) );
}
/**
* @param int $expiredForPeriod
* @return bool
*/
public function is_in_grace( $expiredForPeriod = 0 ) {
return ! $this->is_lifetime()
&& (
self::SUBSCRIPTION_STATUS_ACTIVE === $this->get_status()
&& ( $this->get_expiration() &&
( strtotime( $this->get_expiration() ) >= time() - $expiredForPeriod &&
strtotime( $this->get_expiration() ) <= time() ) )
);
}
public function is_refunded() {
return ! $this->is_lifetime() &&
$this->get_status() === self::SUBSCRIPTION_STATUS_INACTIVE &&
$this->notes === 'Payment refunded to user';
}
}