get_image_formats(); if ( !in_array( $extension, $image_formats ) ) { wp_send_json_error( '❌ ' . esc_html__( 'Invalid image format', 'squeeze' ) ); } $attach_id = (int) $_POST["attachmentID"]; $meta_data = wp_get_attachment_metadata( $attach_id ); $url = sanitize_text_field( $_POST["url"] ); // sanitize_url() replaces spaces with %20, so we use sanitize_text_field() instead $process = sanitize_text_field( $_POST["process"] ); // process: all, uncompressed, path $is_backup_original = self::$SqueezeHelpers->get_option( 'backup_original' ); // Upload path. $upload_path = self::$SqueezeHelpers->get_upload_path( $attach_id, $filename, $url ); $decoded = self::$SqueezeHelpers->decode_base64_image( $base64, $file_format ); if ( $is_backup_original && $process !== 'path' ) { // do not backup for non library images // backup original $backup_original_image = self::$SqueezeHelpers->backup_original_image( $upload_path, $filename ); if ( is_wp_error( $backup_original_image ) ) { wp_send_json_error( $backup_original_image->get_error_message() ); } } $sizes['original']['original_size'] = filesize( $upload_path . $filename ); $sizes['original']['compressed_size'] = strlen( $decoded ); // Save the image in the uploads directory. $upload_image = self::$SqueezeHelpers->upload_image( $upload_path, $filename, $decoded ); if ( is_wp_error( $upload_image ) ) { wp_send_json_error( $upload_image->get_error_message() ); } if ($base64_webp) { $upload_webp = self::$SqueezeHelpers->upload_webp($upload_path, $base64_webp, $filename); if (is_wp_error( $upload_webp )) { wp_send_json_error( $upload_webp->get_error_message() ); } self::$SqueezeHelpers->upload_webp_thumbs($upload_path, $sizes_webp); // skip handling errors for webp thumbs, because they are not always required } // upload thumbnails if ( $process !== 'path' ) { $sizes = self::$SqueezeHelpers->upload_image_thumbs( $upload_path, $sizes, $file_format ); if ( is_wp_error( $sizes ) ) { wp_send_json_error( $sizes->get_error_message() ); } update_post_meta( $attach_id, "squeeze_is_compressed", true ); $response_msg = self::$SqueezeHelpers->get_comparison_table( $sizes ); $response_msg = '✅ ' . esc_html__( 'Squeezed successfully', 'squeeze' ) . '! ' . $response_msg; $uncompressed_images = self::$SqueezeHelpers->get_stats_option( 'uncompressed_images' ); $uncompressed_images--; update_option( 'squeeze_stats', array( 'uncompressed_images' => $uncompressed_images, ) ); wp_send_json_success( $response_msg ); } else { wp_send_json_success( '✅ ' . esc_html__( 'Squeezed successfully', 'squeeze' ) ); } wp_die(); } public function restore_attachment() { check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' ); if ( !isset( $_POST["attachmentID"] ) || empty( $_POST["attachmentID"] ) || !wp_get_attachment_url( $_POST["attachmentID"] ) ) { wp_send_json_error( '❌ ' . esc_html__( 'Attachment not found', 'squeeze' ) ); } $attach_id = (int) $_POST["attachmentID"]; $can_restore = self::$SqueezeHelpers->can_restore( $attach_id ); if ( $can_restore ) { $is_restore_attachment = self::$SqueezeHelpers->restore_attachment( $attach_id ); if ( $is_restore_attachment ) { wp_send_json_success( '✅ ' . esc_html__( 'Restored successfully', 'squeeze' ) ); } else { wp_send_json_error( '❌ ' . esc_html__( 'Attachment not restored', 'squeeze' ) ); } } wp_die(); } public function get_attachment() { check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' ); if ( !isset( $_POST["attachmentID"] ) || empty( $_POST["attachmentID"] ) || !wp_get_attachment_url( $_POST["attachmentID"] ) ) { wp_send_json_error( '❌ ' . esc_html__( 'Attachment not found', 'squeeze' ) ); } $attach_id = (int) $_POST["attachmentID"]; $sizes = wp_get_attachment_metadata( $attach_id ); $sizes = $sizes['sizes']; $full_image = wp_get_attachment_image_src( $attach_id, 'full' ); $sizes['full'] = array( 'url' => $full_image[0], 'width' => $full_image[1], 'height' => $full_image[2], ); foreach ( $sizes as $size_name => $size_data ) { $sizes[$size_name]['url'] = wp_get_attachment_image_url( $attach_id, $size_name ); } $attach_data = array( 'id' => $attach_id, 'url' => wp_get_original_image_url( $attach_id ), 'mime' => get_post_mime_type( $attach_id ), 'name' => get_the_title( $attach_id ), 'filename' => basename( wp_get_original_image_path( $attach_id ) ), 'sizes' => $sizes, ); wp_send_json_success( $attach_data ); wp_die(); } public function get_attachment_by_path() { check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' ); if ( !isset( $_POST["path"] ) || empty( $_POST["path"] ) ) { wp_send_json_error( '❌ ' . esc_html__( 'Path not found', 'squeeze' ) ); } $pathes = sanitize_text_field( $_POST["path"] ); $pathes = json_decode( stripslashes( $pathes ), true ); $attach_data = array(); $image_formats = self::$SqueezeHelpers->get_image_formats(); $image_formats = implode( ',', $image_formats ); foreach ( $pathes as $path ) { $path = str_replace( ['\\\\', '\\'], '/', $path ); // replace double and then single backslashes with slashes if ( substr( $path, -1 ) !== '/' ) { $path .= '/'; } if ( substr( $path, 0, 1 ) !== '/' ) { $path = '/' . $path; } $images = glob( ABSPATH . $path . '*.{' . $image_formats . '}', GLOB_BRACE ); if ( empty( $images ) ) { continue; } foreach ( $images as $image ) { $attach_id = attachment_url_to_postid( $image ); $attach_mime = image_type_to_mime_type( exif_imagetype( $image ) ); $attach_url = str_replace( ABSPATH, home_url(), $image ); $attach_name = pathinfo( $image, PATHINFO_FILENAME ); $attach_data[] = array( 'id' => $attach_id, 'url' => $attach_url, 'mime' => $attach_mime, 'name' => $attach_name, 'filename' => basename( $image ), ); } } // Save pathes to cache set_transient( 'squeeze_bulk_path', $pathes, MONTH_IN_SECONDS ); if ( empty( $attach_data ) ) { wp_send_json_error( '❌ ' . esc_html__( 'Images were not found in the selected directories', 'squeeze' ) ); } wp_send_json_success( $attach_data ); wp_die(); } public function delete_backup_attachment( $attach_id ) { $original_img_path = wp_get_original_image_path( (int) $attach_id ); $backup_img_path = preg_replace( "/(\\.(?!.*\\.))/", '.bak.', $original_img_path ); if ( file_exists( $backup_img_path ) ) { return wp_delete_file( $backup_img_path ); } return false; } public function delete_webp_images( $attach_id ) { $original_img_path = wp_get_original_image_path( (int) $attach_id ); $attach_data = wp_get_attachment_metadata( $attach_id ); $delete_webp_images = self::$SqueezeHelpers->delete_webp_images( $original_img_path, $attach_data ); return $delete_webp_images; } public function bulk_actions( $actions ) { $actions['squeeze_bulk_restore'] = esc_html__( 'Restore Original Image', 'squeeze' ); $actions['squeeze_bulk_compress'] = esc_html__( 'Squeeze Image', 'squeeze' ); $actions['squeeze_bulk_delete_backup'] = esc_html__( 'Delete Backup Image', 'squeeze' ); $actions['squeeze_bulk_delete_webp'] = esc_html__( 'Delete WEBP Image', 'squeeze' ); return $actions; } public function handle_bulk_actions( $redirect_to, $doaction, $post_ids ) { if ( $doaction === 'squeeze_bulk_restore' ) { $restored_ids_count = 0; foreach ( $post_ids as $post_id ) { $can_restore = self::$SqueezeHelpers->can_restore( $post_id ); if ( $can_restore ) { $is_restore_attachment = self::$SqueezeHelpers->restore_attachment( $post_id, true ); if ( $is_restore_attachment ) { $restored_ids_count += 1; } } } $redirect_to = add_query_arg( 'squeeze_bulk_restored', $restored_ids_count, $redirect_to ); } if ( $doaction === 'squeeze_bulk_compress' ) { foreach ( $post_ids as $post_id ) { $redirect_to = add_query_arg( 'squeeze_bulk_compressed', count( $post_ids ), $redirect_to ); } } if ( $doaction === 'squeeze_bulk_delete_backup' ) { $deleted_ids_count = 0; foreach ( $post_ids as $post_id ) { $is_delete_backup = $this->delete_backup_attachment( $post_id ); if ( $is_delete_backup ) { $deleted_ids_count += 1; } } $redirect_to = add_query_arg( 'squeeze_bulk_deleted', $deleted_ids_count, $redirect_to ); } if ( $doaction === 'squeeze_bulk_delete_webp' ) { $deleted_ids_count = 0; foreach ( $post_ids as $post_id ) { $is_delete_webp = $this->delete_webp_images( $post_id ); if ( $is_delete_webp ) { $deleted_ids_count += 1; } } $redirect_to = add_query_arg( 'squeeze_bulk_webp_deleted', $deleted_ids_count, $redirect_to ); } return $redirect_to; } public function bulk_action_admin_notice() { if ( !empty( $_REQUEST['squeeze_bulk_restored'] ) ) { $message = sprintf( /* translators: %d: number of attachments restored */ _n( '%d attachment restored.', '%d attachments restored.', $_REQUEST['squeeze_bulk_restored'], 'squeeze' ), number_format_i18n( $_REQUEST['squeeze_bulk_restored'] ) ); printf( '
%s
%s
%s
%s