1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115:
<?php
namespace WPGMZA;
class NominatimGeocodeCache
{
public function __construct()
{
global $wpdb;
$this->table = $wpdb->prefix . "wpgmza_nominatim_geocode_cache";
if(!$wpdb->get_var("SHOW TABLES LIKE '{$this->table}'"))
{
require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
\dbDelta("CREATE TABLE {$this->table} (
id int(11) NOT NULL AUTO_INCREMENT,
query VARCHAR(512),
response TEXT,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1");
}
}
public function get($query)
{
global $wpdb;
$stmt = $wpdb->prepare("SELECT response FROM {$this->table} WHERE query=%s LIMIT 1", array($query));
$stmt = apply_filters( 'wpgmza_ol_nomination_cache_query_get', $stmt, $query );
$string = $wpdb->get_var($stmt);
$json = null;
if(!empty($string))
$json = json_decode(stripslashes($string));
return $json;
}
public function set($query, $response)
{
global $wpdb;
if(empty($query))
throw new \Exception("First argument cannot be empty");
$stmt = $wpdb->prepare("INSERT INTO {$this->table} (query, response) VALUES (%s, %s)", array(
$query,
$response
));
$stmt = apply_filters( 'wpgmza_ol_nomination_cache_query_set', $stmt, $query, $response );
$wpdb->query($stmt);
}
}
function query_nominatim_cache()
{
$cache = new NominatimGeocodeCache();
$record = $cache->get($_GET['query']);
if(!$record)
$record = array();
wp_send_json($record);
exit;
}
function store_nominatim_cache()
{
$cache = new NominatimGeocodeCache();
$cache->set($_POST['query'], $_POST['response']);
wp_send_json(array(
'success' => 1
));
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');
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');