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/clone/wp-conte.../plugins/wp-smush.../core/modules
File: class-backup.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Smush backup class
[2] Fix | Delete
*
[3] Fix | Delete
* @package Smush\Core\Modules
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace Smush\Core\Modules;
[7] Fix | Delete
[8] Fix | Delete
use Smush\Core\Core;
[9] Fix | Delete
use Smush\Core\Helper;
[10] Fix | Delete
use Smush\Core\Media\Media_Item_Cache;
[11] Fix | Delete
use Smush\Core\Media\Media_Item_Optimizer;
[12] Fix | Delete
use WP_Smush;
[13] Fix | Delete
[14] Fix | Delete
if ( ! defined( 'WPINC' ) ) {
[15] Fix | Delete
die;
[16] Fix | Delete
}
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Class Backup
[20] Fix | Delete
*/
[21] Fix | Delete
class Backup extends Abstract_Module {
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Module slug.
[25] Fix | Delete
*
[26] Fix | Delete
* @var string
[27] Fix | Delete
*/
[28] Fix | Delete
protected $slug = 'backup';
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Key for storing file path for image backup
[32] Fix | Delete
*
[33] Fix | Delete
* @var string
[34] Fix | Delete
*/
[35] Fix | Delete
private $backup_key = 'smush-full';
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Backup constructor.
[39] Fix | Delete
*/
[40] Fix | Delete
public function init() {
[41] Fix | Delete
// Handle Restore operation.
[42] Fix | Delete
//add_action( 'wp_ajax_smush_restore_image', array( $this, 'restore_image' ) );
[43] Fix | Delete
[44] Fix | Delete
// Handle bulk restore from modal.
[45] Fix | Delete
add_action( 'wp_ajax_get_image_count', array( $this, 'get_image_count' ) );
[46] Fix | Delete
add_action( 'wp_ajax_restore_step', array( $this, 'restore_step' ) );
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* Check if the backup file exists.
[51] Fix | Delete
*
[52] Fix | Delete
* @param int $attachment_id Attachment ID.
[53] Fix | Delete
* @param string $file_path Current file path.
[54] Fix | Delete
* @return bool True if the backup file exists, false otherwise.
[55] Fix | Delete
*/
[56] Fix | Delete
public function backup_exists( $attachment_id, $file_path = false ) {
[57] Fix | Delete
$media_item = Media_Item_Cache::get_instance()->get( $attachment_id );
[58] Fix | Delete
return $media_item->can_be_restored();
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Generate unique .bak file.
[63] Fix | Delete
*
[64] Fix | Delete
* @param string $bak_file The .bak file.
[65] Fix | Delete
* @param int $attachment_id Attachment ID.
[66] Fix | Delete
* @return string Returns a unique backup file.
[67] Fix | Delete
*/
[68] Fix | Delete
private function generate_unique_bak_file( $bak_file, $attachment_id ) {
[69] Fix | Delete
if ( strpos( $bak_file, '.bak' ) && Helper::file_exists( $bak_file, $attachment_id ) ) {
[70] Fix | Delete
$count = 1;
[71] Fix | Delete
$ext = Helper::get_file_ext( $bak_file );
[72] Fix | Delete
$ext = ".bak.$ext";
[73] Fix | Delete
$file_without_ext = rtrim( $bak_file, $ext );
[74] Fix | Delete
$bak_file = $file_without_ext . '-' . $count . $ext;
[75] Fix | Delete
[76] Fix | Delete
while ( Helper::file_exists( $bak_file, $attachment_id ) ) {
[77] Fix | Delete
$count++;
[78] Fix | Delete
$bak_file = $file_without_ext . '-' . $count . $ext;
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
return $bak_file;
[82] Fix | Delete
}
[83] Fix | Delete
return $bak_file;
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Creates a backup of file for the given attachment path.
[88] Fix | Delete
*
[89] Fix | Delete
* Checks if there is an existing backup, else create one.
[90] Fix | Delete
*
[91] Fix | Delete
* @param string $file_path File path.
[92] Fix | Delete
* @param int $attachment_id Attachment ID.
[93] Fix | Delete
*
[94] Fix | Delete
* @return void
[95] Fix | Delete
*/
[96] Fix | Delete
public function create_backup( $file_path, $attachment_id ) {
[97] Fix | Delete
if ( empty( $file_path ) || empty( $attachment_id ) ) {
[98] Fix | Delete
return;
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
// If backup not enabled, return.
[102] Fix | Delete
if ( ! $this->is_active() ) {
[103] Fix | Delete
return;
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
/**
[107] Fix | Delete
* If [ not compress original ]:
[108] Fix | Delete
* if [ is-scaled.file ]:
[109] Fix | Delete
* Backup original file.
[110] Fix | Delete
* elseif [ no-resize + no-png2jpg ]:
[111] Fix | Delete
* We don't need to backup, let user try to use regenerate plugin
[112] Fix | Delete
* to restore the compressed thumbnails size.
[113] Fix | Delete
* else: continue as compress_original.
[114] Fix | Delete
* else:
[115] Fix | Delete
* We don't need to backup if we had a backup file for PNG2JPG,
[116] Fix | Delete
* or .bak file. But if the .bak file is from third party, we will generate our new backup file.
[117] Fix | Delete
* end.
[118] Fix | Delete
*/
[119] Fix | Delete
[120] Fix | Delete
// We might not need to backup the file if we're not compressing original.
[121] Fix | Delete
if ( ! $this->settings->get( 'original' ) ) {
[122] Fix | Delete
/**
[123] Fix | Delete
* Add WordPress 5.3 support for -scaled images size, and those can always be used to restore.
[124] Fix | Delete
* Maybe user doesn't want to auto-scale JPG from WP for some images,
[125] Fix | Delete
* so we allow user to restore it even we don't Smush this image.
[126] Fix | Delete
*/
[127] Fix | Delete
if ( false !== strpos( $file_path, '-scaled.' ) && function_exists( 'wp_get_original_image_path' ) ) {
[128] Fix | Delete
// Scaled images already have a backup. Use that and don't create a new one.
[129] Fix | Delete
$file_path = Helper::get_attached_file( $attachment_id, 'backup' );// Supported S3.
[130] Fix | Delete
if ( file_exists( $file_path ) ) {
[131] Fix | Delete
/**
[132] Fix | Delete
* We do not need an additional backup file if we're not compressing originals.
[133] Fix | Delete
* But we need to save the original file as a backup file in the metadata to allow restoring this image later.
[134] Fix | Delete
*/
[135] Fix | Delete
$this->add_to_image_backup_sizes( $attachment_id, $file_path );
[136] Fix | Delete
return;
[137] Fix | Delete
}
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
$mod = WP_Smush::get_instance()->core()->mod;
[141] Fix | Delete
[142] Fix | Delete
// If there is not *-scaled.jpg file, we don't need to backup the file if we don't work with original file.
[143] Fix | Delete
if ( ! $mod->resize->is_active() && ! $mod->png2jpg->is_active() ) {
[144] Fix | Delete
/**
[145] Fix | Delete
* In this case, we can add the meta to save the original file as a backup file,
[146] Fix | Delete
* but if there is a lot of images, might take a lot of row for postmeta table,
[147] Fix | Delete
* so leave it for user to use a "regenerate thumbnail" plugin instead.
[148] Fix | Delete
*/
[149] Fix | Delete
Helper::logger()->backup()->info( sprintf( 'Not modify the original file [%s(%d)], skip the backup.', Helper::clean_file_path( $file_path ), $attachment_id ) );
[150] Fix | Delete
return;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
$should_backup = false;
[154] Fix | Delete
[155] Fix | Delete
// We should backup this image if we can resize it.
[156] Fix | Delete
if ( $mod->resize->is_active() && $mod->resize->should_resize( $attachment_id ) ) {
[157] Fix | Delete
$should_backup = true;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
// We should backup this image if we can convert it from PNG to JPEG.
[161] Fix | Delete
if (
[162] Fix | Delete
! $should_backup && $mod->png2jpg->is_active() && Helper::get_file_ext( $file_path, 'png' )
[163] Fix | Delete
&& $mod->png2jpg->can_be_converted( $attachment_id, 'full', 'image/png', $file_path )
[164] Fix | Delete
) {
[165] Fix | Delete
$should_backup = true;
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
// As we don't work with the original file, so we don't back it up.
[169] Fix | Delete
if ( ! $should_backup ) {
[170] Fix | Delete
Helper::logger()->backup()->info( sprintf( 'Not modify the original file [%s(%d)], skip the backup.', Helper::clean_file_path( $file_path ), $attachment_id ) );
[171] Fix | Delete
return;
[172] Fix | Delete
}
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Check if exists backup file from meta,
[177] Fix | Delete
* Because we will compress the original file,
[178] Fix | Delete
* so we only keep the backup file if there is PNG2JPG or .bak file.
[179] Fix | Delete
*/
[180] Fix | Delete
$backup_path = $this->get_backup_file( $attachment_id, $file_path );
[181] Fix | Delete
if ( $backup_path ) {
[182] Fix | Delete
/**
[183] Fix | Delete
* We will compress the original file so the backup file have to different from current file.
[184] Fix | Delete
* And the backup file should be the same folder with the main file.
[185] Fix | Delete
*/
[186] Fix | Delete
if ( $backup_path !== $file_path && dirname( $file_path ) === dirname( $backup_path ) ) {
[187] Fix | Delete
// Check if there is a .bak file or PNG2JPG file.
[188] Fix | Delete
if ( strpos( $backup_path, '.bak' ) || ( Helper::get_file_ext( $backup_path, 'png' ) && Helper::get_file_ext( $file_path, 'jpg' ) ) ) {
[189] Fix | Delete
Helper::logger()->backup()->info( sprintf( 'Found backed up file [%s(%d)].', Helper::clean_file_path( $backup_path ), $attachment_id ) );
[190] Fix | Delete
return;
[191] Fix | Delete
}
[192] Fix | Delete
}
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
/**
[196] Fix | Delete
* To avoid the conflict with 3rd party, we will generate a new backup file.
[197] Fix | Delete
* Because how about if 3rd party delete the backup file before trying to restore it from Smush?
[198] Fix | Delete
* We only try to use their bak file while restoring the backup file.
[199] Fix | Delete
*/
[200] Fix | Delete
$backup_path = $this->generate_unique_bak_file( $this->get_image_backup_path( $file_path ), $attachment_id );
[201] Fix | Delete
[202] Fix | Delete
/**
[203] Fix | Delete
* We need to save the .bak file to the meta. Because if there is a PNG, when we convert PNG2JPG,
[204] Fix | Delete
* the converted file is .jpg, so the bak file will be .bak.jpg not .bak.png
[205] Fix | Delete
*/
[206] Fix | Delete
// Store the backup path in image backup sizes.
[207] Fix | Delete
if ( copy( $file_path, $backup_path ) ) {
[208] Fix | Delete
$this->add_to_image_backup_sizes( $attachment_id, $backup_path );
[209] Fix | Delete
} else {
[210] Fix | Delete
Helper::logger()->backup()->error( sprintf( 'Cannot backup file [%s(%d)].', Helper::clean_file_path( $file_path ), $attachment_id ) );
[211] Fix | Delete
}
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Store new backup path for the image.
[216] Fix | Delete
*
[217] Fix | Delete
* @param int $attachment_id Attachment ID.
[218] Fix | Delete
* @param string $backup_path Backup path.
[219] Fix | Delete
* @param string $backup_key Backup key.
[220] Fix | Delete
*/
[221] Fix | Delete
public function add_to_image_backup_sizes( $attachment_id, $backup_path, $backup_key = '' ) {
[222] Fix | Delete
if ( empty( $attachment_id ) || empty( $backup_path ) ) {
[223] Fix | Delete
return;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
// Get the Existing backup sizes.
[227] Fix | Delete
$backup_sizes = $this->get_backup_sizes( $attachment_id );
[228] Fix | Delete
if ( empty( $backup_sizes ) ) {
[229] Fix | Delete
$backup_sizes = array();
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
// Prevent phar deserialization vulnerability.
[233] Fix | Delete
if ( false !== stripos( $backup_path, 'phar://' ) ) {
[234] Fix | Delete
Helper::logger()->backup()->info( sprintf( 'Prevent phar deserialization vulnerability [%s(%d)].', Helper::clean_file_path( $backup_path ), $attachment_id ) );
[235] Fix | Delete
return;
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
// Return if backup file doesn't exist.
[239] Fix | Delete
if ( ! file_exists( $backup_path ) ) {
[240] Fix | Delete
Helper::logger()->backup()->notice( sprintf( 'Back file [%s(%d)] does not exist.', Helper::clean_file_path( $backup_path ), $attachment_id ) );
[241] Fix | Delete
return;
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
list( $width, $height ) = getimagesize( $backup_path );
[245] Fix | Delete
[246] Fix | Delete
// Store our backup path.
[247] Fix | Delete
$backup_key = empty( $backup_key ) ? $this->backup_key : $backup_key;
[248] Fix | Delete
$backup_sizes[ $backup_key ] = array(
[249] Fix | Delete
'file' => wp_basename( $backup_path ),
[250] Fix | Delete
'width' => $width,
[251] Fix | Delete
'height' => $height,
[252] Fix | Delete
);
[253] Fix | Delete
[254] Fix | Delete
wp_cache_delete( 'images_with_backups', 'wp-smush' );
[255] Fix | Delete
update_post_meta( $attachment_id, '_wp_attachment_backup_sizes', $backup_sizes );
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
/**
[259] Fix | Delete
* Get backup sizes.
[260] Fix | Delete
*
[261] Fix | Delete
* @param int $attachment_id Attachment ID.
[262] Fix | Delete
* @return mixed False or an array of backup sizes.
[263] Fix | Delete
*/
[264] Fix | Delete
public function get_backup_sizes( $attachment_id ) {
[265] Fix | Delete
return get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
/**
[269] Fix | Delete
* Back up an image if it hasn't backed up yet.
[270] Fix | Delete
*
[271] Fix | Delete
* @since 3.9.6
[272] Fix | Delete
*
[273] Fix | Delete
* @param int $attachment_id Image id.
[274] Fix | Delete
* @param string $backup_file File path to back up.
[275] Fix | Delete
*
[276] Fix | Delete
* Note, we used it to manage backup PNG2JPG to keep the backup file is the original file to avoid conflicts with a duplicate PNG file.
[277] Fix | Delete
* If the backup file exists it will rename the original backup file to
[278] Fix | Delete
* the new backup file.
[279] Fix | Delete
*
[280] Fix | Delete
* @return bool True if added this file to the backup sizes, false if the image was backed up before.
[281] Fix | Delete
*/
[282] Fix | Delete
public function maybe_backup_image( $attachment_id, $backup_file ) {
[283] Fix | Delete
if ( ! file_exists( $backup_file ) ) {
[284] Fix | Delete
return false;
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
// We don't use .bak file from 3rd party while backing up.
[288] Fix | Delete
$backed_up_file = $this->get_backup_file( $attachment_id, $backup_file );
[289] Fix | Delete
$was_backed_up = true;
[290] Fix | Delete
[291] Fix | Delete
if ( $backed_up_file && $backed_up_file !== $backup_file && dirname( $backed_up_file ) === dirname( $backup_file ) ) {
[292] Fix | Delete
$was_backed_up = rename( $backed_up_file, $backup_file );
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
// Backup the image.
[296] Fix | Delete
if ( $was_backed_up ) {
[297] Fix | Delete
$this->add_to_image_backup_sizes( $attachment_id, $backup_file );
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
return $was_backed_up;
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
/**
[304] Fix | Delete
* Get the backup file from the meta.
[305] Fix | Delete
*
[306] Fix | Delete
* @since 3.9.6
[307] Fix | Delete
*
[308] Fix | Delete
* @param int $id Image ID.
[309] Fix | Delete
* @param string $file_path Current file path.
[310] Fix | Delete
*
[311] Fix | Delete
* @return bool|null Backup file or false|null if the image doesn't exist.
[312] Fix | Delete
*/
[313] Fix | Delete
public function get_backup_file( $id, $file_path = false ) {
[314] Fix | Delete
if ( empty( $id ) ) {
[315] Fix | Delete
return null;
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
if ( empty( $file_path ) ) {
[319] Fix | Delete
// Get unfiltered path file.
[320] Fix | Delete
$file_path = Helper::get_attached_file( $id, 'original' );
[321] Fix | Delete
[322] Fix | Delete
// If the file path is still empty, nothing to check here.
[323] Fix | Delete
if ( empty( $file_path ) ) {
[324] Fix | Delete
return null;
[325] Fix | Delete
}
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
// Initial result.
[329] Fix | Delete
$backup_file = false;
[330] Fix | Delete
// Try to get the backup file from _wp_attachment_backup_sizes.
[331] Fix | Delete
$backup_sizes = $this->get_backup_sizes( $id );
[332] Fix | Delete
// Check if we have backup file from the metadata.
[333] Fix | Delete
if ( $backup_sizes ) {
[334] Fix | Delete
// Try to get the original file first.
[335] Fix | Delete
if ( isset( $backup_sizes[ $this->backup_key ]['file'] ) ) {
[336] Fix | Delete
$original_file = str_replace( wp_basename( $file_path ), wp_basename( $backup_sizes[ $this->backup_key ]['file'] ), $file_path );
[337] Fix | Delete
if ( Helper::file_exists( $original_file, $id ) ) {
[338] Fix | Delete
$backup_file = $original_file;
[339] Fix | Delete
}
[340] Fix | Delete
}
[341] Fix | Delete
[342] Fix | Delete
// Try to check it from legacy original file or from the resized PNG file.
[343] Fix | Delete
if ( ! $backup_file ) {
[344] Fix | Delete
// If we don't have the original backup path in backup sizes, check for legacy original file path. It's for old version < V.2.7.0.
[345] Fix | Delete
$original_file = get_post_meta( $id, 'wp-smush-original_file', true );
[346] Fix | Delete
if ( ! empty( $original_file ) ) {
[347] Fix | Delete
// For old version < v.2.7.0, we are saving meta['file'] or _wp_attached_file.
[348] Fix | Delete
$original_file = Helper::original_file( $original_file );
[349] Fix | Delete
if ( Helper::file_exists( $original_file, $id ) ) {
[350] Fix | Delete
$backup_file = $original_file;
[351] Fix | Delete
// As we don't use this meta key so save it as a full backup file and delete the old metadata.
[352] Fix | Delete
WP_Smush::get_instance()->core()->mod->backup->add_to_image_backup_sizes( $id, $backup_file );
[353] Fix | Delete
delete_post_meta( $id, 'wp-smush-original_file' );
[354] Fix | Delete
}
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
// Check the backup file from resized PNG file.
[358] Fix | Delete
if ( ! $backup_file && isset( $backup_sizes['smush_png_path']['file'] ) ) {
[359] Fix | Delete
$original_file = str_replace( wp_basename( $file_path ), wp_basename( $backup_sizes['smush_png_path']['file'] ), $file_path );
[360] Fix | Delete
if ( Helper::file_exists( $original_file, $id ) ) {
[361] Fix | Delete
$backup_file = $original_file;
[362] Fix | Delete
}
[363] Fix | Delete
}
[364] Fix | Delete
}
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
return $backup_file;
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
/**
[371] Fix | Delete
* Restore the image and its sizes from backup
[372] Fix | Delete
*
[373] Fix | Delete
* @param string $attachment_id Attachment ID.
[374] Fix | Delete
* @param bool $resp Send JSON response or not.
[375] Fix | Delete
*
[376] Fix | Delete
* @return bool
[377] Fix | Delete
*/
[378] Fix | Delete
public function restore_image( $attachment_id = '', $resp = true ) {
[379] Fix | Delete
// TODO: (stats refactor) handle properly
[380] Fix | Delete
// If no attachment id is provided, check $_POST variable for attachment_id.
[381] Fix | Delete
if ( empty( $attachment_id ) ) {
[382] Fix | Delete
// Check Empty fields.
[383] Fix | Delete
if ( empty( $_POST['attachment_id'] ) || empty( $_POST['_nonce'] ) ) {
[384] Fix | Delete
wp_send_json_error(
[385] Fix | Delete
array(
[386] Fix | Delete
'error_msg' => esc_html__( 'Error in processing restore action, fields empty.', 'wp-smushit' ),
[387] Fix | Delete
)
[388] Fix | Delete
);
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
$nonce_value = filter_input( INPUT_POST, '_nonce', FILTER_SANITIZE_SPECIAL_CHARS );
[392] Fix | Delete
$attachment_id = filter_input( INPUT_POST, 'attachment_id', FILTER_SANITIZE_NUMBER_INT );
[393] Fix | Delete
[394] Fix | Delete
if ( ! wp_verify_nonce( $nonce_value, "wp-smush-restore-$attachment_id" ) ) {
[395] Fix | Delete
wp_send_json_error(
[396] Fix | Delete
array(
[397] Fix | Delete
'error_msg' => esc_html__( 'Image not restored, nonce verification failed.', 'wp-smushit' ),
[398] Fix | Delete
)
[399] Fix | Delete
);
[400] Fix | Delete
}
[401] Fix | Delete
[402] Fix | Delete
// Check capability.
[403] Fix | Delete
if ( ! Helper::is_user_allowed( 'upload_files' ) ) {
[404] Fix | Delete
wp_send_json_error(
[405] Fix | Delete
array(
[406] Fix | Delete
'error_msg' => esc_html__( "You don't have permission to work with uploaded files.", 'wp-smushit' ),
[407] Fix | Delete
)
[408] Fix | Delete
);
[409] Fix | Delete
}
[410] Fix | Delete
}
[411] Fix | Delete
[412] Fix | Delete
$attachment_id = (int) $attachment_id;
[413] Fix | Delete
[414] Fix | Delete
$mod = WP_Smush::get_instance()->core()->mod;
[415] Fix | Delete
[416] Fix | Delete
// Set an option to avoid the smush-restore-smush loop.
[417] Fix | Delete
set_transient( 'wp-smush-restore-' . $attachment_id, 1, HOUR_IN_SECONDS );
[418] Fix | Delete
[419] Fix | Delete
/**
[420] Fix | Delete
* Delete WebP.
[421] Fix | Delete
*
[422] Fix | Delete
* Run WebP::delete_images always even when the module is deactivated.
[423] Fix | Delete
*
[424] Fix | Delete
* @since 3.8.0
[425] Fix | Delete
*/
[426] Fix | Delete
$mod->webp->delete_images( $attachment_id );
[427] Fix | Delete
[428] Fix | Delete
// Restore Full size -> get other image sizes -> restore other images.
[429] Fix | Delete
// Get the Original Path, supported S3.
[430] Fix | Delete
$file_path = Helper::get_attached_file( $attachment_id, 'original' );
[431] Fix | Delete
[432] Fix | Delete
// Store the restore success/failure for full size image.
[433] Fix | Delete
$restored = false;
[434] Fix | Delete
// Retrieve backup file.
[435] Fix | Delete
$backup_full_path = $this->get_backup_file( $attachment_id, $file_path );
[436] Fix | Delete
// Is restoring the PNG which is converted to JPG or not.
[437] Fix | Delete
$restore_png = false;
[438] Fix | Delete
[439] Fix | Delete
/**
[440] Fix | Delete
* Fires before restoring a file.
[441] Fix | Delete
*
[442] Fix | Delete
* @since 3.9.6
[443] Fix | Delete
*
[444] Fix | Delete
* @param string|false $backup_full_path Full backup path.
[445] Fix | Delete
* @param int $attachment_id Attachment id.
[446] Fix | Delete
* @param string $file_path Original unfiltered file path.
[447] Fix | Delete
*
[448] Fix | Delete
* @hooked Smush\Core\Integrations\s3::maybe_download_file()
[449] Fix | Delete
*/
[450] Fix | Delete
do_action( 'wp_smush_before_restore_backup', $backup_full_path, $attachment_id, $file_path );
[451] Fix | Delete
[452] Fix | Delete
// Finally, if we have the backup path, perform the restore operation.
[453] Fix | Delete
if ( ! empty( $backup_full_path ) ) {
[454] Fix | Delete
// If the backup file is the same as the main file, we only need to re-generate the metadata.
[455] Fix | Delete
if ( $backup_full_path === $file_path ) {
[456] Fix | Delete
$restored = true;
[457] Fix | Delete
} else {
[458] Fix | Delete
// Is real backup file or .bak file.
[459] Fix | Delete
$is_real_filename = false === strpos( $backup_full_path, '.bak' );
[460] Fix | Delete
$restore_png = Helper::get_file_ext( trim( $backup_full_path ), 'png' ) && ! Helper::get_file_ext( $file_path, 'png' );
[461] Fix | Delete
[462] Fix | Delete
if ( $restore_png ) {
[463] Fix | Delete
// Restore PNG full size.
[464] Fix | Delete
$org_backup_full_path = $backup_full_path;
[465] Fix | Delete
if ( ! $is_real_filename ) {
[466] Fix | Delete
// Try to get a unique file name.
[467] Fix | Delete
$dirname = dirname( $backup_full_path );
[468] Fix | Delete
$new_file_name = wp_unique_filename( $dirname, wp_basename( str_replace( '.bak', '', $backup_full_path ) ) );
[469] Fix | Delete
$new_png_file = path_join( $dirname, $new_file_name );
[470] Fix | Delete
// Restore PNG full size.
[471] Fix | Delete
$restored = copy( $backup_full_path, $new_png_file );
[472] Fix | Delete
if ( $restored ) {
[473] Fix | Delete
// Assign the new PNG file to the backup file.
[474] Fix | Delete
$backup_full_path = $new_png_file;
[475] Fix | Delete
}
[476] Fix | Delete
} else {
[477] Fix | Delete
$restored = true;
[478] Fix | Delete
}
[479] Fix | Delete
[480] Fix | Delete
// Restore all other image sizes.
[481] Fix | Delete
if ( $restored ) {
[482] Fix | Delete
$metadata = $this->restore_png( $attachment_id, $backup_full_path, $file_path );
[483] Fix | Delete
$restored = ! empty( $metadata );
[484] Fix | Delete
if ( $restored && ! $is_real_filename ) {
[485] Fix | Delete
// Reset the backup file to delete it later.
[486] Fix | Delete
$backup_full_path = $org_backup_full_path;
[487] Fix | Delete
}
[488] Fix | Delete
}
[489] Fix | Delete
} else {
[490] Fix | Delete
// If file exists, corresponding to our backup path - restore.
[491] Fix | Delete
if ( ! $is_real_filename ) {
[492] Fix | Delete
$restored = copy( $backup_full_path, $file_path );
[493] Fix | Delete
} else {
[494] Fix | Delete
$restored = true;
[495] Fix | Delete
}
[496] Fix | Delete
}
[497] Fix | Delete
[498] Fix | Delete
// Remove the backup, if we were able to restore the image.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function