bozdoz / wp-plugin-leaflet-map

Add leaflet maps to Wordpress with shortcodes
https://wordpress.org/plugins/leaflet-map/
GNU General Public License v2.0
140 stars 71 forks source link

Fix Geocoder Undefined offset #190

Closed bozdoz closed 1 year ago

bozdoz commented 1 year ago

Your plugin throws some PHP notices that need to be addressed when OSM address cannot be geocoded (for whatever reason) ...

[16-Dec-2022 07:38:10 UTC] PHP Notice: Undefined offset: 0 in /wp-content/plugins/leaflet-map/class.geocoder.php on line 171 [16-Dec-2022 07:38:10 UTC] PHP Notice: Trying to get property 'lat' of non-object in /wp-content/plugins/leaflet-map/class.geocoder.php on line 171 [16-Dec-2022 07:38:10 UTC] PHP Notice: Undefined offset: 0 in /wp-content/plugins/leaflet-map/class.geocoder.php on line 172 [16-Dec-2022 07:38:10 UTC] PHP Notice: Trying to get property 'lon' of non-object in /wp-content/plugins/leaflet-map/class.geocoder.php on line 172

They can be resolved by changing the code at line 170 of /class.geocoder.php from ...

    return (Object) array(
        'lat' => $json[0]->lat,
        'lng' => $json[0]->lon,
    );

... to ...

    if (isset($json[0]->lat) && isset($json[0]->lon)) {
        return (Object) array(
            'lat' => $json[0]->lat,
            'lng' => $json[0]->lon,
        );
    } else {
        throw new Exception('No Address Found.');
        return false;
    }

Oliver

Link: https://wordpress.org/support/topic/php-notices-156/