Open radeno opened 3 years ago
We can fix when plugins requesting assets with plugins_url
function.
Within add_action init we can replace URLs
$muPluginList = array_filter(array_map(function($pluginKey) { return substr($pluginKey, 0, strrpos( $pluginKey, '/') ?: 0); }, array_keys(get_mu_autoloader_plugins())));
\add_filter('plugins_url', function($url, $path, $plugin) use($muPluginList) {
if (strpos($url, WP_PLUGIN_URL . '/') === false) {
return $url;
}
foreach ($muPluginList as $plugin) {
$pluginUrl = WP_PLUGIN_URL . '/' . $plugin;
$muPluginUrl = WPMU_PLUGIN_URL . '/' . $plugin;
if (strpos($url, $pluginUrl) !== false) {
return str_replace($pluginUrl, $muPluginUrl, $url);
}
}
}, 10, 3);
This is follow up ticket for #1
Motivation?
Lot of plugins interacts with each others and they checks if plugin is installed and what is more important when different plugin is active. For example plugins extensions or plugins which uses API of different plugins (dependencies).
By my experience they are using
get_plugins
function to check if specific plugin is installed and active.In the current situation mu-plugins are loaded automatically, but WP does know nothing about them.
Implementation?
Curiously it could be very easy.
get_plugins
checks every sub-folder in theplugins
folder. And this plugin is used heavily. This function doesn't have any filter what is sad, but happily it useswp_get_cache
, so there is a workaround.Step 1
Copy code from get_plugins (https://developer.wordpress.org/reference/functions/get_plugins/) and change just
wp_cache_get
andwp_cache_set
namespace tomu_plugins
and$plugin_root
toWPMU_PLUGIN_DIR
. Name this function for example as get_mu_autoloader_plugins()Step 2
Merge both lists together. Eg:
This code is not optimized, so cache is rewrite on every request, which is not good.
Drawbacks?
Listed all mu-plugins as regular plugins. Which is not good.
Drawbacks fixes
Append specific array key which will say that that plugin is MU plugin. Then in the
all_plugins
filter, just skip this plugins list.Proof of concept, workable code