Skip to content

Commit

Permalink
Update 3rd-party compatibilities for multiple next-gen formats (#836)
Browse files Browse the repository at this point in the history
Co-authored-by: Gaël Robin <[email protected]>
Co-authored-by: Opeyemi Ibrahim <[email protected]>
Co-authored-by: Michael Lee <[email protected]>
Co-authored-by: WordPress Fan <[email protected]>
  • Loading branch information
5 people authored Mar 20, 2024
1 parent 4469a2a commit a92dd2c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
6 changes: 3 additions & 3 deletions inc/3rd-party/WooCommerce/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ class WooCommerce {
* @return void
*/
public function init() {
add_action( 'woocommerce_single_product_summary', [ $this, 'variable_products_webp_compat' ] );
add_action( 'woocommerce_single_product_summary', [ $this, 'variable_products_nextgen_compat' ] );
}

/**
* Add Variable Products Webp Compatibility.
* Add Variable Products Next-gen images Compatibility.
*
* @since 1.10.0
*
* @return void
*/
public function variable_products_webp_compat() {
public function variable_products_nextgen_compat() {
global $product;

if ( ! isset( $product ) || ! $product->is_type( 'variable' ) ) {
Expand Down
5 changes: 2 additions & 3 deletions inc/3rd-party/amp/amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
*/
if ( function_exists( 'is_amp_endpoint' ) ) :

add_filter( 'imagify_allow_picture_tags_for_webp', 'imagify_amp_disable_picture_on_endpoint' );
add_filter( 'imagify_allow_picture_tags_for_nextgen', 'imagify_amp_disable_picture_on_endpoint' );
/**
* Do not use <picture> tags in AMP pages.
*
* @since 1.9
* @author Grégory Viguier
* @since 1.9
*
* @param bool $allow True to allow the use of <picture> tags (default). False to prevent their use.
* @return bool
Expand Down
40 changes: 15 additions & 25 deletions inc/3rd-party/enable-media-replace/classes/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Imagify_Enable_Media_Replace_Deprecated;
use Imagify_Filesystem;

defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );

/**
* Compat class for Enable Media Replace plugin.
*
Expand Down Expand Up @@ -40,12 +38,12 @@ class Main extends Imagify_Enable_Media_Replace_Deprecated {
protected $old_backup_path;

/**
* List of paths to the old WebP files.
* List of paths to the old next-gen files.
*
* @var array
* @since 1.9.8
*/
protected $old_webp_paths = [];
protected $old_nextgen_paths = [];

/**
* Launch the hooks before the files and data are replaced.
Expand Down Expand Up @@ -84,29 +82,26 @@ public function init( $args = [] ) {
}

/**
* Keep track of existing WebP files.
* Keep track of existing next-gen files.
*
* Whether the user chooses to rename the files or not, we will need to delete the current WebP files before creating new ones:
* Whether the user chooses to rename the files or not, we will need to delete the current next-gen files before creating new ones:
* - Rename the files: the old ones must be removed, they are useless now.
* - Do not rename the files: the thumbnails may still get new names because of the suffix containing the image dimensions, which may differ (for example when thumbnails are scaled, not cropped).
* In this last case, the thumbnails with the old dimensions are removed from the drive and from the WP’s post meta, so there is no need of keeping orphan WebP files that would stay on the drive for ever, even after the attachment is deleted from WP.
* In this last case, the thumbnails with the old dimensions are removed from the drive and from the WP’s post meta, so there is no need of keeping orphan next-gen files that would stay on the drive for ever, even after the attachment is deleted from WP.
*/
foreach ( $this->process->get_media()->get_media_files() as $media_file ) {
$this->old_webp_paths[] = imagify_path_to_webp( $media_file['path'] );
foreach ( [ 'avif', 'webp' ] as $format ) {
$this->old_nextgen_paths[] = imagify_path_to_nextgen( $media_file['path'], $format );
}
}

// Delete the old backup file and old WebP files.
// Delete the old backup file and old next-gen files.
add_action( 'imagify_before_auto_optimization', [ $this, 'delete_backup' ] );
add_action( 'imagify_not_optimized_attachment_updated', [ $this, 'delete_backup' ] );
}


/** ----------------------------------------------------------------------------------------- */
/** HOOKS =================================================================================== */
/** ----------------------------------------------------------------------------------------- */

/**
* Delete previous backup file and WebP files.
* Delete previous backup file and next-gen files.
* This is done after the images have been already replaced by Enable Media Replace.
*
* @since 1.8.4
Expand All @@ -126,19 +121,14 @@ public function delete_backup( $media_id ) {
$this->old_backup_path = false;
}

if ( ! empty( $this->old_webp_paths ) ) {
// Delete old WebP files.
$this->old_webp_paths = array_filter( $this->old_webp_paths, [ $filesystem, 'exists' ] );
array_map( [ $filesystem, 'delete' ], $this->old_webp_paths );
$this->old_webp_paths = [];
if ( ! empty( $this->old_nextgen_paths ) ) {
// Delete old next-gen files.
$this->old_nextgen_paths = array_filter( $this->old_nextgen_paths, [ $filesystem, 'exists' ] );
array_map( [ $filesystem, 'delete' ], $this->old_nextgen_paths );
$this->old_nextgen_paths = [];
}
}


/** ----------------------------------------------------------------------------------------- */
/** TOOLS =================================================================================== */
/** ----------------------------------------------------------------------------------------- */

/**
* Get the optimization process corresponding to the current media.
*
Expand Down
8 changes: 4 additions & 4 deletions inc/3rd-party/regenerate-thumbnails/classes/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,19 @@ public function launch_async_optimization( $metadata, $media_id ) {
/**
* Optimize the sizes that have been regenerated.
*/
// If the media has WebP versions, recreate them for the sizes that have been regenerated.
// If the media has next-gen versions, recreate them for the sizes that have been regenerated.
$data = $process->get_data();
$optimization_data = $data->get_optimization_data();

if ( ! empty( $optimization_data['sizes'] ) ) {
foreach ( $optimization_data['sizes'] as $size_name => $size_data ) {
$non_webp_size_name = $process->is_size_next_gen( $size_name );
$non_nextgen_size_name = $process->is_size_next_gen( $size_name );

if ( ! $non_webp_size_name || ! isset( $sizes[ $non_webp_size_name ] ) ) {
if ( ! $non_nextgen_size_name || ! isset( $sizes[ $non_nextgen_size_name ] ) ) {
continue;
}

// Add the WebP size.
// Add the next-gen size.
$sizes[ $size_name ] = [];
}
}
Expand Down

0 comments on commit a92dd2c

Please sign in to comment.