| 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 |
$sizes = $_POST["base64Sizes"]; |
| 24 |
$file_format = sanitize_text_field($_POST["format"]); |
| 25 |
$filename = sanitize_file_name($_POST["filename"]); |
| 26 |
$extension = pathinfo($filename, PATHINFO_EXTENSION); |
| 27 |
|
| 28 |
if (!in_array($extension, explode(',', SQUEEZE_ALLOWED_IMAGE_FORMATS))) { |
| 29 |
wp_send_json_error('❌ '.__('Invalid image format', 'squeeze')); |
| 30 |
} |
| 31 |
|
| 32 |
$attach_id = (int) $_POST["attachmentID"]; |
| 33 |
$meta_data = wp_get_attachment_metadata($attach_id); |
| 34 |
$url = sanitize_url($_POST["url"]); |
| 35 |
$process = sanitize_text_field($_POST["process"]); // process: all, uncompressed, path |
| 36 |
|
| 37 |
$options = get_option( 'squeeze_options' ); |
| 38 |
$is_backup_original = isset($options['backup_original']) ? $options['backup_original'] : squeeze_get_default_value('backup_original'); |
| 39 |
|
| 40 |
// Upload dir. |
| 41 |
if ($attach_id > 0) { |
| 42 |
$upload_dir = str_replace($filename, "", wp_get_original_image_path($attach_id)); // get upload dir from attachment path |
| 43 |
} else { |
| 44 |
$upload_dir = str_replace($filename, "", $url); |
| 45 |
$upload_dir = str_replace(home_url(), ABSPATH, $upload_dir); |
| 46 |
} |
| 47 |
$upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir) . DIRECTORY_SEPARATOR; |
| 48 |
|
| 49 |
$img = str_replace('data:image/'.$file_format.';base64,', '', $base64); |
| 50 |
$img = str_replace(' ', '+', $img); |
| 51 |
$decoded = base64_decode($img); |
| 52 |
|
| 53 |
if ($is_backup_original && $process !== 'path') { |
| 54 |
// backup original |
| 55 |
$backup_filename = preg_replace("/(\.(?!.*\.))/", '.bak.', $filename); // add .bak. before extension |
| 56 |
|
| 57 |
if ( !file_exists($upload_path . $backup_filename) ) { |
| 58 |
$upload_backup_file = copy($upload_path . $filename, $upload_path . $backup_filename); |
| 59 |
|
| 60 |
if (!$upload_backup_file) { |
| 61 |
wp_send_json_error('❌ '.__('Backup original image failed', 'squeeze') . ': '. $upload_path . $backup_filename); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// Save the image in the uploads directory. |
| 67 |
|
| 68 |
global $wp_filesystem; |
| 69 |
// Initialize the WP filesystem, no more using 'file-put-contents' function |
| 70 |
if (empty($wp_filesystem)) { |
| 71 |
require_once (ABSPATH . '/wp-admin/includes/file.php'); |
| 72 |
WP_Filesystem(); |
| 73 |
} |
| 74 |
|
| 75 |
$upload_file = $wp_filesystem->put_contents($upload_path . $filename, $decoded); |
| 76 |
|
| 77 |
if (!$upload_file) { |
| 78 |
wp_send_json_error('❌ '.__('Upload image failed', 'squeeze'). ': '. $upload_path . $filename); |
| 79 |
} |
| 80 |
|
| 81 |
// upload thumbnails |
| 82 |
if ($process !== 'path') { |
| 83 |
$response_msg = ''; |
| 84 |
update_post_meta($attach_id, "squeeze_is_compressed", true); |
| 85 |
|
| 86 |
foreach ($sizes as $size_name => $size_data) { |
| 87 |
$size_base64 = sanitize_text_field($size_data['base64']); |
| 88 |
$size_base64 = str_replace('data:image/'.$file_format.';base64,', '', $size_base64); |
| 89 |
$size_base64 = str_replace(' ', '+', $size_base64); |
| 90 |
$size_decoded = base64_decode($size_base64); |
| 91 |
|
| 92 |
$size_filename = basename(sanitize_url($size_data['url'])); |
| 93 |
$upload_size_file = $wp_filesystem->put_contents($upload_path . $size_filename, $size_decoded); |
| 94 |
|
| 95 |
if (!$upload_size_file) { |
| 96 |
wp_send_json_error('❌ '.__('Attachment not updated', 'squeeze'). ': '. $upload_path . $size_filename); |
| 97 |
} else { |
| 98 |
$response_msg .= $size_filename . ',<br> '; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
$response_msg = $filename. ',<br>' . rtrim($response_msg, ',<br> '); |
| 103 |
$response_msg = '<strong>� |
| 104 |
'.__('Compressed successfully', 'squeeze') . ':</strong> ' . $response_msg; |
| 105 |
|
| 106 |
wp_send_json_success($response_msg); |
| 107 |
} else { |
| 108 |
wp_send_json_success('� |
| 109 |
'.__('Compressed successfully', 'squeeze')); |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
wp_die(); |
| 114 |
} |
| 115 |
|
| 116 |
add_action('wp_ajax_squeeze_restore_attachment', 'squeeze_restore_attachment'); |
| 117 |
/** |
| 118 |
* Restore original attachment |
| 119 |
*/ |
| 120 |
function squeeze_restore_attachment() { |
| 121 |
check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' ); |
| 122 |
if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) { |
| 123 |
$attach_id = (int) $_POST["attachmentID"]; |
| 124 |
$can_restore = squeeze_can_restore($attach_id); |
| 125 |
|
| 126 |
if ($can_restore) { |
| 127 |
$is_restore_attachment = _squeeze_restore_attachment($attach_id); |
| 128 |
|
| 129 |
if ($is_restore_attachment) { |
| 130 |
wp_send_json_success('� |
| 131 |
'.__('Restored successfully', 'squeeze')); |
| 132 |
} else { |
| 133 |
wp_send_json_error('❌ '.__('Attachment not updated', 'squeeze')); |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
} else { |
| 138 |
wp_send_json_error('❌ '.__('Attachment not found', 'squeeze')); |
| 139 |
} |
| 140 |
wp_die(); |
| 141 |
} |
| 142 |
|
| 143 |
add_action('wp_ajax_squeeze_get_attachment', 'squeeze_get_attachment'); |
| 144 |
/** |
| 145 |
* Get attachment data |
| 146 |
*/ |
| 147 |
function squeeze_get_attachment() { |
| 148 |
check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' ); |
| 149 |
|
| 150 |
if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) { |
| 151 |
$attach_id = (int) $_POST["attachmentID"]; |
| 152 |
$attach_data = array( |
| 153 |
'id' => $attach_id, |
| 154 |
'url' => wp_get_attachment_url($attach_id), |
| 155 |
'mime' => get_post_mime_type($attach_id), |
| 156 |
'name' => get_the_title($attach_id), |
| 157 |
'filename' => basename( wp_get_original_image_path( $attach_id ) ), |
| 158 |
); |
| 159 |
|
| 160 |
wp_send_json_success($attach_data); |
| 161 |
|
| 162 |
} else { |
| 163 |
wp_send_json_error('❌ '.__('Attachment not found', 'squeeze')); |
| 164 |
} |
| 165 |
|
| 166 |
wp_die(); |
| 167 |
} |
| 168 |
|
| 169 |
add_action('wp_ajax_squeeze_get_attachment_by_path', 'squeeze_get_attachment_by_path'); |
| 170 |
/** |
| 171 |
* Get attachment data by path |
| 172 |
*/ |
| 173 |
function squeeze_get_attachment_by_path() { |
| 174 |
check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' ); |
| 175 |
|
| 176 |
if (isset($_POST["path"]) && !empty($_POST["path"])) { |
| 177 |
$path = sanitize_text_field($_POST["path"]); |
| 178 |
$images = glob( ABSPATH . $path . '*.{'.SQUEEZE_ALLOWED_IMAGE_FORMATS.'}', GLOB_BRACE); |
| 179 |
$attach_data = array(); |
| 180 |
|
| 181 |
if (empty($images)) { |
| 182 |
wp_send_json_error('❌ '.__('Images not found in the path', 'squeeze') . ' ' . $path); |
| 183 |
} |
| 184 |
|
| 185 |
foreach ($images as $image) { |
| 186 |
$attach_id = attachment_url_to_postid($image); |
| 187 |
$attach_mime = image_type_to_mime_type(exif_imagetype($image)); |
| 188 |
$attach_url = str_replace(ABSPATH, home_url(), $image); |
| 189 |
$attach_name = pathinfo($image, PATHINFO_FILENAME); |
| 190 |
$attach_data[] = array( |
| 191 |
'id' => $attach_id, |
| 192 |
'url' => $attach_url, |
| 193 |
'mime' => $attach_mime, |
| 194 |
'name' => $attach_name, |
| 195 |
'filename' => basename( $image ), |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
wp_send_json_success($attach_data); |
| 200 |
|
| 201 |
} else { |
| 202 |
wp_send_json_error('❌ '.__('Path not found', 'squeeze')); |
| 203 |
} |
| 204 |
|
| 205 |
wp_die(); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Check if attachment can be restored |
| 210 |
* |
| 211 |
* @param int $attach_id |
| 212 |
* @return bool |
| 213 |
*/ |
| 214 |
function squeeze_can_restore($attach_id) { |
| 215 |
$original_img_path = wp_get_original_image_path((int) $attach_id); |
| 216 |
$backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path); |
| 217 |
$can_restore = file_exists($backup_img_path); |
| 218 |
|
| 219 |
return $can_restore; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Handle restore attachment |
| 224 |
* Internal function |
| 225 |
* |
| 226 |
* @param int $attach_id |
| 227 |
* @return bool |
| 228 |
*/ |
| 229 |
function _squeeze_restore_attachment($attach_id, $is_bulk = false) { |
| 230 |
$original_img_path = wp_get_original_image_path($attach_id); |
| 231 |
$backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path); |
| 232 |
$upload_restore_file = copy($backup_img_path, $original_img_path); |
| 233 |
|
| 234 |
if (!$upload_restore_file) { |
| 235 |
if ($is_bulk) { |
| 236 |
wp_die('❌ '.__('Restore original image failed', 'squeeze')); |
| 237 |
} else { |
| 238 |
wp_send_json_error('❌ '.__('Restore original image failed', 'squeeze')); |
| 239 |
} |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
// create thumbnails from original image |
| 244 |
$attach_data = wp_create_image_subsizes($original_img_path, $attach_id); |
| 245 |
|
| 246 |
$is_update_attachment = delete_post_meta($attach_id, "squeeze_is_compressed"); |
| 247 |
|
| 248 |
if ($is_update_attachment) { |
| 249 |
wp_delete_file($backup_img_path); // delete backup file |
| 250 |
return true; |
| 251 |
} else { |
| 252 |
return false; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
add_action('delete_attachment', 'squeeze_delete_attachment'); |
| 257 |
/** |
| 258 |
* Delete backup image when attachment is deleted |
| 259 |
* |
| 260 |
* @param int $attach_id |
| 261 |
*/ |
| 262 |
function squeeze_delete_attachment($attach_id) { |
| 263 |
$original_img_path = wp_get_original_image_path((int) $attach_id); |
| 264 |
$backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path); |
| 265 |
|
| 266 |
if (file_exists($backup_img_path)) { |
| 267 |
wp_delete_file($backup_img_path); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
add_filter('bulk_actions-upload', 'squeeze_bulk_actions'); |
| 272 |
/** |
| 273 |
* Add bulk action to media library |
| 274 |
* |
| 275 |
* @param array $actions |
| 276 |
* @return array |
| 277 |
*/ |
| 278 |
function squeeze_bulk_actions($actions) { |
| 279 |
$actions['squeeze_restore'] = __('Restore Original', 'squeeze'); |
| 280 |
return $actions; |
| 281 |
} |
| 282 |
|
| 283 |
add_filter('handle_bulk_actions-upload', 'squeeze_handle_bulk_actions', 10, 3); |
| 284 |
/** |
| 285 |
* Handle bulk action |
| 286 |
* |
| 287 |
* @param string $redirect_to |
| 288 |
* @param string $doaction |
| 289 |
* @param array $post_ids |
| 290 |
* @return string |
| 291 |
*/ |
| 292 |
function squeeze_handle_bulk_actions($redirect_to, $doaction, $post_ids) { |
| 293 |
if ($doaction !== 'squeeze_restore') { |
| 294 |
return $redirect_to; |
| 295 |
} |
| 296 |
|
| 297 |
foreach ($post_ids as $post_id) { |
| 298 |
$can_restore = squeeze_can_restore($post_id); |
| 299 |
|
| 300 |
if ($can_restore) { |
| 301 |
$is_restore_attachment = _squeeze_restore_attachment($post_id, true); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
$redirect_to = add_query_arg('bulk_restored', count($post_ids), $redirect_to); |
| 306 |
return $redirect_to; |
| 307 |
} |
| 308 |
|
| 309 |
add_action('admin_notices', 'squeeze_bulk_action_admin_notice'); |
| 310 |
/** |
| 311 |
* Display admin notice after bulk action |
| 312 |
*/ |
| 313 |
function squeeze_bulk_action_admin_notice() { |
| 314 |
if (!empty($_REQUEST['bulk_restored'])) { |
| 315 |
$message = sprintf( |
| 316 |
_n( |
| 317 |
'Attachment restored.', |
| 318 |
'%s attachments restored.', |
| 319 |
$_REQUEST['bulk_restored'], |
| 320 |
'squeeze' |
| 321 |
), |
| 322 |
number_format_i18n($_REQUEST['bulk_restored']) |
| 323 |
); |
| 324 |
printf('<div class="notice notice-success is-dismissible"><p>%s</p></div>', $message); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
add_filter('image_size_names_choose', 'squeeze_custom_image_sizes'); |
| 329 |
/** |
| 330 |
* Add custom image sizes to media library |
| 331 |
* |
| 332 |
* @param array $sizes |
| 333 |
* @return array |
| 334 |
*/ |
| 335 |
function squeeze_custom_image_sizes($sizes) { |
| 336 |
$available_sizes = wp_get_registered_image_subsizes(); |
| 337 |
|
| 338 |
foreach ($available_sizes as $size_name => $size_data) { |
| 339 |
$sizes[$size_name] = $size_data['width'] . 'x' . $size_data['height']; |
| 340 |
} |
| 341 |
|
| 342 |
return $sizes; |
| 343 |
} |