Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save patrickfreitasdev/99bd8bb58a6c97fda05a22aa7a86c119 to your computer and use it in GitHub Desktop.
Save patrickfreitasdev/99bd8bb58a6c97fda05a22aa7a86c119 to your computer and use it in GitHub Desktop.
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WDEV_Smush_Fix_Keep_Showing_Resmush {
private $smush_settings;
private $webp_helper;
private $webp_dir;
private $media_item_cache;
public $exclude_size_names = array( 'full' );
public function __construct() {
$this->smush_settings = Smush\Core\Settings::get_instance();
$should_optimize_original = $this->smush_settings->get( 'original' );
if ( ! $this->smush_settings->is_webp_module_active() || ! $should_optimize_original ) {
return;
}
$this->webp_helper = new Smush\Core\Webp\Webp_Helper();
$this->webp_dir = new Smush\Core\Webp\Webp_Dir();
$this->media_item_cache = Smush\Core\Media\Media_Item_Cache::get_instance();
add_filter( 'wp_smush_scan_library_slice_handle_attachment', array( $this, 'maybe_disable_local_webp' ), 0, 2 );
add_filter( 'wp_smush_scan_library_slice_handle_attachment', array( $this, 'maybe_revert_local_webp_status' ), 999, 2 );
}
public function maybe_disable_local_webp( $slice_data, $attachment_id ) {
if ( $this->is_webp_converted( $attachment_id ) ) {
$this->temporary_disable_local_webp();
}
return $slice_data;
}
private function is_webp_converted ( $attachment_id ) {
if ( $this->webp_helper->get_webp_flag( $attachment_id ) ) {
return true;
}
return $this->is_all_thumbnail_sizes_converted_to_webp( $attachment_id );
}
private function is_all_thumbnail_sizes_converted_to_webp( $attachment_id ) {
$media_item = $this->media_item_cache->get( $attachment_id );
foreach ( $media_item->get_sizes() as $size ) {
if ( in_array( $size->get_key(), $this->exclude_size_names ) ) {
continue;
}
$webp_file_path = $this->webp_helper->get_webp_file_path( $size->get_file_path() );
if ( ! file_exists( $webp_file_path ) ) {
$webp_file_path = false;
break;
}
}
if ( empty( $webp_file_path ) ) {
return false;
}
// Update webp flag.
$relative_path = substr( $webp_file_path, strlen( $this->webp_dir->get_webp_path() . '/' ) );
$this->webp_helper->update_webp_flag( $attachment_id, $relative_path );
return true;
}
public function maybe_revert_local_webp_status( $slice_data, $attachment_id ) {
if ( has_filter( 'pre_update_option_wp-smush-settings', array( $this, 'temporary_disable_save_settings') ) ) {
$this->revert_local_webp_status();
}
return $slice_data;
}
private function temporary_disable_local_webp() {
add_filter( 'pre_update_site_option_wp-smush-settings', array( $this, 'temporary_disable_save_settings' ), 10, 2 );
add_filter( 'pre_update_option_wp-smush-settings', array( $this, 'temporary_disable_save_settings' ), 10, 2 );
$this->smush_settings->set( 'webp_mod', false );
}
private function revert_local_webp_status() {
$this->smush_settings->set( 'webp_mod', true );
remove_filter( 'pre_update_site_option_wp-smush-settings', array( $this, 'temporary_disable_save_settings' ), 10, 2 );
remove_filter( 'pre_update_option_wp-smush-settings', array( $this, 'temporary_disable_save_settings' ), 10, 2 );
}
public function temporary_disable_save_settings( $value, $old_value ) {
return $old_value;
}
}
add_action( 'plugins_loaded', function() {
if ( ! class_exists('WP_Smush' )
|| ! class_exists('Smush\Core\Settings' )
|| ! class_exists( 'Smush\Core\Webp\Webp_Helper' )
|| ! class_exists( 'Smush\Core\Media\Media_Item_Cache' )
|| ! class_exists( 'Smush\Core\Webp\webp_dir' )
) {
return;
}
new WDEV_Smush_Fix_Keep_Showing_Resmush();
}, 99);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment