diff --git a/classes/Bulk/Bulk.php b/classes/Bulk/Bulk.php index 100203120..cd6f05833 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' ] ); @@ -398,12 +398,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. diff --git a/classes/Optimization/Process/AbstractProcess.php b/classes/Optimization/Process/AbstractProcess.php index 086e8abf3..22a4751a7 100644 --- a/classes/Optimization/Process/AbstractProcess.php +++ b/classes/Optimization/Process/AbstractProcess.php @@ -105,6 +105,16 @@ abstract class AbstractProcess implements ProcessInterface { */ protected $format; + /** + * Array of image extensions processed. + * + * @var array + */ + protected $extensions = [ + 'webp', + 'avif', + ]; + /** * The constructor. * @@ -1314,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; @@ -1349,58 +1359,6 @@ protected function can_backup( $size ) { return $this->get_media()->get_context_instance()->can_backup(); } - /** - * 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 * @@ -1479,7 +1437,7 @@ 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|WP_Error A \WP_Error object on failure. */ protected function delete_nextgen_file( $file_path ) { if ( ! $file_path ) { @@ -1487,8 +1445,20 @@ protected function delete_nextgen_file( $file_path ) { } $next_gen_file = new File( $file_path ); - $next_gen_path = $next_gen_file->get_path_to_nextgen( $this->format ); + // Delete next-gen images. + foreach ( $this->extensions as $extension ) { + $this->delete_file( $next_gen_file->get_path_to_nextgen( $extension ) ); + } + } + + /** + * Delete a next gen format image, given its non-next-gen version's path. + * + * @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 $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' ) ); } @@ -1535,7 +1505,6 @@ protected function delete_nextgen_file( $file_path ) { return true; } - /** * Gives the next-gen image format we are processing. * @@ -1684,7 +1653,7 @@ public function can_create_next_gen_version( $file_path ) { * * @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' ) ); } @@ -1712,7 +1681,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 ) { diff --git a/inc/classes/class-imagify-files-list-table.php b/inc/classes/class-imagify-files-list-table.php index 771c5bb77..edd8f4fc0 100755 --- a/inc/classes/class-imagify-files-list-table.php +++ b/inc/classes/class-imagify-files-list-table.php @@ -701,7 +701,7 @@ 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 ); } @@ -821,14 +821,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, ] );