Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wp-smush.../core/backups
File: class-backups.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Smush\Core\Backups;
[2] Fix | Delete
[3] Fix | Delete
use Smush\Core\File_System;
[4] Fix | Delete
use Smush\Core\Helper;
[5] Fix | Delete
use Smush\Core\Media\Media_Item;
[6] Fix | Delete
use Smush\Core\Media\Media_Item_Optimizer;
[7] Fix | Delete
use Smush\Core\Settings;
[8] Fix | Delete
use WDEV_Logger;
[9] Fix | Delete
[10] Fix | Delete
class Backups {
[11] Fix | Delete
/**
[12] Fix | Delete
* @var WDEV_Logger
[13] Fix | Delete
*/
[14] Fix | Delete
private $logger;
[15] Fix | Delete
/**
[16] Fix | Delete
* @var Settings|null
[17] Fix | Delete
*/
[18] Fix | Delete
private $settings;
[19] Fix | Delete
/**
[20] Fix | Delete
* @var File_System
[21] Fix | Delete
*/
[22] Fix | Delete
private $fs;
[23] Fix | Delete
[24] Fix | Delete
public function __construct() {
[25] Fix | Delete
$this->logger = Helper::logger()->backup();
[26] Fix | Delete
$this->settings = Settings::get_instance();
[27] Fix | Delete
$this->fs = new File_System();
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
public function create_backup_file( $source_image_path ) {
[31] Fix | Delete
$bak_file_path = $this->generate_unique_bak_file_path( $source_image_path );
[32] Fix | Delete
$copied = $this->fs->copy( $source_image_path, $bak_file_path );
[33] Fix | Delete
if ( $copied ) {
[34] Fix | Delete
return basename( $bak_file_path );
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
return false;
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
private function generate_unique_bak_file_path( $source_image_path ) {
[41] Fix | Delete
// TODO: why not use the wp_unique_filename method for this?
[42] Fix | Delete
$path_info = pathinfo( $source_image_path );
[43] Fix | Delete
$ext = $path_info['extension'];
[44] Fix | Delete
$bak_ext = ".bak.$ext";
[45] Fix | Delete
$file_without_ext = trailingslashit( $path_info['dirname'] ) . $path_info['filename'];
[46] Fix | Delete
$bak_file_path = $file_without_ext . $bak_ext;
[47] Fix | Delete
if ( ! $this->fs->file_exists( $bak_file_path ) ) {
[48] Fix | Delete
return $bak_file_path;
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
$count = 1;
[52] Fix | Delete
$bak_file_path = $file_without_ext . '-' . $count . $bak_ext;
[53] Fix | Delete
while ( $this->fs->file_exists( $bak_file_path ) ) {
[54] Fix | Delete
$count ++;
[55] Fix | Delete
$bak_file_path = $file_without_ext . '-' . $count . $bak_ext;
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
return $bak_file_path;
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* @param $media_item Media_Item
[63] Fix | Delete
* @param $optimizer Media_Item_Optimizer
[64] Fix | Delete
*
[65] Fix | Delete
* @return bool
[66] Fix | Delete
*/
[67] Fix | Delete
public function maybe_create_backup( $media_item, $optimizer ) {
[68] Fix | Delete
if ( ! $this->settings->is_backup_active() ) {
[69] Fix | Delete
return false;
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
// Maybe it already exists?
[73] Fix | Delete
$backup_size = $media_item->get_default_backup_size();
[74] Fix | Delete
if ( $backup_size && $backup_size->get_file_path() && $this->fs->file_exists( $backup_size->get_file_path() ) ) {
[75] Fix | Delete
// We have a perfectly viable backup available already
[76] Fix | Delete
$this->logger->info( sprintf( 'Found an existing backed up file [%s] for attachment [%d].', $backup_size->get_file(), $media_item->get_id() ) );
[77] Fix | Delete
[78] Fix | Delete
return false;
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
$size_to_backup = $media_item->get_full_or_scaled_size();
[82] Fix | Delete
if ( ! $size_to_backup || ! $size_to_backup->file_exists() ) {
[83] Fix | Delete
$this->logger->warning( sprintf( 'File not found, could not backup up for attachment [%d].', $media_item->get_id() ) );
[84] Fix | Delete
[85] Fix | Delete
return false;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
$create_copy = $optimizer->should_optimize_size( $size_to_backup );
[89] Fix | Delete
[90] Fix | Delete
// Create the backup
[91] Fix | Delete
$file_name = $size_to_backup->get_file_name();
[92] Fix | Delete
$width = $size_to_backup->get_width();
[93] Fix | Delete
$height = $size_to_backup->get_height();
[94] Fix | Delete
if ( $create_copy ) {
[95] Fix | Delete
$file_name = $this->create_backup_file( $size_to_backup->get_file_path() );
[96] Fix | Delete
if ( ! $file_name ) {
[97] Fix | Delete
$this->logger->info( sprintf( 'File operation failed when trying to create backup file [%s] for attachment [%d].', $size_to_backup->get_file_path(), $media_item->get_id() ) );
[98] Fix | Delete
[99] Fix | Delete
return false;
[100] Fix | Delete
}
[101] Fix | Delete
}
[102] Fix | Delete
$media_item->add_backup_size( $file_name, $width, $height );
[103] Fix | Delete
$media_item->save();
[104] Fix | Delete
[105] Fix | Delete
return true;
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
/**
[109] Fix | Delete
* @param $media_item Media_Item
[110] Fix | Delete
*
[111] Fix | Delete
* @return bool
[112] Fix | Delete
*/
[113] Fix | Delete
public function restore_backup( $media_item ) {
[114] Fix | Delete
do_action( 'wp_smush_before_restore_backup_attempt', $media_item->get_id() );
[115] Fix | Delete
[116] Fix | Delete
return $this->restore_backup_to_file_path(
[117] Fix | Delete
$media_item,
[118] Fix | Delete
$media_item->get_original_image_path() // Directly using the path here because the size object is not available when the file doesn't exist on the disk
[119] Fix | Delete
);
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
/**
[123] Fix | Delete
* @param $media_item Media_Item
[124] Fix | Delete
* @param $file_path
[125] Fix | Delete
*
[126] Fix | Delete
* @return bool
[127] Fix | Delete
*/
[128] Fix | Delete
public function restore_backup_to_file_path( $media_item, $file_path ) {
[129] Fix | Delete
$restored = false;
[130] Fix | Delete
$backup_file_path = '';
[131] Fix | Delete
$attachment_id = $media_item->get_id();
[132] Fix | Delete
[133] Fix | Delete
do {
[134] Fix | Delete
$backup_size = $media_item->get_default_backup_size();
[135] Fix | Delete
if ( ! $backup_size ) {
[136] Fix | Delete
$this->logger->warning( sprintf( 'A restore was attempted for attachment [%d] but we did not find a backup file.', $media_item->get_id() ) );
[137] Fix | Delete
[138] Fix | Delete
break;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
$backup_file_path = $backup_size->get_file_path();
[142] Fix | Delete
[143] Fix | Delete
do_action( 'wp_smush_before_restore_backup', $backup_file_path, $attachment_id, $file_path );
[144] Fix | Delete
[145] Fix | Delete
if ( ! $this->fs->file_exists( $backup_file_path ) ) {
[146] Fix | Delete
// Clean up
[147] Fix | Delete
$media_item->remove_default_backup_size();
[148] Fix | Delete
$media_item->save();
[149] Fix | Delete
[150] Fix | Delete
$this->logger->warning( sprintf( 'A restore was attempted for attachment [%d] but the backup file does not exist.', $media_item->get_id() ) );
[151] Fix | Delete
[152] Fix | Delete
break;
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$is_separate_backup_file = $backup_file_path !== $file_path;
[156] Fix | Delete
if ( $is_separate_backup_file ) {
[157] Fix | Delete
$copied = $this->fs->copy( $backup_file_path, $file_path );
[158] Fix | Delete
if ( $copied ) {
[159] Fix | Delete
$this->fs->unlink( $backup_file_path );
[160] Fix | Delete
} else {
[161] Fix | Delete
$this->logger->warning( sprintf( 'Error copying from [%s] to [%s].', $backup_file_path, $file_path ) );
[162] Fix | Delete
[163] Fix | Delete
break;
[164] Fix | Delete
}
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
wp_generate_attachment_metadata( $attachment_id, $file_path );
[168] Fix | Delete
/*
[169] Fix | Delete
* TODO: we might want to follow media_handle_upload which makes an extra update attachment call like this:
[170] Fix | Delete
* wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
[171] Fix | Delete
*/
[172] Fix | Delete
[173] Fix | Delete
// The metadata has changed because we called wp_generate_attachment_metadata. We need to reset before saving.
[174] Fix | Delete
$media_item->reset();
[175] Fix | Delete
[176] Fix | Delete
$media_item->remove_default_backup_size();
[177] Fix | Delete
$media_item->save();
[178] Fix | Delete
[179] Fix | Delete
$restored = true;
[180] Fix | Delete
} while ( 0 );
[181] Fix | Delete
[182] Fix | Delete
do_action( 'wp_smush_after_restore_backup', $restored, $backup_file_path, $attachment_id, $file_path );
[183] Fix | Delete
[184] Fix | Delete
return $restored;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* TODO: merge somehow with \Smush\Core\Modules\Backup::get_attachments_with_backups
[189] Fix | Delete
* @return int
[190] Fix | Delete
*/
[191] Fix | Delete
private function count_attachments_with_backups() {
[192] Fix | Delete
global $wpdb;
[193] Fix | Delete
$wild = '%';
[194] Fix | Delete
$backup_key_like = $wild . $wpdb->esc_like( Media_Item::DEFAULT_BACKUP_KEY ) . $wild;
[195] Fix | Delete
$no_backup_files = $wpdb->get_var(
[196] Fix | Delete
$wpdb->prepare(
[197] Fix | Delete
"SELECT count(*)
[198] Fix | Delete
FROM {$wpdb->postmeta}
[199] Fix | Delete
WHERE meta_key=%s AND `meta_value` LIKE %s",
[200] Fix | Delete
Media_Item::BACKUP_SIZES_META_KEY,
[201] Fix | Delete
$backup_key_like
[202] Fix | Delete
)
[203] Fix | Delete
);
[204] Fix | Delete
[205] Fix | Delete
return (int) $no_backup_files;
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
public function items_with_backup_exist() {
[209] Fix | Delete
return $this->count_attachments_with_backups() > 0;
[210] Fix | Delete
}
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function