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

Change methods name #785

Merged
merged 4 commits into from
Jan 25, 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
1 change: 0 additions & 1 deletion classes/Avif/RewriteRules/Apache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

/**
* Add and remove rewrite rules to the .htaccess file to display AVIF images on the site.
*
*/
class Apache extends AbstractApacheDirConfFile {

Expand Down
1 change: 0 additions & 1 deletion classes/Avif/RewriteRules/Nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

/**
* Add and remove rewrite rules to the imagify.conf file to display AVIF images on the site.
*
*/
class Nginx extends AbstractNginxDirConfFile {

Expand Down
1 change: 0 additions & 1 deletion classes/Bulk/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ public function run_generate_webp( array $contexts ) {
[
'id' => $media_id,
'context' => $context,
'format' => static::,
],
"imagify-{$context}-convert-webp"
);
Expand Down
4 changes: 2 additions & 2 deletions classes/Optimization/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ public function optimize( $args = [] ) {
'avif',
];
if ( in_array( $args['convert'], $formats, true ) ) {
$destination_path = $this->get_path_to_next_gen( $args['convert'] );
$destination_path = $this->get_path_to_nextgen( $args['convert'] );
$this->path = $destination_path;
$this->file_type = null;
$this->editor = null;
Expand Down Expand Up @@ -778,7 +778,7 @@ public function get_path_to_webp() {
* @param string $format the format we are targeting.
* @return string|bool The file path on success. False if not an image or on failure.
*/
public function get_path_to_next_gen( string $format ) {
public function get_path_to_nextgen( string $format ) {
if ( ! $this->is_image() ) {
return false;
}
Expand Down
69 changes: 60 additions & 9 deletions classes/Optimization/Process/AbstractProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function optimize( $optimization_level = null, $args = [] ) {
if ( $data->is_already_optimized() && $this->has_next_gen() ) {
// If already optimized but has WebP, delete WebP versions and optimization data.
$data->delete_optimization_data();
$deleted = $this->delete_next_gen_files();
$deleted = $this->delete_nextgen_files();

if ( is_wp_error( $deleted ) ) {
return new WP_Error( 'next_gen_not_deleted', __( 'Previous Next-Gen files could not be deleted.', 'imagify' ) );
Expand Down Expand Up @@ -810,7 +810,7 @@ protected function compare_next_gen_file_size( $args ) {
}

// The new optimized file is lighter than the next-gen file: delete the next-gen file and store an error.
$next_gen_path = $args['file']->get_path_to_next_gen( $this->format );
$next_gen_path = $args['file']->get_path_to_nextgen( $this->format );

if ( $next_gen_path && $this->filesystem->is_writable( $next_gen_path ) ) {
$this->filesystem->delete( $next_gen_path );
Expand Down Expand Up @@ -1020,7 +1020,7 @@ public function restore() {
$media->update_dimensions();

// Delete the WebP version.
$this->delete_next_gen_file( $original_path );
$this->delete_nextgen_file( $original_path );

// Restore the thumbnails.
$response = $this->restore_thumbnails();
Expand Down Expand Up @@ -1061,7 +1061,7 @@ protected function restore_thumbnails() {
* In that case we must also delete the WebP file associated to the full size.
*/
$keep_full_webp = $media->get_raw_original_path() === $media->get_raw_fullsize_path();
$this->delete_next_gen_files( $keep_full_webp );
$this->delete_nextgen_files( $keep_full_webp );

// Generate new thumbnails.
return $media->generate_thumbnails();
Expand Down Expand Up @@ -1444,9 +1444,60 @@ protected function can_backup( $size ) {
/**
* Get MIME type based on the image format.
*
* @param string $format Image format ('webp' or 'avif').
* @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
*
* @param string $format nextgen image format.
*/
private function get_mime_type( $format ) {
$mime_types = [
static::AVIF_SUFFIX => 'image/avif',
Expand All @@ -1465,7 +1516,7 @@ private function get_mime_type( $format ) {
* @param bool $keep_full Set to true to keep the full size.
* @return bool|WP_Error True on success. A \WP_Error object on failure.
*/
public function delete_next_gen_files( $keep_full = false ) {
public function delete_nextgen_files( $keep_full = false ) {
if ( ! $this->is_valid() ) {
return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
}
Expand All @@ -1490,7 +1541,7 @@ public function delete_next_gen_files( $keep_full = false ) {

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

if ( is_wp_error( $deleted ) ) {
++$error_count;
Expand Down Expand Up @@ -1521,13 +1572,13 @@ public function delete_next_gen_files( $keep_full = false ) {
* @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_next_gen_file( $file_path ) {
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_next_gen( $this->format );
$next_gen_path = $next_gen_file->get_path_to_nextgen( $this->format );

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
6 changes: 3 additions & 3 deletions classes/Optimization/Process/Noop.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ public function maybe_resize( $size, $file ) {
/** ----------------------------------------------------------------------------------------- */

/**
* Generate WebP images if they are missing.
* Generate Nextgen images if they are missing.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @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() {
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
}

Expand All @@ -316,7 +316,7 @@ public function generate_next_gen_versions() {
* @param bool $keep_full Set to true to keep the full size.
* @return bool|WP_Error True on success. A \WP_Error object on failure.
*/
public function delete_next_gen_files( $keep_full = false ) {
public function delete_nextgen_files( $keep_full = false ) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions classes/Optimization/Process/ProcessInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function maybe_resize( $size, $file );
*
* @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();

/**
* Delete the next gen format images.
Expand All @@ -257,7 +257,7 @@ public function generate_next_gen_versions();
* @param bool $keep_full Set to true to keep the full size.
* @return bool|WP_Error True on success. A \WP_Error object on failure.
*/
public function delete_next_gen_files( $keep_full = false );
public function delete_nextgen_files( $keep_full = false );

/**
* Tell if a thumbnail size is an "Imagify Next-Gen" size.
Expand Down
3 changes: 2 additions & 1 deletion inc/3rd-party/nextgen-gallery/inc/common/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ function imagify_ngg_cleanup_after_media_deletion( $image_id, $image ) {
* The backup file has already been deleted by NGG.
* Delete the WebP versions and the optimization data.
*/
$process->delete_next_gen_files();
$process->delete_nextgen_files();

$process->get_data()->delete_optimization_data();
}

Expand Down
26 changes: 13 additions & 13 deletions inc/classes/class-imagify-admin-ajax-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class Imagify_Admin_Ajax_Post extends Imagify_Admin_Ajax_Post_Deprecated {
'imagify_manual_optimize',
'imagify_manual_reoptimize',
'imagify_optimize_missing_sizes',
'imagify_generate_next_gen_versions',
'imagify_delete_webp_versions',
'imagify_generate_nextgen_versions',
Copy link
Contributor

@Miraeld Miraeld Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ this function has been refactored here with a different name :

'imagify_generate_next_gen_versions',

'imagify_delete_nextgen_versions',
'imagify_restore',
// Custom folders optimization.
'imagify_optimize_file',
Expand Down Expand Up @@ -202,20 +202,20 @@ protected function optimize_missing_sizes( $media_id, $context ) {
* @param string $context The context.
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
*/
protected function generate_next_gen_versions( $media_id, $context ) {
return imagify_get_optimization_process( $media_id, $context )->generate_next_gen_versions();
protected function generate_nextgen_versions( $media_id, $context ) {
Copy link
Contributor

@Miraeld Miraeld Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ this function has been refactored here with a different name :

protected function generate_next_gen_versions( $media_id, $context ) {

return imagify_get_optimization_process( $media_id, $context )->generate_nextgen_versions();
}

/**
* Delete WebP images for media that are "already_optimize".
* Delete Next gen images for media that are "already_optimize".
*
* @since 1.9.6
*
* @param int $media_id The media ID.
* @param string $context The context.
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
*/
protected function delete_webp_versions( $media_id, $context ) {
protected function delete_nextgen_versions( $media_id, $context ) {
$process = imagify_get_optimization_process( $media_id, $context );

if ( ! $process->is_valid() ) {
Expand All @@ -233,7 +233,7 @@ protected function delete_webp_versions( $media_id, $context ) {
}

$data->delete_optimization_data();
$deleted = $process->delete_next_gen_files();
$deleted = $process->delete_nextgen_files();

if ( is_wp_error( $deleted ) ) {
return new WP_Error( 'webp_not_deleted', __( 'Previous WebP files could not be deleted.', 'imagify' ) );
Expand Down Expand Up @@ -363,21 +363,21 @@ public function imagify_optimize_missing_sizes_callback() {
*
* @since 1.9
*/
public function imagify_generate_next_gen_versions_callback() {
public function imagify_generate_nextgen_versions_callback() {
Copy link
Contributor

@Miraeld Miraeld Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ this function has been refactored here with a different name :

public function imagify_generate_next_gen_versions_callback() {

$context = $this->get_context();
$media_id = $this->get_media_id();

if ( ! $media_id || ! $context ) {
imagify_die( __( 'Invalid request', 'imagify' ) );
}

imagify_check_nonce( 'imagify-generate-webp-versions-' . $media_id . '-' . $context );
imagify_check_nonce( 'imagify-generate-nextgen-versions-' . $media_id . '-' . $context );

if ( ! imagify_get_context( $context )->current_user_can( 'manual-optimize', $media_id ) ) {
imagify_die();
}

$result = $this->generate_next_gen_versions( $media_id, $context );
$result = $this->generate_nextgen_versions( $media_id, $context );

imagify_maybe_redirect( is_wp_error( $result ) ? $result : false );

Expand All @@ -396,21 +396,21 @@ public function imagify_generate_next_gen_versions_callback() {
*
* @since 1.9.6
*/
public function imagify_delete_webp_versions_callback() {
public function imagify_delete_nextgen_versions_callback() {
$context = $this->get_context();
$media_id = $this->get_media_id();

if ( ! $media_id || ! $context ) {
imagify_die( __( 'Invalid request', 'imagify' ) );
}

imagify_check_nonce( 'imagify-delete-webp-versions-' . $media_id . '-' . $context );
imagify_check_nonce( 'imagify-delete-nextgen-versions-' . $media_id . '-' . $context );

if ( ! imagify_get_context( $context )->current_user_can( 'manual-restore', $media_id ) ) {
imagify_die();
}

$result = $this->delete_webp_versions( $media_id, $context );
$result = $this->delete_nextgen_versions( $media_id, $context );

imagify_maybe_redirect( is_wp_error( $result ) ? $result : false );

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 @@ -700,7 +700,7 @@ public function column_actions( $item ) {
$this->optimize_button( $item );
$this->retry_button( $item );
$this->reoptimize_buttons( $item );
$this->generate_next_gen_versions_button( $item );
$this->generate_nextgen_versions_button( $item );
$this->delete_webp_versions_button( $item );
$this->restore_button( $item );
}
Expand Down Expand Up @@ -806,14 +806,14 @@ protected function reoptimize_buttons( $item ) {
}

/**
* Prints a button to generate WebP versions if they are missing.
* Prints a button to generate Next gen versions if they are missing.
*
* @since 1.7
*
* @param object $item The current item. It must contain at least a $process property.
*/
protected function generate_next_gen_versions_button( $item ) {
$button = get_imagify_attachment_generate_next_gen_versions_link( $item->process );
protected function generate_nextgen_versions_button( $item ) {
Copy link
Contributor

@Miraeld Miraeld Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ this function has been refactored here with a different name :

protected function generate_next_gen_versions_button( $item ) {

$button = get_imagify_attachment_generate_nextgen_versions_link( $item->process );

if ( $button ) {
echo $button . '<br/>';
Expand Down
5 changes: 3 additions & 2 deletions inc/common/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ function imagify_cleanup_after_media_deletion( $process ) {

/**
* The optimization data will be automatically deleted by WP (post metas).
* Delete the WebP versions and the backup file.
* Delete the Nextgen versions and the backup file.
*/
$process->delete_next_gen_files();
$process->delete_nextgen_files();

$process->delete_backup();
}

Expand Down
4 changes: 2 additions & 2 deletions inc/functions/admin-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function get_imagify_attachment_optimization_text( $process ) {
$output_after = $is_media_page ? '<br/>' : '</li>';
$reoptimize_link = get_imagify_attachment_reoptimize_link( $process );
$reoptimize_link .= get_imagify_attachment_optimize_missing_thumbnails_link( $process );
$reoptimize_link .= get_imagify_attachment_generate_next_gen_versions_link( $process );
$reoptimize_link .= get_imagify_attachment_generate_nextgen_versions_link( $process );
$reoptimize_link .= get_imagify_attachment_delete_webp_versions_link( $process );
$reoptimize_output = $reoptimize_link ? $reoptimize_link : '';
$reoptimize_output_before = '<div class="imagify-datas-actions-links">';
Expand Down Expand Up @@ -325,7 +325,7 @@ function get_imagify_attachment_optimize_missing_thumbnails_link( $process ) {
* @param ProcessInterface $process The optimization process object.
* @return string The output to print.
*/
function get_imagify_attachment_generate_next_gen_versions_link( $process ) {
function get_imagify_attachment_generate_nextgen_versions_link( $process ) {
if ( ! $process->is_valid() ) {
return '';
}
Expand Down
8 changes: 4 additions & 4 deletions inc/functions/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ function get_imagify_admin_url( $action = 'settings', $arg = [] ) {
case 'optimize-missing-sizes':
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_optimize_missing_sizes&attachment_id=' . $id . '&context=' . $context ), 'imagify-optimize-missing-sizes-' . $id . '-' . $context );

case 'generate-next-gen-versions':
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_generate_next_gen_versions&attachment_id=' . $id . '&context=' . $context ), 'imagify-generate-webp-versions-' . $id . '-' . $context );
case 'generate-nextgen-versions':
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_generate_nextgen_versions&attachment_id=' . $id . '&context=' . $context ), 'imagify-generate-nextgen-versions-' . $id . '-' . $context );

case 'delete-webp-versions':
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_delete_webp_versions&attachment_id=' . $id . '&context=' . $context ), 'imagify-delete-webp-versions-' . $id . '-' . $context );
case 'delete-nextgen-versions':
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_delete_nextgen_versions&attachment_id=' . $id . '&context=' . $context ), 'imagify-delete-nextgen-versions-' . $id . '-' . $context );

case 'optimize':
case 'manual-upload': // Deprecated.
Expand Down