blockonomics / woocommerce-plugin

Accept bitcoins on your wordpress site, payments go directly into your wallet
24 stars 31 forks source link

blockonomics_woocommerce_init repeatedly getting exceuted on each page load #235

Open shivaenigma opened 3 years ago

shivaenigma commented 3 years ago

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

thisisayush commented 2 years ago

Here's my initial thoughts on this:

  1. 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.

  2. 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:

image

On seeing the logs, "Plugin Already Initialized" was never called! Check Screenshots below:

image
  1. We can break down the code into different functions for modularity, but we should do it in a separate release.
DarrenWestwood commented 2 years ago