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

update permalink structure handling #251

Closed coreykn closed 3 years ago

coreykn commented 3 years ago

Update permalink structure handling to use the $use_trailing_slashes class property in the WP_Rewrite class instead of the Cache_Enabler::get_permalink_structure() method. This performs the check when that class is initialized and does it better as it uses substr() instead of preg_match(). I was unaware of this class property when creating this handling in the past. Deprecate the Cache_Enabler::get_permalink_structure() method and leave the permalink_structure setting in the settings file for now.

Update the URL in the Cache_Enabler::process_clear_cache_request() method to not have a scheme because this is no longer required since PR #237.

Update the cache iterator to reinitialize the rewrite rules if the blog has been switched. Switching the blog can occur in the cache iterator itself, or beforehand in Cache_Enabler::each_site(). That is why $GLOBALS['switched'] is being checked instead of doing it in the conditional above. This is required to ensure the $permalink_structure, $use_trailing_slashes, and $use_verbose_page_rules class properties are up to date for the url_to_postid() and user_trailingslashit() functions that follow. This is somewhat expensive but is required. If this was not called then the originating site WP_Rewrite instance would be used because switch_to_blog() does not track the rewrite state of the old and new site. That would result in potentially returning the wrong post IDs and appending/stripping the trailing slash for all sites based on the permalink structure of the originating site.

Update the Cache_Enabler_Disk::get_cache_url() method to be more simple. Always append or remove the trailing slash, even if it is the home page. This means the $site_cleared_url in the cache_enabler_site_cache_cleared hook will now have the same behavior. In version 1.7.2 and before this never had a trailing slash as it did not take into account the site's settings. Since PR #237 this would append the trailing slash if set but not the home page.

Update the Cache_Enabler_Engine::is_wrong_permalink_structure() method to only check the trailing slash instead of if the permalink structure is plain. (If a permalink structure is plain it does not use trailing slashes.) This means we will no longer always be bypassing the cache if a custom permalink structure in not set. Instead, a warning will be displayed as it has been and only the home page will be cached by default (as it does not have a query string). All pages, such as https://example.com/?p=123 and https://example.com/?page_id=321, will bypass the cache by default due to the query string.