Skip to content
Merged

9.0.49 #1087

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions includes/class.rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ protected function registerRoutes()
'callback' => array($this, 'geocodeCache'),
'useCompressedPathVariable' => true
));

$this->registerRoute('/query-nominatim', array(
'methods' => array('GET'),
'callback' => array($this, 'queryNominatim'),
'useCompressedPathVariable' => true
));

$this->registerRoute('/decompress', array(
'methods' => array('GET'),
Expand Down Expand Up @@ -1088,6 +1094,30 @@ public function geocodeCache($request)

return $record;
}

public function queryNominatim(){
$params = $this->getRequestParameters();
$cache = new NominatimGeocodeCache();

if(!empty($params) && !empty($params['data'])){
$query = (object) $params['data'];
$results = $cache->queryNominatimProxy($query);
if(!empty($results)){
if(!empty($query->_query) && empty($results->error)){
/* Cache key passed */
try{
$cache->set(sanitize_text_field($query->_query), $results);
} catch (\Exception $ex){

} catch (\Error $err){

}
}
return $results;
}
}
return array();
}

public function decompress($request)
{
Expand Down
128 changes: 100 additions & 28 deletions includes/open-layers/class.nominatim-geocode-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,73 @@ public function clear()
$stmt = $wpdb->query("TRUNCATE TABLE {$this->table}");
}

/**
* Queries the nominatim directly, as if it was a client side request
*
* This offloads the process from the client, adding security and reliability to the request
*
* Ideally this would be in some nominatim shared class, but this was a later addition to mitigate cache poisoning and
* as such needs to exist here fro backwards compat, and long term reuse.
*
* We'll likely streamline this down the road into one geocoder system or something
*
* @param object $query
*
* @return array
*/
public function queryNominatimProxy($query){
if(!empty($query) && !empty($query->q)){
$args = array(
'q' => $query->q,
'format' => !empty($query->format) ? $query->format : 'json'
);

if(!empty($query->countrycodes)){
$args['countrycodes'] = $query->countrycodes;
}

$endpoint = 'https://nominatim.openstreetmap.org/search';

$url = add_query_arg($args, $endpoint);

$siteUrl = get_site_url();
$response = wp_remote_get($url,
array(
'headers' => array(
'User-Agent' => 'WPGoMapsGeocoder (' . $siteUrl . ')',
'Referer' => $siteUrl
),
'timeout' => 10
)
);

if(!is_wp_error($response)){
try {
$response = wp_remote_retrieve_body($response);
$json = json_decode($response);

if(!empty($json)){
return $json;
} else {
/* Check if the user has been blocked by OSM/Nominatim */
if(strpos($response, 'Access blocked') !== FALSE){
$notice = strip_tags($response);
$notice = str_replace("Access blocked", "", $notice);
return (object) array(
'error' => $notice
);
}
}
} catch (\Exception $ex){
return false;
} catch (\Error $err){
return false;
}
}
}
return false;
}

public function sanitizeDataRecursive($data){
if(!is_array($data) && !is_object($data)){
return sanitize_text_field($data);
Expand Down Expand Up @@ -167,14 +234,16 @@ public function sanitizeDataRecursive($data){
*/
function query_nominatim_cache()
{
$cache = new NominatimGeocodeCache();
$record = $cache->get(sanitize_text_field($_GET['query']));
return;

// $cache = new NominatimGeocodeCache();
// $record = $cache->get(sanitize_text_field($_GET['query']));

if(!$record)
$record = array();
// if(!$record)
// $record = array();

wp_send_json($record);
exit;
// wp_send_json($record);
// exit;
}

/**
Expand All @@ -183,25 +252,27 @@ function query_nominatim_cache()
*/
function store_nominatim_cache()
{
$cache = new NominatimGeocodeCache();
try{
$cache->set(sanitize_text_field($_POST['query']), $_POST['response']);
return;

// $cache = new NominatimGeocodeCache();
// try{
// $cache->set(sanitize_text_field($_POST['query']), $_POST['response']);

wp_send_json(array(
'success' => 1
));
} catch (\Exception $ex){
wp_send_json(array(
'success' => 0,
'message' => $ex->getMessage()
));
} catch (\Error $err){
wp_send_json(array(
'success' => 0,
'message' => $err->getMessage()
));
}
exit;
// wp_send_json(array(
// 'success' => 1
// ));
// } catch (\Exception $ex){
// wp_send_json(array(
// 'success' => 0,
// 'message' => $ex->getMessage()
// ));
// } catch (\Error $err){
// wp_send_json(array(
// 'success' => 0,
// 'message' => $err->getMessage()
// ));
// }
// exit;
}

/**
Expand All @@ -226,10 +297,11 @@ function clear_nominatim_cache()
exit;
}

add_action('wp_ajax_wpgmza_query_nominatim_cache', 'WPGMZA\\query_nominatim_cache');
add_action('wp_ajax_nopriv_wpgmza_query_nominatim_cache', 'WPGMZA\\query_nominatim_cache');
/* Deprecated since 9.0.49 */
// add_action('wp_ajax_wpgmza_query_nominatim_cache', 'WPGMZA\\query_nominatim_cache');
// add_action('wp_ajax_nopriv_wpgmza_query_nominatim_cache', 'WPGMZA\\query_nominatim_cache');

add_action('wp_ajax_wpgmza_store_nominatim_cache', 'WPGMZA\\store_nominatim_cache');
add_action('wp_ajax_nopriv_wpgmza_store_nominatim_cache', 'WPGMZA\\store_nominatim_cache');
// add_action('wp_ajax_wpgmza_store_nominatim_cache', 'WPGMZA\\store_nominatim_cache');
// add_action('wp_ajax_nopriv_wpgmza_store_nominatim_cache', 'WPGMZA\\store_nominatim_cache');

add_action('wp_ajax_wpgmza_clear_nominatim_cache', 'WPGMZA\\clear_nominatim_cache');
61 changes: 28 additions & 33 deletions js/v8/open-layers/ol-geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,15 @@ jQuery(function($) {
query: JSON.stringify(query)
},
success: function(response, xhr, status) {
// Legacy compatibility support
response.lng = response.lon;
if(response && response.lon){
// Legacy compatibility support
response.lng = response.lon;
}

callback(response);
},
useCompressedPathVariable: true
});

/*$.ajax(WPGMZA.ajaxurl, {
data: {
action: "wpgmza_query_nominatim_cache",
query: JSON.stringify(query)
},
success: function(response, xhr, status) {
// Legacy compatibility support
response.lng = response.lon;

callback(response);
}
});*/
}

/**
Expand All @@ -74,11 +63,26 @@ jQuery(function($) {
} else if(options.country){
data.countrycodes = options.country;
}

$.ajax("https://nominatim.openstreetmap.org/search", {
data: data,

if(options._query){
data._query = options._query;
}

WPGMZA.restAPI.call("/query-nominatim", {
data: {
data: data,
},
success: function(response, xhr, status) {
callback(response);
if(response && response.length){
callback(response);
} else {
if(response && response.error){
/* There may be an additional error in place */
callback(response.error, WPGMZA.Geocoder.FAIL)
} else {
callback(null, WPGMZA.Geocoder.FAIL)
}
}
},
error: function(response, xhr, status) {
callback(null, WPGMZA.Geocoder.FAIL)
Expand All @@ -89,21 +93,14 @@ jQuery(function($) {
/**
* @function cacheResponse
* @access protected
* @summary Caches a response on the server, usually after it's been returned from Nominatim
* @summary Caches a response on the server, usually after it's been returned from Nominatim. Deprecated
* @param {string} address The street address
* @param {object|array} response The response to cache
* @returns {void}
*/
WPGMZA.OLGeocoder.prototype.cacheResponse = function(query, response)
{
$.ajax(WPGMZA.ajaxurl, {
data: {
action: "wpgmza_store_nominatim_cache",
query: JSON.stringify(query),
response: JSON.stringify(response)
},
method: "POST"
});

}

/**
Expand Down Expand Up @@ -231,11 +228,11 @@ jQuery(function($) {
finish(response, WPGMZA.Geocoder.SUCCESS);
return;
}
self.getResponseFromNominatim($.extend(options, {address: location}), function(response, status) {

self.getResponseFromNominatim($.extend(options, {address: location, _query : JSON.stringify({location: location, options: options})}), function(response, status) {
if(status == WPGMZA.Geocoder.FAIL)
{
callback(null, WPGMZA.Geocoder.FAIL);
callback(typeof response === 'string' ? response : null, WPGMZA.Geocoder.FAIL);
return;
}

Expand All @@ -246,8 +243,6 @@ jQuery(function($) {
}

finish(response, WPGMZA.Geocoder.SUCCESS);

self.cacheResponse(query, response);
});
});
}
Expand Down
10 changes: 8 additions & 2 deletions js/v8/store-locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,18 @@ jQuery(function($) {
if(status == WPGMZA.Geocoder.SUCCESS)
callback(results, status);
else{
let error = WPGMZA.localized_strings.address_not_found;
if(status == WPGMZA.Geocoder.FAIL && typeof results === 'string'){
error = results;
}

if(WPGMZA.InternalEngine.isLegacy()){
alert(WPGMZA.localized_strings.address_not_found);
alert(error);
} else {
self.showError(WPGMZA.localized_strings.address_not_found);
self.showError(error);
self.setVisualState(false);
}

}

});
Expand Down
Loading