PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.7
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.7, at classes/User/User.php

362 lines 6.4 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 Imagify_Data;
5 use WP_Error;
6
7 /**
8 * Imagify User class.
9 *
10 * @since 1.0
11 */
12 class User {
13 /**
14 * The Imagify user ID.
15 *
16 * @since 1.0
17 *
18 * @var string
19 */
20 public $id;
21
22 /**
23 * The user email.
24 *
25 * @since 1.0
26 *
27 * @var string
28 */
29 public $email;
30
31 /**
32 * The plan ID.
33 *
34 * @since 1.0
35 *
36 * @var int
37 */
38 public $plan_id;
39
40 /**
41 * The plan label.
42 *
43 * @since 1.2
44 *
45 * @var string
46 */
47 public $plan_label;
48
49 /**
50 * The total quota.
51 *
52 * @since 1.0
53 *
54 * @var int
55 */
56 public $quota;
57
58 /**
59 * The total extra quota (Imagify Pack).
60 *
61 * @since 1.0
62 *
63 * @var int
64 */
65 public $extra_quota;
66
67 /**
68 * The extra quota consumed.
69 *
70 * @since 1.0
71 *
72 * @var int
73 */
74 public $extra_quota_consumed;
75
76 /**
77 * The current month consumed quota.
78 *
79 * @since 1.0
80 *
81 * @var int
82 */
83 public $consumed_current_month_quota;
84
85 /**
86 * The next month date to credit the account.
87 *
88 * @since 1.1.1
89 *
90 * @var string
91 */
92 public $next_date_update;
93
94 /**
95 * If the account is activate or not.
96 *
97 * @since 1.0.1
98 *
99 * @var bool
100 */
101 public $is_active;
102
103 /**
104 * If the account is monthly or yearly.
105 *
106 * @var bool
107 */
108 public $is_monthly;
109
110 /**
111 * Store a \WP_Error object if the request to fetch the user data failed.
112 * False overwise.
113 *
114 * @var bool|WP_Error
115 * @since 1.9.9
116 */
117 private $error = false;
118
119 /**
120 * Initialisation.
121 *
122 * @var bool
123 */
124 protected $initialized = false;
125
126 /**
127 * Initialise the user data by fetching the api data
128 *
129 * @return void
130 */
131 public function init_user() {
132 if ( $this->initialized ) {
133 return;
134 }
135
136 $user = get_imagify_user();
137
138 if ( is_wp_error( $user ) ) {
139 $this->error = $user;
140 return;
141 }
142
143 $this->set_user_properties( $user );
144 $this->initialized = true;
145 }
146
147 /**
148 * Set user properties
149 *
150 * @param object $user User object data.
151 *
152 * @return void
153 */
154 private function set_user_properties( $user ) {
155 $this->id = $user->id;
156 $this->email = $user->email;
157 $this->plan_id = (int) $user->plan_id;
158 $this->plan_label = ucfirst( $user->plan_label );
159 $this->quota = $user->quota;
160 $this->extra_quota = $user->extra_quota;
161 $this->extra_quota_consumed = $user->extra_quota_consumed;
162 $this->consumed_current_month_quota = $user->consumed_current_month_quota;
163 $this->next_date_update = $user->next_date_update;
164 $this->is_active = $user->is_active;
165 $this->is_monthly = $user->is_monthly;
166 }
167
168 /**
169 * Get the possible error returned when fetching user data.
170 *
171 * @return bool|WP_Error A \WP_Error object if the request to fetch the user data failed. False overwise.
172 * @since 1.9.9
173 */
174 public function get_error() {
175 $this->init_user();
176
177 return $this->error;
178 }
179
180 /**
181 * Percentage of consumed quota, including extra quota.
182 *
183 * @since 1.0
184 *
185 * @return float|int
186 */
187 public function get_percent_consumed_quota() {
188 static $done = false;
189
190 if ( $this->get_error() ) {
191 return 0;
192 }
193
194 $quota = $this->quota;
195 $consumed_quota = $this->consumed_current_month_quota;
196
197 if ( imagify_round_half_five( $this->extra_quota_consumed ) < $this->extra_quota ) {
198 $quota += $this->extra_quota;
199 $consumed_quota += $this->extra_quota_consumed;
200 }
201
202 if ( ! $quota || ! $consumed_quota ) {
203 $percent = 0;
204 } else {
205 $percent = 100 * $consumed_quota / $quota;
206 $percent = round( $percent, 1 );
207 $percent = min( max( 0, $percent ), 100 );
208 }
209
210 $percent = (float) $percent;
211
212 if ( $done ) {
213 return $percent;
214 }
215
216 $previous_percent = Imagify_Data::get_instance()->get( 'previous_quota_percent' );
217
218 // Percent is not 100% anymore.
219 if ( 100.0 === (float) $previous_percent && $percent < 100 ) {
220 /**
221 * Triggered when the consumed quota percent decreases below 100%.
222 *
223 * @since 1.7
224 * @author Grégory Viguier
225 *
226 * @param float|int $percent The current percentage of consumed quota.
227 */
228 do_action( 'imagify_not_over_quota_anymore', $percent );
229 }
230
231 // Percent is not >= 80% anymore.
232 if ( ( (float) $previous_percent >= 80.0 && $percent < 80 ) ) {
233 /**
234 * Triggered when the consumed quota percent decreases below 80%.
235 *
236 * @since 1.7
237 * @author Grégory Viguier
238 *
239 * @param float|int $percent The current percentage of consumed quota.
240 * @param float|int $previous_percent The previous percentage of consumed quota.
241 */
242 do_action( 'imagify_not_almost_over_quota_anymore', $percent, $previous_percent );
243 }
244
245 if ( (float) $previous_percent !== (float) $percent ) {
246 Imagify_Data::get_instance()->set( 'previous_quota_percent', $percent );
247 }
248
249 $done = true;
250
251 return $percent;
252 }
253
254 /**
255 * Count percent of unconsumed quota.
256 *
257 * @since 1.0
258 *
259 * @return float|int
260 */
261 public function get_percent_unconsumed_quota() {
262 $this->init_user();
263 return 100 - $this->get_percent_consumed_quota();
264 }
265
266 /**
267 * Check if the user has a free account.
268 *
269 * @since 1.1.1
270 *
271 * @return bool
272 */
273 public function is_free() {
274 $this->init_user();
275 return 1 === $this->plan_id;
276 }
277
278 /**
279 * Check if the user is a growth account
280 *
281 * @return bool
282 */
283 public function is_growth() {
284 $this->init_user();
285 return ( 16 === $this->plan_id || 18 === $this->plan_id );
286 }
287
288 /**
289 * Check if the user is an infinite account
290 *
291 * @return bool
292 */
293 public function is_infinite() {
294 $this->init_user();
295 return ( 15 === $this->plan_id || 17 === $this->plan_id );
296 }
297
298 /**
299 * Check if the user has consumed all his/her quota.
300 *
301 * @since 1.1.1
302 * @since 1.9.9 Return false if the request to fetch the user data failed.
303 *
304 * @return bool
305 */
306 public function is_over_quota() {
307 if ( $this->get_error() ) {
308 return false;
309 }
310
311 return (
312 $this->is_free()
313 &&
314 floatval( 100 ) === round( $this->get_percent_consumed_quota() )
315 );
316 }
317
318 /**
319 * Get user Id
320 *
321 * @return string
322 */
323 public function get_id() {
324 $this->init_user();
325
326 return $this->id;
327 }
328
329 /**
330 * Get user email.
331 *
332 * @return string
333 */
334 public function get_email() {
335 $this->init_user();
336
337 return $this->email;
338 }
339
340 /**
341 * Get plan id.
342 *
343 * @return int
344 */
345 public function get_plan_id() {
346 $this->init_user();
347
348 return $this->plan_id;
349 }
350
351 /**
352 * Get user quota.
353 *
354 * @return int
355 */
356 public function get_quota() {
357 $this->init_user();
358
359 return $this->quota;
360 }
361 }
362