Skip to content

Commit

Permalink
v1.9.10
Browse files Browse the repository at this point in the history
- Bugfix: Correctly optimize thumbnails during auto-optimization of image upload (#491)
- Bugfix: Fix broken compatibility with Enable Media Replace plugin after WordPress 5.3 (#493)
  • Loading branch information
remyperona authored May 26, 2020
2 parents d81c3c2 + 37fe3ed commit cff41d1
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 341 deletions.
4 changes: 2 additions & 2 deletions imagify.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Imagify
* Plugin URI: https://wordpress.org/plugins/imagify/
* Description: Dramaticaly reduce image file sizes without losing quality, make your website load faster, boost your SEO and save money on your bandwidth using Imagify, the new most advanced image optimization tool.
* Version: 1.9.9
* Version: 1.9.10
* Requires PHP: 5.4
* Author: WP Media
* Author URI: https://wp-media.me/
Expand All @@ -20,7 +20,7 @@
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );

// Imagify defines.
define( 'IMAGIFY_VERSION', '1.9.9' );
define( 'IMAGIFY_VERSION', '1.9.10' );
define( 'IMAGIFY_SLUG', 'imagify' );
define( 'IMAGIFY_FILE', __FILE__ );
define( 'IMAGIFY_PATH', realpath( plugin_dir_path( IMAGIFY_FILE ) ) . '/' );
Expand Down
162 changes: 51 additions & 111 deletions inc/3rd-party/enable-media-replace/classes/Main.php
Original file line number Diff line number Diff line change
@@ -1,113 +1,103 @@
<?php
namespace Imagify\ThirdParty\EnableMediaReplace;

use Imagify\Traits\InstanceGetterTrait;
use Imagify_Enable_Media_Replace_Deprecated;
use Imagify_Filesystem;

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

/**
* Compat class for Enable Media Replace plugin.
*
* @since 1.6.9
* @author Grégory Viguier
* @since 1.6.9
*/
class Main extends \Imagify_Enable_Media_Replace_Deprecated {
use \Imagify\Traits\InstanceGetterTrait;

/**
* Class version.
*
* @var string
* @since 1.6.9
* @author Grégory Viguier
*/
const VERSION = '2.1';
class Main extends Imagify_Enable_Media_Replace_Deprecated {
use InstanceGetterTrait;

/**
* The media ID.
*
* @var int
* @since 1.9
* @access protected
* @author Grégory Viguier
* @var int
* @since 1.9
*/
protected $media_id;

/**
* The process instance for the current attachment.
*
* @var ProcessInterface
* @since 1.9
* @access protected
* @author Grégory Viguier
* @var ProcessInterface
* @since 1.9
*/
protected $process;

/**
* The path to the old backup file.
*
* @var string
* @since 1.6.9
* @access protected
* @author Grégory Viguier
* @var string
* @since 1.6.9
*/
protected $old_backup_path;

/**
* List of paths to the old webp files.
*
* @var array
* @since 1.9.8
* @access protected
* @author Grégory Viguier
* @var array
* @since 1.9.8
*/
protected $old_webp_paths = [];

/**
* Launch the hooks before the files and data are replaced.
*
* @since 1.6.9
* @author Grégory Viguier
* @since 1.6.9
* @since 1.9.10 The parameter changed from boolean to array. The method doesn’t return anything.
*
* @param bool $unfiltered Whether to allow filters when retrieving the file path.
* @return bool The same value.
* @param array $args An array containing the post ID.
*/
public function init( $unfiltered = true ) {
$this->media_id = (int) filter_input( INPUT_POST, 'ID' );
$this->media_id = max( 0, $this->media_id );

if ( ! $this->media_id || empty( $_FILES['userfile']['tmp_name'] ) ) {
$this->media_id = 0;
return $unfiltered;
}

$tmp_name = wp_unslash( $_FILES['userfile']['tmp_name'] );
public function init( $args = [] ) {
if ( is_array( $args ) && ! empty( $args['post_id'] ) ) {
$this->media_id = $args['post_id'];
} else {
// Backward compatibility.
$this->media_id = (int) filter_input( INPUT_POST, 'ID' );
$this->media_id = max( 0, $this->media_id );

if ( ! is_uploaded_file( $tmp_name ) ) {
$this->media_id = 0;
return $unfiltered;
if ( ! $this->media_id ) {
return;
}
}

// Store the old backup file path.
$this->get_process();

if ( ! $this->process ) {
$this->media_id = 0;
return $unfiltered;
return;
}

$this->old_backup_path = $this->process->get_media()->get_backup_path();

if ( ! $this->old_backup_path ) {
$this->media_id = 0;
return $unfiltered;
return;
}

// Store the old backup file path.
add_filter( 'emr_unique_filename', [ $this, 'store_old_backup_path' ], 10, 3 );
// Delete the old backup file.
/**
* Keep track of existing webp files.
*
* Whether the user chooses to rename the files or not, we will need to delete the current webp 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.
*/
foreach ( $this->process->get_media()->get_media_files() as $media_file ) {
$this->old_webp_paths[] = imagify_path_to_webp( $media_file['path'] );
}

// Delete the old backup file and old webp files.
add_action( 'imagify_before_auto_optimization', [ $this, 'delete_backup' ] );
add_action( 'imagify_not_optimized_attachment_updated', [ $this, 'delete_backup' ] );

return $unfiltered;
}


Expand All @@ -116,59 +106,10 @@ public function init( $unfiltered = true ) {
/** ----------------------------------------------------------------------------------------- */

/**
* When the user chooses to change the file name, store the old backup file path. This path will be used later to delete the file.
*
* @since 1.6.9
* @access public
* @author Grégory Viguier
*
* @param string $new_filename The new file name.
* @param string $current_path The current file path.
* @param int $post_id The attachment ID.
* @return string The same file name.
*/
public function store_old_backup_path( $new_filename, $current_path, $post_id ) {
if ( ! $this->media_id || $post_id !== $this->media_id ) {
return $new_filename;
}

$this->get_process();

if ( ! $this->process ) {
$this->media_id = 0;
return $new_filename;
}

$media = $this->process->get_media();
$backup_path = $media->get_backup_path();

if ( $backup_path ) {
$this->old_backup_path = $backup_path;

// Keep track of existing webp files.
$media_files = $media->get_media_files();

if ( $media_files ) {
foreach ( $media_files as $media_file ) {
$this->old_webp_paths[] = imagify_path_to_webp( $media_file['path'] );
}
}
} else {
$this->media_id = 0;
$this->old_backup_path = false;
$this->old_webp_paths = [];
}

return $new_filename;
}

/**
* Delete previous backup file. This is done after the images have been already replaced by Enable Media Replace.
* It will prevent having a backup file not corresponding to the current images.
* Delete previous backup file and webp files.
* This is done after the images have been already replaced by Enable Media Replace.
*
* @since 1.8.4
* @access public
* @author Grégory Viguier
* @since 1.8.4
*
* @param int $media_id The attachment ID.
*/
Expand All @@ -177,15 +118,16 @@ public function delete_backup( $media_id ) {
return;
}

$filesystem = \Imagify_Filesystem::get_instance();
$filesystem = Imagify_Filesystem::get_instance();

if ( $filesystem->exists( $this->old_backup_path ) ) {
// Delete old backup file.
$filesystem->delete( $this->old_backup_path );
$this->old_backup_path = false;
}

if ( $this->old_webp_paths ) {
// If the files have been renamed, delete old webp files.
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 = [];
Expand All @@ -200,9 +142,7 @@ public function delete_backup( $media_id ) {
/**
* Get the optimization process corresponding to the current media.
*
* @since 1.9
* @author Grégory Viguier
* @access protected
* @since 1.9
*
* @return ProcessInterface|bool False if invalid.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

class_alias( '\\Imagify\\ThirdParty\\EnableMediaReplace\\Main', '\\Imagify_Enable_Media_Replace' );

add_filter( 'emr_unfiltered_get_attached_file', [ \Imagify\ThirdParty\EnableMediaReplace\Main::get_instance(), 'init' ] );
add_filter( 'wp_handle_replace', [ \Imagify\ThirdParty\EnableMediaReplace\Main::get_instance(), 'init' ] );

endif;
Loading

0 comments on commit cff41d1

Please sign in to comment.