Created
August 5, 2023 20:31
-
-
Save wpmudev-sls/b955c16333999e92220366b0ff8b7184 to your computer and use it in GitHub Desktop.
smush-mu-fix-image-recheck-fail.php
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] Image Recheck Fix. | |
* Description: [Smush Pro] Fixes the issue of image recheck failure. | |
* Jira: SLS-5161 | |
* Author: Abdoljabbar Bakhshande | |
* Author URI: https://wpmudev.com | |
* License: GPLv2 or later | |
* | |
* @package WordPress | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
// If the WordPress base file is not defined, halt execution. | |
exit; | |
} | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
// If the site is accessed via WP-CLI, do not run. | |
return; | |
} | |
add_action( 'after_setup_theme', 'smush_mu_fix_image_recheck_fail', 100 ); | |
/** | |
* The function for fixing the image recheck failure. | |
*/ | |
function smush_mu_fix_image_recheck_fail() { | |
// Check if the Smush plugin is activated. | |
if ( class_exists( 'WP_Smush' ) ) { | |
// Get the MD5 hash of the file during the last run. | |
$old_md5 = get_site_option( 'smush-mu-fix-recheck-fail' ); | |
// Define the file path of the file to be checked. | |
$original_file = WP_PLUGIN_DIR . '/wp-smush-pro/core/media-library/class-media-library-scan-background-process.php'; | |
// Check if the defined file exists. | |
if ( file_exists( $original_file ) ) { | |
// Get the MD5 hash of the current file. | |
$md5 = md5_file( $original_file ); | |
// Compare the old and the new MD5. | |
if ( $old_md5 !== $md5 ) { | |
// Get the contents of the file. | |
$content = file_get_contents( $original_file ); | |
// Make sure the file has content. | |
if ( $content ) { | |
// Define the pattern to find in the file contents. | |
$pattern = '/(protected function attempt_restart_during_health_check\(\) \{\\s*return) false;/'; | |
// Define what to replace the pattern with. | |
$replacement = '$1 true;'; | |
// Replace the pattern in the file contents. | |
$new_content = preg_replace( $pattern, $replacement, $content ); | |
// Make sure the content was actually replaced. | |
if ( $new_content && $new_content !== $content ) { | |
// Overwrite the original file with the new contents. | |
file_put_contents( $original_file, $new_content ); | |
// Get the new MD5 of the modified file. | |
$md5 = md5_file( $original_file ); | |
// Update the stored MD5 with the new MD5. | |
update_site_option( 'smush-mu-fix-recheck-fail', $md5 ); | |
} | |
} | |
} | |
} | |
} | |
} | |
// Reduce the cron interval directly to 3. | |
add_filter( 'wp_smush_background_scan_process_cron_interval', function( $interval ) { | |
return 3; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment