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 #790, #792, #803: Delete/Generate next-gen in media not working #807

Merged
Merged
6 changes: 3 additions & 3 deletions classes/Bulk/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
Expand Down Expand Up @@ -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.
Expand Down
85 changes: 27 additions & 58 deletions classes/Optimization/Process/AbstractProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ abstract class AbstractProcess implements ProcessInterface {
*/
protected $format;

/**
* Array of image extensions processed.
*
* @var array
*/
protected $extensions = [
'webp',
'avif',
];

/**
* The constructor.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -1479,16 +1437,28 @@ 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 ) {
return new WP_Error( 'no_path', __( 'Path to non-next-gen file not provided.', 'imagify' ) );
}

$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' ) );
}
Expand Down Expand Up @@ -1535,7 +1505,6 @@ protected function delete_nextgen_file( $file_path ) {
return true;
}


/**
* Gives the next-gen image format we are processing.
*
Expand Down Expand Up @@ -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' ) );
}
Expand Down Expand Up @@ -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 ) {
Expand Down
8 changes: 4 additions & 4 deletions inc/classes/class-imagify-files-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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 . '<br/>';
Expand Down
12 changes: 6 additions & 6 deletions inc/functions/admin-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div class="imagify-datas-actions-links">';
$reoptimize_output_after = '</div><!-- .imagify-datas-actions-links -->';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
] );
Expand All @@ -388,15 +388,15 @@ 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
*
* @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 '';
}
Expand All @@ -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,
] );
Expand Down