keycdn / cache-enabler

A lightweight caching plugin for WordPress that makes your website faster by generating static HTML files.

Home Page:https://wordpress.org/plugins/cache-enabler/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

$network_wide param for each_site() method is inaccurate

nosilver4u opened this issue · comments

I've noticed in several places that each_site() is called with the $network_wide parameter set to the value of is_multisite(). Unfortunately, the result of is_multsite() is not the same as whether or not the plugin is activated network-wide, which will cause the callback function to run on all sites when it should only be running on the active sites (or perhaps the current site).

It might be worth making a "wrapper" method like so:

function is_network_active() {
	if ( ! function_exists( 'is_plugin_active_for_network' ) && is_multisite() ) {
		require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	}
	if ( is_multisite() && is_plugin_active_for_network( CE_BASE ) ) {
		return true;
	}
	return false;
}
commented

Thanks for your feedback, @nosilver4u. That's actually intentional based on what's being performed as the Cache_Enabler::each_site() $network parameter focuses on whether or not to enter each site in the network (maybe some renaming of variables for more clarity is required). Methods that are using just is_multisite() and the reasoning behind it:

  • Cache_Enabler::on_ce_update():

    • This Cache_Enabler::each_site() call will clean the system files for each site. If the settings files are removed then the single files will be cleaned as well (e.g. advanced-cache.php). That will prevent trying to clean those single files for each site in the network. It will also prevent those single files from being removed on a multisite network without network activation. From what I can tell this is the only place that could (maybe even should as you suggest) be adjusted. The reason why I initially chose to go through each site in a multisite network here, even if those that do not have Cache Enabler activated, was because just in case some old settings file that is somehow still there is cleaned. Cache_Enabler_Disk::clean() went through several iterations and if I recall correctly it was actually required here before but now when I look at it I don't think it is, but it does cover a few (probably really rare) edge cases.

    • This Cache_Enabler::each_site() call will update the backend requirements. $plugin_update is passed to ensure that a multisite network without network activation will not have the backend updated for sites that do not have Cache Enabler activated. It might need to be checked if this is a multisite here for single site behavior, that's on my list of todos. (Still working on tuning this part of the update as I don't feel like it's quite right, but the database is now automatically updated if the version number changes.)

  • Cache_Enabler::on_uninstall():

    • This Cache_Enabler::each_site() call will go through each site just in case at some point Cache Enabler was activated and added our database option, whether it was network activated or activated on individual sites.

Hopefully that helps to show why this was done, but of course if there is a better way to handle this or if the handling above is not quite right we can make changes at any time.

Ah, that does make sense! I can definitely see where you might end up with orphaned settings files and what not that you want to make sure to cleanup--even more so for the on_uninstall() method.
And since the plugin checks is_plugin_active() within the update_backend() method, you won't have anything running on sites where it shouldn't.