blockonomics_woocommerce_init repeatedly getting exceuted on each page load
This can be verified by this patch
diff --git a/blockonomics-woocommerce.php b/blockonomics-woocommerce.php
index 65f6395..a6826b1 100755
--- a/blockonomics-woocommerce.php
+++ b/blockonomics-woocommerce.php
@@ -393,6 +393,8 @@ function blockonomics_woocommerce_init()
else
return str_replace( '#deferload', '', $url )."' defer='defer";
}
+
+ error_log('Calling woocommerce init');
}
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
Here's my initial thoughts on this:
-
Investigating into Paypal's Plugin as mentioned, it seems they're using the
_bootstrappedvariable 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_loadedis 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:

- We can break down the code into different functions for modularity, but we should do it in a separate release.
- Confirmed plugin is not being initialized multiple times and that the paypal module also has additional calls. eg from /wp-cron.php.
- We appear to be using the correct hooks: https://github.com/blockonomics/woocommerce-plugin/blob/master/blockonomics-woocommerce.php#L55-L63, and like the paypal module we are only using 'init' hook to load the plugin translations.
- As @thisisayush has mentioned, we can refactor our initialization code to be more modular. This will help with the code structure and simplify the main plugin file: https://github.com/woocommerce/woocommerce-gateway-paypal-express-checkout/blob/trunk/woocommerce-gateway-paypal-express-checkout.php#L36-L48. This will have a big impact on our code and should be handled as a separate release.