keycdn / cache-enabler

A lightweight caching plugin for WordPress that makes your website faster by generating static HTML files.
https://wordpress.org/plugins/cache-enabler/
123 stars 46 forks source link

Bypass cache filter allow true/false values #258

Closed pacotole closed 3 years ago

pacotole commented 3 years ago

Current 'cache_enabler_bypass_cache' filter only allow "to force bypass" the cache:

        // bypass cache hook
        if ( apply_filters( 'cache_enabler_bypass_cache', false ) ) {
            return true;
        }

In some scenarios we want "to force not to bypass" the cache. It only takes a small change:

        // bypass cache hook
        if ( null !== $bypass = apply_filters( 'cache_enabler_bypass_cache', null ) ) {
            return boolval( $bypass );
        }

If it's okay with you, I can make a pull request.

Note: A filter for the is_cacheable() function would also be great (e.g. I would like to cache the output of custom post that return data in json format).

coreykn commented 3 years ago

I can imagine a couple scenarios when this would be needed. In case there is a different approach and to better understand your setup, what bypass check(s) are you trying to avoid that is causing the page to be bypassed?

As for the potential hook in the Cache_Enabler_Engine::is_cacheable() method, would that be just JSON saved in an HTML file? As you may already know, the cache signature is appended later on (HTML comment in bottom of the page source), which may make that invalid if I'm understanding your use case correctly.

pacotole commented 3 years ago

In my case, a third party plugin is declaring DONOTCACHEPAGE constant but in some customized pages that don't change I would like to use 'cache_enabler_bypass_cache' filter to don't bypass cache.

About JSON ouput, you are right. I hadn't thought of the html comment.

coreykn commented 3 years ago

Is the third party plugin checking if it is defined first? If yes, would that allow you to define it as false beforehand on these customized pages to prevent them from being bypassed?