PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.3.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.3.1
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.2.3.1, at classes/Stats/OptimizedMediaWithoutNextGen.php

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