ItellaPlugins / itella-shipping-woocommerce

Itella Shipping Plugin for Woocommerce
GNU General Public License v2.0
4 stars 3 forks source link

This plugin makes website load 15 seconds when logged in as admin #89

Closed skyzer closed 11 months ago

skyzer commented 11 months ago

Without this plugin activated loads 0.5 seconds, everything loads at least 10 seconds when I'm logged in. Using Query Monitor to track it. See Query Monitor output. Nothing visibly eyecatching that cause problems.

Plugin deactivated:

image

With plugin activated

image

I am getting some PHP warnings on deprecated issues

image

Environment (PHP 8.2.12):

image
skyzer commented 11 months ago

Hint: Issue is in admin/class-itella-shipping-method.php file on update_locations() function that pulls the huge JSON file new PickupPoints('https://delivery.plugins.itella.com/api/locations'); and for some reason filesystem cache approach does not work nor even added wordpress's built in transient DB cache layer. Upon further digging the API method returns only location of 4 countries: EE, LV, LT, FI. But the foreach loop in update_locations has all EU countries, so it loops through all of them. So this piece of code

              if ($update_file) {
                  $itella_pickup_points_obj = new PickupPoints('https://delivery.plugins.itella.com/api/locations');
                  $locations = $itella_pickup_points_obj->getLocationsByCountry($country_code);

                  if (!empty($locations)) {
                      // Save to a file
                      file_put_contents($filename, json_encode($locations));
                  }
              } else {
                  // Load from the file if the transient is not available but the file is up-to-date
              }

is going to update the file so it goes and does API request with getLocationsByCountry. As I've said above API returns only 4 countries (EE, LV, LT, FI) and does not return any other country. Because the country was not found in API request, it does not go into !empty($locations) condition, therefore it does not save that location into a file, because there is nothing to save. Because of that every other EU country is receiving API call on every request :)

https://github.com/ItellaPlugins/itella-shipping-woocommerce/blob/7ed17489e5c9ef7819d40161eed46a4f94cdff8e/includes/class-itella-shipping.php#L172

skyzer commented 11 months ago

Proposed fix https://github.com/ItellaPlugins/itella-shipping-woocommerce/pull/90

markakk commented 11 months ago

Hello,

Thank you for reporting the issue you noticed. The fix you suggested didn't acceptable for us, but we built a fix based on it in PR #91.