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

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

184 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\Stats;
3
4 use Imagify\Bulk\Bulk;
5 use Imagify\Traits\InstanceGetterTrait;
6
7 /**
8 * Class to get and cache the number of optimized media without WebP versions.
9 *
10 * @since 1.9
11 */
12 class OptimizedMediaWithoutWebp implements StatInterface {
13 use InstanceGetterTrait;
14
15 /**
16 * Name of the transient storing the cached result.
17 *
18 * @var string
19 */
20 const NAME = 'imagify_stat_without_webp';
21
22 /**
23 * Launch hooks.
24 *
25 * @since 1.9
26 */
27 public function init() {
28 add_action( 'imagify_after_optimize', [ $this, 'maybe_clear_cache_after_optimization' ], 10, 2 );
29 add_action( 'imagify_after_restore_media', [ $this, 'maybe_clear_cache_after_restoration' ], 10, 4 );
30 add_action( 'imagify_delete_media', [ $this, 'maybe_clear_cache_on_deletion' ] );
31 }
32
33
34 /** ----------------------------------------------------------------------------------------- */
35 /** GET/CACHE THE STAT ====================================================================== */
36 /** ----------------------------------------------------------------------------------------- */
37
38 /**
39 * Get the number of optimized media without WebP versions.
40 *
41 * @since 1.9
42 *
43 * @return int
44 */
45 public function get_stat() {
46 $bulk = Bulk::get_instance();
47 $stat = 0;
48
49 // Sum the counts of each context.
50 foreach ( imagify_get_context_names() as $context ) {
51 $stat += $bulk->get_bulk_instance( $context )->has_optimized_media_without_webp();
52 }
53
54 return $stat;
55 }
56
57 /**
58 * Get and cache the number of optimized media without WebP versions.
59 *
60 * @since 1.9
61 *
62 * @return int
63 */
64 public function get_cached_stat() {
65 $contexts = implode( '|', imagify_get_context_names() );
66 $stat = get_transient( static::NAME );
67
68 if ( isset( $stat['stat'], $stat['contexts'] ) && $stat['contexts'] === $contexts ) {
69 // The number is stored and the contexts are the same.
70 return (int) $stat['stat'];
71 }
72
73 $stat = [
74 'contexts' => $contexts,
75 'stat' => $this->get_stat(),
76 ];
77
78 set_transient( static::NAME, $stat, 2 * DAY_IN_SECONDS );
79
80 return $stat['stat'];
81 }
82
83 /**
84 * Clear the stat cache.
85 *
86 * @since 1.9
87 */
88 public function clear_cache() {
89 delete_transient( static::NAME );
90 }
91
92
93 /** ----------------------------------------------------------------------------------------- */
94 /** HOOKS =================================================================================== */
95 /** ----------------------------------------------------------------------------------------- */
96
97 /**
98 * Clear cache after optimizing a media.
99 *
100 * @since 1.9
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( static::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 ( ! isset( $new_sizes['full'] ) && ! empty( $new_sizes[ $size_name ]['success'] ) ) {
117 /**
118 * We just successfully generated the WebP version of the full size.
119 * The full size was not optimized at the same time, that means it was optimized previously.
120 * Meaning: we just added a WebP version to a media that was previously optimized, so there is one less optimized media without WebP.
121 */
122 $this->clear_cache();
123 return;
124 }
125
126 if ( ! empty( $new_sizes['full']['success'] ) && empty( $new_sizes[ $size_name ]['success'] ) ) {
127 /**
128 * We now have a new optimized media without WebP.
129 */
130 $this->clear_cache();
131 }
132 }
133
134 /**
135 * Clear cache after restoring a media.
136 *
137 * @since 1.9
138 *
139 * @param ProcessInterface $process The optimization process.
140 * @param bool|WP_Error $response The result of the operation: true on success, a WP_Error object on failure.
141 * @param array $files The list of files, before restoring them.
142 * @param array $data The optimization data, before deleting it.
143 */
144 public function maybe_clear_cache_after_restoration( $process, $response, $files, $data ) {
145 if ( ! $process->get_media()->is_image() || false === get_transient( static::NAME ) ) {
146 return;
147 }
148
149 $sizes = isset( $data['sizes'] ) ? (array) $data['sizes'] : [];
150 $size_name = 'full' . $process::WEBP_SUFFIX;
151
152 if ( ! empty( $sizes['full']['success'] ) && empty( $sizes[ $size_name ]['success'] ) ) {
153 /**
154 * This media had no WebP versions.
155 */
156 $this->clear_cache();
157 }
158 }
159
160 /**
161 * Clear cache on media deletion.
162 *
163 * @since 1.9
164 *
165 * @param ProcessInterface $process An optimization process.
166 */
167 public function maybe_clear_cache_on_deletion( $process ) {
168 if ( false === get_transient( static::NAME ) ) {
169 return;
170 }
171
172 $data = $process->get_data()->get_optimization_data();
173 $sizes = isset( $data['sizes'] ) ? (array) $data['sizes'] : [];
174 $size_name = 'full' . $process::WEBP_SUFFIX;
175
176 if ( ! empty( $sizes['full']['success'] ) && empty( $sizes[ $size_name ]['success'] ) ) {
177 /**
178 * This media had no WebP versions.
179 */
180 $this->clear_cache();
181 }
182 }
183 }
184