PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.9
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.9
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-files-stats.php

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

477 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || exit;
3
4 /**
5 * Class handling stats related to "custom folders optimization".
6 *
7 * @since 1.7
8 * @author Grégory Viguier
9 */
10 class Imagify_Files_Stats {
11
12 /**
13 * Class version.
14 *
15 * @var string
16 * @since 1.7
17 * @author Grégory Viguier
18 */
19 const VERSION = '1.0';
20
21
22 /** ----------------------------------------------------------------------------------------- */
23 /** COUNT FILES ============================================================================= */
24 /** ----------------------------------------------------------------------------------------- */
25
26 /**
27 * Count number of images in custom folders.
28 *
29 * @since 1.7
30 * @access public
31 * @author Grégory Viguier
32 *
33 * @return int The number of images.
34 */
35 public static function count_all_files() {
36 /**
37 * Filter the number of images in custom folders.
38 *
39 * @since 1.7
40 * @author Grégory Viguier
41 *
42 * @param int|bool $pre_count Default is false. Provide an integer.
43 */
44 $pre_count = apply_filters( 'imagify_count_files', false );
45
46 if ( false !== $pre_count ) {
47 return (int) $pre_count;
48 }
49
50 return self::count_files( 'all' );
51 }
52
53 /**
54 * Count number of images in custom folders with an error.
55 *
56 * @since 1.7
57 * @access public
58 * @author Grégory Viguier
59 *
60 * @return int The number of images.
61 */
62 public static function count_error_files() {
63 /**
64 * Filter the number of images in custom folders with an error.
65 *
66 * @since 1.7
67 * @author Grégory Viguier
68 *
69 * @param int|bool $pre_count Default is false. Provide an integer.
70 */
71 $pre_count = apply_filters( 'imagify_count_error_files', false );
72
73 if ( false !== $pre_count ) {
74 return (int) $pre_count;
75 }
76
77 return self::count_files( 'error' );
78 }
79
80 /**
81 * Count number of images successfully optimized by Imagify in custom folders.
82 *
83 * @since 1.7
84 * @access public
85 * @author Grégory Viguier
86 *
87 * @return int The number of images.
88 */
89 public static function count_success_files() {
90 /**
91 * Filter the number of images successfully optimized by Imagify in custom folders.
92 *
93 * @since 1.7
94 * @author Grégory Viguier
95 *
96 * @param int|bool $pre_count Default is false. Provide an integer.
97 */
98 $pre_count = apply_filters( 'imagify_count_success_files', false );
99
100 if ( false !== $pre_count ) {
101 return (int) $pre_count;
102 }
103
104 return self::count_files( 'success' );
105 }
106
107 /**
108 * Count number of optimized images in custom folders.
109 *
110 * @since 1.7
111 * @access public
112 * @author Grégory Viguier
113 *
114 * @return int The number of images.
115 */
116 public static function count_optimized_files() {
117 /**
118 * Filter the number of optimized images in custom folders.
119 *
120 * @since 1.7
121 * @author Grégory Viguier
122 *
123 * @param int|bool $pre_count Default is false. Provide an integer.
124 */
125 $pre_count = apply_filters( 'imagify_count_optimized_files', false );
126
127 if ( false !== $pre_count ) {
128 return (int) $pre_count;
129 }
130
131 return self::count_files( 'optimized' );
132 }
133
134 /**
135 * Count number of unoptimized images in custom folders.
136 *
137 * @since 1.7
138 * @access public
139 * @author Grégory Viguier
140 *
141 * @return int The number of images.
142 */
143 public static function count_unoptimized_files() {
144 /**
145 * Filter the number of unoptimized images in custom folders.
146 *
147 * @since 1.7
148 * @author Grégory Viguier
149 *
150 * @param int|bool $pre_count Default is false. Provide an integer.
151 */
152 $pre_count = apply_filters( 'imagify_count_unoptimized_files', false );
153
154 if ( false !== $pre_count ) {
155 return (int) $pre_count;
156 }
157
158 return self::count_files( 'unoptimized' );
159 }
160
161 /**
162 * Count number of images without status in custom folders.
163 *
164 * @since 1.7
165 * @access public
166 * @author Grégory Viguier
167 *
168 * @return int The number of images.
169 */
170 public static function count_no_status_files() {
171 /**
172 * Filter the number of images without status in custom folders.
173 *
174 * @since 1.7
175 * @author Grégory Viguier
176 *
177 * @param int|bool $pre_count Default is false. Provide an integer.
178 */
179 $pre_count = apply_filters( 'imagify_count_no_status_files', false );
180
181 if ( false !== $pre_count ) {
182 return (int) $pre_count;
183 }
184
185 return self::count_files( 'none' );
186 }
187
188 /**
189 * Count number of images in custom folders.
190 *
191 * @since 1.7
192 * @access public
193 * @author Grégory Viguier
194 *
195 * @param string $status The status of these folders: all, success, already_optimized, optimized, error, none, unoptimized.
196 * "none" if for files without status.
197 * "optimized" regroups "success" and "already_optimized".
198 * "unoptimized" regroups "error" and "none".
199 * @return int The number of images.
200 */
201 public static function count_files( $status = 'all' ) {
202 global $wpdb;
203 static $count = [];
204
205 $status = self::validate_status( $status );
206
207 if ( isset( $count[ $status ] ) ) {
208 return $count[ $status ];
209 }
210
211 $files_db = Imagify_Files_DB::get_instance();
212
213 if ( ! $files_db->can_operate() ) {
214 $count[ $status ] = 0;
215 return $count[ $status ];
216 }
217
218 switch ( $status ) {
219 case 'all':
220 $status = '';
221 break;
222
223 case 'none':
224 $status = 'status IS NULL';
225 break;
226
227 case 'optimized':
228 $status = "status IN ('success','already_optimized')";
229 break;
230
231 case 'unoptimized':
232 $status = "( status = 'error' OR status IS NULL )";
233 break;
234
235 default:
236 // success, already_optimized or error.
237 $status = "status = '$status'";
238 }
239
240 $table_name = $files_db->get_table_name();
241 $status = $status ? "WHERE $status" : '';
242
243 $count[ $status ] = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
244 "SELECT COUNT( file_id ) FROM $table_name $status"
245 );
246
247 return $count[ $status ];
248 }
249
250
251 /** ----------------------------------------------------------------------------------------- */
252 /** PERCENTS ================================================================================ */
253 /** ----------------------------------------------------------------------------------------- */
254
255 /**
256 * Count percent of optimized images in custom folders.
257 *
258 * @since 1.7
259 * @access public
260 * @author Grégory Viguier
261 *
262 * @return int The percent of optimized images.
263 */
264 public static function percent_optimized_files() {
265 /**
266 * Filter the percent of optimized images in custom folders.
267 *
268 * @since 1.7
269 * @author Grégory Viguier
270 *
271 * @param int|bool $percent Default is false. Provide an integer.
272 */
273 $percent = apply_filters( 'imagify_percent_optimized_files', false );
274
275 if ( false !== $percent ) {
276 return (int) $percent;
277 }
278
279 $total_files = self::count_all_files();
280 $total_optimized_files = self::count_optimized_files();
281
282 if ( ! $total_files || ! $total_optimized_files ) {
283 return 0;
284 }
285
286 return min( round( 100 * $total_optimized_files / $total_files ), 100 );
287 }
288
289
290 /** ----------------------------------------------------------------------------------------- */
291 /** GET FILE SIZES ========================================================================== */
292 /** ----------------------------------------------------------------------------------------- */
293
294 /**
295 * Sum up all optimized sizes of all successfully optimized files.
296 *
297 * @since 1.7
298 * @access public
299 * @author Grégory Viguier
300 *
301 * @return int The sizes sum in bytes.
302 */
303 public static function get_optimized_size() {
304 /**
305 * Filter the optimized sizes of all successfully optimized files.
306 *
307 * @since 1.7
308 * @author Grégory Viguier
309 *
310 * @param int|bool $pre_size Default is false. Provide an integer.
311 */
312 $pre_size = apply_filters( 'imagify_get_optimized_files_size', false );
313
314 if ( false !== $pre_size ) {
315 return (int) $pre_size;
316 }
317
318 return self::get_size( 'optimized' );
319 }
320
321 /**
322 * Sum up all original sizes of all successfully optimized files.
323 *
324 * @since 1.7
325 * @access public
326 * @author Grégory Viguier
327 *
328 * @return int The sizes sum in bytes.
329 */
330 public static function get_original_size() {
331 /**
332 * Filter the original sizes of all successfully optimized files.
333 *
334 * @since 1.7
335 * @author Grégory Viguier
336 *
337 * @param int|bool $pre_size Default is false. Provide an integer.
338 */
339 $pre_size = apply_filters( 'imagify_get_original_files_size', false );
340
341 if ( false !== $pre_size ) {
342 return (int) $pre_size;
343 }
344
345 return self::get_size( 'original' );
346 }
347
348 /**
349 * Sum up all (optimized|original) sizes of all successfully optimized files.
350 *
351 * @since 1.7
352 * @access public
353 * @author Grégory Viguier
354 *
355 * @param string $type "optimized" or "original".
356 * @return int The sizes sum in bytes.
357 */
358 public static function get_size( $type = null ) {
359 global $wpdb;
360 static $sizes = [];
361
362 $type = 'optimized' === $type ? 'optimized_size' : 'original_size';
363
364 if ( isset( $sizes[ $type ] ) ) {
365 return $sizes[ $type ];
366 }
367
368 $files_db = Imagify_Files_DB::get_instance();
369
370 if ( ! $files_db->can_operate() ) {
371 $sizes[ $type ] = 0;
372 return $sizes[ $type ];
373 }
374
375 $table_name = $files_db->get_table_name();
376 $sizes[ $type ] = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
377 "SELECT SUM( $type ) FROM $table_name WHERE status = 'success'"
378 );
379
380 return $sizes[ $type ];
381 }
382
383 /**
384 * Sum up all original sizes.
385 *
386 * @since 1.7
387 * @access public
388 * @author Grégory Viguier
389 *
390 * @return int The sizes sum in bytes.
391 */
392 public static function get_overall_original_size() {
393 global $wpdb;
394 static $size;
395
396 if ( isset( $size ) ) {
397 return $size;
398 }
399
400 $files_db = Imagify_Files_DB::get_instance();
401
402 if ( ! $files_db->can_operate() ) {
403 $size = 0;
404 return $size;
405 }
406
407 $table_name = $files_db->get_table_name();
408 $sql = $wpdb->get_var( "SELECT SUM( original_size ) FROM $table_name" ); // WPCS: unprepared SQL ok.
409 $size = is_null( $sql ) ? 0 : round( $sql );
410
411 return $size;
412 }
413
414 /**
415 * Calculate the average size of the images uploaded per month.
416 *
417 * @since 1.7
418 * @access public
419 * @author Grégory Viguier
420 *
421 * @return int The current average size of images uploaded per month in bytes.
422 */
423 public static function calculate_average_size_per_month() {
424 global $wpdb;
425 static $average;
426
427 if ( isset( $average ) ) {
428 return $average;
429 }
430
431 $files_db = Imagify_Files_DB::get_instance();
432
433 if ( ! $files_db->can_operate() ) {
434 $average = 0;
435 return $average;
436 }
437
438 $table_name = $files_db->get_table_name();
439 $sql = $wpdb->get_var( "SELECT AVG( size ) AS average_size_per_month FROM ( SELECT SUM( original_size ) AS size FROM $table_name GROUP BY YEAR( file_date ), MONTH( file_date ) ) AS size_per_month" ); // WPCS: unprepared SQL ok.
440 $average = is_null( $sql ) ? 0 : round( $sql );
441
442 return $average;
443 }
444
445
446 /** ----------------------------------------------------------------------------------------- */
447 /** TOOLS =================================================================================== */
448 /** ----------------------------------------------------------------------------------------- */
449
450 /**
451 * Validate a status.
452 *
453 * @since 1.7
454 * @access public
455 * @author Grégory Viguier
456 *
457 * @param string $status The status of these folders: all, success, already_optimized, optimized, error, none, unoptimized.
458 * "none" if for files without status.
459 * "optimized" regroups "success" and "already_optimized".
460 * "unoptimized" regroups "error" and "none".
461 * @return string Fallback to 'all' if the status is not valid.
462 */
463 public static function validate_status( $status = 'all' ) {
464 $statuses = [
465 'all' => 1,
466 'success' => 1,
467 'already_optimized' => 1,
468 'error' => 1,
469 'none' => 1,
470 'optimized' => 1,
471 'unoptimized' => 1,
472 ];
473
474 return isset( $statuses[ $status ] ) ? $status : 'all';
475 }
476 }
477