Skip to content

Commit

Permalink
Closes #796: Add the -scaled version to backup so we can convert it
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld committed Feb 23, 2024
1 parent 2dbf2e8 commit e32ea03
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions classes/Optimization/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,22 @@ public function backup( $backup_path = null, $backup_source = null ) {
) );
}

// Check if a '-scaled' version of the image exists.
$scaled_path = preg_replace('/(\.)([^\.]+)$/', '-scaled.$2', $backup_source);

Check notice on line 427 in classes/Optimization/File.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Optimization/File.php#L427

Expected 1 spaces after opening parenthesis; 0 found
if ($this->filesystem->exists($scaled_path)) {

Check notice on line 428 in classes/Optimization/File.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Optimization/File.php#L428

Expected 1 spaces after opening parenthesis; 0 found

Check notice on line 428 in classes/Optimization/File.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Optimization/File.php#L428

No space after opening parenthesis is prohibited
// Create a backup path for the scaled image.
$scaled_backup_path = preg_replace('/(\.)([^\.]+)$/', '-scaled.$2', $backup_path);

Check notice on line 430 in classes/Optimization/File.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Optimization/File.php#L430

Expected 1 spaces after opening parenthesis; 0 found
// Copy the '-scaled' version to the backup.
$this->filesystem->copy( $scaled_path, $scaled_backup_path, $overwrite, FS_CHMOD_FILE );

if ( ! $this->filesystem->exists( $scaled_backup_path ) ) {
return new \WP_Error( 'backup_doesnt_exist', __( 'The file could not be saved.', 'imagify' ), array(
'file_path' => $this->filesystem->make_path_relative( $scaled_path ),
'backup_path' => $this->filesystem->make_path_relative( $scaled_backup_path ),
) );
}
}

return true;
}

Expand Down
7 changes: 7 additions & 0 deletions classes/Optimization/Process/AbstractProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,13 @@ public function delete_backup() {

if ( $backup_path ) {
$this->filesystem->delete( $backup_path );

// Check for the -scaled version in the backup.
$scaled_backup_path = preg_replace('/(\.)([^\.]+)$/', '-scaled.$2', $backup_path);

Check notice on line 1091 in classes/Optimization/Process/AbstractProcess.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Optimization/Process/AbstractProcess.php#L1091

Expected 1 spaces after opening parenthesis; 0 found
if ($this->filesystem->exists($scaled_backup_path)) {

Check notice on line 1092 in classes/Optimization/Process/AbstractProcess.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Optimization/Process/AbstractProcess.php#L1092

Expected 1 spaces after opening parenthesis; 0 found

Check notice on line 1092 in classes/Optimization/Process/AbstractProcess.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Optimization/Process/AbstractProcess.php#L1092

No space after opening parenthesis is prohibited
// Delete the -scaled version from the backup.
$this->filesystem->delete($scaled_backup_path);

Check notice on line 1094 in classes/Optimization/Process/AbstractProcess.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Optimization/Process/AbstractProcess.php#L1094

Expected 1 spaces after opening parenthesis; 0 found
}
}
}

Expand Down

0 comments on commit e32ea03

Please sign in to comment.