Open shivaenigma opened 3 years ago
Here's my initial thoughts on this:
Investigating into Paypal's Plugin as mentioned, it seems they're using the _bootstrapped
variable to deal with this, but that is more of a fail-safe than a solution. It just prevents the plugin to bootstrap twice if it happens sometimes.
Upon several tests, I found that the reason you must be seeing the "Calling Woocommerce Init" is because the hook plugins_loaded
is executed multiple times, but it's only being executed once per page load, the reason you're seeing it multiple times is because of the AJAX Requests such as wc-ajax, or sometimes static resources such as favicons or combined js/css files may trigger the page loads in WordPress causing the plugin to be initialised for that request and it seems that the function is being called twice, to test it out I made the following changes:
On seeing the logs, "Plugin Already Initialized" was never called! Check Screenshots below:
This can be verified by this patch
Find how other plugins are behaving. For example see how paypal woocommerce gateway still uses plugins_loaded to called bootstrap() function but avoids it running multiple times by checking a flag variable
Also looks like we have too much initialization code in one place. Anyway we can follow better practices