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

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

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