tinuzz / wp-plugin-trackserver

A WordPress plugin for GPS tracking and publishing
https://wordpress.org/plugins/trackserver/
36 stars 12 forks source link

Uncaught TypeError: container is null #43

Closed larusalka closed 10 months ago

larusalka commented 1 year ago

Hi,

We have been using the Trackserver plugin to display live tracking on our posts for our weekly bike ride at https://www.siclas.org/ and recently updated to version 5.0.2. It was working as expected until yesterday, but today, embedded maps on our posts are not showing anymore, not even on old posts that were previously showing the map, for instance https://www.siclas.org/sicleada-573-dejese-llevar/ (the map should be below the image). The console shows the following error: Uncaught TypeError: container is null create_maps https://www.siclas.org/wp-content/plugins/trackserver/trackserver.js?ver=4.3-20190906:513 init https://www.siclas.org/wp-content/plugins/trackserver/trackserver.js?ver=4.3-20190906:33

https://www.siclas.org/wp-content/plugins/trackserver/trackserver.js?ver=4.3-20190906:581 Everything seems to be working fine on the backend, including viewing and editing the tracks on the Manage tracks page. Any help to make the maps visible again will be greatly appreciated. Thanks in advance.
tinuzz commented 1 year ago

Hi,

Which versions of PHP and WordPress are you running?

Regards, Martijn.

tinuzz commented 1 year ago

Hi,

I looked at your page, and something strange is going on. You said it was working as expected until yesterday, but today it doesn't. Did you change anything in the setup of WordPress, install a plugin, change your theme, anything like that?

It seems to me, that the [tsmap] shortcode is evaluated twice in your post. Trackserver generates two instances of the map, and they are both the same. You can see this in the source of the page, look for the 'trackserver_mapdata' variable. You can of course also inspect that global variable in the JS console.

Trackserver automatically generates div elements with indexed names ('tsmap_1', 'tsmap_2', etc.). Then it iterates over the mapdata and plots the maps in their respective divs. Your page has map data for two maps, but it only contains one div: 'tsmap_2'. The one called 'tsmap_1' should have been in there somewhere, but it isn't.

Now when the code tries to get the div element called 'tsmap_1', the result (that it wants to put in the 'container' var) is null, and that's where it breaks.

To me, it seems there is something (a plugin?) in your WordPress that causes this behaviour. It certainly isn't something that Trackserver can do on its own.

If you want a workaround, you can try inserting a line just above line 513 in trackserver.js:

if (container === null) continue;

That should prevent the error and just skip the rendering of 'tsmap_1', but it's not a real fix.

If you want to find the culprit, look for a plugin that calls the builtin function do_shortcode() somewhere on the post content. That indicates that the code is evaluating shortcodes outside the normal rendering of the post, which could cause this behaviour.

Best regards, Martijn.

larusalka commented 1 year ago

Hi,

We currently have php 7.4 and WP 6.3.1. No manual changes to the site, but since we have configured automatic updates, it is possible that an updated plugin is causing this behaviour... will need some time to check one by one. In any case, thanks to your workaround the maps are visible again. Thank you very much for your prompt answer.

Best regards, Silvia

tinuzz commented 1 year ago

Glad to be of help.

Martijn.

tinuzz commented 1 year ago

Hi @larusalka ,

Not sure if this will reach you after I closed the issue, but out of curiosity... can you try something for me?

Revert the JavaScript workaround I posted earlier, and in stead, add the following to class-trackserver-shortcode.php at line 59:

              if ( ! in_the_loop() ) {
                      return '';
              }

This should skip the shortcode expansion when plugins process the content of a post outside the WP loop, which should prevent the behavior that you saw. If this works well, I would consider this a proper fix and I'd add it to the code.

Thanks, Martijn.

larusalka commented 1 year ago

Hi again,

I reverted the workaround and added your code at line 59 of class-trackserver-shortcode.php. It works well and all the maps are visible.

Thank you. Silvia

tinuzz commented 1 year ago

Thank you, I will add this fix to the next release.