      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;

    /**
     * Used to facilitate communication and caching between the client and the Nominatim Geocoding service
     */
    class NominatimGeocodeCache
    {
        /**
         * Constructor.
         */
        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");
            }
        }

        /**
         * Returns the cached result from the local geocode cache
         * @param string $query The address being queried
         * @return object|null The cached geocode result, or null where no result is found
         */
        public function get($query)
        {
            global $wpdb;

            // Check the cache first, as per the nominatim usage policy
            $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;
        }

        /**
         * Caches the supplied response, from the supplied query
         * @param string $query The queried address
         * @param string $response The returned response to cache
         */
        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);
        }
    }

    /**
     * Bind function to query Nominatim cache.
     * @deprecated This will be moved to the REST API in the future
     */
    function query_nominatim_cache()
    {

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

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

        wp_send_json($record);
        exit;
    }

    /**
     * Bind function to store a response on the Nominatim cache.
     * @deprecated This will be moved to the REST API in the future
     */
    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');