Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix/792-delete-generate-next-gen…
Browse files Browse the repository at this point in the history
…-in-media-not-working' into debug/auto-optimize
  • Loading branch information
Miraeld committed Feb 22, 2024
2 parents ab748bb + 1720719 commit 2dbf2e8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 71 deletions.
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
81 changes: 23 additions & 58 deletions classes/Optimization/Process/AbstractProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -1570,16 +1518,34 @@ 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' ) );
}

$extensions = imagify_nextgen_images_formats();

if ( empty( $extensions ) ) {
return;
}

$next_gen_file = new File( $file_path );
$next_gen_path = $next_gen_file->get_path_to_nextgen( $this->format );

// Delete next-gen images.
foreach ( $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 @@ -1626,7 +1592,6 @@ protected function delete_nextgen_file( $file_path ) {
return true;
}


/**
* Gives the next-gen image format we are processing.
*
Expand Down Expand Up @@ -1767,13 +1732,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' ) );
}
Expand Down Expand Up @@ -1801,7 +1766,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

0 comments on commit 2dbf2e8

Please sign in to comment.