PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.10
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.10
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 / classes / class-imagify-user.php

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

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