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

restore_current_blog() during network activation doesn't work #117

Closed nosilver4u closed 4 years ago

nosilver4u commented 4 years ago

https://github.com/keycdn/cache-enabler/blob/master/inc/cache_enabler.class.php#L215

Per the docs on restore_current_blog() at https://developer.wordpress.org/reference/functions/restore_current_blog/ If you don't run it after each switch_to_blog(), strange things can happen.

Now, practically, because it only happens during a network-wide activation, and there probably isn't a "current blog", I'm guessing no one has ever noticed that it doesn't quite work. So it might be that it could just be removed. Otherwise, restore_current_blog() should be at the end (within) the foreach loop.

Only reason I noticed, is because I'm using some of the CE code in a new project and running the activation routine a bit differently, and I kept getting stuck in the wrong blog when the activation routine was triggered.

coreykn commented 4 years ago

You're right, restore_current_blog() should come after every switch_to_blog(), which isn't the case if it's included outside the foreach loop. This could lead to strange things occurring as you pointed out (fortunately I haven't been able to produce any issues in regards to the functionality of the plugin).

I'm unsure in what case there won't be a current blog though, because as far as I'm aware if a plugin is network activated the current blog is the main site (as the network admin is on the main site). I don't think restore_current_blog() should be removed, as per the documentation:

restore_current_blog() should be called after every switch_to_blog(). If not, a global variable which monitors the switching, $GLOBALS['_wp_switched_stack'], will not be empty even if you use switch_to_blog() to return to the original blog. If $GLOBALS['_wp_switched_stack'] is not empty, WP will think it is in a switched state and can potentially return the wrong URL for the site via wp_upload_dir(). See http://wordpress.stackexchange.com/a/123516/27757

Instead it should just be included within the foreach loop. Thanks @nosilver4u for bringing attention to this.