PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.8
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.8
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 / inc / deprecated / classes / class-imagify-user.php

class-imagify-user.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.8, at inc/deprecated/classes/class-imagify-user.php

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