From 311a9ab68571f67f083c3a1cca782145ea597aab Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 19 Feb 2024 23:40:12 +0100 Subject: [PATCH 01/16] Fixes deleting next gen images --- .../Optimization/Process/AbstractProcess.php | 30 +++++++++++++++++-- .../class-imagify-files-list-table.php | 7 +++-- inc/functions/admin-ui.php | 12 ++++---- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index d46284eb0..9fc1ca14e 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1570,16 +1570,32 @@ public function delete_nextgen_files( $keep_full = false ) { * @since 2.2 * * @param string $file_path Path to the non-next-gen file. - * @return bool|WP_Error True on success. A \WP_Error object on failure. + * @return void */ protected function delete_nextgen_file( $file_path ) { if ( ! $file_path ) { return new WP_Error( 'no_path', __( 'Path to non-next-gen file not provided.', 'imagify' ) ); } + $next_gen_ext = $this->get_nextgen_extension(); $next_gen_file = new File( $file_path ); - $next_gen_path = $next_gen_file->get_path_to_nextgen( $this->format ); + $next_gen_path = $next_gen_file->get_path_to_nextgen( $next_gen_ext ); + + $this->delete_file( $next_gen_path ); + + if ( 'avif' === $next_gen_ext ) { + $next_gen_path = $next_gen_file->get_path_to_nextgen( 'webp' ); + $this->delete_file( $next_gen_path ); + } + } + /** + * Delete a next gen format image, given its non-WebP version's path. + * + * @param string $file_path Path to the non-next-gen file. + * @return bool|WP_Error True on success. A \WP_Error object on failure. + */ + protected function delete_file( string $file_path ) { if ( ! $next_gen_path ) { return new WP_Error( 'no_$next_gen_path', __( 'Could not get the path to the Next-Gen format file.', 'imagify' ) ); } @@ -1626,7 +1642,6 @@ protected function delete_nextgen_file( $file_path ) { return true; } - /** * Gives the next-gen image format we are processing. * @@ -1636,6 +1651,15 @@ public function get_current_format() { return $this->get_option( 'convert_to_avif' ) ? static::AVIF_SUFFIX : static::WEBP_SUFFIX; } + /** + * Gets the expected extension of next-gen image. + * + * @return string Targeted extension. + */ + protected function get_nextgen_extension() { + return $this->get_option( 'convert_to_avif' ) ? 'avif' : 'webp'; + } + /** * Tell if a thumbnail size is an "Imagify Next-Gen" size. * diff --git a/inc/classes/class-imagify-files-list-table.php b/inc/classes/class-imagify-files-list-table.php index 771c5bb77..cd96981b5 100755 --- a/inc/classes/class-imagify-files-list-table.php +++ b/inc/classes/class-imagify-files-list-table.php @@ -702,6 +702,7 @@ public function column_actions( $item ) { $this->reoptimize_buttons( $item ); $this->generate_nextgen_versions_button( $item ); $this->delete_webp_versions_button( $item ); + $this->delete_nextgen_versions_button( $item ); $this->restore_button( $item ); } @@ -821,14 +822,14 @@ protected function generate_nextgen_versions_button( $item ) { } /** - * Prints a button to delete WebP versions when the status is "already_optimized". + * Prints a button to delete next-gen versions when the status is "already_optimized". * * @since 1.9.6 * * @param object $item The current item. It must contain at least a $process property. */ - protected function delete_webp_versions_button( $item ) { - $button = get_imagify_attachment_delete_webp_versions_link( $item->process ); + protected function delete_nextgen_versions_button( $item ) { + $button = get_imagify_attachment_delete_nextgen_versions_link( $item->process ); if ( $button ) { echo $button . '
'; diff --git a/inc/functions/admin-ui.php b/inc/functions/admin-ui.php index d170b3708..ff76a2033 100644 --- a/inc/functions/admin-ui.php +++ b/inc/functions/admin-ui.php @@ -24,7 +24,7 @@ function get_imagify_attachment_optimization_text( $process ) { $reoptimize_link = get_imagify_attachment_reoptimize_link( $process ); $reoptimize_link .= get_imagify_attachment_optimize_missing_thumbnails_link( $process ); $reoptimize_link .= get_imagify_attachment_generate_nextgen_versions_link( $process ); - $reoptimize_link .= get_imagify_attachment_delete_webp_versions_link( $process ); + $reoptimize_link .= get_imagify_attachment_delete_nextgen_versions_link( $process ); $reoptimize_output = $reoptimize_link ? $reoptimize_link : ''; $reoptimize_output_before = ''; @@ -317,7 +317,7 @@ function get_imagify_attachment_optimize_missing_thumbnails_link( $process ) { } /** - * Get the link to generate WebP versions if they are missing. + * Get the link to generate next-gen versions if they are missing. * * @since 1.9 * @author Grégory Viguier @@ -375,7 +375,7 @@ function get_imagify_attachment_generate_nextgen_versions_link( $process ) { return ''; } - $url = get_imagify_admin_url( 'generate-webp-versions', [ + $url = get_imagify_admin_url( 'generate-nextgen-versions', [ 'attachment_id' => $media->get_id(), 'context' => $context, ] ); @@ -388,7 +388,7 @@ function get_imagify_attachment_generate_nextgen_versions_link( $process ) { } /** - * Get the link to delete WebP versions when the status is "already_optimized". + * Get the link to delete next-gen versions when the status is "already_optimized". * * @since 1.9.6 * @author Grégory Viguier @@ -396,7 +396,7 @@ function get_imagify_attachment_generate_nextgen_versions_link( $process ) { * @param ProcessInterface $process The optimization process object. * @return string The output to print. */ -function get_imagify_attachment_delete_webp_versions_link( $process ) { +function get_imagify_attachment_delete_nextgen_versions_link( $process ) { if ( ! $process->is_valid() ) { return ''; } @@ -416,7 +416,7 @@ function get_imagify_attachment_delete_webp_versions_link( $process ) { } $class = ''; - $url = get_imagify_admin_url( 'delete-webp-versions', [ + $url = get_imagify_admin_url( 'delete-nextgen-versions', [ 'attachment_id' => $media_id, 'context' => $context, ] ); From 678e33566bb2d928d8ab122202b83f8c717adf56 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 10:06:36 +0100 Subject: [PATCH 02/16] Removed method with duplicate body --- .../Optimization/Process/AbstractProcess.php | 58 +------------------ 1 file changed, 3 insertions(+), 55 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 9fc1ca14e..8c7dff68f 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1441,58 +1441,6 @@ protected function can_backup( $size ) { /** WEBP & AVIF ============================================================================= */ /** ----------------------------------------------------------------------------------------- */ - /** - * Get MIME type based on the image format. - * - * @return string|bool The MIME type if valid format, false otherwise. - */ - public function generate_nextgen_versions() { - if ( ! $this->is_valid() ) { - return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); - } - - $media = $this->get_media(); - - if ( ! $media->is_image() ) { - return new WP_Error( 'no_webp', __( 'This media is not an image and cannot be converted to WebP format.', 'imagify' ) ); - } - - if ( ! $media->has_backup() ) { - return new WP_Error( 'no_backup', __( 'This media has no backup file.', 'imagify' ) ); - } - - $data = $this->get_data(); - - if ( ! $data->is_optimized() && ! $data->is_already_optimized() ) { - return new WP_Error( 'not_optimized', __( 'This media has not been optimized by Imagify yet.', 'imagify' ) ); - } - - if ( $this->has_webp() ) { - return new WP_Error( 'has_webp', __( 'This media already has WebP versions.', 'imagify' ) ); - } - - $files = $media->get_media_files(); - $sizes = []; - $args = [ - 'hook_suffix' => 'generate_nextgen_versions', - ]; - - foreach ( $files as $size_name => $file ) { - if ( 'image/webp' !== $files[ $size_name ]['mime-type'] ) { - array_unshift( $sizes, $size_name . static::WEBP_SUFFIX ); - } - } - - if ( ! $sizes ) { - return new \WP_Error( 'no_sizes', __( 'This media does not have files that can be converted to WebP format.', 'imagify' ) ); - } - - $optimization_level = $data->get_optimization_level(); - - // Optimize. - return $this->optimize_sizes( $sizes, $optimization_level, $args ); - } - /** * Get mime type * @@ -1791,13 +1739,13 @@ public function can_create_next_gen_version( $file_path ) { /** ----------------------------------------------------------------------------------------- */ /** - * Generate WebP images if they are missing. + * Generate next-gen images if they are missing. * * @since 1.9 * * @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure. */ - public function generate_next_gen_versions() { + public function generate_nextgen_versions() { if ( ! $this->is_valid() ) { return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); } @@ -1825,7 +1773,7 @@ public function generate_next_gen_versions() { $files = $media->get_media_files(); $sizes = []; $args = [ - 'hook_suffix' => 'generate_next_gen_versions', + 'hook_suffix' => 'generate_nextgen_versions', ]; foreach ( $files as $size_name => $file ) { From 2901df1ccdd3482e2cb5532252fff3e96d5ce9f2 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 10:12:50 +0100 Subject: [PATCH 03/16] Updated method name --- classes/Bulk/Bulk.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/Bulk/Bulk.php b/classes/Bulk/Bulk.php index d1e4d5a66..7702915e5 100644 --- a/classes/Bulk/Bulk.php +++ b/classes/Bulk/Bulk.php @@ -17,7 +17,7 @@ class Bulk { */ public function init() { add_action( 'imagify_optimize_media', [ $this, 'optimize_media' ], 10, 3 ); - add_action( 'imagify_convert_next_gen', [ $this, 'generate_next_gen_versions' ], 10, 2 ); + add_action( 'imagify_convert_next_gen', [ $this, 'generate_nextgen_versions' ], 10, 2 ); add_action( 'imagify_convert_webp_finished', [ $this, 'clear_webp_transients' ], 10, 2 ); add_action( 'wp_ajax_imagify_bulk_optimize', [ $this, 'bulk_optimize_callback' ] ); add_action( 'wp_ajax_imagify_missing_nextgen_generation', [ $this, 'missing_nextgen_callback' ] ); @@ -386,12 +386,12 @@ private function force_optimize( int $media_id, string $context, int $level ) { * * @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure. */ - public function generate_next_gen_versions( int $media_id, string $context ) { + public function generate_nextgen_versions( int $media_id, string $context ) { if ( ! $this->can_optimize() ) { return false; } - return imagify_get_optimization_process( $media_id, $context )->generate_next_gen_versions(); + return imagify_get_optimization_process( $media_id, $context )->generate_nextgen_versions(); } /** * Generate AVIF images if they are missing. From 582f2e4f52918c6fe2e915329256f566886e4331 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 10:22:52 +0100 Subject: [PATCH 04/16] Updated docblock --- classes/Optimization/Process/AbstractProcess.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 8c7dff68f..c923ed210 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1538,7 +1538,7 @@ protected function delete_nextgen_file( $file_path ) { } /** - * Delete a next gen format image, given its non-WebP version's path. + * Delete a next gen format image, given its non-next-gen version's path. * * @param string $file_path Path to the non-next-gen file. * @return bool|WP_Error True on success. A \WP_Error object on failure. From 94e3006e503f815554256d323a54009747c9ef76 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 10:29:32 +0100 Subject: [PATCH 05/16] Updated method and fix phpcs --- classes/Optimization/Process/AbstractProcess.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index c923ed210..898bbf28a 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1518,7 +1518,7 @@ public function delete_nextgen_files( $keep_full = false ) { * @since 2.2 * * @param string $file_path Path to the non-next-gen file. - * @return void + * @return void|WP_Error A \WP_Error object on failure. */ protected function delete_nextgen_file( $file_path ) { if ( ! $file_path ) { @@ -1540,10 +1540,10 @@ protected function delete_nextgen_file( $file_path ) { /** * Delete a next gen format image, given its non-next-gen version's path. * - * @param string $file_path Path to the non-next-gen file. + * @param string $next_gen_path Path to the non-next-gen file. * @return bool|WP_Error True on success. A \WP_Error object on failure. */ - protected function delete_file( string $file_path ) { + protected function delete_file( string $next_gen_path ) { if ( ! $next_gen_path ) { return new WP_Error( 'no_$next_gen_path', __( 'Could not get the path to the Next-Gen format file.', 'imagify' ) ); } @@ -1601,7 +1601,7 @@ public function get_current_format() { /** * Gets the expected extension of next-gen image. - * + * * @return string Targeted extension. */ protected function get_nextgen_extension() { From 7fb7eff53802a2f730f57775556066130c1a73a5 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 10:55:24 +0100 Subject: [PATCH 06/16] Changed method to the one Ahmed used in PR 808 which is now closed --- classes/Optimization/Process/AbstractProcess.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 898bbf28a..c08e7fff5 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1525,7 +1525,7 @@ protected function delete_nextgen_file( $file_path ) { return new WP_Error( 'no_path', __( 'Path to non-next-gen file not provided.', 'imagify' ) ); } - $next_gen_ext = $this->get_nextgen_extension(); + $next_gen_ext = $this->get_format_extension(); $next_gen_file = new File( $file_path ); $next_gen_path = $next_gen_file->get_path_to_nextgen( $next_gen_ext ); @@ -1600,12 +1600,17 @@ public function get_current_format() { } /** - * Gets the expected extension of next-gen image. + * Get specific format extension. * - * @return string Targeted extension. + * @param string $format nextgen image format. */ - protected function get_nextgen_extension() { - return $this->get_option( 'convert_to_avif' ) ? 'avif' : 'webp'; + private function get_format_extension( $format ) { + $extensions = [ + static::AVIF_SUFFIX => 'avif', + static::WEBP_SUFFIX => 'webp', + ]; + + return isset( $extensions[ $format ] ) ? $extensions[ $format ] : ''; } /** From 90955a74fedbb014cb5dcf2429ba2d6e4adb27d5 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 11:45:24 +0100 Subject: [PATCH 07/16] Updated method --- classes/Optimization/Process/AbstractProcess.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index c08e7fff5..4147fa792 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1525,7 +1525,7 @@ protected function delete_nextgen_file( $file_path ) { return new WP_Error( 'no_path', __( 'Path to non-next-gen file not provided.', 'imagify' ) ); } - $next_gen_ext = $this->get_format_extension(); + $next_gen_ext = $this->get_format_extension( $this->format ); $next_gen_file = new File( $file_path ); $next_gen_path = $next_gen_file->get_path_to_nextgen( $next_gen_ext ); From fbcd76a0a591b047de32dcd428a971cc7984a5d5 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 11:49:04 +0100 Subject: [PATCH 08/16] Updated method --- classes/Optimization/Process/AbstractProcess.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 4147fa792..79bc9ba4c 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1531,10 +1531,12 @@ protected function delete_nextgen_file( $file_path ) { $this->delete_file( $next_gen_path ); - if ( 'avif' === $next_gen_ext ) { - $next_gen_path = $next_gen_file->get_path_to_nextgen( 'webp' ); - $this->delete_file( $next_gen_path ); + if ( 'avif' !== $next_gen_ext ) { + return; } + + $next_gen_path = $next_gen_file->get_path_to_nextgen( 'webp' ); + $this->delete_file( $next_gen_path ); } /** From e470fe22f27e46cbe4bbe1ae74bdccc55313d675 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 11:59:10 +0100 Subject: [PATCH 09/16] Updated method --- .../Optimization/Process/AbstractProcess.php | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 79bc9ba4c..7bc6f0711 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -103,6 +103,14 @@ abstract class AbstractProcess implements ProcessInterface { */ protected $format; + /** + * An array of valid extensions. + * + * @var array + * @since 2.2 + */ + protected $extensions = []; + /** * The constructor. * @@ -124,6 +132,10 @@ public function __construct( $id ) { $this->filesystem = \Imagify_Filesystem::get_instance(); $this->format = $this->get_current_format(); + $this->extensions = [ + static::AVIF_SUFFIX => 'avif', + static::WEBP_SUFFIX => 'webp', + ]; } /** @@ -1534,9 +1546,15 @@ protected function delete_nextgen_file( $file_path ) { if ( 'avif' !== $next_gen_ext ) { return; } - - $next_gen_path = $next_gen_file->get_path_to_nextgen( 'webp' ); - $this->delete_file( $next_gen_path ); + + // Delete other next-gen images if they exist. + foreach ( $this->extensions as $format => $ext ) { + if ( $next_gen_ext === $ext ) { + continue; + } + + $this->delete_file( $next_gen_file->get_path_to_nextgen( $ext ) ); + } } /** @@ -1607,12 +1625,7 @@ public function get_current_format() { * @param string $format nextgen image format. */ private function get_format_extension( $format ) { - $extensions = [ - static::AVIF_SUFFIX => 'avif', - static::WEBP_SUFFIX => 'webp', - ]; - - return isset( $extensions[ $format ] ) ? $extensions[ $format ] : ''; + return isset( $this->extensions[ $format ] ) ? $this->extensions[ $format ] : ''; } /** From 09ff3b37334a4d1a30c1f57cfac03fa5a4542e35 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 12:08:52 +0100 Subject: [PATCH 10/16] Fix codacy error --- classes/Optimization/Process/AbstractProcess.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 7bc6f0711..465fe03e9 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1548,12 +1548,12 @@ protected function delete_nextgen_file( $file_path ) { } // Delete other next-gen images if they exist. - foreach ( $this->extensions as $format => $ext ) { - if ( $next_gen_ext === $ext ) { + foreach ( $this->extensions as $extension ) { + if ( $next_gen_ext === $extension ) { continue; } - $this->delete_file( $next_gen_file->get_path_to_nextgen( $ext ) ); + $this->delete_file( $next_gen_file->get_path_to_nextgen( $extension ) ); } } From 4fb95ba4a7f0bdb95b0bab46cc729b454409ab84 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 20 Feb 2024 12:10:57 +0100 Subject: [PATCH 11/16] Updated method --- classes/Optimization/Process/AbstractProcess.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 465fe03e9..b30203b0c 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1539,13 +1539,7 @@ protected function delete_nextgen_file( $file_path ) { $next_gen_ext = $this->get_format_extension( $this->format ); $next_gen_file = new File( $file_path ); - $next_gen_path = $next_gen_file->get_path_to_nextgen( $next_gen_ext ); - - $this->delete_file( $next_gen_path ); - - if ( 'avif' !== $next_gen_ext ) { - return; - } + $this->delete_file( $next_gen_file->get_path_to_nextgen( $next_gen_ext ) ); // Delete other next-gen images if they exist. foreach ( $this->extensions as $extension ) { From 38c92118f6996b2eab52c52f8f79499e4374963d Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 21 Feb 2024 09:27:28 +0100 Subject: [PATCH 12/16] Removed unused method call --- inc/classes/class-imagify-files-list-table.php | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/classes/class-imagify-files-list-table.php b/inc/classes/class-imagify-files-list-table.php index cd96981b5..edd8f4fc0 100755 --- a/inc/classes/class-imagify-files-list-table.php +++ b/inc/classes/class-imagify-files-list-table.php @@ -701,7 +701,6 @@ public function column_actions( $item ) { $this->retry_button( $item ); $this->reoptimize_buttons( $item ); $this->generate_nextgen_versions_button( $item ); - $this->delete_webp_versions_button( $item ); $this->delete_nextgen_versions_button( $item ); $this->restore_button( $item ); } From 1720719961c08e39a136184a2a9487d0077d9012 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 21 Feb 2024 14:15:06 +0100 Subject: [PATCH 13/16] Remove all next-gen images version --- .../Optimization/Process/AbstractProcess.php | 37 ++++--------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index b30203b0c..291209584 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -103,14 +103,6 @@ abstract class AbstractProcess implements ProcessInterface { */ protected $format; - /** - * An array of valid extensions. - * - * @var array - * @since 2.2 - */ - protected $extensions = []; - /** * The constructor. * @@ -132,10 +124,6 @@ public function __construct( $id ) { $this->filesystem = \Imagify_Filesystem::get_instance(); $this->format = $this->get_current_format(); - $this->extensions = [ - static::AVIF_SUFFIX => 'avif', - static::WEBP_SUFFIX => 'webp', - ]; } /** @@ -1537,16 +1525,16 @@ protected function delete_nextgen_file( $file_path ) { return new WP_Error( 'no_path', __( 'Path to non-next-gen file not provided.', 'imagify' ) ); } - $next_gen_ext = $this->get_format_extension( $this->format ); - $next_gen_file = new File( $file_path ); - $this->delete_file( $next_gen_file->get_path_to_nextgen( $next_gen_ext ) ); + $extensions = imagify_nextgen_images_formats(); - // Delete other next-gen images if they exist. - foreach ( $this->extensions as $extension ) { - if ( $next_gen_ext === $extension ) { - continue; - } + if ( empty( $extensions ) ) { + return; + } + + $next_gen_file = new File( $file_path ); + // Delete next-gen images. + foreach ( $extensions as $extension ) { $this->delete_file( $next_gen_file->get_path_to_nextgen( $extension ) ); } } @@ -1613,15 +1601,6 @@ public function get_current_format() { return $this->get_option( 'convert_to_avif' ) ? static::AVIF_SUFFIX : static::WEBP_SUFFIX; } - /** - * Get specific format extension. - * - * @param string $format nextgen image format. - */ - private function get_format_extension( $format ) { - return isset( $this->extensions[ $format ] ) ? $this->extensions[ $format ] : ''; - } - /** * Tell if a thumbnail size is an "Imagify Next-Gen" size. * From 81004280fe235635b98d4c0ac922f7c7627fc4ed Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Feb 2024 16:15:37 +0100 Subject: [PATCH 14/16] Delete all next-gen versions --- .../Optimization/Process/AbstractProcess.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 291209584..9acd62ebb 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -103,6 +103,16 @@ abstract class AbstractProcess implements ProcessInterface { */ protected $format; + /** + * Array of extesions to be processed. + * + * @var array + */ + protected $extensions = [ + 'webp', + 'avif', + ]; + /** * The constructor. * @@ -1525,16 +1535,10 @@ protected function delete_nextgen_file( $file_path ) { return new WP_Error( 'no_path', __( 'Path to non-next-gen file not provided.', 'imagify' ) ); } - $extensions = imagify_nextgen_images_formats(); - - if ( empty( $extensions ) ) { - return; - } - $next_gen_file = new File( $file_path ); // Delete next-gen images. - foreach ( $extensions as $extension ) { + foreach ( $this->extensions as $extension ) { $this->delete_file( $next_gen_file->get_path_to_nextgen( $extension ) ); } } From b99472afdf50529413d409c7c360e19f308402aa Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Feb 2024 16:17:04 +0100 Subject: [PATCH 15/16] Updated docblock --- classes/Optimization/Process/AbstractProcess.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 9acd62ebb..4ca2221ed 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -104,7 +104,7 @@ abstract class AbstractProcess implements ProcessInterface { protected $format; /** - * Array of extesions to be processed. + * Array of image extensions processed. * * @var array */ From 8ddb0dd6c33bb40db9c93f850d92181aba70a817 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Feb 2024 16:30:43 +0100 Subject: [PATCH 16/16] Fix codacy erros --- classes/Optimization/Process/AbstractProcess.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 70cffceae..22a4751a7 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -1324,7 +1324,7 @@ protected function can_resize( $size, $file ) { 'full' . static::WEBP_SUFFIX !== $size || 'full' . static::AVIF_SUFFIX !== $size - ) + ) ) { // We resize only the main file and its next-gen version. return false; @@ -1437,11 +1437,7 @@ public function delete_nextgen_files( $keep_full = false ) { * @since 2.2 * * @param string $file_path Path to the non-next-gen file. -<<<<<<< HEAD * @return void|WP_Error A \WP_Error object on failure. -======= - * @return bool|WP_Error True on success. A WP_Error object on failure. ->>>>>>> feature/avif */ protected function delete_nextgen_file( $file_path ) { if ( ! $file_path ) {