PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.5
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.5
1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 All 32 releases
image-optimization / modules / stats / classes / stats.php

stats.php in Image Optimization – Compress Images and Convert to WebP or AVIF 1.6.5, at modules/stats/classes/stats.php

124 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Modules\Stats\Classes;
4
5 use ImageOptimization\Classes\Async_Operation\{
6 Async_Operation,
7 Async_Operation_Hook,
8 Queries\Image_Optimization_Operation_Query,
9 Queries\Operation_Query,
10 };
11 use ImageOptimization\Classes\Image\Image_Query_Builder;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 class Stats {
18 public static function calculate_global_stats(): array {
19 $bulk_optimization_operation_status = self::get_bulk_optimization_status();
20 $bulk_optimization_operation_id = Async_Operation::OPERATION_STATUS_RUNNING === $bulk_optimization_operation_status
21 ? self::get_bulk_optimization_active_operation_id()
22 : null;
23
24 return [
25 'optimization_stats' => Optimization_Stats::get_image_stats(),
26 'bulk_optimization_status' => $bulk_optimization_operation_status,
27 'bulk_optimization_operation_id' => $bulk_optimization_operation_id,
28 'bulk_restoring_status' => self::get_bulk_restoring_status(),
29 'bulk_backup_removing_status' => self::get_bulk_backup_removing_status(),
30 'backups_exist' => self::backups_exist(),
31 ];
32 }
33
34 private static function get_bulk_optimization_status(): string {
35 $active_query = ( new Image_Optimization_Operation_Query() )
36 ->set_hook( Async_Operation_Hook::OPTIMIZE_BULK )
37 ->set_status( [
38 Async_Operation::OPERATION_STATUS_PENDING,
39 Async_Operation::OPERATION_STATUS_RUNNING,
40 ] )
41 ->set_limit( 1 );
42
43 $active_operations = Async_Operation::get( $active_query );
44
45 if ( empty( $active_operations ) ) {
46 return Async_Operation::OPERATION_STATUS_NOT_STARTED;
47 }
48
49 $operation_id = $active_operations[0]->get_args()['operation_id'];
50 $cancelled_query = ( new Image_Optimization_Operation_Query() )
51 ->set_hook( Async_Operation_Hook::OPTIMIZE_BULK )
52 ->set_status( Async_Operation::OPERATION_STATUS_CANCELED )
53 ->set_bulk_operation_id( $operation_id )
54 ->set_limit( 1 );
55
56 $cancelled_operations = Async_Operation::get( $cancelled_query );
57
58 if ( ! empty( $cancelled_operations ) ) {
59 return Async_Operation::OPERATION_STATUS_CANCELED;
60 }
61
62 return Async_Operation::OPERATION_STATUS_RUNNING;
63 }
64
65 private static function get_bulk_optimization_active_operation_id(): ?string {
66 $active_query = ( new Image_Optimization_Operation_Query() )
67 ->set_hook( Async_Operation_Hook::OPTIMIZE_BULK )
68 ->set_status( [
69 Async_Operation::OPERATION_STATUS_PENDING,
70 Async_Operation::OPERATION_STATUS_RUNNING,
71 ] )
72 ->set_limit( 1 );
73
74 $active_operation = Async_Operation::get( $active_query );
75
76 if ( empty( $active_operation ) ) {
77 return null;
78 }
79
80 return $active_operation[0]->get_args()['operation_id'];
81 }
82
83 private static function get_bulk_restoring_status(): string {
84 $active_query = ( new Operation_Query() )
85 ->set_hook( Async_Operation_Hook::RESTORE_MANY_IMAGES )
86 ->set_status( [
87 Async_Operation::OPERATION_STATUS_PENDING,
88 Async_Operation::OPERATION_STATUS_RUNNING,
89 ] )
90 ->set_limit( 1 );
91
92 $active_operations = Async_Operation::get( $active_query );
93
94 return ! empty( $active_operations )
95 ? Async_Operation::OPERATION_STATUS_RUNNING
96 : Async_Operation::OPERATION_STATUS_NOT_STARTED;
97 }
98
99 private static function get_bulk_backup_removing_status(): string {
100 $active_query = ( new Operation_Query() )
101 ->set_hook( Async_Operation_Hook::REMOVE_MANY_BACKUPS )
102 ->set_status( [
103 Async_Operation::OPERATION_STATUS_PENDING,
104 Async_Operation::OPERATION_STATUS_RUNNING,
105 ] )
106 ->set_limit( 1 );
107
108 $active_operations = Async_Operation::get( $active_query );
109
110 return ! empty( $active_operations )
111 ? Async_Operation::OPERATION_STATUS_RUNNING
112 : Async_Operation::OPERATION_STATUS_NOT_STARTED;
113 }
114
115 private static function backups_exist(): bool {
116 $query = ( new Image_Query_Builder() )
117 ->set_paging_size( 1 )
118 ->return_images_only_with_backups()
119 ->execute();
120
121 return $query->post_count > 0;
122 }
123 }
124