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

add filter for plugin settings #298

Closed coreykn closed 2 years ago

coreykn commented 2 years ago

Add cache_enabler_settings_before_validation as a new filter to allow settings to be filtered before validation. The system settings, such as version and use_trailing_slashes, cannot be overwritten. This will allow any user setting to be forced, providing the ability to overwrite the plugin settings. This partially adds support for what was requested in PR #173, which provides a couple examples for when this will be useful, and then later discussed in #286 (thanks @ouun). Here is an example of how this can be used:

add_filter( 'cache_enabler_settings_before_validation', 'filter_cache_enabler_settings_before_validation' );

function filter_cache_enabler_settings_before_validation( $settings ) {

    $forced_settings = array(
        'convert_image_urls_to_webp' => 1,
        'compress_cache'             => 1,
        'minify_html'                => 1,
        'minify_inline_css_js'       => 1,
    );

    $settings = wp_parse_args( $forced_settings, $settings );

    return $settings;
}

This also updates Cache_Enabler::upgrade_settings() to remove incl_attributes when upgrading from a version that contained this setting before version 1.4. This somehow was never noticed until adding this filter. This does not change any behavior because it would have been dropped and never saved due to the settings validation. We remove it beforehand when upgrading the settings to more or less keep record of what has changed.