PluginProbe
Squeeze – Image Optimization & Compression, WEBP Conversion / 1.2
Squeeze – Image Optimization & Compression, WEBP Conversion v1.2
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.2, at src/handlers.php

155 lines 5.3 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
8 add_action('wp_ajax_squeeze_update_attachment', 'squeeze_update_attachment');
9 /**
10 * Update attachment with compressed image
11 * https://gist.github.com/cyberwani/ad5452b040001878d692c3165836ebff
12 */
13 function squeeze_update_attachment() {
14 check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
15 if (isset($_POST["base64"]) && !empty($_POST["base64"])) {
16
17 $base64 = sanitize_text_field($_POST["base64"]);
18 $file_format = sanitize_text_field($_POST["format"]);
19 $filename = sanitize_file_name($_POST["filename"]);
20 $attach_id = (int) $_POST["attachmentID"];
21 $url = sanitize_url($_POST["url"]);
22
23 $options = get_option( 'squeeze_options' );
24 $is_backup_original = isset($options['backup_original']) ? $options['backup_original'] : squeeze_get_default_value('backup_original');
25
26 // Upload dir.
27 $upload_dir = str_replace($filename, "", get_attached_file($attach_id)); // get upload dir from attachment path
28 $upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir) . DIRECTORY_SEPARATOR;
29
30 $img = str_replace('data:image/'.$file_format.';base64,', '', $base64);
31 $img = str_replace(' ', '+', $img);
32 $decoded = base64_decode($img);
33
34 if ($is_backup_original) {
35 // backup original
36 $backup_filename = preg_replace("/(\.(?!.*\.))/", '.bak.', $filename); // add .bak. before extension
37 $upload_backup_file = copy($upload_path . $filename, $upload_path . $backup_filename);
38
39 if (!$upload_backup_file) {
40 wp_send_json_error(__('Backup original image failed', 'squeeze'));
41 }
42 }
43
44 // Save the image in the uploads directory.
45 $upload_file = file_put_contents($upload_path . $filename, $decoded);
46
47 if (!$upload_file) {
48 wp_send_json_error(__('Upload image failed', 'squeeze'));
49 }
50
51 // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
52 if ( ! function_exists( 'wp_crop_image' ) ) {
53 require_once(ABSPATH . 'wp-admin/includes/image.php');
54 }
55
56 // Generate the metadata for the attachment, and update the database record.
57 //$attach_data = wp_generate_attachment_metadata($attach_id, $upload_dir . '/' . $filename);
58
59 // create thumbnails from COMPRESSED original image
60 $attach_data = wp_create_image_subsizes($upload_dir . '/' . $filename, $attach_id);
61
62 $is_update_attachment = update_post_meta($attach_id, "squeeze_is_compressed", true);
63
64 if ($is_update_attachment) {
65 wp_send_json_success(__('Compressed successfully', 'squeeze'));
66 } else {
67 wp_send_json_error(__('Attachment not updated', 'squeeze'));
68 }
69
70 }
71 wp_die();
72 }
73
74 add_action('wp_ajax_squeeze_restore_attachment', 'squeeze_restore_attachment');
75 /**
76 * Restore original attachment
77 */
78 function squeeze_restore_attachment() {
79 check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
80 if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) {
81 $attach_id = (int) $_POST["attachmentID"];
82 $can_restore = squeeze_can_restore($attach_id);
83
84 if ($can_restore) {
85 $original_img_path = wp_get_original_image_path($attach_id);
86 $backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path);
87 $upload_restore_file = copy($backup_img_path, $original_img_path);
88
89 if (!$upload_restore_file) {
90 wp_send_json_error(__('Restore original image failed', 'squeeze'));
91 }
92
93 // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
94 if ( ! function_exists( 'wp_crop_image' ) ) {
95 require_once(ABSPATH . 'wp-admin/includes/image.php');
96 }
97
98 // create thumbnails from original image
99 $attach_data = wp_create_image_subsizes($original_img_path, $attach_id);
100
101 $is_update_attachment = delete_post_meta($attach_id, "squeeze_is_compressed");
102
103 if ($is_update_attachment) {
104 unlink($backup_img_path); // delete backup file
105 wp_send_json_success(__('Restored successfully', 'squeeze'));
106 } else {
107 wp_send_json_error(__('Attachment not updated', 'squeeze'));
108 }
109
110 }
111 } else {
112 wp_send_json_error(__('Attachment not found', 'squeeze'));
113 }
114 wp_die();
115 }
116
117 add_action('wp_ajax_squeeze_get_attachment', 'squeeze_get_attachment');
118 /**
119 * Get attachment data
120 */
121 function squeeze_get_attachment() {
122 check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
123
124 if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) {
125 $attach_id = (int) $_POST["attachmentID"];
126 $attach_data = array(
127 'id' => $attach_id,
128 'url' => wp_get_attachment_url($attach_id),
129 'mime' => get_post_mime_type($attach_id),
130 'name' => get_the_title($attach_id),
131 'filename' => basename( get_attached_file( $attach_id ) ),
132 );
133
134 wp_send_json_success($attach_data);
135
136 } else {
137 wp_send_json_error(__('Attachment not found', 'squeeze'));
138 }
139
140 wp_die();
141 }
142
143 /**
144 * Check if attachment can be restored
145 *
146 * @param int $attach_id
147 * @return bool
148 */
149 function squeeze_can_restore($attach_id) {
150 $original_img_path = wp_get_original_image_path((int) $attach_id);
151 $backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path);
152 $can_restore = file_exists($backup_img_path);
153
154 return $can_restore;
155 }