# squeeze/1.2/src/handlers.php

Squeeze – Image Optimization &amp; Compression, WEBP Conversion, version 1.2. 155 lines.

- Page: https://pluginprobe.com/plugins/squeeze/1.2/code/src/handlers.php
- Raw: https://pluginprobe.com/plugins/squeeze/1.2/raw/src/handlers.php
- Modified: 2023-08-14T17:05:12+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/squeeze/1.2/code/src/handlers.php#L10-L20`.

```php
<?php
// Exit if accessed directly.
if (!defined('ABSPATH')) {
	exit;
}


add_action('wp_ajax_squeeze_update_attachment', 'squeeze_update_attachment');
/**
 * Update attachment with compressed image
 * https://gist.github.com/cyberwani/ad5452b040001878d692c3165836ebff
 */
function squeeze_update_attachment() {
	check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
	if (isset($_POST["base64"]) && !empty($_POST["base64"])) {

		$base64 = sanitize_text_field($_POST["base64"]);
		$file_format = sanitize_text_field($_POST["format"]);
		$filename = sanitize_file_name($_POST["filename"]);
		$attach_id = (int) $_POST["attachmentID"];
        $url = sanitize_url($_POST["url"]);

		$options = get_option( 'squeeze_options' );
		$is_backup_original = isset($options['backup_original']) ? $options['backup_original'] : squeeze_get_default_value('backup_original');

		// Upload dir.
        $upload_dir = str_replace($filename, "", get_attached_file($attach_id)); // get upload dir from attachment path
		$upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir) . DIRECTORY_SEPARATOR;

		$img = str_replace('data:image/'.$file_format.';base64,', '', $base64);
		$img = str_replace(' ', '+', $img);
		$decoded = base64_decode($img);

		if ($is_backup_original) {
			// backup original
			$backup_filename = preg_replace("/(\.(?!.*\.))/", '.bak.', $filename); // add .bak. before extension
			$upload_backup_file = copy($upload_path . $filename, $upload_path . $backup_filename);

			if (!$upload_backup_file) {
				wp_send_json_error(__('Backup original image failed', 'squeeze'));
			}
		}

		// Save the image in the uploads directory.
		$upload_file = file_put_contents($upload_path . $filename, $decoded);

		if (!$upload_file) {
			wp_send_json_error(__('Upload image failed', 'squeeze'));
		}

		// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
		if ( ! function_exists( 'wp_crop_image' ) ) {
			require_once(ABSPATH . 'wp-admin/includes/image.php');
		}

		// Generate the metadata for the attachment, and update the database record.
		//$attach_data = wp_generate_attachment_metadata($attach_id, $upload_dir . '/' . $filename);

		// create thumbnails from COMPRESSED original image
		$attach_data = wp_create_image_subsizes($upload_dir . '/' . $filename, $attach_id); 

		$is_update_attachment = update_post_meta($attach_id, "squeeze_is_compressed", true);

		if ($is_update_attachment) {
			wp_send_json_success(__('Compressed successfully', 'squeeze'));
		} else {
			wp_send_json_error(__('Attachment not updated', 'squeeze'));
		}

	}
	wp_die();
}

add_action('wp_ajax_squeeze_restore_attachment', 'squeeze_restore_attachment');
/**
 * Restore original attachment
 */
function squeeze_restore_attachment() {
	check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
	if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) {
		$attach_id = (int) $_POST["attachmentID"];
		$can_restore = squeeze_can_restore($attach_id);

		if ($can_restore) {
			$original_img_path = wp_get_original_image_path($attach_id);
			$backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path);
			$upload_restore_file = copy($backup_img_path, $original_img_path);

			if (!$upload_restore_file) {
				wp_send_json_error(__('Restore original image failed', 'squeeze'));
			}

			// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
			if ( ! function_exists( 'wp_crop_image' ) ) {
				require_once(ABSPATH . 'wp-admin/includes/image.php');
			}

			// create thumbnails from original image
			$attach_data = wp_create_image_subsizes($original_img_path, $attach_id); 

			$is_update_attachment = delete_post_meta($attach_id, "squeeze_is_compressed");

			if ($is_update_attachment) {
                unlink($backup_img_path); // delete backup file
				wp_send_json_success(__('Restored successfully', 'squeeze'));
			} else {
				wp_send_json_error(__('Attachment not updated', 'squeeze'));
			}

		}
	} else {
		wp_send_json_error(__('Attachment not found', 'squeeze'));
	}
	wp_die();
}

add_action('wp_ajax_squeeze_get_attachment', 'squeeze_get_attachment');
/**
 * Get attachment data
 */
function squeeze_get_attachment() {
	check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );

	if (isset($_POST["attachmentID"]) && !empty($_POST["attachmentID"]) && wp_get_attachment_url($_POST["attachmentID"])) {
		$attach_id = (int) $_POST["attachmentID"];
		$attach_data = array(
			'id' => $attach_id,
			'url' => wp_get_attachment_url($attach_id),
			'mime' => get_post_mime_type($attach_id),
			'name' => get_the_title($attach_id),
			'filename' => basename( get_attached_file( $attach_id ) ),
		);

		wp_send_json_success($attach_data);

	} else {
		wp_send_json_error(__('Attachment not found', 'squeeze'));
	}

	wp_die();
}

/**
 * Check if attachment can be restored
 * 
 * @param int $attach_id
 * @return bool
 */
function squeeze_can_restore($attach_id) {
	$original_img_path = wp_get_original_image_path((int) $attach_id);
	$backup_img_path = preg_replace("/(\.(?!.*\.))/", '.bak.', $original_img_path);
	$can_restore = file_exists($backup_img_path);

	return $can_restore;
}
```
