PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
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 / Stats / OptimizedMediaWithoutNextGen.php

OptimizedMediaWithoutNextGen.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/Stats/OptimizedMediaWithoutNextGen.php

218 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace Imagify\Stats;
5
6 use Imagify\Bulk\Bulk;
7 use Imagify\Optimization\Process\ProcessInterface;
8 use Imagify\EventManagement\SubscriberInterface;
9 use Imagify\Traits\InstanceGetterTrait;
10 use WP_Error;
11
12 /**
13 * Class to get and cache the number of optimized media without next-gen versions.
14 */
15 final class OptimizedMediaWithoutNextGen implements StatInterface, SubscriberInterface {
16 use InstanceGetterTrait;
17
18 /**
19 * Name of the transient storing the cached result.
20 *
21 * @var string
22 */
23 const NAME = 'imagify_stat_without_next_gen';
24
25 /**
26 * Array of events this subscriber listens to
27 *
28 * @return array
29 */
30 public static function get_subscribed_events() {
31 return [
32 // @action
33 'imagify_after_optimize' => [ 'maybe_clear_cache_after_optimization', 10, 2 ],
34 // @action
35 'imagify_after_restore_media' => [ 'maybe_clear_cache_after_restoration', 10, 4 ],
36 // @action
37 'imagify_delete_media' => 'maybe_clear_cache_on_deletion',
38 // @action
39 'update_option_imagify_settings' => [ 'maybe_clear_stat_cache', 9, 2 ],
40 ];
41 }
42
43 /**
44 * Get the number of optimized media without next-gen versions.
45 *
46 * @since 2.2
47 *
48 * @return int
49 */
50 public function get_stat() {
51 $bulk = Bulk::get_instance();
52 $stat = 0;
53
54 // Sum the counts of each context.
55 foreach ( imagify_get_context_names() as $context ) {
56 $stat += $bulk->get_bulk_instance( $context )->has_optimized_media_without_nextgen();
57 }
58
59 return $stat;
60 }
61
62 /**
63 * Get and cache the number of optimized media without next-gen versions.
64 *
65 * @since 2.2
66 *
67 * @return int
68 */
69 public function get_cached_stat() {
70 $contexts = implode( '|', imagify_get_context_names() );
71 $stat = get_transient( self::NAME );
72
73 if ( isset( $stat['stat'], $stat['contexts'] ) && $stat['contexts'] === $contexts ) {
74 // The number is stored and the contexts are the same.
75 return (int) $stat['stat'];
76 }
77
78 $stat = [
79 'contexts' => $contexts,
80 'stat' => $this->get_stat(),
81 ];
82
83 set_transient( self::NAME, $stat, 2 * DAY_IN_SECONDS );
84
85 return $stat['stat'];
86 }
87
88 /**
89 * Clear the stat cache.
90 *
91 * @since 2.2
92 */
93 public function clear_cache() {
94 delete_transient( self::NAME );
95 }
96
97 /**
98 * Clear cache after optimizing a media.
99 *
100 * @since 2.2
101 *
102 * @param ProcessInterface $process The optimization process.
103 * @param array $item The item being processed.
104 */
105 public function maybe_clear_cache_after_optimization( $process, $item ) {
106 if ( ! $process->get_media()->is_image() || false === get_transient( self::NAME ) ) {
107 return;
108 }
109
110 $sizes = $process->get_data()->get_optimization_data();
111 $sizes = isset( $sizes['sizes'] ) ? (array) $sizes['sizes'] : [];
112 $new_sizes = array_flip( $item['sizes_done'] );
113 $new_sizes = array_intersect_key( $sizes, $new_sizes );
114 $size_name = 'full' . $process::WEBP_SUFFIX;
115
116 if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
117 $size_name = 'full' . $process::AVIF_SUFFIX;
118 }
119
120 if ( ! isset( $new_sizes['full'] ) && ! empty( $new_sizes[ $size_name ]['success'] ) ) {
121 /**
122 * We just successfully generated the next-gen version of the full size.
123 * The full size was not optimized at the same time, that means it was optimized previously.
124 * Meaning: we just added a next-gen version to a media that was previously optimized, so there is one less optimized media without next-gen.
125 */
126 $this->clear_cache();
127 return;
128 }
129
130 if ( ! empty( $new_sizes['full']['success'] ) && empty( $new_sizes[ $size_name ]['success'] ) ) {
131 /**
132 * We now have a new optimized media without next-gen.
133 */
134 $this->clear_cache();
135 }
136 }
137
138 /**
139 * Clear cache after restoring a media.
140 *
141 * @since 2.2
142 *
143 * @param ProcessInterface $process The optimization process.
144 * @param bool|WP_Error $response The result of the operation: true on success, a WP_Error object on failure.
145 * @param array $files The list of files, before restoring them.
146 * @param array $data The optimization data, before deleting it.
147 */
148 public function maybe_clear_cache_after_restoration( $process, $response, $files, $data ) {
149 if ( ! $process->get_media()->is_image() || false === get_transient( self::NAME ) ) {
150 return;
151 }
152
153 $sizes = isset( $data['sizes'] ) ? (array) $data['sizes'] : [];
154 $size_name = 'full' . $process::WEBP_SUFFIX;
155
156 if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
157 $size_name = 'full' . $process::AVIF_SUFFIX;
158 }
159
160 if ( ! empty( $sizes['full']['success'] ) && empty( $sizes[ $size_name ]['success'] ) ) {
161 /**
162 * This media had no next-gen versions.
163 */
164 $this->clear_cache();
165 }
166 }
167
168 /**
169 * Clear cache on media deletion.
170 *
171 * @since 2.2
172 *
173 * @param ProcessInterface $process An optimization process.
174 */
175 public function maybe_clear_cache_on_deletion( $process ) {
176 if ( false === get_transient( self::NAME ) ) {
177 return;
178 }
179
180 $data = $process->get_data()->get_optimization_data();
181 $sizes = isset( $data['sizes'] ) ? (array) $data['sizes'] : [];
182 $size_name = 'full' . $process::WEBP_SUFFIX;
183
184 if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
185 $size_name = 'full' . $process::AVIF_SUFFIX;
186 }
187
188 if ( ! empty( $sizes['full']['success'] ) && empty( $sizes[ $size_name ]['success'] ) ) {
189 /**
190 * This media had no next-gen versions.
191 */
192 $this->clear_cache();
193 }
194 }
195
196 /**
197 * Maybe clear the stat cache on option change
198 *
199 * @since 2.2
200 *
201 * @param array $old_value The old option value.
202 * @param array $value The new option value.
203 *
204 * @return void
205 */
206 public function maybe_clear_stat_cache( $old_value, $value ) {
207 if ( ! isset( $old_value['optimization_format'], $value['optimization_format'] ) ) {
208 return;
209 }
210
211 if ( $old_value['optimization_format'] === $value['optimization_format'] ) {
212 return;
213 }
214
215 $this->clear_cache();
216 }
217 }
218