PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.3.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.3.1
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / User / User.php

User.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.3.1, at classes/User/User.php

293 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\User;
3
4 use Date;
5 use Imagify_Data;
6 use WP_Error;
7
8 /**
9 * Imagify User class.
10 *
11 * @since 1.0
12 */
13 class User {
14 /**
15 * The Imagify user ID.
16 *
17 * @since 1.0
18 *
19 * @var string
20 */
21 public $id;
22
23 /**
24 * The user email.
25 *
26 * @since 1.0
27 *
28 * @var string
29 */
30 public $email;
31
32 /**
33 * The plan ID.
34 *
35 * @since 1.0
36 *
37 * @var int
38 */
39 public $plan_id;
40
41 /**
42 * The plan label.
43 *
44 * @since 1.2
45 *
46 * @var string
47 */
48 public $plan_label;
49
50 /**
51 * The total quota.
52 *
53 * @since 1.0
54 *
55 * @var int
56 */
57 public $quota;
58
59 /**
60 * The total extra quota (Imagify Pack).
61 *
62 * @since 1.0
63 *
64 * @var int
65 */
66 public $extra_quota;
67
68 /**
69 * The extra quota consumed.
70 *
71 * @since 1.0
72 *
73 * @var int
74 */
75 public $extra_quota_consumed;
76
77 /**
78 * The current month consumed quota.
79 *
80 * @since 1.0
81 *
82 * @var int
83 */
84 public $consumed_current_month_quota;
85
86 /**
87 * The next month date to credit the account.
88 *
89 * @since 1.1.1
90 *
91 * @var Date
92 */
93 public $next_date_update;
94
95 /**
96 * If the account is activate or not.
97 *
98 * @since 1.0.1
99 *
100 * @var bool
101 */
102 public $is_active;
103
104 /**
105 * If the account is monthly or yearly.
106 *
107 * @var bool
108 */
109 public $is_monthly;
110
111 /**
112 * Store a \WP_Error object if the request to fetch the user data failed.
113 * False overwise.
114 *
115 * @var bool|WP_Error
116 * @since 1.9.9
117 */
118 private $error;
119
120 /**
121 * The constructor.
122 *
123 * @since 1.0
124 *
125 * @return void
126 */
127 public function __construct() {
128 $user = get_imagify_user();
129
130 if ( is_wp_error( $user ) ) {
131 $this->error = $user;
132 return;
133 }
134
135 $this->id = $user->id;
136 $this->email = $user->email;
137 $this->plan_id = (int) $user->plan_id;
138 $this->plan_label = ucfirst( $user->plan_label );
139 $this->quota = $user->quota;
140 $this->extra_quota = $user->extra_quota;
141 $this->extra_quota_consumed = $user->extra_quota_consumed;
142 $this->consumed_current_month_quota = $user->consumed_current_month_quota;
143 $this->next_date_update = $user->next_date_update;
144 $this->is_active = $user->is_active;
145 $this->is_monthly = $user->is_monthly;
146 $this->error = is_wp_error( $user );
147 }
148
149 /**
150 * Get the possible error returned when fetching user data.
151 *
152 * @return bool|WP_Error A \WP_Error object if the request to fetch the user data failed. False overwise.
153 * @since 1.9.9
154 */
155 public function get_error() {
156 return $this->error;
157 }
158
159 /**
160 * Percentage of consumed quota, including extra quota.
161 *
162 * @since 1.0
163 *
164 * @return float|int
165 */
166 public function get_percent_consumed_quota() {
167 static $done = false;
168
169 if ( $this->get_error() ) {
170 return 0;
171 }
172
173 $quota = $this->quota;
174 $consumed_quota = $this->consumed_current_month_quota;
175
176 if ( imagify_round_half_five( $this->extra_quota_consumed ) < $this->extra_quota ) {
177 $quota += $this->extra_quota;
178 $consumed_quota += $this->extra_quota_consumed;
179 }
180
181 if ( ! $quota || ! $consumed_quota ) {
182 $percent = 0;
183 } else {
184 $percent = 100 * $consumed_quota / $quota;
185 $percent = round( $percent, 1 );
186 $percent = min( max( 0, $percent ), 100 );
187 }
188
189 $percent = (float) $percent;
190
191 if ( $done ) {
192 return $percent;
193 }
194
195 $previous_percent = Imagify_Data::get_instance()->get( 'previous_quota_percent' );
196
197 // Percent is not 100% anymore.
198 if ( 100.0 === (float) $previous_percent && $percent < 100 ) {
199 /**
200 * Triggered when the consumed quota percent decreases below 100%.
201 *
202 * @since 1.7
203 * @author Grégory Viguier
204 *
205 * @param float|int $percent The current percentage of consumed quota.
206 */
207 do_action( 'imagify_not_over_quota_anymore', $percent );
208 }
209
210 // Percent is not >= 80% anymore.
211 if ( ( (float) $previous_percent >= 80.0 && $percent < 80 ) ) {
212 /**
213 * Triggered when the consumed quota percent decreases below 80%.
214 *
215 * @since 1.7
216 * @author Grégory Viguier
217 *
218 * @param float|int $percent The current percentage of consumed quota.
219 * @param float|int $previous_percent The previous percentage of consumed quota.
220 */
221 do_action( 'imagify_not_almost_over_quota_anymore', $percent, $previous_percent );
222 }
223
224 if ( (float) $previous_percent !== (float) $percent ) {
225 Imagify_Data::get_instance()->set( 'previous_quota_percent', $percent );
226 }
227
228 $done = true;
229
230 return $percent;
231 }
232
233 /**
234 * Count percent of unconsumed quota.
235 *
236 * @since 1.0
237 *
238 * @return float|int
239 */
240 public function get_percent_unconsumed_quota() {
241 return 100 - $this->get_percent_consumed_quota();
242 }
243
244 /**
245 * Check if the user has a free account.
246 *
247 * @since 1.1.1
248 *
249 * @return bool
250 */
251 public function is_free() {
252 return 1 === $this->plan_id;
253 }
254
255 /**
256 * Check if the user is a growth account
257 *
258 * @return bool
259 */
260 public function is_growth() {
261 return ( 16 === $this->plan_id || 18 === $this->plan_id );
262 }
263
264 /**
265 * Check if the user is an infinite account
266 *
267 * @return bool
268 */
269 public function is_infinite() {
270 return ( 15 === $this->plan_id || 17 === $this->plan_id );
271 }
272
273 /**
274 * Check if the user has consumed all his/her quota.
275 *
276 * @since 1.1.1
277 * @since 1.9.9 Return false if the request to fetch the user data failed.
278 *
279 * @return bool
280 */
281 public function is_over_quota() {
282 if ( $this->get_error() ) {
283 return false;
284 }
285
286 return (
287 $this->is_free()
288 &&
289 floatval( 100 ) === round( $this->get_percent_consumed_quota() )
290 );
291 }
292 }
293