PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 4.1.2
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v4.1.2
4.1.2 4.1.1 4.1.0 4.0.1 4.0.0 3.3.1 3.3.0 trunk 1.0.0 2.0.0 2.1.0 3.0.0 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8
← All changes | admin/class-image-optimizer.php +1077 -427 3.3.14.1.2 View file →
@@ -1,8 +1,6 @@
1 1 <?php
2 2
3 -use Intervention\Image\ImageManager;
4 -
5 3 /**
6 4 * The core functionality for image optimizing.
7 5 *
8 6 * @link http://webdeclic.com
@@ -44,203 +42,172 @@
44 42 * @param string $version The version of this plugin.
45 43 */
46 44 public function __construct( $plugin_name, $version ) {
47 45
48 - $this->plugin_name = $plugin_name;
49 - $this->version = $version;
50 - $this->allowed_mime_types = array( 'image/jpeg', 'image/png', 'image/webp' );
51 -
46 + $this->plugin_name = $plugin_name;
47 + $this->version = $version;
48 + $this->allowed_mime_types = array( 'image/jpeg', 'image/png', 'image/webp', 'image/avif' );
52 49 }
53 50
54 51 /**
55 52 * Optimize an image added in wp_media
56 53 */
57 - public function image_optimizition( $file ) {
54 + public function image_optimization( $file ) {
55 + global $quickwebp_surecart_client;
58 56
57 + quickwebp_debug_log( 'image_optimization:start', array(
58 + 'file_name' => $file['name'] ?? '',
59 + 'file_type' => $file['type'] ?? '',
60 + ) );
61 +
59 62 $settings = $this->get_settings();
63 + $image_file = $this->file_is_image( $file, $settings );
60 64
61 - $image_file = $this->file_is_image( $file, $settings );
62 65 if ( ! $image_file ) {
66 + quickwebp_debug_log( 'image_optimization:not_image' );
63 67 return $file;
64 68 }
65 69
66 - if ( $settings['quickwebp_settings_conversion'] != '1' ) {
70 + $mode_enabled = $settings['quickwebp_settings_conversion'];
71 + if ( $mode_enabled === '0' ) {
72 + quickwebp_debug_log( 'image_optimization:conversion_disabled' );
67 73 return $image_file;
68 74 }
69 75
70 - $save_original = is_array( $settings['quickwebp_settings_conversion_save_original'] ) ? $settings['quickwebp_settings_conversion_save_original'] : array();
76 + $save_original = is_array( $settings['quickwebp_settings_conversion_save_original'] ) ? $settings['quickwebp_settings_conversion_save_original'] : array();
71 77 if ( in_array( 'checked', $save_original ) ) {
78 + quickwebp_debug_log( 'image_optimization:save_original_enabled' );
72 79 return $image_file;
73 80 }
74 81
75 - $extension_to_use = $this->image_extension_loaded( $settings );
76 - if ( ! $extension_to_use ) {
77 - return $image_file;
82 + $ignore_same_format = is_array( $settings['quickwebp_settings_conversion_ignore_webp'] ) ? $settings['quickwebp_settings_conversion_ignore_webp'] : array();
83 + $mime_type = wp_get_image_mime( $image_file['tmp_name'] );
84 + if ( in_array( 'checked', $ignore_same_format ) ) {
85 + if ( '1' == $mode_enabled && 'image/webp' == $mime_type ) {
86 + quickwebp_debug_log( 'image_optimization:skip_same_format', array( 'mime_type' => $mime_type ) );
87 + return $image_file;
88 + } elseif( '2' == $mode_enabled && 'image/avif' == $mime_type ) {
89 + quickwebp_debug_log( 'image_optimization:skip_same_format', array( 'mime_type' => $mime_type ) );
90 + return $image_file;
91 + }
78 92 }
79 93
80 - $manager = new ImageManager(array('driver'=>$extension_to_use));
81 - $image = $manager->make($image_file['tmp_name']);
82 -
83 - $exif_data = @exif_read_data( $image_file['tmp_name'] );
84 - if ( ! empty( $exif_data['Orientation'] ) ) {
85 - $orientation = (int) $exif_data['Orientation'];
86 - $orientation = apply_filters( 'wp_image_maybe_exif_rotate', $orientation, $image_file['tmp_name'] );
87 - if ( $orientation && 1 !== $orientation ) {
88 - switch ( $orientation ) {
89 - case 2:
90 - // Flip horizontally.
91 - $result = $image->flip( false, true );
92 - break;
93 - case 3:
94 - /*
95 - * Rotate 180 degrees or flip horizontally and vertically.
96 - * Flipping seems faster and uses less resources.
97 - */
98 - $result = $image->flip( true, true );
99 - break;
100 - case 4:
101 - // Flip vertically.
102 - $result = $image->flip( true, false );
103 - break;
104 - case 5:
105 - // Rotate 90 degrees counter-clockwise and flip vertically.
106 - $result = $image->rotate( 90 );
107 - if ( ! is_wp_error( $result ) ) {
108 - $result = $image->flip( true, false );
109 - }
110 - break;
111 - case 6:
112 - // Rotate 90 degrees clockwise (270 counter-clockwise).
113 - $result = $image->rotate( 270 );
114 - break;
115 - case 7:
116 - // Rotate 90 degrees counter-clockwise and flip horizontally.
117 - $result = $image->rotate( 90 );
118 - if ( ! is_wp_error( $result ) ) {
119 - $result = $image->flip( false, true );
120 - }
121 - break;
122 - case 8:
123 - // Rotate 90 degrees counter-clockwise.
124 - $result = $image->rotate( 90 );
125 - break;
126 - }
94 + $quality = $this->get_the_quality( $settings );
95 + $is_pro = false;
96 + if ( $quickwebp_surecart_client ) {
97 + $is_pro = $quickwebp_surecart_client->license()->is_valid();
98 + }
99 +
100 + $image_file['new_size'] = $image_file['size'];
101 + $image_file['new_type'] = $image_file['type'];
102 +
103 + if ( '2' === $mode_enabled && $is_pro ) {
104 + quickwebp_debug_log( 'image_optimization:try_avif', array( 'mime_type' => $mime_type, 'quality' => $quality ) );
105 + $avif_image = $this->create_avif_image( $image_file['tmp_name'], $image_file['tmp_name'], $quality );
106 + if ( $avif_image ) {
107 + $image_file['size'] = filesize( $image_file['tmp_name'] );
108 + $image_file['type'] = 'image/avif';
109 + $image_file['quickwebp'] = 'optimized';
110 + $this->record_optimization_stats( $image_file['new_size'], $image_file['size'] );
111 + quickwebp_debug_log( 'image_optimization:avif_success', array( 'new_size' => $image_file['size'] ) );
127 112 }
128 113 }
129 - $quality = $this->get_quality( $image_file['tmp_name'], $settings );
130 114
131 - $image->sharpen($settings['quickwebp_settings_conversion_sharpen']);
132 - $image->save( $image_file['tmp_name'], $quality, 'webp' );
115 + if ( '1' === $mode_enabled ) {
116 + quickwebp_debug_log( 'image_optimization:try_webp', array( 'mime_type' => $mime_type, 'quality' => $quality ) );
117 + $webp_image = $this->create_webp_image( $image_file['tmp_name'], $image_file['tmp_name'], $quality );
118 + if ( $webp_image ) {
119 + $image_file['size'] = filesize( $image_file['tmp_name'] );
120 + $image_file['type'] = 'image/webp';
121 + $image_file['quickwebp'] = 'optimized';
122 + $this->record_optimization_stats( $image_file['new_size'], $image_file['size'] );
123 + quickwebp_debug_log( 'image_optimization:webp_success', array( 'new_size' => $image_file['size'] ) );
124 + }
125 + }
133 126
134 - $size_after = $image->filesize();
135 - $mime = $image->mime();
136 -
137 - $image_file['size'] = $size_after;
138 - $image_file['type'] = $mime;
139 -
140 127 return $image_file;
141 128 }
142 129
143 130 /**
144 - * Get the package used by the server
145 - *
131 + * Add post meta to the attachment that already optimized
146 132 */
147 - public function image_extension_loaded( $settings ) {
133 + public function add_already_optimized_meta( $metadata, $attachment_id, $context ) {
134 + //phpcs:ignore WordPress.Security.NonceVerification.Missing
135 + $quickwebp = sanitize_text_field( wp_unslash( $_FILES['async-upload']['quickwebp'] ?? '' ) );
136 +
137 + if ( 'optimized' == $quickwebp ) {
138 + //phpcs:ignore WordPress.Security.NonceVerification.Missing
139 + $type = sanitize_text_field( wp_unslash( $_FILES['async-upload']['type'] ?? '' ) );
148 140
149 - $accepted_librarys = array( 'gd', 'imagick' );
150 - $library_to_use = $settings['quickwebp_settings_library'];
151 -
152 - if ( in_array( $library_to_use, $accepted_librarys ) ) {
153 -
154 - return $library_to_use;
141 + if ( 'image/webp' == $type ) {
142 + update_post_meta( $attachment_id, 'quickwebp_already_optimized', '1' );
143 + } elseif ( 'image/avif' == $type ) {
144 + update_post_meta( $attachment_id, 'quickwebp_already_optimized', '2' );
145 + }
155 146 }
156 147
157 - return false;
148 + return $metadata;
158 149 }
159 150
160 151 /**
161 - * Check if the file is an image
162 - *
152 + * Save the original image in the attachment meta
163 153 */
164 - public function file_is_image( $file, $settings ) {
154 + public function save_original_image( $metadata, $attachment_id, $context ) {
165 155
166 - $file_name = isset($file['name']) ? $file['name'] : '';
167 - $file_type = isset($file['type']) ? $file['type'] : '';
168 - $file_tmp_name = isset($file['tmp_name']) ? $file['tmp_name'] : '';
169 - $file_error = isset($file['error']) ? $file['error'] : '';
170 - $file_size = isset($file['size']) ? $file['size'] : '';
156 + if ( $context != 'create' ) {
157 + return $metadata;
158 + }
171 159
172 - if ( empty($file_tmp_name) ) {
173 - return false;
160 + $settings = $this->get_settings();
161 +
162 + $mode_enabled = $settings['quickwebp_settings_conversion'];
163 + if ( $mode_enabled === '0' ) {
164 + return $metadata;
174 165 }
175 166
176 - $allowed_mime_types = apply_filters( 'quickwebp_mime_types_allowed', $this->allowed_mime_types );
177 - $is_image = wp_getimagesize($file_tmp_name);
178 - $mime_type = wp_get_image_mime($file_tmp_name);
179 - if ( ! $is_image || ! in_array( $mime_type, $allowed_mime_types ) ) {
180 - return false;
167 + $save_original = is_array( $settings['quickwebp_settings_conversion_save_original'] ) ? $settings['quickwebp_settings_conversion_save_original'] : array();
168 + if ( ! in_array( 'checked', $save_original ) ) {
169 + return $metadata;
181 170 }
182 171
183 - $name = $file_name;
184 -
185 - if ( $settings['quickwebp_settings_cleanup'] == '1' ) {
186 - $name = quickwebp_sanitize_name($file_name);
172 + $ignore_same_format = is_array( $settings['quickwebp_settings_conversion_ignore_webp'] ) ? $settings['quickwebp_settings_conversion_ignore_webp'] : array();
173 + //phpcs:ignore WordPress.Security.NonceVerification.Missing
174 + $mime_type = sanitize_text_field( wp_unslash( $_FILES['async-upload']['type'] ?? '' ) );
175 + if ( in_array( 'checked', $ignore_same_format ) ) {
176 + if ( '1' == $mode_enabled && 'image/webp' == $mime_type ) {
177 + return $metadata;
178 + } elseif( '2' == $mode_enabled && 'image/avif' == $mime_type ) {
179 + return $metadata;
180 + }
187 181 }
188 182
189 - return array(
190 - 'original_name' => $file_name,
191 - 'name' => $name,
192 - 'type' => $file_type,
193 - 'tmp_name' => $file_tmp_name,
194 - 'error' => $file_error,
195 - 'size' => $file_size
196 - );
197 - }
183 + $sizes = $this->get_media_files( $attachment_id );
184 + $new_sizes = array();
185 +
186 + foreach ( $sizes as $key => $size ) {
187 + $result = $this->optimize_local_file( $size );
198 188
199 - /**
200 - * Get the optimization settings
201 - */
202 - public function get_settings() {
189 + if ( $result ) {
190 + $new_sizes[$key] = $result;
191 + }
192 + }
203 193
204 - return array(
205 - 'quickwebp_settings_conversion' => get_option('quickwebp_settings_conversion', quickwebp_settings_default('quickwebp_settings_conversion') ),
206 - 'quickwebp_settings_conversion_quality' => get_option('quickwebp_settings_conversion_quality', quickwebp_settings_default('quickwebp_settings_conversion_quality') ),
207 - 'quickwebp_settings_conversion_sharpen' => get_option('quickwebp_settings_conversion_sharpen', quickwebp_settings_default('quickwebp_settings_conversion_sharpen') ),
208 - 'quickwebp_settings_conversion_ignore_webp' => get_option('quickwebp_settings_conversion_ignore_webp', quickwebp_settings_default('quickwebp_settings_conversion_ignore_webp') ),
209 - 'quickwebp_settings_conversion_save_original' => get_option('quickwebp_settings_conversion_save_original', quickwebp_settings_default('quickwebp_settings_conversion_save_original') ),
210 - 'quickwebp_settings_resize' => get_option('quickwebp_settings_resize', quickwebp_settings_default('quickwebp_settings_resize') ),
211 - 'quickwebp_settings_resize_value' => get_option('quickwebp_settings_resize_value', quickwebp_settings_default('quickwebp_settings_resize_value') ),
212 - 'quickwebp_settings_completion' => get_option('quickwebp_settings_completion', quickwebp_settings_default('quickwebp_settings_completion') ),
213 - 'quickwebp_settings_completion_options' => get_option('quickwebp_settings_completion_options', quickwebp_settings_default('quickwebp_settings_completion_options') ),
214 - 'quickwebp_settings_cleanup' => get_option('quickwebp_settings_cleanup', quickwebp_settings_default('quickwebp_settings_cleanup') ),
215 - 'quickwebp_settings_library' => get_option( 'quickwebp_settings_library', quickwebp_settings_default('quickwebp_settings_library') )
216 - );
217 - }
218 -
219 - /**
220 - * Get the optimization settings
221 - */
222 - public function get_ajax_settings() {
194 + if ( ! empty( $new_sizes ) ) {
195 + $this->record_attachment_optimization_stats( $new_sizes );
223 196
224 - $raw_settings = isset( $_POST['settings'] ) ? wp_unslash( $_POST['settings'] ) : '';
225 - $settings = is_string( $raw_settings ) ? json_decode( $raw_settings, true ) : array();
197 + if ( '1' == $mode_enabled ) {
198 + update_post_meta( $attachment_id, 'quickwebp_already_optimized', '1' );
199 + } elseif ( '2' == $mode_enabled ) {
200 + update_post_meta( $attachment_id, 'quickwebp_already_optimized', '2' );
201 + }
226 202
227 - if ( ! is_array( $settings ) ) {
228 - $settings = array();
203 + update_post_meta( $attachment_id, 'quickwebp_data', $new_sizes );
204 + delete_post_meta( $attachment_id, 'quickwebp_has_error' );
205 + } else {
206 + update_post_meta( $attachment_id, 'quickwebp_has_error', '1' );
229 207 }
230 208
231 - return array(
232 - 'quickwebp_settings_conversion' => isset( $settings['quickwebp_settings_conversion'] ) ? sanitize_text_field( (string) $settings['quickwebp_settings_conversion'] ) : '',
233 - 'quickwebp_settings_conversion_quality' => isset( $settings['quickwebp_settings_conversion_quality'] ) ? absint( $settings['quickwebp_settings_conversion_quality'] ) : 0,
234 - 'quickwebp_settings_conversion_sharpen' => isset( $settings['quickwebp_settings_conversion_sharpen'] ) ? absint( $settings['quickwebp_settings_conversion_sharpen'] ) : 0,
235 - 'quickwebp_settings_conversion_ignore_webp' => isset( $settings['quickwebp_settings_conversion_ignore_webp'] ) && is_array( $settings['quickwebp_settings_conversion_ignore_webp'] ) ? array_map( 'sanitize_text_field', $settings['quickwebp_settings_conversion_ignore_webp'] ) : array(),
236 - 'quickwebp_settings_resize' => isset( $settings['quickwebp_settings_resize'] ) ? sanitize_text_field( (string) $settings['quickwebp_settings_resize'] ) : '',
237 - 'quickwebp_settings_resize_value' => isset( $settings['quickwebp_settings_resize_value'] ) ? absint( $settings['quickwebp_settings_resize_value'] ) : 0,
238 - 'quickwebp_settings_completion' => isset( $settings['quickwebp_settings_completion'] ) ? sanitize_text_field( (string) $settings['quickwebp_settings_completion'] ) : '',
239 - 'quickwebp_settings_completion_options' => isset( $settings['quickwebp_settings_completion_options'] ) && is_array( $settings['quickwebp_settings_completion_options'] ) ? array_map( 'sanitize_text_field', $settings['quickwebp_settings_completion_options'] ) : array(),
240 - 'quickwebp_settings_cleanup' => isset( $settings['quickwebp_settings_cleanup'] ) ? sanitize_text_field( (string) $settings['quickwebp_settings_cleanup'] ) : '',
241 - 'quickwebp_settings_library' => isset( $settings['quickwebp_settings_library'] ) ? sanitize_text_field( (string) $settings['quickwebp_settings_library'] ) : ''
242 - );
209 + return $metadata;
243 210 }
244 211
245 212 /**
246 213 * Add data to the attachment
@@ -246,29 +213,25 @@
246 213 * Add data to the attachment
247 214 */
248 215 public function add_data_to_attachment( $metadata, $attachment_id, $context ) {
249 216
250 - $settings = $this->get_settings();
251 - if ( $settings['quickwebp_settings_completion'] != '1' ) {
217 + if ( $context != 'create' ) {
252 218 return $metadata;
253 219 }
254 220
255 - if ( $context != 'create' ) {
221 + $settings = $this->get_settings();
222 + if ( $settings['quickwebp_settings_completion'] != '1' ) {
256 223 return $metadata;
257 224 }
258 225
259 - if ( isset($_FILES['async-upload']['original_name']) ) {
260 -
261 - $original_name = sanitize_text_field( wp_unslash( $_FILES['async-upload']['original_name'] ) );
226 + //phpcs:ignore WordPress.Security.NonceVerification.Missing
227 + $original_name = isset( $_FILES['async-upload']['original_name'] ) ? sanitize_text_field( wp_unslash( $_FILES['async-upload']['original_name'] ) ) : sanitize_text_field( $_FILES['async-upload']['name'] ?? '' );
228 + if ( ! empty( $original_name ) ) {
262 229 $original_name = pathinfo( $original_name, PATHINFO_FILENAME );
230 + $post_arr = array();
263 231
264 - $post_arr = array(
265 - 'ID' => $attachment_id,
266 - 'meta_input' => array()
267 - );
232 + $completion_options = is_array( $settings['quickwebp_settings_completion_options'] ) ? $settings['quickwebp_settings_completion_options'] : array();
268 233
269 - $completion_options = is_array($settings['quickwebp_settings_completion_options']) ? $settings['quickwebp_settings_completion_options'] : array();
270 -
271 234 if ( in_array( 'title', $completion_options ) ) {
272 235 $post_arr['post_title'] = $original_name;
273 236 }
274 237
@@ -283,32 +246,12 @@
283 246 if ( in_array( 'description', $completion_options ) ) {
284 247 $post_arr['post_content'] = $original_name;
285 248 }
286 249
287 - wp_update_post( $post_arr );
288 - }
289 -
290 - $save_original = is_array( $settings['quickwebp_settings_conversion_save_original'] ) ? $settings['quickwebp_settings_conversion_save_original'] : array();
291 - if ( in_array( 'checked', $save_original ) ) {
292 - $sizes = $this->get_media_files( $attachment_id );
293 - $new_sizes = array();
294 -
295 - foreach ( $sizes as $key => $size ) {
296 - $result = $this->optimize_local_file( $size );
297 -
298 - if ( $result ) {
299 - $new_sizes[$key] = $result;
300 - }
250 + if ( ! empty( $post_arr ) ) {
251 + $post_arr['ID'] = $attachment_id;
252 + wp_update_post( $post_arr );
301 253 }
302 -
303 - if ( ! empty( $new_sizes ) ) {
304 -
305 - update_post_meta( $attachment_id, 'quickwebp_already_optimized', '1' );
306 - update_post_meta( $attachment_id, 'quickwebp_data', $new_sizes );
307 - delete_post_meta( $attachment_id, 'quickwebp_has_error' );
308 - } else {
309 - update_post_meta( $attachment_id, 'quickwebp_has_error', '1' );
310 - }
311 254 }
312 255
313 256 return $metadata;
314 257 }
@@ -313,41 +256,25 @@
313 256 return $metadata;
314 257 }
315 258
316 259 /**
317 - * Get the quality
260 + * Change the default size of wp editor
318 261 */
319 - public function get_quality( $file, $settings ) {
262 + public function change_wp_max_size( $max_size, $imagesize ) {
320 263
321 - $ignore_webp = is_array( $settings['quickwebp_settings_conversion_ignore_webp'] ) ? $settings['quickwebp_settings_conversion_ignore_webp'] : array();
322 - $quality = $settings['quickwebp_settings_conversion_quality'];
323 - $mime_type = wp_get_image_mime($file);
264 + $settings = $this->get_settings();
265 + $resize_active = $settings['quickwebp_settings_resize'];
266 +
267 + if ( $resize_active == '1' ) {
324 268
325 - switch ( $mime_type ) {
269 + $imagesize_width = isset($imagesize[0]) ? $imagesize[0] : 0;
270 + $resize_value = $settings['quickwebp_settings_resize_value'];
326 271
327 - case 'image/webp':
328 - if ( in_array( 'checked', $ignore_webp ) ){
329 - $quality = 99;
330 - }
331 - break;
272 + if ( $imagesize_width > $resize_value ) {
273 + $max_size = $resize_value;
274 + }
332 275 }
333 276
334 - return $quality;
335 - }
336 -
337 - /**
338 - * Change the default size of wp editor
339 - */
340 - public function change_wp_max_size( $max_size = 2560 ) {
341 -
342 - $settings = $this->get_settings();
343 - $resize_active = $settings['quickwebp_settings_resize'];
344 - $resize_value = $settings['quickwebp_settings_resize_value'];
345 -
346 - if ( $resize_active == '1' ) {
347 - $max_size = $resize_value;
348 - }
349 -
350 277 return $max_size;
351 278 }
352 279
353 280 /**
@@ -352,111 +279,327 @@
352 279
353 280 /**
354 281 * Change the default quality of wp editor
355 282 */
356 - public function change_wp_quality( $default_quality, $mime_type ) {
283 + public function change_wp_quality( $default_quality ) {
357 284
358 - $default_quality = 90;
285 + $settings = $this->get_settings();
286 + $mode_enabled = $settings['quickwebp_settings_conversion'];
287 + if ( $mode_enabled === '0' ) {
288 + return $default_quality;
289 + }
359 290
360 - return $default_quality;
291 + return 100;
361 292 }
362 293
363 294 /**
364 295 * Optimize image through ajax
365 296 */
366 - public function image_optimizition_ajax() {
297 + public function image_optimization_ajax() {
298 + global $quickwebp_surecart_client;
367 299
300 + quickwebp_debug_log( 'image_optimization_ajax:start' );
301 +
368 302 if ( ! current_user_can( 'upload_files' ) ) {
369 - wp_send_json_error( __( 'You are not allowed to upload files.', QUICKWEBP_TEXT_DOMAIN ), 403 );
303 + wp_send_json_error( __( 'You are not allowed to upload files.', 'quickwebp' ), 403 );
370 304 }
371 305
372 306 // verify the nonce.
373 307 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
374 308 if( !wp_verify_nonce( $nonce, 'image_optimize_nonce' ) ) {
375 - wp_send_json_error( __( 'Refresh the page and try again.', QUICKWEBP_TEXT_DOMAIN ) );
309 + wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
376 310 }
377 311
378 - // Get settings
379 - $settings = $this->get_ajax_settings();
380 -
381 312 // Get the file
382 313 $file = count($_FILES) > 0 ? array_shift($_FILES) : array();
383 314 if ( empty($file) ) {
384 - wp_send_json_error( __( 'No image uploaded, try again.', QUICKWEBP_TEXT_DOMAIN ) );
315 + quickwebp_debug_log( 'image_optimization_ajax:missing_file' );
316 + wp_send_json_error( __( 'No image uploaded, try again.', 'quickwebp' ) );
385 317 }
386 -
387 318
319 + $settings = $this->get_ajax_settings();
388 320 $image_file = $this->file_is_image( $file, $settings );
321 +
389 322 if ( ! $image_file ) {
390 - wp_send_json_error( __( 'No image uploaded, try again.', QUICKWEBP_TEXT_DOMAIN ) );
323 + quickwebp_debug_log( 'image_optimization_ajax:not_image' );
324 + wp_send_json_error( __( 'No image uploaded, try again.', 'quickwebp' ) );
391 325 }
392 326
393 - if ( $settings['quickwebp_settings_conversion'] != '1' ) {
394 -
327 + $mode_enabled = $settings['quickwebp_settings_conversion'];
328 + if ( $mode_enabled === '0' ) {
329 + quickwebp_debug_log( 'image_optimization_ajax:conversion_disabled' );
395 330 $image_file['new_size'] = $image_file['size'];
396 331 $image_file['new_type'] = $image_file['type'];
397 332 $this->return_ajax_data( $image_file );
398 333 }
399 334
400 - $extension_to_use = $this->image_extension_loaded( $settings );
401 - if ( ! $extension_to_use ) {
335 + $quality = $this->get_the_quality( $settings );
336 + $is_pro = false;
337 + if ( $quickwebp_surecart_client ) {
338 + $is_pro = $quickwebp_surecart_client->license()->is_valid();
339 + }
402 340
403 - $image_file['new_size'] = $image_file['size'];
404 - $image_file['new_type'] = $image_file['type'];
405 - $this->return_ajax_data( $image_file );
341 + $image_file['new_size'] = $image_file['size'];
342 + $image_file['new_type'] = $image_file['type'];
343 +
344 + if ( '2' === $mode_enabled && $is_pro ) {
345 + quickwebp_debug_log( 'image_optimization_ajax:try_avif', array( 'quality' => $quality ) );
346 + $avif_image = $this->create_avif_image( $image_file['tmp_name'], $image_file['tmp_name'], $quality );
347 + if ( $avif_image ) {
348 + $image_file['new_size'] = filesize( $image_file['tmp_name'] );
349 + $image_file['new_type'] = 'image/avif';
350 + }
406 351 }
407 352
408 - $manager = new ImageManager(array('driver'=>$extension_to_use));
409 - $image = $manager->make($image_file['tmp_name']);
410 - $quality = $this->get_quality( $image_file['tmp_name'], $settings );
353 + if ( '1' === $mode_enabled ) {
354 + quickwebp_debug_log( 'image_optimization_ajax:try_webp', array( 'quality' => $quality ) );
355 + $webp_image = $this->create_webp_image( $image_file['tmp_name'], $image_file['tmp_name'], $quality );
356 + if ( $webp_image ) {
357 + $image_file['new_size'] = filesize( $image_file['tmp_name'] );
358 + $image_file['new_type'] = 'image/webp';
359 + }
360 + }
411 361
412 - $image->sharpen( $settings['quickwebp_settings_conversion_sharpen'] );
413 - $image->save( $image_file['tmp_name'], $quality, 'webp' );
362 + $this->return_ajax_data( $image_file );
363 + }
414 364
415 - $image_file['new_size'] = $image->filesize();
416 - $image_file['new_type'] = $image->mime();
417 - $this->return_ajax_data( $image_file );
365 + /**
366 + * Optimize a single media
367 + */
368 + public function single_optimization_ajax() {
369 + quickwebp_debug_log( 'single_optimization_ajax:start' );
370 +
371 + // verify the nonce.
372 + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
373 + if( !wp_verify_nonce( $nonce, 'quickwebp_admin_attachment' ) ) {
374 + wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
375 + }
376 +
377 + // Sanitize data
378 + $attachment_id = isset( $_POST['attachment_id'] ) ? absint( wp_unslash( $_POST['attachment_id'] ) ) : 0;
379 +
380 + if ( ! $attachment_id ) {
381 + quickwebp_debug_log( 'single_optimization_ajax:missing_attachment_id' );
382 + wp_send_json_error( __( 'No attachment id.', 'quickwebp' ) );
383 + }
384 +
385 + $settings = $this->get_settings();
386 + $mode_enabled = $settings['quickwebp_settings_conversion'];
387 + if ( '0' === $mode_enabled ) {
388 + wp_send_json_error( __( 'Choose an image format and save the settings.', 'quickwebp' ) );
389 + }
390 +
391 + $already_optimized = get_post_meta( $attachment_id, 'quickwebp_already_optimized', true );
392 +
393 + if ( '1' == $already_optimized && '1' == $mode_enabled ) {
394 + wp_send_json_error( __( 'Already optimized.', 'quickwebp' ) );
395 + }
396 +
397 + if ( '2' == $already_optimized && '2' == $mode_enabled ) {
398 + wp_send_json_error( __( 'Already optimized.', 'quickwebp' ) );
399 + }
400 +
401 + $post_mime_type = get_post_mime_type( $attachment_id );
402 + if ( ! in_array( $post_mime_type, $this->allowed_mime_types ) ) {
403 + wp_send_json_error( __( 'Not a valid image.', 'quickwebp' ) );
404 + }
405 +
406 + $sizes = $this->get_media_files( $attachment_id );
407 + $new_sizes = array();
408 +
409 + foreach ( $sizes as $key => $size ) {
410 + quickwebp_debug_log( 'single_optimization_ajax:optimize_size', array( 'size_key' => $key, 'path' => $size['path'] ?? '' ) );
411 + $result = $this->optimize_local_file( $size );
412 +
413 + if ( $result ) {
414 + $new_sizes[$key] = $result;
415 + }
416 + }
417 +
418 + if ( ! empty( $new_sizes ) ) {
419 + $this->record_attachment_optimization_stats( $new_sizes );
420 +
421 + $data = get_post_meta( $attachment_id, 'quickwebp_data', true );
422 + if ( ! empty( $data ) ) {
423 + $this->remove_related_files( $data );
424 + }
425 +
426 + if ( '1' == $mode_enabled ) {
427 + update_post_meta( $attachment_id, 'quickwebp_already_optimized', '1' );
428 + } elseif ( '2' == $mode_enabled ) {
429 + update_post_meta( $attachment_id, 'quickwebp_already_optimized', '2' );
430 + }
431 +
432 + update_post_meta( $attachment_id, 'quickwebp_data', $new_sizes );
433 + delete_post_meta( $attachment_id, 'quickwebp_has_error' );
434 + } else {
435 + update_post_meta( $attachment_id, 'quickwebp_has_error', '1' );
436 + delete_post_meta( $attachment_id, 'quickwebp_already_optimized' );
437 + delete_post_meta( $attachment_id, 'quickwebp_data' );
438 + }
439 +
440 + $wp_media_extend = new Quickwebp_Wp_Media_Extends( $this->plugin_name, $this->version );
441 + $html = $wp_media_extend->attachment_data( $new_sizes, $attachment_id );
442 +
443 + wp_send_json_success( $html );
418 444 }
419 445
420 446 /**
421 - * Return ajax data
447 + * Undo a single media optimization
422 448 */
423 - public function return_ajax_data( $data ) {
449 + public function undo_single_optimization_ajax() {
424 450
425 - $return = array(
426 - 'original_name' => $data['original_name'],
427 - 'size' => $data['size'],
428 - 'type' => $this->type_from_mime_type( $data['type'] ),
429 - 'mime_type' => $data['type'],
430 - 'image' => 'data:'.$data['new_type'].';base64,' . base64_encode( file_get_contents( $data['tmp_name'] ) ),
431 - 'name' => $data['name'],
432 - 'new_size' => $data['new_size'],
433 - 'new_type' => $this->type_from_mime_type( $data['new_type'] ),
434 - 'new_mime_type' => $data['new_type']
435 - );
451 + // verify the nonce.
452 + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
453 + if( !wp_verify_nonce( $nonce, 'quickwebp_admin_attachment' ) ) {
454 + wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
455 + }
436 456
437 - wp_send_json_success( $return );
457 + // Sanitize data
458 + $attachment_id = isset( $_POST['attachment_id'] ) ? absint( wp_unslash( $_POST['attachment_id'] ) ) : 0;
459 +
460 + if ( ! $attachment_id ) {
461 + wp_send_json_error( __( 'No attachment id.', 'quickwebp' ) );
462 + }
463 +
464 + $data = get_post_meta( $attachment_id, 'quickwebp_data', true );
465 + if ( ! empty( $data ) ) {
466 + $this->remove_related_files( $data );
467 + delete_post_meta( $attachment_id, 'quickwebp_already_optimized' );
468 + delete_post_meta( $attachment_id, 'quickwebp_data' );
469 +
470 + $wp_media_extend = new Quickwebp_Wp_Media_Extends( $this->plugin_name, $this->version );
471 + $html = $wp_media_extend->optimize_btn( $attachment_id );
472 + wp_send_json_success( $html );
473 + } else {
474 + wp_send_json_error( __( 'Not optimized.', 'quickwebp' ) );
475 + }
438 476 }
439 477
440 478 /**
441 - * Get the type from the mime type
479 + * Trigger before delete attachment
442 480 */
443 - public function type_from_mime_type( $mime_type ) {
481 + public function before_delete_attachment( $post_id ) {
482 + $data = get_post_meta( $post_id, 'quickwebp_data', true );
483 + if ( ! empty( $data ) ) {
484 + $this->remove_related_files( $data );
485 + }
486 + }
444 487
445 - $array = array(
446 - 'image/jpeg' => 'JPEG',
447 - 'image/png' => 'PNG',
448 - 'image/webp' => 'WebP'
488 + /**
489 + * Get the image data for the preview in the settings page
490 + */
491 + public function get_image_data_for_preview_ajax() {
492 + quickwebp_debug_log( 'get_image_data_for_preview_ajax:start' );
493 +
494 + // verify the nonce.
495 + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
496 + if( !wp_verify_nonce( $nonce, 'image_optimize_nonce' ) ) {
497 + wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
498 + }
499 +
500 + // Get the file
501 + $file = count($_FILES) > 0 ? array_shift($_FILES) : array();
502 + if ( empty($file) ) {
503 + quickwebp_debug_log( 'get_image_data_for_preview_ajax:missing_file' );
504 + wp_send_json_error( __( 'No image uploaded, try again.', 'quickwebp' ) );
505 + }
506 +
507 + $image_file = $this->file_is_image( $file );
508 + if ( ! $image_file ) {
509 + quickwebp_debug_log( 'get_image_data_for_preview_ajax:not_image' );
510 + wp_send_json_error( __( 'No image uploaded, try again.', 'quickwebp' ) );
511 + }
512 +
513 + $preview_image_data = array(
514 + 'name' => $image_file['name'],
515 + 'size' => size_format( $image_file['size'], 2 ),
516 + 'dimensions' => $image_file['width'] . ' x ' . $image_file['height'],
449 517 );
450 518
451 - return $array[$mime_type] ?? '';
519 + $conversions = array( '1', '2' );
520 + $qualities = array( 'low', 'medium', 'high', 'extra_high' );
521 +
522 + foreach ( $conversions as $conversion ) {
523 + foreach ( $qualities as $quality ) {
524 + quickwebp_debug_log( 'get_image_data_for_preview_ajax:preview_variant', array( 'conversion' => $conversion, 'quality' => $quality ) );
525 +
526 + $size_after = 0;
527 +
528 + if ( '2' == $conversion ) {
529 + $new_path = $image_file['tmp_name'] . '-' . $quality . '.avif';
530 + $quality_value = $this->get_the_quality_values( $quality, $conversion );
531 + $avif_image = $this->create_avif_image( $image_file['tmp_name'], $new_path, $quality_value );
532 +
533 + if ( $avif_image ) {
534 + $size_after = filesize( $new_path );
535 + }
536 + } elseif ( '1' == $conversion ) {
537 + $new_path = $image_file['tmp_name'] . '-' . $quality . '.webp';
538 + $quality_value = $this->get_the_quality_values( $quality, $conversion );
539 + $webp_image = $this->create_webp_image( $image_file['tmp_name'], $new_path, $quality_value );
540 +
541 + if ( $webp_image ) {
542 + $size_after = filesize( $new_path );
543 + }
544 + }
545 +
546 + $deference = $image_file['size'] - $size_after;
547 + $percent = $deference / $image_file['size'] * 100;
548 +
549 + $preview_image_data[$conversion . '_' . $quality] = array(
550 + 'size' => size_format( $size_after, 2 ),
551 + 'save' => size_format( $deference, 2 ),
552 + 'save_percent' => round( $percent ) . '%',
553 + 'percent' => round( 100 - $percent ) . '%',
554 + );
555 + }
556 + }
557 +
558 + wp_send_json_success( $preview_image_data );
452 559 }
453 560
454 561 /**
455 562 * Get the unoptimized media ids
563 + *
564 + * @param int $limit Maximum number of media IDs to retrieve.
565 + * @return int[]
456 566 */
457 - public function get_unoptimized_media_ids() {
567 + public function get_unoptimized_media_ids( $limit = 5 ) {
568 + $query_args = $this->get_unoptimized_media_query_args();
569 + $query_args['posts_per_page'] = max( 1, absint( $limit ) );
570 + $query_args['fields'] = 'ids';
571 + $query_args['no_found_rows'] = true;
458 572
573 + return get_posts( $query_args );
574 + }
575 +
576 + /**
577 + * Count unoptimized media.
578 + *
579 + * @return int
580 + */
581 + public function get_unoptimized_media_count() {
582 + $query_args = $this->get_unoptimized_media_query_args();
583 + $query_args['posts_per_page'] = 1;
584 + $query_args['fields'] = 'ids';
585 +
586 + $query = new WP_Query( $query_args );
587 +
588 + return absint( $query->found_posts );
589 + }
590 +
591 + /**
592 + * Get the shared query arguments for unoptimized media.
593 + *
594 + * @return array
595 + */
596 + private function get_unoptimized_media_query_args() {
597 +
598 + $settings = $this->get_settings();
599 + $mode_enabled = $settings['quickwebp_settings_conversion'];
600 + $ignore_same_format = is_array( $settings['quickwebp_settings_conversion_ignore_webp'] ) ? $settings['quickwebp_settings_conversion_ignore_webp'] : array();
601 +
459 602 $statuses = array(
460 603 'inherit' => 'inherit',
461 604 'private' => 'private',
462 605 );
@@ -465,18 +608,46 @@
465 608 if ( $custom_statuses ) {
466 609 $statuses = array_merge( $statuses, $custom_statuses );
467 610 }
468 611
469 - $mime_types = $this->allowed_mime_types;
470 - $index = array_search( 'image/webp', $mime_types );
471 - unset( $mime_types[$index] );
612 + $allowed_mime_types = $this->allowed_mime_types;
613 + if ( in_array( 'checked', $ignore_same_format ) ) {
614 + if ( '1' == $mode_enabled ) {
615 + $webp_index = array_search( 'image/webp', $allowed_mime_types );
616 + unset( $allowed_mime_types[$webp_index] );
617 + } elseif ( '2' == $mode_enabled ) {
618 + $avif_index = array_search( 'image/avif', $allowed_mime_types );
619 + unset( $allowed_mime_types[$avif_index] );
620 + }
621 + }
472 622
473 - $media_ids = get_posts( array(
623 + $meta_query_already_optimized = array(
624 + 'relation' => 'OR',
625 + array(
626 + 'key' => 'quickwebp_already_optimized',
627 + 'compare' => 'NOT EXISTS'
628 + ),
629 + );
630 +
631 + if ( 'webp' == $mode_enabled ) {
632 + $meta_query_already_optimized[] = array(
633 + 'key' => 'quickwebp_already_optimized',
634 + 'compare' => '!=',
635 + 'value' => '1',
636 + );
637 + } elseif ( 'avif' == $mode_enabled ) {
638 + $meta_query_already_optimized[] = array(
639 + 'key' => 'quickwebp_already_optimized',
640 + 'compare' => '!=',
641 + 'value' => '2',
642 + );
643 + }
644 +
645 + return array(
474 646 'post_type' => 'attachment',
475 - 'post_mime_type' => $mime_types,
647 + 'post_mime_type' => $allowed_mime_types,
476 648 'post_status' => array_keys( $statuses ),
477 - 'posts_per_page' => -1,
478 - 'fields' => 'ids',
649 + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
479 650 'meta_query' => array(
480 651 'relation' => 'AND',
481 652 array(
482 653 'key' => 'quickwebp_has_error',
@@ -481,25 +652,11 @@
481 652 array(
482 653 'key' => 'quickwebp_has_error',
483 654 'compare' => 'NOT EXISTS'
484 655 ),
485 - array(
486 - 'relation' => 'OR',
487 - array(
488 - 'key' => 'quickwebp_already_optimized',
489 - 'compare' => 'NOT EXISTS'
490 - ),
491 - array(
492 - 'key' => 'quickwebp_already_optimized',
493 - 'compare' => '=',
494 - 'value' => '0'
495 - )
496 - ),
656 + $meta_query_already_optimized,
497 657 ),
498 - ) );
499 -
500 - return $media_ids;
501 -
658 + );
502 659 }
503 660
504 661 /**
505 662 * Get the list of media files
@@ -504,9 +661,8 @@
504 661 /**
505 662 * Get the list of media files
506 663 */
507 664 public function get_media_files( $media_id ) {
508 -
509 665 $fullsize_path = get_attached_file( $media_id );
510 666
511 667 if ( ! $fullsize_path ) {
512 668 return array();
@@ -545,261 +701,755 @@
545 701 return $all_sizes;
546 702 }
547 703
548 704 /**
549 - * Validate that a post is an optimizable attachment.
705 + * Optimize a local file
550 706 */
551 - public function get_valid_attachment( $attachment_id ) {
707 + public function optimize_local_file( $size ) {
708 + global $quickwebp_surecart_client;
552 709
553 - $attachment_id = absint( $attachment_id );
554 - if ( ! $attachment_id ) {
710 + quickwebp_debug_log( 'optimize_local_file:start', array( 'path' => $size['path'] ?? '' ) );
711 +
712 + if ( !is_file($size['path']) ) {
713 + quickwebp_debug_log( 'optimize_local_file:missing_file', array( 'path' => $size['path'] ?? '' ) );
555 714 return false;
556 715 }
557 716
558 - $attachment = get_post( $attachment_id );
559 - if ( ! $attachment || 'attachment' !== $attachment->post_type ) {
717 + $real_type = mime_content_type( $size['path']);
718 + if ( !in_array( $real_type, $this->allowed_mime_types ) ) {
719 + quickwebp_debug_log( 'optimize_local_file:invalid_mime_type', array( 'mime_type' => $real_type ) );
560 720 return false;
561 721 }
562 722
563 - $mime_types = $this->allowed_mime_types;
564 - $index = array_search( 'image/webp', $mime_types, true );
565 - if ( false !== $index ) {
566 - unset( $mime_types[ $index ] );
723 + $settings = $this->get_settings();
724 + $quality = $this->get_the_quality( $settings );
725 + $is_pro = false;
726 + if ( $quickwebp_surecart_client ) {
727 + $is_pro = $quickwebp_surecart_client->license()->is_valid();
567 728 }
729 + $mode_enabled = $settings['quickwebp_settings_conversion'];
730 + $size_before = filesize( $size['path'] );
731 + $new_path = $size['path'] . '.' . ( '2' === $mode_enabled && $is_pro ? 'avif' : 'webp' );
732 + $size_after = 0;
568 733
569 - $mime_type = get_post_mime_type( $attachment_id );
570 - if ( ! in_array( $mime_type, $mime_types, true ) ) {
734 + if ( '2' === $mode_enabled && $is_pro ) {
735 + quickwebp_debug_log( 'optimize_local_file:try_avif', array( 'path' => $size['path'], 'quality' => $quality ) );
736 + $avif_image = $this->create_avif_image( $size['path'], $new_path, $quality );
737 + if ( $avif_image ) {
738 + $size_after = filesize( $new_path );
739 + }
740 + } elseif ( '1' === $mode_enabled ) {
741 + quickwebp_debug_log( 'optimize_local_file:try_webp', array( 'path' => $size['path'], 'quality' => $quality ) );
742 + $webp_image = $this->create_webp_image( $size['path'], $new_path, $quality );
743 + if ( $webp_image ) {
744 + $size_after = filesize( $new_path );
745 + }
746 + }
747 +
748 + if ( ! $size_after ) {
749 + quickwebp_debug_log( 'optimize_local_file:conversion_failed', array( 'path' => $size['path'], 'target_path' => $new_path ) );
571 750 return false;
572 751 }
573 752
574 - return $attachment;
753 + $deference = $size_before - $size_after;
754 + $percent = $deference / $size_before * 100;
755 +
756 + return array(
757 + 'success' => 1,
758 + 'original_size' => $size_before,
759 + 'optimized_size' => $size_after,
760 + 'percent' => round( $percent, 2 ),
761 + 'path' => $new_path,
762 + 'format' => $mode_enabled,
763 + );
575 764 }
576 765
577 766 /**
578 - * Check whether the current user can manage a specific attachment.
767 + * Record aggregated optimization statistics for one image attachment.
768 + *
769 + * @param array $optimized_sizes Optimization results for the attachment sizes.
579 770 */
580 - public function current_user_can_manage_attachment( $attachment_id ) {
771 + public function record_attachment_optimization_stats( $optimized_sizes ) {
772 + $stats = $this->get_attachment_optimization_stats( $optimized_sizes );
773 + $this->record_optimization_stats( $stats['bytes_before'], $stats['bytes_after'], $stats['images_optimized'] );
774 + }
581 775
582 - return current_user_can( 'upload_files' ) && current_user_can( 'edit_post', $attachment_id );
776 + /**
777 + * Calculate optimization statistics for attachment sizes without persisting them.
778 + *
779 + * @param array $optimized_sizes Optimization results for the attachment sizes.
780 + * @return array
781 + */
782 + public function get_attachment_optimization_stats( $optimized_sizes ) {
783 + $stats = array(
784 + 'images_optimized' => 0,
785 + 'bytes_before' => 0,
786 + 'bytes_after' => 0,
787 + );
788 +
789 + foreach ( $optimized_sizes as $optimized_size ) {
790 + $stats['bytes_before'] += absint( $optimized_size['original_size'] ?? 0 );
791 + $stats['bytes_after'] += absint( $optimized_size['optimized_size'] ?? 0 );
792 + }
793 +
794 + if ( $stats['bytes_before'] && $stats['bytes_after'] ) {
795 + $stats['images_optimized'] = 1;
796 + }
797 +
798 + return $stats;
583 799 }
584 800
585 801 /**
586 - * Check whether a generated WebP path is safe to delete.
802 + * Record local cumulative optimization statistics.
803 + *
804 + * @param int $size_before File size before optimization in bytes.
805 + * @param int $size_after File size after optimization in bytes.
806 + * @param int $images_optimized Number of optimized attachments.
587 807 */
588 - public function is_safe_generated_webp_path( $path ) {
808 + public function record_optimization_stats( $size_before, $size_after, $images_optimized = 1 ) {
809 + $size_before = absint( $size_before );
810 + $size_after = absint( $size_after );
811 + $images_optimized = absint( $images_optimized );
589 812
590 - if ( empty( $path ) || ! is_string( $path ) ) {
591 - return false;
813 + if ( ! $size_before || ! $size_after || ! $images_optimized ) {
814 + return;
592 815 }
593 816
594 - $uploads = wp_get_upload_dir();
595 - if ( empty( $uploads['basedir'] ) ) {
596 - return false;
817 + $lock_token = $this->acquire_optimization_stats_lock();
818 + if ( ! $lock_token ) {
819 + $this->queue_optimization_stats_delta( $size_before, $size_after, $images_optimized );
820 + return;
597 821 }
598 822
599 - $normalized_path = wp_normalize_path( $path );
600 - $normalized_base = trailingslashit( wp_normalize_path( $uploads['basedir'] ) );
823 + $stats = get_option( 'quickwebp_optimization_stats', array() );
824 + $stats = wp_parse_args(
825 + is_array( $stats ) ? $stats : array(),
826 + array(
827 + 'images_optimized' => 0,
828 + 'bytes_before' => 0,
829 + 'bytes_after' => 0,
830 + 'bytes_saved' => 0,
831 + )
832 + );
833 + $pending_stats = $this->consume_optimization_stats_deltas();
601 834
602 - if ( 0 !== strpos( $normalized_path, $normalized_base ) ) {
603 - return false;
835 + $stats['images_optimized'] = absint( $stats['images_optimized'] ) + $images_optimized + $pending_stats['images_optimized'];
836 + $stats['bytes_before'] = absint( $stats['bytes_before'] ) + $size_before + $pending_stats['bytes_before'];
837 + $stats['bytes_after'] = absint( $stats['bytes_after'] ) + $size_after + $pending_stats['bytes_after'];
838 + $stats['bytes_saved'] = max( 0, $stats['bytes_before'] - $stats['bytes_after'] );
839 +
840 + update_option( 'quickwebp_optimization_stats', $stats, false );
841 + $this->release_optimization_stats_lock( $lock_token );
842 + }
843 +
844 + /**
845 + * Queue a statistics delta when another request owns the update lock.
846 + *
847 + * @param int $size_before File size before optimization in bytes.
848 + * @param int $size_after File size after optimization in bytes.
849 + * @param int $images_optimized Number of optimized attachments.
850 + */
851 + private function queue_optimization_stats_delta( $size_before, $size_after, $images_optimized ) {
852 + $option_name = 'quickwebp_optimization_stats_delta_' . str_replace( '-', '', wp_generate_uuid4() );
853 + add_option(
854 + $option_name,
855 + array(
856 + 'images_optimized' => $images_optimized,
857 + 'bytes_before' => $size_before,
858 + 'bytes_after' => $size_after,
859 + ),
860 + '',
861 + false
862 + );
863 + }
864 +
865 + /**
866 + * Consume queued statistics deltas while holding the update lock.
867 + *
868 + * @return array
869 + */
870 + private function consume_optimization_stats_deltas() {
871 + global $wpdb;
872 +
873 + $pending_stats = array(
874 + 'images_optimized' => 0,
875 + 'bytes_before' => 0,
876 + 'bytes_after' => 0,
877 + );
878 + $option_prefix = 'quickwebp_optimization_stats_delta_';
879 + $option_names = $wpdb->get_col(
880 + $wpdb->prepare(
881 + "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s",
882 + $wpdb->esc_like( $option_prefix ) . '%'
883 + )
884 + );
885 +
886 + foreach ( $option_names as $option_name ) {
887 + $delta = get_option( $option_name, array() );
888 + if ( is_array( $delta ) ) {
889 + $pending_stats['images_optimized'] += absint( $delta['images_optimized'] ?? 0 );
890 + $pending_stats['bytes_before'] += absint( $delta['bytes_before'] ?? 0 );
891 + $pending_stats['bytes_after'] += absint( $delta['bytes_after'] ?? 0 );
892 + }
893 + delete_option( $option_name );
604 894 }
605 895
606 - return '.webp' === strtolower( substr( $normalized_path, -5 ) );
896 + return $pending_stats;
607 897 }
608 898
609 899 /**
610 - * Get the generated WebP files for an attachment.
900 + * Acquire an expiring lock for cumulative statistics updates.
901 + *
902 + * @return string|false
611 903 */
612 - public function get_generated_webp_paths( $attachment_id ) {
904 + private function acquire_optimization_stats_lock() {
905 + $lock_token = wp_generate_uuid4();
906 + $lock = array(
907 + 'token' => $lock_token,
908 + 'expires' => time() + MINUTE_IN_SECONDS,
909 + );
613 910
614 - $paths = array();
615 - $sizes = $this->get_media_files( $attachment_id );
911 + if ( add_option( 'quickwebp_optimization_stats_lock', $lock, '', false ) ) {
912 + return $lock_token;
913 + }
616 914
617 - foreach ( $sizes as $size ) {
618 - $path = isset( $size['path'] ) ? $size['path'] . '.webp' : '';
915 + $existing_lock = get_option( 'quickwebp_optimization_stats_lock', array() );
916 + if ( is_array( $existing_lock ) && absint( $existing_lock['expires'] ?? 0 ) < time() ) {
917 + delete_option( 'quickwebp_optimization_stats_lock' );
918 + if ( add_option( 'quickwebp_optimization_stats_lock', $lock, '', false ) ) {
919 + return $lock_token;
920 + }
921 + }
619 922
620 - if ( $this->is_safe_generated_webp_path( $path ) ) {
621 - $paths[] = $path;
923 + return false;
924 + }
925 +
926 + /**
927 + * Release the statistics lock owned by this process.
928 + *
929 + * @param string $lock_token Lock ownership token.
930 + */
931 + private function release_optimization_stats_lock( $lock_token ) {
932 + $lock = get_option( 'quickwebp_optimization_stats_lock', array() );
933 + if ( is_array( $lock ) && hash_equals( (string) ( $lock['token'] ?? '' ), $lock_token ) ) {
934 + delete_option( 'quickwebp_optimization_stats_lock' );
935 + }
936 + }
937 +
938 + /**
939 + * Remove the related files of an optimized attachment
940 + */
941 + public function remove_related_files( $data ) {
942 + foreach ( $data as $value ) {
943 + $path = $value['path'] ?? '';
944 +
945 + if ( ! empty( $path ) && file_exists( $path ) ) {
946 + wp_delete_file( $path );
622 947 }
623 948 }
949 + }
624 950
625 - return array_values( array_unique( $paths ) );
951 + /**
952 + * Get the optimization settings
953 + */
954 + public function get_settings() {
955 + return array(
956 + 'quickwebp_settings_conversion' => get_option('quickwebp_settings_conversion', quickwebp_settings_default('quickwebp_settings_conversion') ),
957 + 'quickwebp_settings_conversion_quality' => get_option('quickwebp_settings_conversion_quality', quickwebp_settings_default('quickwebp_settings_conversion_quality') ),
958 + 'quickwebp_settings_conversion_ignore_webp' => get_option('quickwebp_settings_conversion_ignore_webp', quickwebp_settings_default('quickwebp_settings_conversion_ignore_webp') ),
959 + 'quickwebp_settings_conversion_save_original' => get_option('quickwebp_settings_conversion_save_original', quickwebp_settings_default('quickwebp_settings_conversion_save_original') ),
960 + 'quickwebp_settings_resize' => get_option('quickwebp_settings_resize', quickwebp_settings_default('quickwebp_settings_resize') ),
961 + 'quickwebp_settings_resize_value' => get_option('quickwebp_settings_resize_value', quickwebp_settings_default('quickwebp_settings_resize_value') ),
962 + 'quickwebp_settings_completion' => get_option('quickwebp_settings_completion', quickwebp_settings_default('quickwebp_settings_completion') ),
963 + 'quickwebp_settings_completion_options' => get_option('quickwebp_settings_completion_options', quickwebp_settings_default('quickwebp_settings_completion_options') ),
964 + 'quickwebp_settings_cleanup' => get_option('quickwebp_settings_cleanup', quickwebp_settings_default('quickwebp_settings_cleanup') ),
965 + );
626 966 }
967 +
968 + /**
969 + * Get the optimization settings
970 + */
971 + private function get_ajax_settings() {
972 + //phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
973 + $raw_settings = isset( $_POST['settings'] ) ? wp_unslash( $_POST['settings'] ) : '';
974 + $settings = is_string( $raw_settings ) ? json_decode( $raw_settings, true ) : array();
627 975
976 + if ( ! is_array( $settings ) ) {
977 + $settings = array();
978 + }
979 +
980 + return array(
981 + 'quickwebp_settings_conversion' => isset( $settings['quickwebp_settings_conversion'] ) ? sanitize_text_field( (string) $settings['quickwebp_settings_conversion'] ) : '0',
982 + 'quickwebp_settings_conversion_quality' => isset( $settings['quickwebp_settings_conversion_quality'] ) ? sanitize_text_field( $settings['quickwebp_settings_conversion_quality'] ) : 'high',
983 + 'quickwebp_settings_conversion_ignore_webp' => isset( $settings['quickwebp_settings_conversion_ignore_webp'] ) && is_array( $settings['quickwebp_settings_conversion_ignore_webp'] ) ? array_map( 'sanitize_text_field', $settings['quickwebp_settings_conversion_ignore_webp'] ) : array(),
984 + 'quickwebp_settings_resize' => isset( $settings['quickwebp_settings_resize'] ) ? sanitize_text_field( (string) $settings['quickwebp_settings_resize'] ) : '',
985 + 'quickwebp_settings_resize_value' => isset( $settings['quickwebp_settings_resize_value'] ) ? absint( $settings['quickwebp_settings_resize_value'] ) : 0,
986 + 'quickwebp_settings_completion' => isset( $settings['quickwebp_settings_completion'] ) ? sanitize_text_field( (string) $settings['quickwebp_settings_completion'] ) : '',
987 + 'quickwebp_settings_completion_options' => isset( $settings['quickwebp_settings_completion_options'] ) && is_array( $settings['quickwebp_settings_completion_options'] ) ? array_map( 'sanitize_text_field', $settings['quickwebp_settings_completion_options'] ) : array(),
988 + 'quickwebp_settings_cleanup' => isset( $settings['quickwebp_settings_cleanup'] ) ? sanitize_text_field( (string) $settings['quickwebp_settings_cleanup'] ) : '',
989 + );
990 + }
991 +
628 992 /**
629 - * Optimize a local file
993 + * Check if the file is an image
630 994 */
631 - public function optimize_local_file( $size ) {
995 + private function file_is_image( $file, $settings = array() ) {
632 996
633 - $settings = $this->get_settings();
997 + $file_name = isset($file['name']) ? $file['name'] : '';
998 + $file_type = isset($file['type']) ? $file['type'] : '';
999 + $file_tmp_name = isset($file['tmp_name']) ? $file['tmp_name'] : '';
1000 + $file_error = isset($file['error']) ? $file['error'] : '';
1001 + $file_size = isset($file['size']) ? $file['size'] : '';
634 1002
635 - $extension_to_use = $this->image_extension_loaded( $settings );
636 - if ( ! $extension_to_use ) {
1003 + if ( empty($file_tmp_name) ) {
1004 + quickwebp_debug_log( 'file_is_image:missing_tmp_name', array( 'file_name' => $file_name ) );
637 1005 return false;
638 1006 }
639 1007
640 - if ( !is_file($size['path']) ) {
1008 + $allowed_mime_types = apply_filters( 'quickwebp_mime_types_allowed', $this->allowed_mime_types );
1009 + $is_image = wp_getimagesize($file_tmp_name);
1010 + $mime_type = wp_get_image_mime($file_tmp_name);
1011 + if ( ! $is_image || ! in_array( $mime_type, $allowed_mime_types ) ) {
1012 + quickwebp_debug_log( 'file_is_image:rejected', array( 'file_name' => $file_name, 'mime_type' => $mime_type ) );
641 1013 return false;
642 1014 }
643 1015
644 - $real_type = mime_content_type( $size['path']);
645 - if ( !in_array( $real_type, $this->allowed_mime_types ) ) {
646 - return false;
1016 + $name = $file_name;
1017 +
1018 + if ( isset($settings['quickwebp_settings_cleanup']) && $settings['quickwebp_settings_cleanup'] == '1' ) {
1019 + $name = quickwebp_sanitize_name($file_name);
647 1020 }
648 1021
649 - try {
650 - $size_before = filesize( $size['path'] );
651 - $manager = new ImageManager(array('driver'=>$extension_to_use));
652 - $image = $manager->make($size['path']);
653 - $quality = $this->get_quality( $size['path'], $settings );
654 - $webp_path = $size['path'].'.webp';
655 -
656 - $image->sharpen($settings['quickwebp_settings_conversion_sharpen']);
657 - $image->save( $webp_path, $quality, 'webp' );
658 - $size_after = $image->filesize();
659 - $image->destroy();
660 -
661 - $deference = $size_before - $size_after;
662 - $percent = $deference / $size_before * 100;
663 -
664 - return array(
665 - 'success' => 1,
666 - 'original_size' => $size_before,
667 - 'optimized_size' => $size_after,
668 - 'percent' => round( $percent, 2 ),
669 - 'path' => $webp_path
670 - );
671 - } catch (\Throwable $th) {
672 - return false;
1022 + return array(
1023 + 'original_name' => $file_name,
1024 + 'name' => $name,
1025 + 'type' => $file_type,
1026 + 'tmp_name' => $file_tmp_name,
1027 + 'error' => $file_error,
1028 + 'size' => $file_size,
1029 + 'width' => $is_image[0],
1030 + 'height' => $is_image[1],
1031 + );
1032 + }
1033 +
1034 + /**
1035 + * Return ajax data
1036 + */
1037 + private function return_ajax_data( $data ) {
1038 + $return = array(
1039 + 'original_name' => $data['original_name'],
1040 + 'size' => $data['size'],
1041 + 'type' => $this->type_from_mime_type( $data['type'] ),
1042 + 'mime_type' => $data['type'],
1043 + 'image' => 'data:'.$data['new_type'].';base64,' . base64_encode( file_get_contents( $data['tmp_name'] ) ),
1044 + 'name' => $data['name'],
1045 + 'new_size' => $data['new_size'],
1046 + 'new_type' => $this->type_from_mime_type( $data['new_type'] ),
1047 + 'new_mime_type' => $data['new_type']
1048 + );
1049 +
1050 + wp_send_json_success( $return );
1051 + }
1052 +
1053 + /**
1054 + * Get the type from the mime type
1055 + */
1056 + private function type_from_mime_type( $mime_type ) {
1057 + $array = array(
1058 + 'image/jpeg' => 'JPEG',
1059 + 'image/png' => 'PNG',
1060 + 'image/webp' => 'WebP',
1061 + 'image/avif' => 'AVIF',
1062 + );
1063 +
1064 + return $array[$mime_type] ?? '';
1065 + }
1066 +
1067 + /**
1068 + * Get the quality
1069 + */
1070 + private function get_the_quality( $settings ) {
1071 + $mode_enabled = $settings['quickwebp_settings_conversion'];
1072 + $quality = $settings['quickwebp_settings_conversion_quality'];
1073 +
1074 + return $this->get_the_quality_values( $quality, $mode_enabled );
1075 + }
1076 +
1077 + /**
1078 + * Get the quality values for avif and webp
1079 + */
1080 + private function get_the_quality_values( $quality, $mode_enabled ) {
1081 + $result = 50;
1082 +
1083 + switch ( $quality ) {
1084 + case 'low':
1085 + $result = 50;
1086 + if ( '2' === $mode_enabled ) {
1087 + $result = 30;
1088 + }
1089 + break;
1090 + case 'medium':
1091 + $result = 60;
1092 + if ( '2' === $mode_enabled ) {
1093 + $result = 40;
1094 + }
1095 + break;
1096 + case 'high':
1097 + $result = 75;
1098 + if ( '2' === $mode_enabled ) {
1099 + $result = 50;
1100 + }
1101 + break;
1102 + case 'extra_high':
1103 + $result = 90;
1104 + if ( '2' === $mode_enabled ) {
1105 + $result = 70;
1106 + }
1107 + break;
673 1108 }
1109 +
1110 + return $result;
674 1111 }
675 1112
676 1113 /**
677 - * Optimize a single media
1114 + * Get the extension matching an image mime type.
1115 + *
1116 + * @param string $mime_type Image mime type.
1117 + *
1118 + * @return string
678 1119 */
679 - public function single_optimizition_ajax() {
1120 + private function get_extension_from_mime_type( $mime_type ) {
1121 + $extensions = array(
1122 + 'image/avif' => 'avif',
1123 + 'image/webp' => 'webp',
1124 + 'image/jpeg' => 'jpg',
1125 + 'image/png' => 'png',
1126 + );
680 1127
681 - // verify the nonce.
682 - $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
683 - if( !wp_verify_nonce( $nonce, 'quickwebp_admin_attachment' ) ) {
684 - wp_send_json_error( __( 'Refresh the page and try again.', QUICKWEBP_TEXT_DOMAIN ) );
685 - }
1128 + return $extensions[ $mime_type ] ?? '';
1129 + }
686 1130
687 - // Sanitize data
688 - $attachment_id = isset( $_POST['attachment_id'] ) ? absint( wp_unslash( $_POST['attachment_id'] ) ) : 0;
1131 + /**
1132 + * Create an image using the WordPress image editor abstraction.
1133 + *
1134 + * @param string $file_path Path to the source image.
1135 + * @param string $output_path Path where the converted image should be written.
1136 + * @param int $quality Compression quality.
1137 + * @param string $target_mime_type Output mime type.
1138 + *
1139 + * @return bool
1140 + */
1141 + private function create_image_with_wp_editor( $file_path, $output_path, $quality, $target_mime_type ) {
1142 + quickwebp_debug_log( 'create_image_with_wp_editor:start', array(
1143 + 'file_path' => $file_path,
1144 + 'output_path' => $output_path,
1145 + 'quality' => $quality,
1146 + 'target_mime_type' => $target_mime_type,
1147 + ) );
689 1148
690 - if ( ! $attachment_id ) {
691 - wp_send_json_error( __( 'No attachment id.', QUICKWEBP_TEXT_DOMAIN ) );
1149 + $editor = wp_get_image_editor( $file_path );
1150 +
1151 + if ( is_wp_error( $editor ) ) {
1152 + quickwebp_debug_log( 'create_image_with_wp_editor:editor_error', array( 'message' => $editor->get_error_message() ) );
1153 + return false;
692 1154 }
693 1155
694 - if ( ! $this->get_valid_attachment( $attachment_id ) ) {
695 - wp_send_json_error( __( 'Not a valid image.', QUICKWEBP_TEXT_DOMAIN ), 400 );
1156 + $extension = $this->get_extension_from_mime_type( $target_mime_type );
1157 + if ( empty( $extension ) ) {
1158 + quickwebp_debug_log( 'create_image_with_wp_editor:missing_extension', array( 'target_mime_type' => $target_mime_type ) );
1159 + return false;
696 1160 }
697 1161
698 - if ( ! $this->current_user_can_manage_attachment( $attachment_id ) ) {
699 - wp_send_json_error( __( 'You are not allowed to manage this attachment.', QUICKWEBP_TEXT_DOMAIN ), 403 );
1162 + $save_path = $output_path;
1163 + if ( pathinfo( $output_path, PATHINFO_EXTENSION ) !== $extension ) {
1164 + $save_path = $output_path . '.' . $extension;
700 1165 }
701 1166
702 - $already_optimized = get_post_meta( $attachment_id, 'quickwebp_already_optimized', true );
703 - if ( $already_optimized === '1' ) {
704 - wp_send_json_error( __( 'Already optimized.', QUICKWEBP_TEXT_DOMAIN ) );
1167 + $editor->set_quality( $quality );
1168 + $result = $editor->save( $save_path, $target_mime_type );
1169 +
1170 + if ( is_wp_error( $result ) || empty( $result['path'] ) || ! file_exists( $result['path'] ) ) {
1171 + quickwebp_debug_log( 'create_image_with_wp_editor:save_failed', array(
1172 + 'output_path' => $save_path,
1173 + 'error' => is_wp_error( $result ) ? $result->get_error_message() : 'missing_generated_file',
1174 + ) );
1175 + return false;
705 1176 }
706 1177
707 - $sizes = $this->get_media_files( $attachment_id );
708 - $new_sizes = array();
1178 + $source_size = file_exists( $file_path ) ? filesize( $file_path ) : 0;
1179 + $generated_size = filesize( $result['path'] );
709 1180
710 - foreach ( $sizes as $key => $size ) {
1181 + if ( $source_size > 0 && $generated_size > $source_size ) {
1182 + quickwebp_debug_log( 'create_image_with_wp_editor:larger_than_source', array(
1183 + 'source_size' => $source_size,
1184 + 'generated_size' => $generated_size,
1185 + 'output_path' => $result['path'],
1186 + ) );
1187 + wp_delete_file( $result['path'] );
1188 + return false;
1189 + }
711 1190
712 - $result = $this->optimize_local_file( $size );
1191 + if ( $result['path'] !== $output_path ) {
1192 + $file_contents = file_get_contents( $result['path'] );
713 1193
714 - if ( $result ) {
715 - $new_sizes[$key] = $result;
1194 + if ( false === $file_contents || false === file_put_contents( $output_path, $file_contents ) ) {
1195 + quickwebp_debug_log( 'create_image_with_wp_editor:copy_failed', array( 'output_path' => $output_path ) );
1196 + return false;
716 1197 }
717 - }
718 1198
719 - if ( ! empty( $new_sizes ) ) {
720 - update_post_meta( $attachment_id, 'quickwebp_already_optimized', '1' );
721 - update_post_meta( $attachment_id, 'quickwebp_data', $new_sizes );
722 - delete_post_meta( $attachment_id, 'quickwebp_has_error' );
723 - } else {
724 - update_post_meta( $attachment_id, 'quickwebp_has_error', '1' );
1199 + wp_delete_file( $result['path'] );
725 1200 }
726 1201
727 - $wp_media_extend = new Quickwebp_Wp_Media_Extends( $this->plugin_name, $this->version );
728 - $html = $wp_media_extend->attachment_data( $new_sizes, $attachment_id );
1202 + quickwebp_debug_log( 'create_image_with_wp_editor:success', array(
1203 + 'output_path' => $output_path,
1204 + 'generated_size' => file_exists( $output_path ) ? filesize( $output_path ) : $generated_size,
1205 + 'target_mime_type'=> $target_mime_type,
1206 + ) );
729 1207
730 - wp_send_json_success( $html );
1208 + return true;
731 1209 }
732 1210
733 1211 /**
734 - * Undo a single media optimization
1212 + * Create a GD image resource when the required loader is available.
1213 + *
1214 + * @param string $file_path Path to the source image.
1215 + * @param string $mime_type Source mime type.
1216 + *
1217 + * @return GdImage|resource|false
735 1218 */
736 - public function undo_single_optimizition_ajax() {
1219 + private function create_gd_image_resource( $file_path, $mime_type ) {
1220 + $loaders = array(
1221 + 'image/avif' => 'imagecreatefromavif',
1222 + 'image/webp' => 'imagecreatefromwebp',
1223 + 'image/jpeg' => 'imagecreatefromjpeg',
1224 + 'image/png' => 'imagecreatefrompng',
1225 + );
737 1226
738 - // verify the nonce.
739 - $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
740 - if( !wp_verify_nonce( $nonce, 'quickwebp_admin_attachment' ) ) {
741 - wp_send_json_error( __( 'Refresh the page and try again.', QUICKWEBP_TEXT_DOMAIN ) );
1227 + if ( empty( $loaders[ $mime_type ] ) ) {
1228 + return false;
742 1229 }
743 1230
744 - // Sanitize data
745 - $attachment_id = isset( $_POST['attachment_id'] ) ? absint( wp_unslash( $_POST['attachment_id'] ) ) : 0;
1231 + $loader = $loaders[ $mime_type ];
746 1232
747 - if ( ! $attachment_id ) {
748 - wp_send_json_error( __( 'No attachment id.', QUICKWEBP_TEXT_DOMAIN ) );
1233 + if ( ! function_exists( $loader ) ) {
1234 + return false;
749 1235 }
750 1236
751 - if ( ! $this->get_valid_attachment( $attachment_id ) ) {
752 - wp_send_json_error( __( 'Not a valid image.', QUICKWEBP_TEXT_DOMAIN ), 400 );
1237 + return $loader( $file_path );
1238 + }
1239 +
1240 + /**
1241 + * Check whether the source mime type can be loaded through GD.
1242 + *
1243 + * @param string $mime_type Source mime type.
1244 + *
1245 + * @return bool
1246 + */
1247 + private function can_load_image_with_gd( $mime_type ) {
1248 + $loaders = array(
1249 + 'image/avif' => 'imagecreatefromavif',
1250 + 'image/webp' => 'imagecreatefromwebp',
1251 + 'image/jpeg' => 'imagecreatefromjpeg',
1252 + 'image/png' => 'imagecreatefrompng',
1253 + );
1254 +
1255 + if ( empty( $loaders[ $mime_type ] ) ) {
1256 + return false;
753 1257 }
754 1258
755 - if ( ! $this->current_user_can_manage_attachment( $attachment_id ) ) {
756 - wp_send_json_error( __( 'You are not allowed to manage this attachment.', QUICKWEBP_TEXT_DOMAIN ), 403 );
1259 + return function_exists( $loaders[ $mime_type ] );
1260 + }
1261 +
1262 + /**
1263 + * Create the avif image
1264 + */
1265 + private function create_avif_image( $file_path, $output_path, $quality ) {
1266 + $image = false;
1267 + $mime_type = wp_get_image_mime( $file_path );
1268 +
1269 + quickwebp_debug_log( 'create_avif_image:start', array(
1270 + 'file_path' => $file_path,
1271 + 'output_path' => $output_path,
1272 + 'mime_type' => $mime_type,
1273 + 'quality' => $quality,
1274 + ) );
1275 +
1276 + if ( ! $this->can_load_image_with_gd( $mime_type ) && $this->create_image_with_wp_editor( $file_path, $output_path, $quality, 'image/avif' ) ) {
1277 + quickwebp_debug_log( 'create_avif_image:used_wp_editor', array( 'mime_type' => $mime_type ) );
1278 + return true;
757 1279 }
758 1280
759 - $already_optimized = get_post_meta( $attachment_id, 'quickwebp_already_optimized', true );
760 - if ( $already_optimized === '1' ) {
1281 + $image = $this->create_gd_image_resource( $file_path, $mime_type );
761 1282
762 - $this->remove_related_files( $attachment_id );
763 - delete_post_meta( $attachment_id, 'quickwebp_already_optimized' );
764 - delete_post_meta( $attachment_id, 'quickwebp_data' );
1283 + if ( ! $image ) {
1284 + quickwebp_debug_log( 'create_avif_image:gd_load_failed', array( 'mime_type' => $mime_type ) );
1285 + return false;
1286 + }
765 1287
766 - $wp_media_extend = new Quickwebp_Wp_Media_Extends( $this->plugin_name, $this->version );
767 - $html = $wp_media_extend->optimize_btn( $attachment_id );
1288 + // Handle EXIF orientation for proper image rotation
1289 + $exif_data = @exif_read_data( $file_path );
1290 + if ( ! empty( $exif_data['Orientation'] ) ) {
1291 + $orientation = (int) $exif_data['Orientation'];
1292 + //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
1293 + $orientation = apply_filters( 'wp_image_maybe_exif_rotate', $orientation, $file_path );
1294 + if ( $orientation && 1 !== $orientation ) {
1295 + switch ( $orientation ) {
1296 + case 2:
1297 + // Flip horizontally.
1298 + imageflip( $image, IMG_FLIP_HORIZONTAL );
1299 + break;
1300 + case 3:
1301 + // Rotate 180 degrees.
1302 + $image = imagerotate( $image, 180, 0 );
1303 + break;
1304 + case 4:
1305 + // Flip vertically.
1306 + imageflip( $image, IMG_FLIP_VERTICAL );
1307 + break;
1308 + case 5:
1309 + // Rotate 90 degrees counter-clockwise and flip vertically.
1310 + $image = imagerotate( $image, -90, 0 );
1311 + imageflip( $image, IMG_FLIP_VERTICAL );
1312 + break;
1313 + case 6:
1314 + // Rotate 90 degrees clockwise (270 counter-clockwise).
1315 + $image = imagerotate( $image, -90, 0 );
1316 + break;
1317 + case 7:
1318 + // Rotate 90 degrees counter-clockwise and flip horizontally.
1319 + $image = imagerotate( $image, 90, 0 );
1320 + imageflip( $image, IMG_FLIP_HORIZONTAL );
1321 + break;
1322 + case 8:
1323 + // Rotate 90 degrees counter-clockwise.
1324 + $image = imagerotate( $image, 90, 0 );
1325 + break;
1326 + }
1327 + }
1328 + }
1329 +
1330 + if ( ! imageistruecolor( $image ) ) {
1331 + $truecolor = imagecreatetruecolor( imagesx( $image ), imagesy( $image ) );
1332 +
1333 + if ( $mime_type === 'image/png' ) {
1334 + imagealphablending( $truecolor, false );
1335 + imagesavealpha( $truecolor, true );
1336 + $transparent = imagecolorallocatealpha( $truecolor, 0, 0, 0, 127 );
1337 + imagefilledrectangle( $truecolor, 0, 0, imagesx( $image ), imagesy( $image ), $transparent );
1338 + }
1339 +
1340 + imagecopy( $truecolor, $image, 0, 0, 0, 0, imagesx( $image ), imagesy( $image ) );
1341 + $image = $truecolor;
1342 + }
768 1343
769 - wp_send_json_success( $html );
1344 + $avif_image = imageavif( $image, $output_path, $quality );
1345 + imagedestroy( $image );
1346 + if ( ! $avif_image ) {
1347 + quickwebp_debug_log( 'create_avif_image:gd_save_failed', array( 'output_path' => $output_path ) );
1348 + return false;
1349 + }
770 1350
771 - } else {
772 - wp_send_json_error( __( 'Not optimized.', QUICKWEBP_TEXT_DOMAIN ) );
773 - }
1351 + quickwebp_debug_log( 'create_avif_image:gd_success', array(
1352 + 'output_path' => $output_path,
1353 + 'file_size' => file_exists( $output_path ) ? filesize( $output_path ) : 0,
1354 + ) );
774 1355
1356 + return $avif_image;
775 1357 }
776 1358
777 1359 /**
778 - * Remove the related files of an optimized attachment
1360 + * Create the webp image
779 1361 */
780 - public function remove_related_files( $id ) {
1362 + private function create_webp_image( $file_path, $output_path, $quality ) {
1363 + $image = false;
1364 + $mime_type = wp_get_image_mime( $file_path );
781 1365
782 - $paths = $this->get_generated_webp_paths( $id );
1366 + quickwebp_debug_log( 'create_webp_image:start', array(
1367 + 'file_path' => $file_path,
1368 + 'output_path' => $output_path,
1369 + 'mime_type' => $mime_type,
1370 + 'quality' => $quality,
1371 + ) );
783 1372
784 - foreach ( $paths as $path ) {
785 - if ( file_exists( $path ) ) {
786 - wp_delete_file( $path );
787 - }
1373 + if ( ! $this->can_load_image_with_gd( $mime_type ) && $this->create_image_with_wp_editor( $file_path, $output_path, $quality, 'image/webp' ) ) {
1374 + quickwebp_debug_log( 'create_webp_image:used_wp_editor', array( 'mime_type' => $mime_type ) );
1375 + return true;
788 1376 }
789 1377
790 - }
1378 + $image = $this->create_gd_image_resource( $file_path, $mime_type );
791 1379
792 - /**
793 - * Trigger before delete attachment
794 - */
795 - public function before_delete_attachment( $post_id, $post ) {
1380 + if ( ! $image ) {
1381 + quickwebp_debug_log( 'create_webp_image:gd_load_failed', array( 'mime_type' => $mime_type ) );
1382 + return false;
1383 + }
796 1384
797 - $already_optimized = get_post_meta( $post_id, 'quickwebp_already_optimized', true );
798 - if ( $already_optimized === '1' ) {
1385 + // Handle EXIF orientation for proper image rotation
1386 + $exif_data = @exif_read_data( $file_path );
1387 + if ( ! empty( $exif_data['Orientation'] ) ) {
1388 + $orientation = (int) $exif_data['Orientation'];
1389 + //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
1390 + $orientation = apply_filters( 'wp_image_maybe_exif_rotate', $orientation, $file_path );
1391 + if ( $orientation && 1 !== $orientation ) {
1392 + switch ( $orientation ) {
1393 + case 2:
1394 + // Flip horizontally.
1395 + imageflip( $image, IMG_FLIP_HORIZONTAL );
1396 + break;
1397 + case 3:
1398 + // Rotate 180 degrees.
1399 + $image = imagerotate( $image, 180, 0 );
1400 + break;
1401 + case 4:
1402 + // Flip vertically.
1403 + imageflip( $image, IMG_FLIP_VERTICAL );
1404 + break;
1405 + case 5:
1406 + // Rotate 90 degrees counter-clockwise and flip vertically.
1407 + $image = imagerotate( $image, -90, 0 );
1408 + imageflip( $image, IMG_FLIP_VERTICAL );
1409 + break;
1410 + case 6:
1411 + // Rotate 90 degrees clockwise (270 counter-clockwise).
1412 + $image = imagerotate( $image, -90, 0 );
1413 + break;
1414 + case 7:
1415 + // Rotate 90 degrees counter-clockwise and flip horizontally.
1416 + $image = imagerotate( $image, 90, 0 );
1417 + imageflip( $image, IMG_FLIP_HORIZONTAL );
1418 + break;
1419 + case 8:
1420 + // Rotate 90 degrees counter-clockwise.
1421 + $image = imagerotate( $image, 90, 0 );
1422 + break;
1423 + }
1424 + }
1425 + }
1426 +
1427 + if ( ! imageistruecolor( $image ) ) {
1428 + $truecolor = imagecreatetruecolor( imagesx( $image ), imagesy( $image ) );
1429 +
1430 + if ( $mime_type === 'image/png' ) {
1431 + imagealphablending( $truecolor, false );
1432 + imagesavealpha( $truecolor, true );
1433 + $transparent = imagecolorallocatealpha( $truecolor, 0, 0, 0, 127 );
1434 + imagefilledrectangle( $truecolor, 0, 0, imagesx( $image ), imagesy( $image ), $transparent );
1435 + }
1436 +
1437 + imagecopy( $truecolor, $image, 0, 0, 0, 0, imagesx( $image ), imagesy( $image ) );
1438 + $image = $truecolor;
1439 + }
799 1440
800 - $this->remove_related_files( $post_id );
1441 + $webp_image = imagewebp( $image, $output_path, $quality );
1442 + imagedestroy( $image );
1443 + if ( ! $webp_image ) {
1444 + quickwebp_debug_log( 'create_webp_image:gd_save_failed', array( 'output_path' => $output_path ) );
1445 + return false;
801 1446 }
802 1447
1448 + quickwebp_debug_log( 'create_webp_image:gd_success', array(
1449 + 'output_path' => $output_path,
1450 + 'file_size' => file_exists( $output_path ) ? filesize( $output_path ) : 0,
1451 + ) );
1452 +
1453 + return $webp_image;
803 1454 }
804 -
805 -}
1455 +}