PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.3.6.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.3.6.1
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / common / attachments.php

attachments.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.3.6.1, at inc/common/attachments.php

74 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
3
4 /**
5 * Auto-optimize when a new attachment is generated
6 *
7 * @since 1.0
8 */
9 add_filter( 'wp_generate_attachment_metadata', '_imagify_optimize_attachment', PHP_INT_MAX, 2 );
10 function _imagify_optimize_attachment( $metadata, $attachment_id ) {
11 $api_key = get_imagify_option( 'api_key', false );
12
13 if ( ! empty( $api_key ) && get_imagify_option( 'auto_optimize', false ) ) {
14 $attachment = new Imagify_Attachment( $attachment_id );
15
16 // Optimize it!!!!!
17 $attachment->optimize( null, $metadata );
18 }
19
20 return $metadata;
21 }
22
23 /**
24 * Delete the backup file when an attachement is deleted.
25 *
26 * @since 1.0
27 */
28 add_action( 'delete_attachment', '_imagify_delete_backup_file' );
29 function _imagify_delete_backup_file( $post_id ) {
30 $attachment = new Imagify_Attachment( $post_id );
31 $attachment->delete_backup();
32 }
33
34 /**
35 * Optimize a resized attachment
36 *
37 * @since 1.3.6
38 */
39 add_action( 'shutdown', '_imagify_optimize_save_image_editor_file' );
40 function _imagify_optimize_save_image_editor_file() {
41 if ( isset( $_POST['action'], $_POST['do'], $_POST['postid'] )
42 && 'image-editor' === $_POST['action']
43 && check_ajax_referer( 'image_editor-' . $_POST['postid'] )
44 && get_post_meta( $_POST['postid'], '_imagify_data', true )
45 && 'open' !== $_POST['do']
46 ) {
47 $attachment_id = $_POST['postid'];
48 $optimization_level = get_post_meta( $attachment_id, '_imagify_optimization_level', true );
49 $attachment = new Imagify_Attachment( $attachment_id );
50 $metadata = wp_get_attachment_metadata( $attachment_id );
51
52 // Remove old optimization data
53 delete_post_meta( $attachment_id, '_imagify_data' );
54 delete_post_meta( $attachment_id, '_imagify_status' );
55 delete_post_meta( $attachment_id, '_imagify_optimization_level' );
56
57 if ( 'restore' === $_POST['do'] ) {
58 // Restore the backup file
59 $attachment->restore();
60
61 // Get old metadata to regenerate all thumbnails
62 $metadata = array( 'sizes' => array() );
63 $backup_sizes = (array) get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
64
65 foreach ( $backup_sizes as $size_key => $size_data ) {
66 $size_key = str_replace( '-origin', '' , $size_key );
67 $metadata['sizes'][ $size_key ] = $size_data;
68 }
69 }
70
71 // Optimize it!!!!!
72 $attachment->optimize( $optimization_level, $metadata );
73 }
74 }