PluginProbe ʕ •ᴥ•ʔ
Modern Image Formats / 2.5.0
Modern Image Formats v2.5.0
2.7.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.5.1 2.6.0 2.6.1
webp-uploads / hooks.php
webp-uploads Last commit date
deprecated.php 1 year ago helper.php 1 year ago hooks.php 1 year ago image-edit.php 1 year ago load.php 1 year ago picture-element.php 1 year ago readme.txt 1 year ago rest-api.php 1 year ago settings.php 1 year ago uninstall.php 1 year ago
hooks.php
778 lines
1 <?php
2 /**
3 * Hook callbacks used for Modern Image Formats.
4 *
5 * @package webp-uploads
6 *
7 * @since 1.0.0
8 */
9
10 // @codeCoverageIgnoreStart
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14 // @codeCoverageIgnoreEnd
15
16 /**
17 * Hook called by `wp_generate_attachment_metadata` to create the `sources` property for every image
18 * size, the sources' property would create a new image size with all the mime types specified in
19 * `webp_uploads_get_upload_image_mime_transforms`. If the original image is one of the mimes from
20 * `webp_uploads_get_upload_image_mime_transforms` the image is just added to the `sources` property and not
21 * created again. If the uploaded attachment is not a supported mime by this function, the hook does not alter the
22 * metadata of the attachment. In addition to every single size the `sources` property is added at the
23 * top level of the image metadata to store the references for all the mime types for the `full` size image of the
24 * attachment.
25 *
26 * @since 1.0.0
27 *
28 * @see wp_generate_attachment_metadata()
29 * @see webp_uploads_get_upload_image_mime_transforms()
30 *
31 * @phpstan-param array{
32 * width: int,
33 * height: int,
34 * file: string,
35 * sizes: array<string, array{ file: string, width: int, height: int, 'mime-type': string }>,
36 * image_meta: array<string, mixed>,
37 * filesize: int
38 * } $metadata
39 *
40 * @param array<string, mixed> $metadata An array with the metadata from this attachment.
41 * @param int $attachment_id The ID of the attachment where the hook was dispatched.
42 *
43 * @return array{
44 * width: int,
45 * height: int,
46 * file: string,
47 * sizes: array<string, array{ file: string, width: int, height: int, 'mime-type': string, sources?: array<string, array{ file: string, filesize: int }> }>,
48 * image_meta: array<string, mixed>,
49 * filesize: int,
50 * sources?: array<string, array{
51 * file: string,
52 * filesize: int
53 * }>
54 * } An array with the updated structure for the metadata before is stored in the database.
55 */
56 function webp_uploads_create_sources_property( array $metadata, int $attachment_id ): array {
57 $file = get_attached_file( $attachment_id, true );
58 // File does not exist.
59 if ( false === $file || ! file_exists( $file ) ) {
60 return $metadata;
61 }
62
63 $mime_type = webp_uploads_get_attachment_file_mime_type( $attachment_id, $file );
64 if ( '' === $mime_type ) {
65 return $metadata;
66 }
67
68 $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms();
69
70 // Not a supported mime type to create the sources property.
71 if ( ! isset( $valid_mime_transforms[ $mime_type ] ) ) {
72 return $metadata;
73 }
74
75 // Make sure the top level `sources` key is a valid array.
76 if ( ! isset( $metadata['sources'] ) || ! is_array( $metadata['sources'] ) ) {
77 $metadata['sources'] = array();
78 }
79
80 if ( empty( $metadata['sources'][ $mime_type ] ) ) {
81 $metadata['sources'][ $mime_type ] = array(
82 'file' => wp_basename( $file ),
83 'filesize' => wp_filesize( $file ),
84 );
85 wp_update_attachment_metadata( $attachment_id, $metadata );
86 }
87
88 $original_size_data = array(
89 'width' => isset( $metadata['width'] ) ? (int) $metadata['width'] : 0,
90 'height' => isset( $metadata['height'] ) ? (int) $metadata['height'] : 0,
91 'crop' => false,
92 );
93
94 $original_directory = pathinfo( $file, PATHINFO_DIRNAME );
95 $filename = pathinfo( $file, PATHINFO_FILENAME );
96 $ext = pathinfo( $file, PATHINFO_EXTENSION );
97 $allowed_mimes = array_flip( wp_get_mime_types() );
98
99 // Create the sources for the full sized image.
100 foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) {
101 // If this property exists no need to create the image again.
102 if ( ! empty( $metadata['sources'][ $targeted_mime ] ) ) {
103 continue;
104 }
105
106 // The targeted mime is not allowed in the current installation.
107 if ( empty( $allowed_mimes[ $targeted_mime ] ) ) {
108 continue;
109 }
110
111 $extension = explode( '|', $allowed_mimes[ $targeted_mime ] );
112 $destination = trailingslashit( $original_directory ) . "{$filename}-{$ext}.{$extension[0]}";
113 $image = webp_uploads_generate_additional_image_source( $attachment_id, 'full', $original_size_data, $targeted_mime, $destination );
114
115 if ( is_wp_error( $image ) ) {
116 continue;
117 }
118
119 if ( webp_uploads_should_discard_additional_image_file( $metadata, $image ) ) {
120 wp_delete_file_from_directory( $destination, $original_directory );
121 continue;
122 }
123
124 $metadata['sources'][ $targeted_mime ] = $image;
125 wp_update_attachment_metadata( $attachment_id, $metadata );
126 }
127
128 // If the original MIME type should not be generated/used, override the main image
129 // with the first MIME type image that actually should be generated. In that case,
130 // the original should be backed up.
131 if (
132 ! in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) &&
133 isset( $valid_mime_transforms[ $mime_type ][0] ) &&
134 isset( $allowed_mimes[ $mime_type ] ) &&
135 array_key_exists( 'file', $metadata ) &&
136 is_string( $metadata['file'] )
137 ) {
138 $valid_mime_type = $valid_mime_transforms[ $mime_type ][0];
139
140 // Only do the replacement if the attachment file is still set to the original MIME type one,
141 // and if there is a possible replacement source.
142 $file_data = wp_check_filetype( $metadata['file'], array( $allowed_mimes[ $mime_type ] => $mime_type ) );
143 if ( $file_data['type'] === $mime_type && isset( $metadata['sources'][ $valid_mime_type ] ) ) {
144 $saved_data = array(
145 'path' => trailingslashit( $original_directory ) . $metadata['sources'][ $valid_mime_type ]['file'],
146 'width' => $metadata['width'],
147 'height' => $metadata['height'],
148 );
149
150 $original_image = wp_get_original_image_path( $attachment_id );
151
152 // If WordPress already modified the original itself, keep the original and discard WordPress's generated version.
153 if ( ! empty( $metadata['original_image'] ) ) {
154 $uploadpath = wp_get_upload_dir();
155 $attached_file = get_attached_file( $attachment_id );
156 if ( false !== $attached_file ) {
157 wp_delete_file_from_directory( $attached_file, $uploadpath['basedir'] );
158 }
159 }
160
161 // Replace the attached file with the custom MIME type version.
162 if ( false !== $original_image ) {
163 $metadata = _wp_image_meta_replace_original( $saved_data, $original_image, $metadata, $attachment_id );
164 }
165
166 // Unset sources entry for the original MIME type, then save (to avoid inconsistent data
167 // in case of an error after this logic).
168 unset( $metadata['sources'][ $mime_type ] );
169 wp_update_attachment_metadata( $attachment_id, $metadata );
170 }
171 }
172
173 // Make sure we have some sizes to work with, otherwise avoid any work.
174 if ( empty( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ) {
175 return $metadata;
176 }
177
178 $sizes_with_mime_type_support = webp_uploads_get_image_sizes_additional_mime_type_support();
179
180 foreach ( $metadata['sizes'] as $size_name => $properties ) {
181 // Do nothing if this image size is not an array or is not allowed to have additional mime types.
182 if ( ! is_array( $properties ) || empty( $sizes_with_mime_type_support[ $size_name ] ) ) {
183 continue;
184 }
185
186 // Try to find the mime type of the image size.
187 $current_mime = '';
188 if ( isset( $properties['mime-type'] ) ) {
189 $current_mime = $properties['mime-type'];
190 } elseif ( isset( $properties['file'] ) ) {
191 $current_mime = wp_check_filetype( $properties['file'] )['type'];
192 }
193
194 // The mime type can't be determined.
195 if ( empty( $current_mime ) ) {
196 continue;
197 }
198
199 // Ensure a `sources` property exists on the existing size.
200 if ( empty( $properties['sources'] ) || ! is_array( $properties['sources'] ) ) {
201 $properties['sources'] = array();
202 }
203
204 if ( empty( $properties['sources'][ $current_mime ] ) && isset( $properties['file'] ) ) {
205 $properties['sources'][ $current_mime ] = array(
206 'file' => $properties['file'],
207 'filesize' => 0,
208 );
209 // Set the filesize from the current mime image.
210 $file_location = path_join( $original_directory, $properties['file'] );
211 if ( file_exists( $file_location ) ) {
212 $properties['sources'][ $current_mime ]['filesize'] = wp_filesize( $file_location );
213 }
214 $metadata['sizes'][ $size_name ] = $properties;
215 wp_update_attachment_metadata( $attachment_id, $metadata );
216 }
217
218 foreach ( $valid_mime_transforms[ $mime_type ] as $mime ) {
219 // If this property exists no need to create the image again.
220 if ( ! empty( $properties['sources'][ $mime ] ) ) {
221 continue;
222 }
223
224 $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime );
225 if ( is_wp_error( $source ) ) {
226 continue;
227 }
228
229 if ( webp_uploads_should_discard_additional_image_file( $properties, $source ) ) {
230 $destination = path_join( $original_directory, $source['file'] );
231 wp_delete_file_from_directory( $destination, $original_directory );
232 continue;
233 }
234
235 $properties['sources'][ $mime ] = $source;
236 $metadata['sizes'][ $size_name ] = $properties;
237 wp_update_attachment_metadata( $attachment_id, $metadata );
238 }
239
240 $metadata['sizes'][ $size_name ] = $properties;
241 }
242
243 return $metadata;
244 }
245 add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 2 );
246
247 /**
248 * Filter on `wp_get_missing_image_subsizes` acting as an action for the logic of the plugin
249 * to determine if additional mime types still need to be created.
250 *
251 * This function only exists to work around a missing filter in WordPress core, to call the above
252 * `webp_uploads_create_sources_property()` function correctly.
253 *
254 * @since 1.0.0
255 *
256 * @see wp_get_missing_image_subsizes()
257 *
258 * @phpstan-param array{
259 * width: int,
260 * height: int,
261 * file: string,
262 * sizes: array<string, array{file: string, width: int, height: int, mime-type: string}>,
263 * image_meta: array<string, mixed>,
264 * filesize: int
265 * } $image_meta
266 *
267 * @param array|mixed $missing_sizes Associative array of arrays of image sub-sizes.
268 * @param array<string, mixed> $image_meta The metadata from the image.
269 * @param int $attachment_id The ID of the attachment.
270 * @return array<string, array{ width: int, height: int, crop: bool }> Associative array of arrays of image sub-sizes.
271 */
272 function webp_uploads_wp_get_missing_image_subsizes( $missing_sizes, array $image_meta, int $attachment_id ): array {
273 if ( ! is_array( $missing_sizes ) ) {
274 $missing_sizes = array();
275 }
276
277 // Only setup the trace array if we no longer have more sizes.
278 if ( ! empty( $missing_sizes ) ) {
279 return $missing_sizes;
280 }
281
282 /**
283 * The usage of `debug_backtrace` in this particular case is mainly to ensure the call to
284 * `wp_get_missing_image_subsizes()` originated from `wp_update_image_subsizes()`, since only then the
285 * additional image sizes should be generated. `wp_get_missing_image_subsizes()` could also be called
286 * from other places in which case the custom logic should not trigger. In an ideal world an action
287 * would exist in `wp_update_image_subsizes` that runs any time, but the current
288 * `wp_generate_attachment_metadata` filter is skipped when all core sub-sizes have been generated.
289 * An eventual core implementation will not require this workaround. The limit of 10 is used to allow
290 * for some flexibility. While by default the function would be on index 5, other custom code may
291 * cause the index to be slightly higher.
292 *
293 * @see wp_update_image_subsizes()
294 * @see wp_get_missing_image_subsizes()
295 */
296 // PHPCS ignore reason: Only the way to generate missing image subsize if all core sub-sizes have been generated.
297 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
298 $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 10 );
299
300 foreach ( $trace as $element ) {
301 if ( 'wp_update_image_subsizes' === $element['function'] ) {
302 webp_uploads_create_sources_property( $image_meta, $attachment_id );
303 break;
304 }
305 }
306
307 return array();
308 }
309 add_filter( 'wp_get_missing_image_subsizes', 'webp_uploads_wp_get_missing_image_subsizes', 10, 3 );
310
311 /**
312 * Filter the image editor default output format mapping to select the most appropriate
313 * output format depending on desired output formats and supported mime types by the image
314 * editor.
315 *
316 * @since 1.0.0
317 *
318 * @param array<string, string>|mixed $output_format An array of mime type mappings. Maps a source mime type to a new destination mime type. Default empty array.
319 * @param string|null $filename Path to the image.
320 * @param string|null $mime_type The source image mime type.
321 * @return array<string, string> The new output format mapping.
322 */
323 function webp_uploads_filter_image_editor_output_format( $output_format, ?string $filename, ?string $mime_type ): array {
324 if ( ! is_array( $output_format ) ) {
325 $output_format = array();
326 }
327
328 // Use the original mime type if this type is allowed.
329 $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms();
330 if (
331 ! isset( $valid_mime_transforms[ $mime_type ] ) ||
332 in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true )
333 ) {
334 return $output_format;
335 }
336
337 // Find the first supported mime type by the image editor to use it as the default one.
338 foreach ( $valid_mime_transforms[ $mime_type ] as $target_mime ) {
339 if ( wp_image_editor_supports( array( 'mime_type' => $target_mime ) ) ) {
340 $output_format[ $mime_type ] = $target_mime;
341 break;
342 }
343 }
344
345 return $output_format;
346 }
347 add_filter( 'image_editor_output_format', 'webp_uploads_filter_image_editor_output_format', 10, 3 );
348
349 /**
350 * Hook fired when an attachment is deleted, this hook is in charge of removing any
351 * additional mime types created by this plugin besides the original image. Any source
352 * with the same as the main image would not be removed by this hook due this file would
353 * be removed by WordPress when the attachment is deleted, usually this happens after this
354 * hook is executed.
355 *
356 * @since 1.0.0
357 *
358 * @see wp_delete_attachment()
359 *
360 * @param int $attachment_id The ID of the attachment the sources are going to be deleted.
361 */
362 function webp_uploads_remove_sources_files( int $attachment_id ): void {
363 $file = get_attached_file( $attachment_id );
364
365 if ( empty( $file ) ) {
366 return;
367 }
368
369 $metadata = wp_get_attachment_metadata( $attachment_id );
370 // Make sure $sizes is always defined to allow the removal of original images after the first foreach loop.
371 $sizes = ! isset( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ? array() : $metadata['sizes'];
372
373 $upload_path = wp_get_upload_dir();
374 if ( empty( $upload_path['basedir'] ) ) {
375 return;
376 }
377
378 $intermediate_dir = path_join( $upload_path['basedir'], dirname( $file ) );
379 $basename = wp_basename( $file );
380
381 foreach ( $sizes as $size ) {
382 if ( ! isset( $size['sources'] ) || ! is_array( $size['sources'] ) ) {
383 continue;
384 }
385
386 $original_size_mime = empty( $size['mime-type'] ) ? '' : $size['mime-type'];
387
388 foreach ( $size['sources'] as $mime => $properties ) {
389 /**
390 * When we face the same mime type as the original image, we ignore this file as this file
391 * would be removed when the size is removed by WordPress itself. The meta information as well
392 * would be deleted as soon as the image is removed.
393 *
394 * @see wp_delete_attachment
395 */
396 if ( $original_size_mime === $mime ) {
397 continue;
398 }
399
400 if ( ! is_array( $properties ) || empty( $properties['file'] ) ) {
401 continue;
402 }
403
404 $intermediate_file = str_replace( $basename, $properties['file'], $file );
405 if ( '' === $intermediate_file ) {
406 continue;
407 }
408
409 $intermediate_file = path_join( $upload_path['basedir'], $intermediate_file );
410 if ( ! file_exists( $intermediate_file ) ) {
411 continue;
412 }
413
414 wp_delete_file_from_directory( $intermediate_file, $intermediate_dir );
415 }
416 }
417
418 if ( ! isset( $metadata['sources'] ) || ! is_array( $metadata['sources'] ) ) {
419 return;
420 }
421
422 $original_mime_from_post = get_post_mime_type( $attachment_id );
423 $original_mime_from_file = wp_check_filetype( $file )['type'];
424
425 // Delete full sizes mime types.
426 foreach ( $metadata['sources'] as $mime => $properties ) {
427 // Don't remove the image with the same mime type as the original image as this would be removed by WordPress.
428 if ( $mime === $original_mime_from_post || $mime === $original_mime_from_file ) {
429 continue;
430 }
431
432 if ( ! is_array( $properties ) || empty( $properties['file'] ) ) {
433 continue;
434 }
435
436 $full_size = str_replace( $basename, $properties['file'], $file );
437 if ( '' === $full_size ) {
438 continue;
439 }
440
441 $full_size_file = path_join( $upload_path['basedir'], $full_size );
442 if ( ! file_exists( $full_size_file ) ) {
443 continue;
444 }
445 wp_delete_file_from_directory( $full_size_file, $intermediate_dir );
446 }
447
448 $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
449 $backup_sizes = is_array( $backup_sizes ) ? $backup_sizes : array();
450
451 foreach ( $backup_sizes as $backup_size ) {
452 if ( ! isset( $backup_size['sources'] ) || ! is_array( $backup_size['sources'] ) ) {
453 continue;
454 }
455
456 $original_backup_size_mime = empty( $backup_size['mime-type'] ) ? '' : $backup_size['mime-type'];
457
458 foreach ( $backup_size['sources'] as $backup_mime => $backup_properties ) {
459 /**
460 * When we face the same mime type as the original image, we ignore this file as this file
461 * would be removed when the size is removed by WordPress itself. The meta information as well
462 * would be deleted as soon as the image is removed.
463 *
464 * @see wp_delete_attachment
465 */
466 if ( $original_backup_size_mime === $backup_mime ) {
467 continue;
468 }
469
470 if ( ! is_array( $backup_properties ) || empty( $backup_properties['file'] ) ) {
471 continue;
472 }
473
474 $backup_intermediate_file = str_replace( $basename, $backup_properties['file'], $file );
475 if ( empty( $backup_intermediate_file ) ) {
476 continue;
477 }
478
479 $backup_intermediate_file = path_join( $upload_path['basedir'], $backup_intermediate_file );
480 if ( ! file_exists( $backup_intermediate_file ) ) {
481 continue;
482 }
483
484 wp_delete_file_from_directory( $backup_intermediate_file, $intermediate_dir );
485 }
486 }
487
488 $backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true );
489 $backup_sources = is_array( $backup_sources ) ? $backup_sources : array();
490
491 // Delete full sizes backup mime types.
492 foreach ( $backup_sources as $backup_mimes ) {
493
494 foreach ( $backup_mimes as $backup_mime_properties ) {
495 if ( ! is_array( $backup_mime_properties ) || empty( $backup_mime_properties['file'] ) ) {
496 continue;
497 }
498
499 $full_size = str_replace( $basename, $backup_mime_properties['file'], $file );
500 if ( empty( $full_size ) ) {
501 continue;
502 }
503
504 $full_size_file = path_join( $upload_path['basedir'], $full_size );
505 if ( ! file_exists( $full_size_file ) ) {
506 continue;
507 }
508 wp_delete_file_from_directory( $full_size_file, $intermediate_dir );
509 }
510 }
511 }
512 add_action( 'delete_attachment', 'webp_uploads_remove_sources_files', 10, 1 );
513
514 /**
515 * Filters `wp_content_img_tag` to update images so that they use the preferred MIME type where possible.
516 *
517 * @since 2.5.0
518 *
519 * @param string $filtered_image Full img tag with attributes that will replace the source img tag.
520 * @param string $context Additional context, like the current filter name or the function name from where this was called.
521 * @param int $attachment_id The image attachment ID. May be 0 in case the image is not an attachment.
522 * @return string The updated IMG tag with references to the new MIME type if available.
523 */
524 function webp_uploads_filter_image_tag( string $filtered_image, string $context, int $attachment_id ): string {
525 // Bail early if request is not for the frontend.
526 if ( ! webp_uploads_in_frontend_body() ) {
527 return $filtered_image;
528 }
529
530 $filtered_image = str_replace( $filtered_image, webp_uploads_img_tag_update_mime_type( $filtered_image, 'the_content', $attachment_id ), $filtered_image );
531
532 return $filtered_image;
533 }
534
535 /**
536 * Finds all the urls with *.jpg and *.jpeg extension and updates with *.webp version for the provided image
537 * for the specified image sizes, the *.webp references are stored inside of each size.
538 *
539 * @since 1.0.0
540 *
541 * @param string $original_image An <img> tag where the urls would be updated.
542 * @param string $context The context where this is function is being used.
543 * @param int $attachment_id The ID of the attachment being modified.
544 * @return string The updated img tag.
545 */
546 function webp_uploads_img_tag_update_mime_type( string $original_image, string $context, int $attachment_id ): string {
547 $image = $original_image;
548 $metadata = wp_get_attachment_metadata( $attachment_id );
549
550 if ( empty( $metadata['file'] ) ) {
551 return $image;
552 }
553
554 $original_mime = get_post_mime_type( $attachment_id );
555 $target_mimes = webp_uploads_get_content_image_mimes( $attachment_id, $context );
556
557 foreach ( $target_mimes as $target_mime ) {
558 if ( $target_mime === $original_mime ) {
559 continue;
560 }
561
562 if ( ! isset( $metadata['sources'][ $target_mime ]['file'] ) ) {
563 continue;
564 }
565
566 /**
567 * Filter to replace additional image source file, by locating the original
568 * mime types of the file and return correct file path in the end.
569 *
570 * Altering the $image tag through this filter effectively short-circuits the default replacement logic using the preferred MIME type.
571 *
572 * @since 1.1.0
573 *
574 * @param string $image An <img> tag where the urls would be updated.
575 * @param int $attachment_id The ID of the attachment being modified.
576 * @param string $size The size name that would be used to create this image, out of the registered subsizes.
577 * @param string $target_mime The target mime in which the image should be created.
578 * @param string $context The context where this is function is being used.
579 */
580 $filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, 'full', $target_mime, $context );
581
582 // If filtered image is same as the image, run our own replacement logic, otherwise rely on the filtered image.
583 if ( $filtered_image === $image ) {
584 $basename = wp_basename( $metadata['file'] );
585 $image = str_replace(
586 $basename,
587 $metadata['sources'][ $target_mime ]['file'],
588 $image
589 );
590 } else {
591 $image = $filtered_image;
592 }
593 }
594
595 if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) {
596 // Replace sub sizes for the image if present.
597 foreach ( $metadata['sizes'] as $size => $size_data ) {
598
599 if ( empty( $size_data['file'] ) ) {
600 continue;
601 }
602
603 foreach ( $target_mimes as $target_mime ) {
604 if ( $target_mime === $original_mime ) {
605 continue;
606 }
607
608 if ( ! isset( $size_data['sources'][ $target_mime ]['file'] ) ) {
609 continue;
610 }
611
612 if ( $size_data['file'] === $size_data['sources'][ $target_mime ]['file'] ) {
613 continue;
614 }
615
616 /** This filter is documented in plugins/webp-uploads/load.php */
617 $filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, $size, $target_mime, $context );
618
619 // If filtered image is same as the image, run our own replacement logic, otherwise rely on the filtered image.
620 if ( $filtered_image === $image ) {
621 $image = str_replace(
622 $size_data['file'],
623 $size_data['sources'][ $target_mime ]['file'],
624 $image
625 );
626 } else {
627 $image = $filtered_image;
628 }
629 }
630 }
631 }
632
633 return $image;
634 }
635
636 /**
637 * Updates the references of the featured image to the a new image format if available, in the same way it
638 * occurs in the_content of a post.
639 *
640 * @since 1.0.0
641 *
642 * @param string $html The current HTML markup of the featured image.
643 * @param int $post_id The current post ID where the featured image is requested.
644 * @param int $attachment_id The ID of the attachment image.
645 * @return string The updated HTML markup.
646 */
647 function webp_uploads_update_featured_image( string $html, int $post_id, int $attachment_id ): string {
648 return webp_uploads_img_tag_update_mime_type( $html, 'post_thumbnail_html', $attachment_id );
649 }
650 add_filter( 'post_thumbnail_html', 'webp_uploads_update_featured_image', 10, 3 );
651
652 /**
653 * Returns an array of image size names that have secondary mime type output enabled. Core sizes and
654 * core theme sizes are enabled by default.
655 *
656 * Developers can control the generation of additional mime images for all sizes using the
657 * webp_uploads_image_sizes_with_additional_mime_type_support filter.
658 *
659 * @since 1.0.0
660 *
661 * @return array<string, bool> An array of image sizes that can have additional mime types.
662 */
663 function webp_uploads_get_image_sizes_additional_mime_type_support(): array {
664 $additional_sizes = wp_get_additional_image_sizes();
665 $allowed_sizes = array(
666 'thumbnail' => true,
667 'medium' => true,
668 'medium_large' => true,
669 'large' => true,
670 'post-thumbnail' => true,
671 );
672
673 foreach ( $additional_sizes as $size => $size_details ) {
674 $allowed_sizes[ $size ] = ! empty( $size_details['provide_additional_mime_types'] );
675 }
676
677 /**
678 * Filters whether additional mime types are allowed for image sizes.
679 *
680 * @since 1.0.0
681 *
682 * @param array<string, bool> $allowed_sizes A map of image size names and whether they are allowed to have additional mime types.
683 */
684 $allowed_sizes = (array) apply_filters( 'webp_uploads_image_sizes_with_additional_mime_type_support', $allowed_sizes );
685
686 return $allowed_sizes;
687 }
688
689 /**
690 * Updates the quality of WebP image sizes generated by WordPress to 82.
691 *
692 * @since 1.0.0
693 *
694 * @param int $quality Quality level between 1 (low) and 100 (high).
695 * @param string $mime_type Image mime type.
696 * @return int The updated quality for mime types.
697 */
698 function webp_uploads_modify_webp_quality( int $quality, string $mime_type ): int {
699 // For WebP images, always return 82 (other MIME types were already using 82 by default anyway).
700 if ( 'image/webp' === $mime_type ) {
701 return 82;
702 }
703
704 // Return default quality for non-WebP images in WP.
705 return $quality;
706 }
707 add_filter( 'wp_editor_set_quality', 'webp_uploads_modify_webp_quality', 10, 2 );
708
709 /**
710 * Displays the HTML generator tag for the Modern Image Formats plugin.
711 *
712 * See {@see 'wp_head'}.
713 *
714 * @since 1.0.0
715 */
716 function webp_uploads_render_generator(): void {
717 // Use the plugin slug as it is immutable.
718 echo '<meta name="generator" content="webp-uploads ' . esc_attr( WEBP_UPLOADS_VERSION ) . '">' . "\n";
719 }
720 add_action( 'wp_head', 'webp_uploads_render_generator' );
721
722 /**
723 * Initializes custom functionality for handling image uploads and content filters.
724 *
725 * @since 2.1.0
726 */
727 function webp_uploads_init(): void {
728 add_filter( 'wp_content_img_tag', webp_uploads_is_picture_element_enabled() ? 'webp_uploads_wrap_image_in_picture' : 'webp_uploads_filter_image_tag', 10, 3 );
729 }
730 add_action( 'init', 'webp_uploads_init' );
731
732 /**
733 * Automatically opt into extra image sizes when generating fallback images.
734 *
735 * @since 2.4.0
736 *
737 * @global array $_wp_additional_image_sizes Associative array of additional image sizes.
738 */
739 function webp_uploads_opt_in_extra_image_sizes(): void {
740 if ( ! webp_uploads_is_fallback_enabled() ) {
741 return;
742 }
743
744 global $_wp_additional_image_sizes;
745
746 // Modify global to mimic the "hypothetical" WP core API behavior via an additional `add_image_size()` parameter.
747
748 if ( isset( $_wp_additional_image_sizes['1536x1536'] ) && ! isset( $_wp_additional_image_sizes['1536x1536']['provide_additional_mime_types'] ) ) {
749 $_wp_additional_image_sizes['1536x1536']['provide_additional_mime_types'] = true; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
750 }
751
752 if ( isset( $_wp_additional_image_sizes['2048x2048'] ) && ! isset( $_wp_additional_image_sizes['2048x2048']['provide_additional_mime_types'] ) ) {
753 $_wp_additional_image_sizes['2048x2048']['provide_additional_mime_types'] = true; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
754 }
755 }
756 add_action( 'plugins_loaded', 'webp_uploads_opt_in_extra_image_sizes' );
757
758 /**
759 * Enables additional MIME type support for all image sizes based on the generate all fallback sizes settings.
760 *
761 * @since 2.4.0
762 *
763 * @param array<string, bool> $allowed_sizes A map of image size names and whether they are allowed to have additional MIME types.
764 * @return array<string, bool> Modified map of image sizes with additional MIME type support.
765 */
766 function webp_uploads_enable_additional_mime_type_support_for_all_sizes( array $allowed_sizes ): array {
767 if ( ! webp_uploads_should_generate_all_fallback_sizes() ) {
768 return $allowed_sizes;
769 }
770
771 foreach ( array_keys( $allowed_sizes ) as $size ) {
772 $allowed_sizes[ $size ] = true;
773 }
774
775 return $allowed_sizes;
776 }
777 add_filter( 'webp_uploads_image_sizes_with_additional_mime_type_support', 'webp_uploads_enable_additional_mime_type_support_for_all_sizes' );
778