| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 3 |
|
| 4 |
add_action( 'delete_attachment', 'imagify_trigger_delete_attachment_hook' ); |
| 5 |
/** |
| 6 |
* Trigger a common Imagify hook when an attachment is deleted. |
| 7 |
* |
| 8 |
* @since 1.9 |
| 9 |
* @author Grégory Viguier |
| 10 |
* |
| 11 |
* @param int $post_id Attachment ID. |
| 12 |
*/ |
| 13 |
function imagify_trigger_delete_attachment_hook( $post_id ) { |
| 14 |
$process = imagify_get_optimization_process( $post_id, 'wp' ); |
| 15 |
|
| 16 |
if ( ! $process->is_valid() ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
imagify_trigger_delete_media_hook( $process ); |
| 21 |
} |
| 22 |
|
| 23 |
add_action( 'imagify_delete_media', 'imagify_cleanup_after_media_deletion' ); |
| 24 |
/** |
| 25 |
* Delete the backup file and the webp files when an attachement is deleted. |
| 26 |
* |
| 27 |
* @since 1.9 |
| 28 |
* @author Grégory Viguier |
| 29 |
* |
| 30 |
* @param ProcessInterface $process An optimization process. |
| 31 |
*/ |
| 32 |
function imagify_cleanup_after_media_deletion( $process ) { |
| 33 |
if ( 'wp' !== $process->get_media()->get_context() ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* The optimization data will be automatically deleted by WP (post metas). |
| 39 |
* Delete the webp versions and the backup file. |
| 40 |
*/ |
| 41 |
$process->delete_webp_files(); |
| 42 |
$process->delete_backup(); |
| 43 |
} |
| 44 |
|
| 45 |
add_filter( 'ext2type', 'imagify_add_webp_type' ); |
| 46 |
/** |
| 47 |
* Add the webp extension to wp_get_ext_types(). |
| 48 |
* |
| 49 |
* @since 1.9 |
| 50 |
* @author Grégory Viguier |
| 51 |
* |
| 52 |
* @param array $ext2type Multi-dimensional array with extensions for a default set of file types. |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
function imagify_add_webp_type( $ext2type ) { |
| 56 |
if ( ! in_array( 'webp', $ext2type['image'], true ) ) { |
| 57 |
$ext2type['image'][] = 'webp'; |
| 58 |
} |
| 59 |
return $ext2type; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Set WP’s "big images threshold" to Imagify’s resizing value. |
| 64 |
* |
| 65 |
* @since 1.9.8 |
| 66 |
* @since WP 5.3 |
| 67 |
* @author Grégory Viguier |
| 68 |
*/ |
| 69 |
add_filter( 'big_image_size_threshold', [ imagify_get_context( 'wp' ), 'get_resizing_threshold' ], IMAGIFY_INT_MAX ); |
| 70 |
|