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 new cache clearing structure for post actions #129

Closed coreykn closed 4 years ago

coreykn commented 4 years ago

In with the new and out with the old. Strap in we are in for a big change. This will completely replace the old cache clearing structure for post actions, like publishing, updating, and trashing any post types.

Add a new pagination clear type to Cache_Enabler_Disk::delete_asset() to allow the pagination page(s) to be cleared. The pagination base path is currently obtained by using $GLOBALS['wp_rewrite']->pagination_base (this will get the updated value if it has been set to something other than the default page).

Add Cache_Enabler::clear_cache_on_post_save() to clear the page(s) cache when any published post type is created or updated.

Add Cache_Enabler::clear_associated_cache() to clear the page(s) cache associated with any published or updated post type. Currently this includes the following:

Add Cache_Enabler::on_save_post() to be called whenever the save_post hook is fired. This will update how the cache is cleared when any post type is published or updated. This should expand compatibility with other plugins, like other ecommerce solutions beyond WooCommerce, because the save_post hook will be fired whenever a post or page is created or updated (in any way that it was saved, like through the edit form in the WordPress admin or even an API). The default cache clearing behavior should clear the page cache whenever any post type is updated in any way. Not using the $update parameter because it returns true on new posts in some cases, like when the status changes from inherit to publish (in two separate save_hook fires). Still checking $post->post_date_gmt and $post->post_modified_gmt values are the same for a new post. Added checking if $post->post_date_gmt is also greater than $post->post_modified_gmt for scheduled posts (because scheduled posts are modified before they are published).

Add Cache_Enabler::on_post_updated() to be called whenever the post_updated hook is fired. This checks if the post author has changed. If it has changed then the authors archive cache is cleared for the last author. This is required to ensure both the new and old authors archives pages are cleared.

Add Cache_Enabler::on_transition_post_status() to clear the page cache by default if a published post status is updated to future, draft, pending, or private. The complete or associated cache can be cleared if the new setting below is enabled. This method has been added instead of using Cache_Enabler::on_save_post() because it has the old and new status, whereas the other method would only have the updated status. It will ensure cache clearing is only performed when a published post status has changed.

Add Cache_Enabler::_convert_settings() to change the old settings to the new settings. This is required to ensure the settings are retained on an update. The settings names were changed for improved understanding on what they are for and because we are moving towards better naming conventions. This will also make it easier when configuring Cache Enabler programmatically (like in the future when creating a new configure related subcommand for WP-CLI).

Add CSS for settings page to avoid inline CSS. No preprocessors used at this time to keep it simple as there is minimal required CSS.

Add rel="nofollow noopener" attribute to outbound href elements.

Update caching behavior to ignore query strings by default and deliver the cached page if any query string is present. Previously all query strings would bypass the cache unless included. This change was made because in most cases the cache should not be bypassed if a query string is present. A regular expression matching query strings can be set to bypass the cache (e.g. /.+/ could be set to bypass all query strings).

Update Cache_Enabler::create_advcache_settings() to handle creating and updating the advanced cache settings file (instead of getting the settings from the database and sending them to Cache_Enabler::validate_settings()).

Update Cache_Enabler::validate_settings() to update the advanced cache settings file after the settings have been validated. This fixes the issue where unvalidated settings would be saved to the advanced cache settings file as they were being recorded before being validated (#128). Validate the passed parameter as an array instead of checking if it is empty. Use empty() instead to check if the complete cache needs to be cleared as we just need to check if the item has been set and has a value. This would be the button value, which is currently Save Changes and Clear Cache or its translation.

Update Cache_Enabler::validate_regex() to use empty() instead of checking if it is an empty string. This fixes the edge case where // would be returned if the regular expression being checked was null (like if the database option was deleted).

Update Cache_Enabler::on_trash_post() to only clear the page and associated cache by default (like when any post type is published). Previously the entire cache was cleared when any post type was cleared. This is not done in Cache_Enabler::on_transition_post_status() because the wp_trash_post hook fires before the post is trashed, which we need to get the correct URL to clear (instead of https://www.example.com/hello-word__trashed).

Update Cache_Enabler::clear_page_cache_by_url() to not validate the string as this will be handled when validating the URL.

Update some get_home_url() functions to home_url() instead because we are calling the current site anyway.

Update variable naming to use "settings" instead of "options" to use the same naming across the entire code base.

Update settings page:

Remove Cache_Enabler::check_future_posts() because cache timeouts for future posts are no longer required because the save_post hook will be fired when the post is published (the post status goes from future to publish). This will prevent unnecessarily calling Cache_Enabler_Disk::delete_advcache_settings() every time the save_post hook is fired and there is not scheduled post. It will also prevent undefined index errors when using a client that does not have a $_SERVER['HTTP_HOST'] header, like using WP-CLI to create new posts (#68).

Remove Cache_Enabler::clear_home_page_cache() because this can now be done with Cache_Enabler::clear_page_cache_by_url() because of the recently added clear types for strict cache clearing.

Remove legacy database option rename (from cache to cache-enabler). This was added in version 1.2.1 in May 2017 (744625d). Only 5% of installs are still on version 1.2, which could be version 1.2.0, 1.2.1, 1.2.2, or 1.2.3. This means if a plugin updates from <= 1.2.0 will need to define the settings again (and would have an orphaned cache option in the database).

Remove cache minification constants because they are no longer required with the updated minification setting.

Fix Cache_Enabler::_minify_cache() issue that was introduced in version 1.4.0 during the refactoring of the code base (PR #84). This caused only the script tag to be ignored if minifying the inline JavaScript was disabled.

Closes #112 and fixes #128