| 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'); |
| 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 (isset($_POST["base64"]) && !empty($_POST["base64"])) { |
| 18 |
|
| 19 |
$base64 = sanitize_text_field($_POST["base64"]); |
| 20 |
$file_format = sanitize_text_field($_POST["format"]); |
| 21 |
$filename = sanitize_file_name($_POST["filename"]); |
| 22 |
$attach_id = (int) $_POST["attachmentID"]; |
| 23 |
$url = sanitize_url($_POST["url"]); |
| 24 |
$process = sanitize_text_field($_POST["process"]); // process: all, uncompressed, path |
| 25 |
|
| 26 |
$options = get_option( 'squeeze_options' ); |
| 27 |
$is_backup_original = isset($options['backup_original']) ? $options['backup_original'] : squeeze_get_default_value('backup_original'); |
| 28 |
|
| 29 |
// Upload dir. |
| 30 |
if ($attach_id > 0) { |
| 31 |
$upload_dir = str_replace($filename, "", wp_get_original_image_path($attach_id)); // get upload dir from attachment path |
| 32 |
} else { |
| 33 |
$upload_dir = str_replace($filename, "", $url); |
| 34 |
$upload_dir = str_replace(home_url(), ABSPATH, $upload_dir); |
| 35 |
} |
| 36 |
$upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir) . DIRECTORY_SEPARATOR; |
| 37 |
|
| 38 |
$img = str_replace('data:image/'.$file_format.';base64,', '', $base64); |
| 39 |
$img = str_replace(' ', '+', $img); |
| 40 |
$decoded = base64_decode($img); |
| 41 |
|
| 42 |
if ($is_backup_original && $process !== 'path') { |
| 43 |
// backup original |
| 44 |
$backup_filename = preg_replace("/(\.(?!.*\.))/", '.bak.', $filename); // add .bak. before extension |
| 45 |
|
| 46 |
if ( !file_exists($upload_path . $backup_filename) ) { |
| 47 |
$upload_backup_file = copy($upload_path . $filename, $upload_path . $backup_filename); |
| 48 |
|
| 49 |
if (!$upload_backup_file) { |
| 50 |
wp_send_json_error(__('Backup original image failed', 'squeeze') . ': '. $upload_path . $backup_filename); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Save the image in the uploads directory. |
| 56 |
$upload_file = file_put_contents($upload_path . $filename, $decoded); |
| 57 |
|
| 58 |
if (!$upload_file) { |
| 59 |
wp_send_json_error(__('Upload image failed', 'squeeze'). ': '. $upload_path . $filename); |
| 60 |
} |
| 61 |
|
| 62 |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. |
| 63 |
if ( ! function_exists( 'wp_crop_image' ) ) { |
| 64 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 65 |
} |
| 66 |
|
| 67 |
// Generate the metadata for the attachment, and update the database record. |
| 68 |
//$attach_data = wp_generate_attachment_metadata($attach_id, $upload_dir . '/' . $filename); |
| 69 |
|
| 70 |
// create thumbnails from COMPRESSED original image (not SCALED) |
| 71 |
if ($process !== 'path') { |
| 72 |
$attach_data = wp_create_image_subsizes($upload_dir . '/' . $filename, $attach_id); |
| 73 |
|
| 74 |
update_post_meta($attach_id, "squeeze_is_compressed", true); |
| 75 |
|
| 76 |
if (!empty($attach_data)) { |
| 77 |
wp_send_json_success(__('Compressed successfully', 'squeeze')); |
| 78 |
} else { |
| 79 |
wp_send_json_error(__('Attachment not updated', 'squeeze')); |
| 80 |
} |
| 81 |
} else { |
| 82 |
wp_send_json_success(__('Compressed successfully', 'squeeze')); |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
wp_die(); |
| 87 |
} |
| 88 |
|
| 89 |
add_action('wp_ajax_squeeze_restore_attachment', 'squeeze_restore_attachment'); |
| 90 |
/** |
| 91 |
* Restore original attachment |
| 92 |
*/ |
| 93 |
function squeeze_restore_attachment() { |
| 94 |
check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' ); |
| 95 |
if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) { |
| 96 |
$attach_id = (int) $_POST["attachmentID"]; |
| 97 |
$can_restore = squeeze_can_restore($attach_id); |
| 98 |
|
| 99 |
if ($can_restore) { |
| 100 |
$original_img_path = wp_get_original_image_path($attach_id); |
| 101 |
$backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path); |
| 102 |
$upload_restore_file = copy($backup_img_path, $original_img_path); |
| 103 |
|
| 104 |
if (!$upload_restore_file) { |
| 105 |
wp_send_json_error(__('Restore original image failed', 'squeeze')); |
| 106 |
} |
| 107 |
|
| 108 |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. |
| 109 |
if ( ! function_exists( 'wp_crop_image' ) ) { |
| 110 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 111 |
} |
| 112 |
|
| 113 |
// create thumbnails from original image |
| 114 |
$attach_data = wp_create_image_subsizes($original_img_path, $attach_id); |
| 115 |
|
| 116 |
$is_update_attachment = delete_post_meta($attach_id, "squeeze_is_compressed"); |
| 117 |
|
| 118 |
if ($is_update_attachment) { |
| 119 |
unlink($backup_img_path); // delete backup file |
| 120 |
wp_send_json_success(__('Restored successfully', 'squeeze')); |
| 121 |
} else { |
| 122 |
wp_send_json_error(__('Attachment not updated', 'squeeze')); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
} else { |
| 127 |
wp_send_json_error(__('Attachment not found', 'squeeze')); |
| 128 |
} |
| 129 |
wp_die(); |
| 130 |
} |
| 131 |
|
| 132 |
add_action('wp_ajax_squeeze_get_attachment', 'squeeze_get_attachment'); |
| 133 |
/** |
| 134 |
* Get attachment data |
| 135 |
*/ |
| 136 |
function squeeze_get_attachment() { |
| 137 |
check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' ); |
| 138 |
|
| 139 |
if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) { |
| 140 |
$attach_id = (int) $_POST["attachmentID"]; |
| 141 |
$attach_data = array( |
| 142 |
'id' => $attach_id, |
| 143 |
'url' => wp_get_attachment_url($attach_id), |
| 144 |
'mime' => get_post_mime_type($attach_id), |
| 145 |
'name' => get_the_title($attach_id), |
| 146 |
'filename' => basename( wp_get_original_image_path( $attach_id ) ), |
| 147 |
); |
| 148 |
|
| 149 |
wp_send_json_success($attach_data); |
| 150 |
|
| 151 |
} else { |
| 152 |
wp_send_json_error(__('Attachment not found', 'squeeze')); |
| 153 |
} |
| 154 |
|
| 155 |
wp_die(); |
| 156 |
} |
| 157 |
|
| 158 |
add_action('wp_ajax_squeeze_get_attachment_by_path', 'squeeze_get_attachment_by_path'); |
| 159 |
/** |
| 160 |
* Get attachment data by path |
| 161 |
*/ |
| 162 |
function squeeze_get_attachment_by_path() { |
| 163 |
check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' ); |
| 164 |
|
| 165 |
if (isset($_POST["path"]) && !empty($_POST["path"])) { |
| 166 |
$path = sanitize_text_field($_POST["path"]); |
| 167 |
$images = glob( ABSPATH . $path . '*.{'.SQUEEZE_ALLOWED_IMAGE_FORMATS.'}', GLOB_BRACE); |
| 168 |
$attach_data = array(); |
| 169 |
|
| 170 |
if (empty($images)) { |
| 171 |
wp_send_json_error(__('Images not found in the path', 'squeeze') . ' ' . $path); |
| 172 |
} |
| 173 |
|
| 174 |
foreach ($images as $image) { |
| 175 |
$attach_id = attachment_url_to_postid($image); |
| 176 |
$attach_mime = image_type_to_mime_type(exif_imagetype($image)); |
| 177 |
$attach_url = str_replace(ABSPATH, home_url(), $image); |
| 178 |
$attach_name = pathinfo($image, PATHINFO_FILENAME); |
| 179 |
$attach_data[] = array( |
| 180 |
'id' => $attach_id, |
| 181 |
'url' => $attach_url, |
| 182 |
'mime' => $attach_mime, |
| 183 |
'name' => $attach_name, |
| 184 |
'filename' => basename( $image ), |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
wp_send_json_success($attach_data); |
| 189 |
|
| 190 |
} else { |
| 191 |
wp_send_json_error(__('Path not found', 'squeeze')); |
| 192 |
} |
| 193 |
|
| 194 |
wp_die(); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Check if attachment can be restored |
| 199 |
* |
| 200 |
* @param int $attach_id |
| 201 |
* @return bool |
| 202 |
*/ |
| 203 |
function squeeze_can_restore($attach_id) { |
| 204 |
$original_img_path = wp_get_original_image_path((int) $attach_id); |
| 205 |
$backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path); |
| 206 |
$can_restore = file_exists($backup_img_path); |
| 207 |
|
| 208 |
return $can_restore; |
| 209 |
} |