Skip to content

Commit

Permalink
update plugin upgrade process for multisites (keycdn#303)
Browse files Browse the repository at this point in the history
Update the process for when the disk and backend requirements are
updated for multisite networks to not all be done at once. This does
not scale well.

There is a caveat of this change and it is related to the
`cache_enabler_disk_updated` site transient. An edge case could arise
when this plugin is used on a multisite network as a must-use plugin,
so there is no activation/deactivation/uninstalling occurring, and the
following occurs within an hour:

1. Update plugin.
2. Forcefully delete the plugin (e.g. SFTP or FTP).
3. Install an older version of the plugin.
4. Update the plugin to the same version as in step 1.

The edge case would be the disk requirements would not be updated
because the site transient would still exist thinking it already
updated itself. The caveats of a must-use plugin itself makes handling
that specific scenario a little tricky with what is currently available
in the plugin. Quite rare and unlikely but I thought I would put it out
there that this can occur as of this commit. Once a new queue system is
introduced shortly it will not.
  • Loading branch information
coreykn authored Oct 7, 2021
1 parent b0e21a5 commit ce350c0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 40 deletions.
4 changes: 2 additions & 2 deletions advanced-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* The advanced-cache.php creation method uses this during the disk setup and
* requirements check. You can copy this file to the wp-content directory and edit
* the $cache_enabler_constants_file value as needed. It will be deleted if stale
* or abandoned.
* the $cache_enabler_constants_file value as needed. It will automatically delete
* itself if stale or abandoned.
*
* @since 1.2.0
* @change 1.8.6
Expand Down
50 changes: 31 additions & 19 deletions inc/cache_enabler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ public static function on_deactivation( $network_wide ) {
* When the plugin is uninstalled.
*
* This runs on the 'uninstall_cache-enabler/cache-enabler.php' action. It deletes
* the 'cache_enabler' option and 'cache_enabler_cache_size' transient from the
* database for each site.
* the 'cache_enabler' option and plugin transients from the database for each
* site in the installation.
*
* @since 1.0.0
* @change 1.6.0
Expand Down Expand Up @@ -313,27 +313,28 @@ public static function install_later( $new_site ) {
}

/**
* Update the disk and backend requirements.
* Update the disk and backend requirements for the current site.
*
* This update process begins by first deleting the settings and
* advanced-cache.php files and then maybe unsets the WP_CACHE constant in the
* wp-config.php file. A new advanced-cache.php file is then created and the
* WP_CACHE constant is maybe set. Next, the 'cache_enabler' option is updated in
* the database for each site Cache Enabler is considered active, which triggers a
* new settings file to be created each time that occurs. Lastly, the complete
* cache is cleared.
* WP_CACHE constant is maybe set. If a multisite network, the preceding actions
* will only be done when the first site in the network is updated. Next, the
* 'cache_enabler' option is updated in the database for the current site, which
* triggers a new settings file to be created. Lastly, the site cache is cleared.
*
* @since 1.8.0
* @since 1.8.0
* @change 1.8.7
*/
public static function update() {

self::update_disk();
self::each_site( is_multisite(), 'self::update_backend' );
self::clear_complete_cache();
self::update_backend();
self::clear_site_cache();
}

/**
* Add or update backend requirements.
* Add or update the backend requirements for the current site.
*
* This adds or updates the 'cache_enabler' option in the database, which triggers
* the creation of the settings file. It will call self::on_update_backend() when
Expand Down Expand Up @@ -369,19 +370,29 @@ public static function update_backend() {
}

/**
* Update the disk requirements.
* Update the disk requirements for the current site.
*
* This deletes the settings and advanced-cache.php files and then maybe unsets
* the WP_CACHE constant in the wp-config.php file. A new advanced-cache.php file
* is then created and the WP_CACHE constant is maybe set.
* is then created and the WP_CACHE constant is maybe set. If a multisite network,
* the 'cache_enabler_disk_updated' site transient is set afterward to only allow
* this to be ran once.
*
* @since 1.8.0
* @since 1.8.0
* @change 1.8.7
*/
public static function update_disk() {

self::each_site( is_multisite(), 'Cache_Enabler_Disk::clean' );

Cache_Enabler_Disk::setup();
if ( is_multisite() ) {
if ( get_site_transient( 'cache_enabler_disk_updated' ) !== CACHE_ENABLER_VERSION ) {
self::each_site( true, 'Cache_Enabler_Disk::clean' );
Cache_Enabler_Disk::setup();
set_site_transient( 'cache_enabler_disk_updated', CACHE_ENABLER_VERSION, HOUR_IN_SECONDS );
}
} else {
Cache_Enabler_Disk::clean();
Cache_Enabler_Disk::setup();
}
}

/**
Expand Down Expand Up @@ -557,12 +568,13 @@ public static function uninstall_later( $old_site ) {
* Uninstall backend requirements.
*
* @since 1.5.0
* @change 1.8.0
* @change 1.8.7
*/
private static function uninstall_backend() {

delete_option( 'cache_enabler' );
delete_transient( 'cache_enabler_cache_size' );
delete_site_transient( 'cache_enabler_disk_updated' );
}

/**
Expand Down Expand Up @@ -707,7 +719,7 @@ public static function on_plugin_activation_deactivation() {
/**
* Get the plugin settings from the database for the current site.
*
* This can update the disk and backend requirements and then clear the complete
* This can update the disk and backend requirements and then clear the site
* cache if the settings do not exist or are outdated. If that occurs, the
* settings after the update will be returned.
*
Expand Down
28 changes: 20 additions & 8 deletions inc/cache_enabler_disk.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,11 @@ private static function get_settings_file( $fallback = false ) {
/**
* Get the name of the settings file for the current site.
*
* This uses home_url() in the late cache engine start to get the settings file
* name when creating and deleting the settings file or when getting the plugin
* settings from the settings file. Otherwise, it finds the name of the settings
* file in the settings directory when the cache engine is started early.
*
* @since 1.5.5
* @change 1.8.0
*
Expand All @@ -934,7 +939,6 @@ private static function get_settings_file_name( $fallback = false, $skip_blog_pa

$settings_file_name = '';

// When creating or deleting the settings file.
if ( function_exists( 'home_url' ) ) {
$settings_file_name = parse_url( home_url(), PHP_URL_HOST );

Expand All @@ -944,7 +948,6 @@ private static function get_settings_file_name( $fallback = false, $skip_blog_pa
}

$settings_file_name .= '.php';
// When getting the plugin settings from the settings file.
} elseif ( is_dir( CACHE_ENABLER_SETTINGS_DIR ) ) {
if ( $fallback ) {
$settings_files = array_map( 'basename', self::get_dir_objects( CACHE_ENABLER_SETTINGS_DIR ) );
Expand Down Expand Up @@ -1010,15 +1013,17 @@ private static function get_settings_file_name( $fallback = false, $skip_blog_pa
*
* This will create the settings file if it does not exist and the cache engine
* was started late. If that occurs, the settings from the new settings file will
* be returned.
* be returned. Before it is created, checking if the settings file exists after
* retrieving the database settings is done in case an update occurred, which
* would have resulted in a new settings file being created.
*
* This can update the disk and backend requirements and then clear the complete
* cache if the settings are outdated. If that occurs, a new settings file will be
* This can update the disk and backend requirements and then clear the site cache
* if the settings are outdated. If that occurs, a new settings file will be
* created and an empty array returned.
*
* @since 1.5.0
* @since 1.8.0 The `$update` parameter was added.
* @change 1.8.5
* @change 1.8.7
*
* @param bool $update Whether to update the disk and backend requirements if the settings are
* outdated. Default true.
Expand Down Expand Up @@ -1052,10 +1057,17 @@ public static function get_settings( $update = true ) {
Cache_Enabler::update();
}
} else {
$settings_file = self::create_settings_file( Cache_Enabler::get_settings() );
$_settings = Cache_Enabler::get_settings();
$settings_file = self::get_settings_file();

if ( $settings_file !== false ) {
if ( is_file( $settings_file ) ) {
$settings = include $settings_file;
} else {
$settings_file = self::create_settings_file( $_settings );

if ( $settings_file !== false ) {
$settings = include $settings_file;
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions inc/cache_enabler_engine.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public static function start( $force = false ) {
/**
* Whether the cache engine is started.
*
* The cache engine is considered to have been started early when started in the
* advanced-cache.php drop-in file and started late when started on the 'init' action.
*
* @since 1.5.0
*
* @var bool
Expand Down
26 changes: 15 additions & 11 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ Cache Enabler captures page contents and saves it as a static HTML file on the s

== Changelog ==

= 1.8.7 =
* Update plugin upgrade process for multisite networks (#303)
* Update `wp-config.php` file handling (#302)

= 1.8.6 =
* Add `cache_enabler_settings_before_validation` filter hook (#298)
* Add additional validation when creating cached files (#299)
* Add type casts to several filter hooks (#299)
* Update requirements check notices (#300)
* Update `advanced-cache.php` drop-in file handling (#297)
* Add additional validation when creating cached files (#299)
* Add type casts to several filter hooks (#299)
* Add `cache_enabler_settings_before_validation` filter hook (#298)

= 1.8.5 =
* Update required WordPress version from 5.5 to 5.1 (#295)
Expand All @@ -79,29 +83,29 @@ Cache Enabler captures page contents and saves it as a static HTML file on the s
* Fix requirements check (#285)

= 1.8.0 =
* Update `advanced-cache.php` drop-in file handling to improve reliability and compatibility (#283 and #260)
* Update `advanced-cache.php` drop-in file handling to improve reliability and compatibility (#260 and #283)
* Update settings file to be deleted before the `home` option is updated to prevent a leftover settings file (#279)
* Update `cache_enabler_bypass_cache` filter hook default value to allow a complete override (#277)
* Update cache size transient to be in real time (#269 and #237)
* Update cache size transient to be in real time (#237 and #269)
* Update cache expiry time to always be a non-negative integer (#265)
* Update WP-CLI `clear` subcommand (#261)
* Update required WordPress version from 5.1 to 5.5 (#260)
* Update plugin upgrade process to improve reliability and compatibility (#260)
* Update getting the cache file path to improve creating cache files (#256)
* Update HTML5 doctype check to be less strict (#254)
* Update permalink structure handling (#263 and #251)
* Update requirements check to improve notices shown (#260 and #249)
* Update permalink structure handling (#251 and #263)
* Update requirements check to improve notices shown (#249 and #260)
* Update cache clearing structure to enhance the automatic cache clearing actions (#247)
* Add WP-Cron event to clear the expired cache on an hourly basis (#281, #268, and #237)
* Add new cache clearing structure for option actions (#280 and #272)
* Add cache engine restart support (#278 and #271)
* Add WP-Cron event to clear the expired cache on an hourly basis (#237, #268, and #281)
* Add new cache clearing structure for option actions (#272 and #280)
* Add cache engine restart support (#271 and #278)
* Add `constants.php` file to plugin directory to allow constant overrides (#260)
* Add wildcard cache clearing support (#246)
* Add Brotli compression support (#243 @nlemoine)
* Add new cache clearing structure for term actions (#234 @davelit)
* Add cache iterator to improve cache object handling (#237)
* Fix WebP URL conversion edge case (#275)
* Deprecate `cache_enabler_clear_site_cache_by_blog_id` and `cache_enabler_clear_page_cache_by_post_id` action hooks in favor of replacements (#274 and #247)
* Deprecate `cache_enabler_clear_site_cache_by_blog_id` and `cache_enabler_clear_page_cache_by_post_id` action hooks in favor of replacements (#247 and #274)

= 1.7.2 =
* Update string to be translatable (#235 @timse201)
Expand Down

0 comments on commit ce350c0

Please sign in to comment.