Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockonomics_woocommerce_init repeatedly getting exceuted on each page load #235

Open
shivaenigma opened this issue Feb 17, 2021 · 2 comments
Assignees
Milestone

Comments

@shivaenigma
Copy link
Collaborator

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

@shivaenigma shivaenigma changed the title blockonomics_woocommerce_init repeatedly getting called blockonomics_woocommerce_init repeatedly getting exceuted on each page load Feb 17, 2021
@DarrenWestwood DarrenWestwood added this to the 3.5.4 milestone May 5, 2022
@thisisayush
Copy link
Contributor

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
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants