Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[NOT CONFIRMED YET] - [Smush] - Support blogs.dir for single attachments. Smush single images uploaded to blogs.dir
<?php
/**
* Plugin Name: [Smush] - Support blogs.dir for single attachments
* Plugin URI: https://premium.wpmudev.org/
* Description: Smush single images uploaded to blogs.dir
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Smush_Blogs_Dir' ) ) {
class WPMUDEV_Smush_Blogs_Dir {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Smush_Blogs_Dir();
}
return self::$_instance;
}
private function __construct() {
if ( ! class_exists( 'WP_Smush' ) ) {
return;
}
add_action( "wp_update_attachment_metadata", array( $this, 'bulk_smush_single_attachment' ),20 ,2 );
}
public function bulk_smush_single_attachment( $original_meta, $attachment_id ) {
$attachment_id = (int) $attachment_id;
if ( ! wp_attachment_is_image( $attachment_id ) ) {
return $original_meta;
}
$file_path = WP_Smush_Helper::get_attached_file( $attachment_id );
if ( empty( $file_path ) || strpos( $file_path, DIRECTORY_SEPARATOR . 'blogs.dir' . DIRECTORY_SEPARATOR ) === false ) {
return $original_meta;
}
$smush = WP_Smush::get_instance()->core()->mod->smush;
//$original_meta = wp_get_attachment_metadata( $attachment_id, true );
$_REQUEST['is_bulk_resmush'] = true;
// Download if not exists.
do_action( 'smush_file_exists', $file_path, $attachment_id );
$smush->check_animated_status( $file_path, $attachment_id );
WP_Smush::get_instance()->core()->mod->backup->create_backup( $file_path, '', $attachment_id );
// Proceed only if Smushing Transient is not set for the given attachment id.
if ( ! get_option( 'smush-in-progress-' . $attachment_id, false ) ) {
// Set a transient to avoid multiple request.
update_option( 'smush-in-progress-' . $attachment_id, true );
/**
* Resize the dimensions of the image.
*
* Filter whether the existing image should be resized or not
*
* @since 2.3
*
* @param bool $should_resize Set to True by default.
* @param int $attachment_id Image Attachment ID.
*/
if ( $should_resize = apply_filters( 'wp_smush_resize_media_image', true, $attachment_id ) ) {
$updated_meta = $smush->resize_image( $attachment_id, $original_meta );
$original_meta = ! empty( $updated_meta ) ? $updated_meta : $original_meta;
}
$original_meta = WP_Smush::get_instance()->core()->mod->png2jpg->png_to_jpg( $attachment_id, $original_meta );
$smush_response = $smush->resize_from_meta_data( $original_meta, $attachment_id );
wp_update_attachment_metadata( $attachment_id, $original_meta );
}
// Delete transient.
delete_option( 'smush-in-progress-' . $attachment_id );
$smush_data = get_post_meta( $attachment_id, WP_Smushit::$smushed_meta_key, true );
$resize_savings = get_post_meta( $attachment_id, WP_SMUSH_PREFIX . 'resize_savings', true );
$conversion_savings = WP_Smush_Helper::get_pngjpg_savings( $attachment_id );
$stats = array(
'count' => ! empty( $smush_data['sizes'] ) ? count( $smush_data['sizes'] ) : 0,
'size_before' => ! empty( $smush_data['stats'] ) ? $smush_data['stats']['size_before'] : 0,
'size_after' => ! empty( $smush_data['stats'] ) ? $smush_data['stats']['size_after'] : 0,
'savings_resize' => $resize_savings > 0 ? $resize_savings : 0,
'savings_conversion' => $conversion_savings['bytes'] > 0 ? $conversion_savings : 0,
'is_lossy' => ! empty( $smush_data ['stats'] ) ? $smush_data['stats']['lossy'] : false,
);
if ( isset( $smush_response ) && is_wp_error( $smush_response ) ) {
$error_message = $smush_response->get_error_message();
// Check for timeout error and suggest to filter timeout.
if ( strpos( $error_message, 'timed out' ) ) {
$error = 'timeout';
$error_message = esc_html__( "Timeout error. You can increase the request timeout to make sure Smush has enough time to process larger files. `define('WP_SMUSH_API_TIMEOUT', 150);`", 'wp-smushit' );
}
$error = isset( $error ) ? $error : 'other';
if ( ! empty( $error_message ) ) {
// Used internally to modify the error message.
$error_message = WP_Smush_Helper::filter_error( $error_message, $attachment_id, $error );
}
}
// Check if a resmush request, update the resmush list.
if ( ! empty( $_REQUEST['is_bulk_resmush'] ) && 'false' !== $_REQUEST['is_bulk_resmush'] && $_REQUEST['is_bulk_resmush'] ) {
$smush->update_resmush_list( $attachment_id );
}
// Runs after a image is successfully smushed.
do_action( 'image_smushed', $attachment_id, $stats );
// Update the bulk Limit count.
WP_Smush_Core::update_smush_count();
return $original_meta;
}
}
if ( ! function_exists( 'wpmudev_smush_blogs_dir' ) ) {
function wpmudev_smush_blogs_dir(){
return WPMUDEV_Smush_Blogs_Dir::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_smush_blogs_dir', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.