Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active October 16, 2019 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/324d124f409222e465075b73071fb836 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/324d124f409222e465075b73071fb836 to your computer and use it in GitHub Desktop.
[Smush Pro] - Custom Image Editor
<?php
/**
* Plugin Name: [Smush Pro] - Custom Image Editor
* Description: [Smush Pro] - Custom Image Editor - 1141991506114532
* Author: Thobk @ WPMUDEV
* Version: 1.1
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Please custom priority Image Editor lib by define SMUSH_IMAGE_EDITOR_FOR_RESIZE for resize or SMUSH_IMAGE_EDITOR_FOR_PNG2JPG for PNG2JPG (Imagick | GD | WP_Image_Editor_Imagick || WP_Image_Editor_GD);
*
*/
add_action( 'after_setup_theme', 'wpmudev_smpro_custom_image_editor_func', 100 );
function wpmudev_smpro_custom_image_editor_func() {
if( defined('WP_SMUSH_VERSION') && class_exists( 'Smush\WP_Smush' ) ){
// return if disabled resize or png2jpg
if( ! ( Smush\Core\Settings::get_instance()->get( 'resize' ) || Smush\Core\Settings::get_instance()->get( 'png_to_jpg' ) ) ){
return;
}
class WPMUDEV_SMPro_Custom_Image_Editor{
private $smush_log_file = 'wpmudev-smush-log.php';
public function __construct(){
add_filter( 'wp_smush_resize_sizes', array( $this, 'should_custom_editor_for_resize' ), 100, 3 );
add_filter( 'wp_smush_convert_to_jpg', array( $this, 'should_custom_editor_for_png2jpg' ), 100, 4 );
}
public function should_custom_editor_for_resize( $should_resize, $id, $meta ){
if( $should_resize ){
add_filter( 'wp_image_editors', array( $this, 'custom_editor_for_resize' ), 100 );
}
return $should_resize;
}
public function custom_editor_for_resize( $image_editors ){
if( defined('SMUSH_IMAGE_EDITOR_FOR_RESIZE') && SMUSH_IMAGE_EDITOR_FOR_RESIZE ){
$editor_name = SMUSH_IMAGE_EDITOR_FOR_RESIZE;
if( strpos($editor_name, 'WP_Image_Editor') === false ){
$editor_name = 'WP_Image_Editor_'. $editor_name;
}
if( $pos = array_search($editor_name, $image_editors ) ){
unset( $image_editors[ $pos ] );
array_unshift( $image_editors, $editor_name );
remove_filter( 'wp_image_editors', array( $this, 'custom_editor_for_resize' ), 100 );
}
}
return $image_editors;
}
public function should_custom_editor_for_png2jpg( $should_convert, $id, $file, $size ){
if( $should_convert ){
add_filter( 'wp_image_editors', array( $this, 'custom_editor_for_png2jpg' ), 100 );
}
return $should_convert;
}
public function custom_editor_for_png2jpg( $image_editors ){
if( defined('SMUSH_IMAGE_EDITOR_FOR_PNG2JPG') && SMUSH_IMAGE_EDITOR_FOR_PNG2JPG && $pos = array_search(SMUSH_IMAGE_EDITOR_FOR_PNG2JPG, $image_editors ) ){
$editor_name = SMUSH_IMAGE_EDITOR_FOR_PNG2JPG;
if( strpos($editor_name, 'WP_Image_Editor') === false ){
$editor_name = 'WP_Image_Editor_'. $editor_name;
}
if( $pos = array_search($editor_name, $image_editors ) ){
unset( $image_editors[ $pos ] );
array_unshift( $image_editors, $editor_name );
remove_filter( 'wp_image_editors', array( $this, 'custom_editor_for_png2jpg' ), 100 );
}
}
return $image_editors;
}
}
$run = new WPMUDEV_SMPro_Custom_Image_Editor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment