Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #796: Auto AVIF Convertion on settings save #811

Merged
merged 8 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
if ( $this->filesystem->exists( $scaled_path ) ) {
// Create a backup path for the scaled image.
$scaled_backup_path = preg_replace( '/(\.)([^\.]+)$/', '-scaled.$2', $backup_path );
// 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
25 changes: 19 additions & 6 deletions classes/Optimization/Process/AbstractProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,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 );
if ( $this->filesystem->exists( $scaled_backup_path ) ) {
// Delete the -scaled version from the backup.
$this->filesystem->delete( $scaled_backup_path );
}
}
}

Expand Down Expand Up @@ -1380,10 +1387,11 @@ private function get_mime_type( $format ) {
* @since 2.2
*
* @param bool $keep_full Set to true to keep the full size.
* @param bool $all_next_gen True: will delete every next-gen format. False: will delete only the current enabled format.
*
* @return bool|WP_Error True on success. A WP_Error object on failure.
*/
public function delete_nextgen_files( $keep_full = false ) {
public function delete_nextgen_files( $keep_full = false, $all_next_gen = false ) {
if ( ! $this->is_valid() ) {
return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
}
Expand All @@ -1408,7 +1416,7 @@ public function delete_nextgen_files( $keep_full = false ) {

foreach ( $files as $file ) {
if ( 0 === strpos( $file['mime-type'], 'image/' ) ) {
$deleted = $this->delete_nextgen_file( $file['path'] );
$deleted = $this->delete_nextgen_file( $file['path'], $all_next_gen );

if ( is_wp_error( $deleted ) ) {
++$error_count;
Expand Down Expand Up @@ -1436,19 +1444,24 @@ public function delete_nextgen_files( $keep_full = false ) {
*
* @since 2.2
*
* @param string $file_path Path to the non-next-gen file.
* @param string $file_path Path to the non-next-gen file.
* @param bool $all_next_gen True: will delete every next-gen format. False: will delete only the current enabled format.
*
* @return void|WP_Error A WP_Error object on failure.
* @return void|WP_Error A \WP_Error object on failure.
*/
protected function delete_nextgen_file( $file_path ) {
protected function delete_nextgen_file( $file_path, $all_next_gen = false ) {
if ( ! $file_path ) {
return new WP_Error( 'no_path', __( 'Path to non-next-gen file not provided.', 'imagify' ) );
}

$next_gen_file = new File( $file_path );
$formats = $this->extensions;

if ( ! $all_next_gen ) {
$formats = imagify_nextgen_images_formats();
}
// Delete next-gen images.
foreach ( $this->extensions as $extension ) {
foreach ( $formats as $extension ) {
$path = $next_gen_file->get_path_to_nextgen( $extension );

if ( ! $path ) {
Expand Down
2 changes: 1 addition & 1 deletion inc/classes/class-imagify-admin-ajax-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ protected function delete_nextgen_versions( $media_id, $context ) {
}

$data->delete_optimization_data();
$deleted = $process->delete_nextgen_files();
$deleted = $process->delete_nextgen_files( false, true );

if ( is_wp_error( $deleted ) ) {
return new WP_Error( 'nextgen_not_deleted', __( 'Previous next-gen files could not be deleted.', 'imagify' ) );
Expand Down
2 changes: 1 addition & 1 deletion inc/common/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function imagify_cleanup_after_media_deletion( $process ) {
* The optimization data will be automatically deleted by WP (post metas).
* Delete the Nextgen versions and the backup file.
*/
$process->delete_nextgen_files();
$process->delete_nextgen_files( false, true );

$process->delete_backup();
}
Expand Down