PluginProbe
Squeeze – Image Optimization & Compression, WEBP Conversion / 1.4.4
Squeeze – Image Optimization & Compression, WEBP Conversion v1.4.4
1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 1.7.10 trunk 1.0 1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5 1.5.1 1.5.2 1.6 All 42 releases
squeeze / src / handlers.php

handlers.php in Squeeze – Image Optimization & Compression, WEBP Conversion 1.4.4, at src/handlers.php

242 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly.
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 define('SQUEEZE_ALLOWED_IMAGE_FORMATS', 'jpg,jpeg,png,webp,avif');
8
9
10 add_action('wp_ajax_squeeze_update_attachment', 'squeeze_update_attachment');
11 /**
12 * Update attachment with compressed image
13 * https://gist.github.com/cyberwani/ad5452b040001878d692c3165836ebff
14 */
15 function squeeze_update_attachment() {
16 check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
17 if (!current_user_can('upload_files')) {
18 wp_send_json_error(__('You do not have permission to upload files', 'squeeze'));
19 }
20 if (isset($_POST["base64"]) && !empty($_POST["base64"])) {
21
22 $base64 = sanitize_text_field($_POST["base64"]);
23 $file_format = sanitize_text_field($_POST["format"]);
24 $filename = sanitize_file_name($_POST["filename"]);
25 $extension = pathinfo($filename, PATHINFO_EXTENSION);
26
27 if (!in_array($extension, explode(',', SQUEEZE_ALLOWED_IMAGE_FORMATS))) {
28 wp_send_json_error(__('Invalid image format', 'squeeze'));
29 }
30
31 $attach_id = (int) $_POST["attachmentID"];
32 $url = sanitize_url($_POST["url"]);
33 $process = sanitize_text_field($_POST["process"]); // process: all, uncompressed, path
34
35 $options = get_option( 'squeeze_options' );
36 $is_backup_original = isset($options['backup_original']) ? $options['backup_original'] : squeeze_get_default_value('backup_original');
37
38 // Upload dir.
39 if ($attach_id > 0) {
40 $upload_dir = str_replace($filename, "", wp_get_original_image_path($attach_id)); // get upload dir from attachment path
41 } else {
42 $upload_dir = str_replace($filename, "", $url);
43 $upload_dir = str_replace(home_url(), ABSPATH, $upload_dir);
44 }
45 $upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir) . DIRECTORY_SEPARATOR;
46
47 $img = str_replace('data:image/'.$file_format.';base64,', '', $base64);
48 $img = str_replace(' ', '+', $img);
49 $decoded = base64_decode($img);
50
51 if ($is_backup_original && $process !== 'path') {
52 // backup original
53 $backup_filename = preg_replace("/(\.(?!.*\.))/", '.bak.', $filename); // add .bak. before extension
54
55 if ( !file_exists($upload_path . $backup_filename) ) {
56 $upload_backup_file = copy($upload_path . $filename, $upload_path . $backup_filename);
57
58 if (!$upload_backup_file) {
59 wp_send_json_error(__('Backup original image failed', 'squeeze') . ': '. $upload_path . $backup_filename);
60 }
61 }
62 }
63
64 // Save the image in the uploads directory.
65 //$upload_file = file_put_contents($upload_path . $filename, $decoded);
66
67 global $wp_filesystem;
68 // Initialize the WP filesystem, no more using 'file-put-contents' function
69 if (empty($wp_filesystem)) {
70 require_once (ABSPATH . '/wp-admin/includes/file.php');
71 WP_Filesystem();
72 }
73
74 $upload_file = $wp_filesystem->put_contents($upload_path . $filename, $decoded);
75
76 if (!$upload_file) {
77 wp_send_json_error(__('Upload image failed', 'squeeze'). ': '. $upload_path . $filename);
78 }
79
80 // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
81 if ( ! function_exists( 'wp_crop_image' ) ) {
82 require_once(ABSPATH . 'wp-admin/includes/image.php');
83 }
84
85 // Generate the metadata for the attachment, and update the database record.
86 //$attach_data = wp_generate_attachment_metadata($attach_id, $upload_dir . '/' . $filename);
87
88 // create thumbnails from COMPRESSED original image (not SCALED)
89 if ($process !== 'path') {
90 $attach_data = wp_create_image_subsizes($upload_dir . '/' . $filename, $attach_id);
91
92 update_post_meta($attach_id, "squeeze_is_compressed", true);
93
94 if (!empty($attach_data)) {
95 wp_send_json_success(__('Compressed successfully', 'squeeze'));
96 } else {
97 wp_send_json_error(__('Attachment not updated', 'squeeze'));
98 }
99 } else {
100 wp_send_json_success(__('Compressed successfully', 'squeeze'));
101 }
102
103 }
104 wp_die();
105 }
106
107 add_action('wp_ajax_squeeze_restore_attachment', 'squeeze_restore_attachment');
108 /**
109 * Restore original attachment
110 */
111 function squeeze_restore_attachment() {
112 check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
113 if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) {
114 $attach_id = (int) $_POST["attachmentID"];
115 $can_restore = squeeze_can_restore($attach_id);
116
117 if ($can_restore) {
118 $original_img_path = wp_get_original_image_path($attach_id);
119 $backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path);
120 $upload_restore_file = copy($backup_img_path, $original_img_path);
121
122 if (!$upload_restore_file) {
123 wp_send_json_error(__('Restore original image failed', 'squeeze'));
124 }
125
126 // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
127 if ( ! function_exists( 'wp_crop_image' ) ) {
128 require_once(ABSPATH . 'wp-admin/includes/image.php');
129 }
130
131 // create thumbnails from original image
132 $attach_data = wp_create_image_subsizes($original_img_path, $attach_id);
133
134 $is_update_attachment = delete_post_meta($attach_id, "squeeze_is_compressed");
135
136 if ($is_update_attachment) {
137 wp_delete_file($backup_img_path); // delete backup file
138 wp_send_json_success(__('Restored successfully', 'squeeze'));
139 } else {
140 wp_send_json_error(__('Attachment not updated', 'squeeze'));
141 }
142
143 }
144 } else {
145 wp_send_json_error(__('Attachment not found', 'squeeze'));
146 }
147 wp_die();
148 }
149
150 add_action('wp_ajax_squeeze_get_attachment', 'squeeze_get_attachment');
151 /**
152 * Get attachment data
153 */
154 function squeeze_get_attachment() {
155 check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
156
157 if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) {
158 $attach_id = (int) $_POST["attachmentID"];
159 $attach_data = array(
160 'id' => $attach_id,
161 'url' => wp_get_attachment_url($attach_id),
162 'mime' => get_post_mime_type($attach_id),
163 'name' => get_the_title($attach_id),
164 'filename' => basename( wp_get_original_image_path( $attach_id ) ),
165 );
166
167 wp_send_json_success($attach_data);
168
169 } else {
170 wp_send_json_error(__('Attachment not found', 'squeeze'));
171 }
172
173 wp_die();
174 }
175
176 add_action('wp_ajax_squeeze_get_attachment_by_path', 'squeeze_get_attachment_by_path');
177 /**
178 * Get attachment data by path
179 */
180 function squeeze_get_attachment_by_path() {
181 check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
182
183 if (isset($_POST["path"]) && !empty($_POST["path"])) {
184 $path = sanitize_text_field($_POST["path"]);
185 $images = glob( ABSPATH . $path . '*.{'.SQUEEZE_ALLOWED_IMAGE_FORMATS.'}', GLOB_BRACE);
186 $attach_data = array();
187
188 if (empty($images)) {
189 wp_send_json_error(__('Images not found in the path', 'squeeze') . ' ' . $path);
190 }
191
192 foreach ($images as $image) {
193 $attach_id = attachment_url_to_postid($image);
194 $attach_mime = image_type_to_mime_type(exif_imagetype($image));
195 $attach_url = str_replace(ABSPATH, home_url(), $image);
196 $attach_name = pathinfo($image, PATHINFO_FILENAME);
197 $attach_data[] = array(
198 'id' => $attach_id,
199 'url' => $attach_url,
200 'mime' => $attach_mime,
201 'name' => $attach_name,
202 'filename' => basename( $image ),
203 );
204 }
205
206 wp_send_json_success($attach_data);
207
208 } else {
209 wp_send_json_error(__('Path not found', 'squeeze'));
210 }
211
212 wp_die();
213 }
214
215 /**
216 * Check if attachment can be restored
217 *
218 * @param int $attach_id
219 * @return bool
220 */
221 function squeeze_can_restore($attach_id) {
222 $original_img_path = wp_get_original_image_path((int) $attach_id);
223 $backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path);
224 $can_restore = file_exists($backup_img_path);
225
226 return $can_restore;
227 }
228
229 add_action('delete_attachment', 'squeeze_delete_attachment');
230 /**
231 * Delete backup image when attachment is deleted
232 *
233 * @param int $attach_id
234 */
235 function squeeze_delete_attachment($attach_id) {
236 $original_img_path = wp_get_original_image_path((int) $attach_id);
237 $backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path);
238
239 if (file_exists($backup_img_path)) {
240 wp_delete_file($backup_img_path);
241 }
242 }