Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. patrickfreitasdev renamed this gist Dec 9, 2023. 1 changed file with 0 additions and 0 deletions.
  2. patrickfreitasdev created this gist Dec 7, 2023.
    95 changes: 95 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    <?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);