SILENT KILLERPanel

Current Path: > home > codekrsu > > cuddlebuds.lk > wp-content > plugins > image-optimization > classes > image >


Operation   : Linux premium131.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
Software     : Apache
Server IP    : 162.0.232.56 | Your IP: 216.73.216.111
Domains      : 1034 Domain(s)
Permission   : [ 0755 ]

Files and Folders in: /home/codekrsu//cuddlebuds.lk/wp-content/plugins/image-optimization/classes/image/

NameTypeSizeLast ModifiedActions
exceptions Directory - -
image-backup.php File 4086 bytes February 06 2024 19:05:42.
image-conversion-option.php File 320 bytes July 29 2024 16:34:36.
image-conversion.php File 1203 bytes July 29 2024 16:34:36.
image-db-update.php File 655 bytes February 06 2024 19:05:42.
image-dimensions.php File 1057 bytes August 22 2024 13:32:50.
image-meta.php File 4725 bytes April 10 2025 14:45:10.
image-optimization-error-type.php File 433 bytes April 10 2025 14:45:10.
image-query-builder.php File 2407 bytes September 24 2024 14:22:20.
image-restore.php File 4062 bytes September 24 2024 14:22:20.
image-status.php File 672 bytes February 06 2024 19:05:42.
image.php File 4577 bytes April 10 2025 14:45:10.
wp-image-meta.php File 7451 bytes February 06 2024 19:05:42.

Reading File: /home/codekrsu//cuddlebuds.lk/wp-content/plugins/image-optimization/classes/image//image-backup.php

<?php

namespace ImageOptimization\Classes\Image;

use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error;
use ImageOptimization\Classes\File_System\File_System;
use ImageOptimization\Classes\File_Utils;
use ImageOptimization\Classes\Image\Exceptions\Image_Backup_Creation_Error;
use ImageOptimization\Classes\Logger;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * We're saving backup paths in meta even though the backup path could be dynamically generated.
 * The next reasons affected this decision:
 *
 * 1. Possible path conflicts with other plugins and/or custom user logic. We can't guarantee that the image
 * we find using the dynamically created path will be the same image we saved.
 * 2. If a backup doesn't exist, but stored in our meta -- we can be almost sure it's not because of us.
 * Probably, some other plugin removed it or even user manually did it, but it's not our fault. Makes this logic
 * easier to debug.
 * 3. We can change backup file name using `wp_unique_filename()` if needed and easily point a backup path to an
 * image.
 */
class Image_Backup {
	/**
	 * Creates a backup of a file by copying it to a new file with the backup extension.
	 * Also, attaches a newly created backup to image's meta.
	 *
	 * @param int $image_id Attachment id.
	 * @param string $image_size Image size (e.g. 'full', 'thumbnail', etc.).
	 * @param string $image_path Path to an image we plan to back up.
	 *
	 * @return string Backup path if successfully created, false otherwise.
	 *
	 * @throws Image_Backup_Creation_Error
	 */
	public static function create( int $image_id, string $image_size, string $image_path ): string {
		$extension = File_Utils::get_extension( $image_path );
		$backup_path = File_Utils::replace_extension( $image_path, "backup.$extension" );

		try {
			File_System::copy( $image_path, $backup_path, true );
		} catch ( File_System_Operation_Error $e ) {
			Logger::log(
				Logger::LEVEL_ERROR,
				"Error while creating a backup for image {$image_id} and size {$image_size}"
			);

			throw new Image_Backup_Creation_Error(
				"Error while creating a backup for image {$image_id} and size {$image_size}"
			);
		}

		$meta = new Image_Meta( $image_id );

		$meta->set_image_backup_path( $image_size, $backup_path );
		$meta->save();

		return $backup_path;
	}

	/**
	 * Looks for registered backups and remove all files found.
	 * Also, wipes removed files from image meta.
	 *
	 * @param int[] $image_ids Array of attachment ids.
	 * @return void
	 */
	public static function remove_many( array $image_ids ): void {
		foreach ( $image_ids as $image_id ) {
			self::remove( $image_id );
		}
	}

	/**
	 * Removes one or all backups for a specific image.
	 * Also, wipes removed files from image meta.
	 *
	 * @param int $image_id Attachment id.
	 * @param string|null $image_size Image size (e.g. 'full', 'thumbnail', etc.). All backups will be removed if no size provided.
	 *
	 * @return bool Returns true if backups were removed successfully, false otherwise.
	 */
	public static function remove( int $image_id, ?string $image_size = null ): bool {
		$meta = new Image_Meta( $image_id );
		$backups = $meta->get_image_backup_paths();

		if ( empty( $backups ) ) {
			return false;
		}

		if ( $image_size ) {
			if ( ! key_exists( $image_size, $backups ) ) {
				return false;
			}

			try {
				File_System::delete( $backups[ $image_size ], false, 'f' );
			} catch ( File_System_Operation_Error $e ) {
				Logger::log(
					Logger::LEVEL_ERROR,
					"Error while removing a backup for image {$image_id} and size {$image_size}"
				);
			}

			$meta->remove_image_backup_path( $image_size );
			$meta->save();

			return true;
		}

		foreach ( $backups as $image_size => $backup_path ) {
			try {
				File_System::delete( $backup_path, false, 'f' );
			} catch ( File_System_Operation_Error $e ) {
				Logger::log( Logger::LEVEL_ERROR, "Error while removing backups {$image_id}" );
			}

			$meta->remove_image_backup_path( $image_size );
		}

		$meta->save();

		return true;
	}
}

SILENT KILLER Tool