• Love the concept of this plugin!

    This is referring to Elementor’s clear_cache hook. It would be very helpful to have this added:

    \Elementor\Plugin::instance()->files_manager->clear_cache();

    LINK

Viewing 15 replies - 1 through 15 (of 17 total)
  • Thread Starter ronr1999

    (@ronr1999)

    Looks like this addition to caching-plugins.php file will work:

    
    /**
     * Return an array of plugin slugs that exist on this install.
     * If $clear_cache is true or not set, run functions.
     */
    function ccfm_clear_addtional_cache( $clear_cache = true ) {
        $plugin_slugs = [];
    
        if ( class_exists( 'PremiumAddons\Admin\Includes\Admin_Helper' ) &&
             method_exists(PremiumAddons\Admin\Includes\Admin_Helper::class, 'get_instance') && 
             method_exists(PremiumAddons\Admin\Includes\Admin_Helper::class, 'delete_assets_options') ) {
                $plugin_slugs[] = 'premium-addons-for-elementor';
                if ( $clear_cache ) {
                    $admin_helper = PremiumAddons\Admin\Includes\Admin_Helper::get_instance();
                    $admin_helper->delete_assets_options();
                }
        }
    	
        // Check if Elementor is loaded
        if ( did_action( 'elementor/loaded' ) ) {
    	error_log("Elementor Clear Cache has Run!");
            // Automatically purge and regenerate the Elementor CSS cache
            \Elementor\Plugin::instance()->files_manager->clear_cache();
            $plugin_slugs[] = 'elementor';
        }	
    
        return $plugin_slugs; 
    }

    Thread Starter ronr1999

    (@ronr1999)

    I see a problem. This Elementor Clear Cache needs to run BEFORE clearing any page caching plugin.

    Plugin Author webheadcoder

    (@webheadllc)

    Hi @ronr1999,

    Thanks for finding this cache clearing function! I don’t seem to have that option in my Elementor (free version) so I can’t try it out right now. Your code looks good, but are you saying it doesn’t work and it needs to be run before the other caching plugins? Why is that?

    Thread Starter ronr1999

    (@ronr1999)

    Thanks for the response. I moved it to the clear-cache-for-widgets.php page and it’s working. It needs to clear the Elementor css files and then clear caching.

    function ccfm_clear_cache_for_all() {
    	
    	// Check if Elementor is loaded
        if ( did_action( 'elementor/loaded' ) ) {
            // Automatically purge and regenerate the Elementor CSS cache
            \Elementor\Plugin::instance()->files_manager->clear_cache();
            $plugin_slugs[] = 'elementor';
        }	
    
        global $wp_fastest_cache, $kinsta_cache, $admin, $ccfm_source;
    
        if ( empty( $ccfm_source ) ) {
            $ccfm_source = '';
        }

    When there is an Elementor plugin update it does a Regenerate but caching has not cleared. Regenerate removes all of the Elementor CSS files (wp-content/uploads/elementor/css) and on first page load it regenerates the files. If there is caching active, then the page will not recognize the new css files, especially if you’re doing css minification. So cache has to be cleared every time Elementor is updated.

    It just occurred to me, is your code also clearing minification? I’ve used WP-Optimize with minification but am now turning that off and moving to WP Super Cache.

    Thanks again, it would be awesome to have the incorporate in a new release of the plugin. I know the Elementor community would love you for it!

    Plugin Author webheadcoder

    (@webheadllc)

    Thank you very much for the explanation and updated code. I’ll definitely be adding this to the plugin after I try it out.

    I just took a look at WP Optimize and it doesn’t look like it clears the minification. I’ll need to add that as well.

    Thread Starter ronr1999

    (@ronr1999)

    If you have a test site, I’m happy to install a copy of Elementor Pro.

    Plugin Author webheadcoder

    (@webheadllc)

    I found the setting and code in the free version so I’ll try it out first. Thanks for the offer though!

    Plugin Author webheadcoder

    (@webheadllc)

    Could you try adding your code to the ccfm_clear_addtional_cache function again? I tried the following steps and it cleared the css cache. I tried with Breeze and WP Super Cache. Let me know if I’m missing anything.

    1. Rollback Elementor to a previous version (using WP Rollback plugin)
    2. Visit a public page edited with Elementor (this creates the css cache)
    3. Update Elementor
    4. CSS cache should be cleared.

    I added one additional check in the code so it only runs when cache is meant to be cleared:

    function ccfm_clear_addtional_cache( $clear_cache = true ) {
        $plugin_slugs = [];
    
    
    ...
    
    
    	// Check if Elementor is loaded
        if ( $clear_cache && did_action( 'elementor/loaded' ) ) {
            error_log('elementor loaded');
            // Automatically purge and regenerate the Elementor CSS cache
            \Elementor\Plugin::instance()->files_manager->clear_cache();
            $plugin_slugs[] = 'elementor';
        }
    
        return $plugin_slugs;
    }

    Thread Starter ronr1999

    (@ronr1999)

    I found an easier way to test. I go into file manager and delete the css files here: /wp-content/uploads/elementor/css

    This simulates what the Elementor community has been seeing for the past couple of years. This seems to happen when Elementor releases a new version. The web pages are missing its css so render is messed up.

    I tried your above edits but doesn’t seem to work when css files are missing. My original fix – adding to: clear-cache-for-widgets.php > ccfm_clear_cache_for_all is working fine. Elementor Regenerate needs to run before clearing cache. Or am I confused and your above code should also be installed… ?

    • This reply was modified 10 months, 2 weeks ago by ronr1999.
    Plugin Author webheadcoder

    (@webheadllc)

    I think I see the issue. The ccfm_clear_cache_for_all function runs in the ‘shutdown’ hook so if a function takes too long within a hook, the hook isn’t run due to time constraints.

    You can add the code to the beginning or end of ccfm_clear_cache_for_all and it will run. But if you add it in a function that gets called from a hook (i.e. in ccfm_clear_addtional_cache), it may not run. It depends on the caching plugin.

    Can you try placing your code at the end of ccfm_clear_cache_for_all()? I want to make sure it works for you too.

    Thread Starter ronr1999

    (@ronr1999)

    Yes, it’s working fine at the end of: ccfm_clear_cache_for_all?

    Seems like winner!

    Thread Starter ronr1999

    (@ronr1999)

    I thought of another enhancement that would be great. How about a log of when this is activated. I know that manually clicking on “Clear Cache for Me” gives the Success banner, but when it’s running in the background, it’s an unknown if it’s working or not.

    Plugin Author webheadcoder

    (@webheadllc)

    That’s great news… and yet not great news. so any hooks in that function are not guaranteed to run.

    I’ll think about the log. I have a last run time right now. I’m not sure if people would care about reviewing cache clearing activity. I get what you mean though, it’s hard to know when it’s doing its thing.

    Thank you very much for trying it out and working through this with me. I’ll have the code in the next update.

    Thread Starter ronr1999

    (@ronr1999)

    I’ve test everything that I can think of and it’s working. The key is that this works when a plugin is updated – especially Elementor.

    Again, thanks for all your help. I know that this will benefit a ton of folks – Elementor now has over 5 million plugin downloads. Yikes!

    Ok, here’s my latest. As a test, I’m writing to the error_log and sending myself an email. Those are working.

    /**
     * Clear the caches!
     */
    function ccfm_clear_cache_for_all() {
    	
        global $wp_fastest_cache, $kinsta_cache, $admin, $ccfm_source;
    
        if ( empty( $ccfm_source ) ) {
            $ccfm_source = '';
        }
    
        do_action( 'ccfm_clear_cache_for_me_before', $ccfm_source );
    
    	// Set the timezone to Pacific Standard Time (PST)
    	date_default_timezone_set('America/Los_Angeles');
    
    	// Check if Elementor is loaded
    	if (did_action('elementor/loaded')) {
    		// Automatically purge and regenerate the Elementor CSS cache
    		\Elementor\Plugin::instance()->files_manager->clear_cache();
    		$plugin_slugs[] = 'elementor';
    
    		// Log message
    		$timestamp = date("d-M-Y H:i:s T");
    		$log_message = "[{$timestamp}] Elementor Clear Cache has Run!";
    		error_log($log_message . PHP_EOL, 3, "/home/tfcahost/public_html/cache/error_log");
    
    		// Send email notification
    		$to = "[email protected]"; // recipient's email address
    		$subject = "Elementor Cache Cleared";
    		$message = "Elementor cache has been cleared at {$timestamp} PST.";
    		$headers = "From: [email protected]"; // Replace with the sender's email address
    
    		// Check if the email is sent successfully
    		if (mail($to, $subject, $message, $headers)) {
    			error_log("Email notification sent successfully." . PHP_EOL, 3, "/home/tfcahost/public_html/cache/error_log");
    		} else {
    			error_log("Failed to send email notification." . PHP_EOL, 3, "/home/tfcahost/public_html/cache/error_log");
    		}
    	}
    	
    Thread Starter ronr1999

    (@ronr1999)

    With my latest update, with outgoing emails, I’m feeling that it’s like a client’s website heartbeat. I manage 32 websites and these emails give me a sense of the overall pulse of these sites. Might be nice to have more details in the emails, like plugin updates, page/post updates, etc. I’ll have to look more closely at the code.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Clear Elementor’s CSS and Data Cache’ is closed to new replies.