Last active
October 16, 2019 19:15
-
-
Save wpmudev-sls/6744b45f2186f3608aa74a52bbbd3d59 to your computer and use it in GitHub Desktop.
[Smush Pro] - Try to Resize Image On WPEngine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Smush Pro] - Try to Resize Image On WPEngine | |
* Description: [Smush Pro] - Try to Resize Image On WPEngine - 1141991506114532 | |
* Version: 1.1 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_sm_try_to_resize_image_on_wpengine', 100 ); | |
function wpmudev_sm_try_to_resize_image_on_wpengine() { | |
if( defined('WP_SMUSH_VERSION') && class_exists( 'Smush\WP_Smush' ) ){ | |
// return if disabled resize or is not WP Engine | |
if( ! ( empty( $_SERVER['IS_WPE'] ) && empty( $_SERVER['IS_WPE_SNAPSHOT'] ) ) && ! ( Smush\Core\Settings::get_instance()->get( 'original' ) && Smush\Core\Settings::get_instance()->get( 'resize' ) ) ){ | |
return; | |
} | |
class WPMUDEV_SM_Resize_Image_On_WPEngine{ | |
private $on_duplicating_images; | |
private $resized_meta = []; | |
public function __construct(){ | |
add_filter( 'update_post_metadata', array( $this, 'maybe_try_to_resize_image_again'), 10, 4 ); | |
add_filter( 'wp_update_attachment_metadata', array( $this, 'update_attachment_metadata'), 10, 2 ); | |
if( $this->is_wpml_duplicating_images() ){ | |
add_action('wp_smush_image_optimised', array( $this, 'disable_resize_on_duplicate' ), 9 ); | |
add_action('wp_smush_image_optimised', array( $this, 'enable_resize_on_wpengine' ), 11 ); | |
} | |
} | |
public function maybe_try_to_resize_image_again( $check, $id, $meta_key, $savings ){ | |
if( WP_SMUSH_PREFIX . 'resize_savings' === $meta_key && ! $this->on_duplicating_images ){ | |
if( isset( $savings['mu-tried-resize'] ) ){ | |
return $check; | |
} | |
if( isset( $savings['bytes'], $savings['size_before'] ) && $savings['bytes'] === $savings['size_before'] ){ | |
// maybe try to resize image again | |
$meta = wp_get_attachment_metadata( $id ); | |
$file_path = Smush\Core\Helper::get_attached_file( $id ); | |
$original_file_size = filesize( $file_path ); | |
$resize = $this->perform_resize( $file_path, $original_file_size, $id, $meta ); | |
// If resize wasn't successful. | |
if ( ! $resize || $resize['filesize'] >= $original_file_size ) { | |
remove_filter( 'update_post_metadata', array( $this, 'maybe_try_to_resize_image_again'), 10, 4 ); | |
update_post_meta( $id, WP_SMUSH_PREFIX . 'resize_savings', $savings ); | |
add_filter( 'update_post_metadata', array( $this, 'maybe_try_to_resize_image_again'), 10, 4 ); | |
if( ! $resize ){ | |
error_log( sprintf("The image wasn't resized(%d)", $id) ); | |
} | |
return $meta; | |
} | |
// Else Replace the Original file with resized file. | |
$replaced = $this->replace_original_image( $file_path, $resize, $meta ); | |
if ( $replaced ) { | |
// Clear Stat Cache, Else the size obtained is same as the original file size. | |
clearstatcache(); | |
// Updated File size. | |
$u_file_size = filesize( $file_path ); | |
$savings['bytes'] = $original_file_size > $u_file_size ? $original_file_size - $u_file_size : 0; | |
$savings['size_before'] = $original_file_size; | |
$savings['size_after'] = $u_file_size; | |
// Store savings in metadata. | |
if ( ! empty( $savings ) ) { | |
remove_filter( 'update_post_metadata', array( $this, 'maybe_try_to_resize_image_again'), 10, 4 ); | |
update_post_meta( $id, WP_SMUSH_PREFIX . 'resize_savings', $savings ); | |
add_filter( 'update_post_metadata', array( $this, 'maybe_try_to_resize_image_again'), 10, 4 ); | |
} | |
// save resized id | |
$this->resized_meta['id'] = $id; | |
$this->resized_meta['file_path'] = $file_path; | |
/** | |
* Called after the image has been successfully resized | |
* Can be used to update the stored stats | |
*/ | |
do_action( 'wp_smush_image_resized', $id, $savings ); | |
} | |
} | |
} | |
return $check; | |
} | |
public function update_attachment_metadata( $data, $attachment_id ){ | |
if( $this->resized_meta && $this->resized_meta['id'] === $attachment_id ){ | |
$file_path = isset( $this->resized_meta['file_path'] ) ? $this->resized_meta['file_path'] : Smush\Core\Helper::get_attached_file( $attachment_id ); | |
$this->resized_meta = []; | |
$size = getimagesize( $file_path ); | |
if( $size ){ | |
$data['width'] = $size[0]; | |
$data['height'] = $size[1]; | |
} | |
} | |
return $data; | |
} | |
private function replace_original_image( $file_path, $resized, $meta ) { | |
$replaced = @copy( $resized['file_path'], $file_path ); | |
$this->maybe_unlink( $resized['file_path'], $meta ); | |
return $replaced; | |
} | |
public function perform_resize( $file_path, $original_file_size, $id, $meta = '' ){ | |
$resize = Smush\WP_Smush::get_instance()->core()->mod->resize; | |
$sizes = apply_filters( | |
'wp_smush_resize_sizes', | |
array( | |
'width' => $resize->max_w, | |
'height' => $resize->max_h, | |
), | |
$file_path, | |
$id | |
); | |
$data = image_make_intermediate_size( $file_path, $sizes['width'], $sizes['height'] ); | |
// If the image wasn't resized. | |
if ( empty( $data['file'] ) || is_wp_error( $data ) ) { | |
return false; | |
} | |
// Check if file size is lesser than original image. | |
$resize_path = path_join( dirname( $file_path ), $data['file'] ); | |
if ( ! file_exists( $resize_path ) ) { | |
return false; | |
} | |
$data['file_path'] = $resize_path; | |
$file_size = filesize( $resize_path ); | |
$data['filesize'] = $file_size; | |
if ( $file_size > $original_file_size ) { | |
$unlink = true; | |
// maybe try to check smushed file | |
$smush = Smush\WP_Smush::get_instance()->core()->mod->smush; | |
$file_stats = $smush->do_smushit( $file_path ); | |
$resize_stats = $smush->do_smushit( $resize_path ); | |
if( ! is_wp_error( $resize_stats ) && ! is_wp_error( $file_stats ) ){ | |
if( $resize_stats['data']->after_size < $file_stats['data']->after_size ){ | |
$unlink = false; | |
$data['filesize'] = $resize_stats['data']->after_size; | |
}elseif( defined("WP_DEBUG") && WP_DEBUG ){ | |
error_log(sprintf(__("The resized file(%s) seems large than original ones, keep the original ones.",'wpmudev'), $id ) ); | |
} | |
}else{ | |
error_log( sprintf("Error while trying to smush image (%d)", $id) ); | |
} | |
if ( $unlink ) { | |
$this->maybe_unlink( $resize_path, $meta ); | |
} | |
} | |
return $data; | |
} | |
public function disable_resize_on_duplicate(){ | |
$this->on_duplicating_images = true; | |
} | |
public function enable_resize_on_wpengine(){ | |
$this->on_duplicating_images = false; | |
} | |
private function is_wpml_duplicating_images() { | |
if ( ! class_exists( 'SitePress' ) ) { | |
return false; | |
} | |
$media_settings = get_site_option( '_wpml_media' ); | |
// Check if WPML media translations are active. | |
if ( ! $media_settings || ! isset( $media_settings['new_content_settings']['duplicate_media'] ) ) { | |
return false; | |
} | |
// WPML duplicate existing media for translated content? | |
if ( ! $media_settings['new_content_settings']['duplicate_media'] ) { | |
return false; | |
} | |
return true; | |
} | |
private function update_smush_stats_single( $id, $smush_stats, $image_size = '' ) { | |
// Return, if we don't have image id or stats for it. | |
if ( empty( $id ) || empty( $smush_stats ) || empty( $image_size ) ) { | |
return; | |
} | |
$smush = Smush\WP_Smush::get_instance()->core()->mod->smush; | |
$data = $smush_stats['data']; | |
// Get existing Stats. | |
$stats = get_post_meta( $id, Smush\Core\Modules\Smush::$smushed_meta_key, true ); | |
// Update existing Stats. | |
if ( ! empty( $stats ) ) { | |
// Update stats for each size. | |
if ( isset( $stats['sizes'] ) ) { | |
// if stats for a particular size doesn't exists. | |
if ( empty( $stats['sizes'][ $image_size ] ) ) { | |
// Update size wise details. | |
$stats['sizes'][ $image_size ] = (object) $smush->_array_fill_placeholders( $smush->_get_size_signature(), (array) $data ); | |
} else { | |
// Update compression percent and bytes saved for each size. | |
$stats['sizes'][ $image_size ]->bytes = $stats['sizes'][ $image_size ]->bytes + $data->bytes_saved; | |
$stats['sizes'][ $image_size ]->percent = $stats['sizes'][ $image_size ]->percent + $data->compression; | |
} | |
} | |
} else { | |
// Create new stats. | |
$stats = array( | |
'stats' => array_merge( | |
$smush->_get_size_signature(), | |
array( | |
'api_version' => - 1, | |
'lossy' => - 1, | |
) | |
), | |
'sizes' => array(), | |
); | |
$stats['stats']['api_version'] = $data->api_version; | |
$stats['stats']['lossy'] = $data->lossy; | |
$stats['stats']['keep_exif'] = ! empty( $data->keep_exif ) ? $data->keep_exif : 0; | |
// Update size wise details. | |
$stats['sizes'][ $image_size ] = (object) $smush->_array_fill_placeholders( $smush->_get_size_signature(), (array) $data ); | |
} | |
// Calculate the total compression. | |
$stats = $smush->total_compression( $stats ); | |
update_post_meta( $id, Smush\Core\Modules\Smush::$smushed_meta_key, $stats ); | |
} | |
private function maybe_unlink( $path, $meta ) { | |
if ( empty( $path ) ) { | |
return true; | |
} | |
// Unlink directly if meta value is not specified. | |
if ( empty( $meta['sizes'] ) ) { | |
@unlink( $path ); | |
} | |
$unlink = true; | |
// Check if the file name is similar to one of the image sizes. | |
$path_parts = pathinfo( $path ); | |
$filename = ! empty( $path_parts['basename'] ) ? $path_parts['basename'] : $path_parts['filename']; | |
if ( ! empty( $meta['sizes'] ) ) { | |
foreach ( $meta['sizes'] as $image_size ) { | |
if ( false === strpos( $image_size['file'], $filename ) ) { | |
continue; | |
} | |
$unlink = false; | |
} | |
} | |
if ( $unlink ) { | |
@unlink( $path ); | |
} | |
return true; | |
} | |
} | |
$run = new WPMUDEV_SM_Resize_Image_On_WPEngine; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment