first commit
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Get a jQuery Promise that will delive the AJAX data.
|
||||
* @param locales Locales to fill in the 'name' field (optional)
|
||||
* @returns promise
|
||||
*/
|
||||
|
||||
function geoip_detect_ajax_promise(locales) {
|
||||
locales = locales || '';
|
||||
|
||||
var promise = jQuery.ajax(geoip_detect.ajaxurl, {
|
||||
dataType: 'json',
|
||||
type: 'GET',
|
||||
data: {
|
||||
action: 'geoip_detect2_get_info_from_current_ip',
|
||||
locales: locales
|
||||
}
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get property value from data
|
||||
*
|
||||
* @param data
|
||||
* @param property_name
|
||||
* @param options
|
||||
*/
|
||||
function geoip_detect_get_property_value(data, property_name, options) {
|
||||
var _get_name = function(names, locales) {
|
||||
if (typeof(locales) === 'string') {
|
||||
locales = locales.split(',');
|
||||
}
|
||||
|
||||
for (var l in locales) {
|
||||
//l = l.trim();
|
||||
if (typeof(names[l]) != 'undefined' && names[l])
|
||||
return names[l];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
var $ = jQuery;
|
||||
var default_options = {
|
||||
'locales' : 'en',
|
||||
'default' : '',
|
||||
};
|
||||
options = $.extend(options, default_options);
|
||||
|
||||
var properties = property_name.split('.');
|
||||
var next_property = properties.shift();
|
||||
if (next_property == 'name' || !next_property) {
|
||||
if (typeof(data['names']) == 'object') {
|
||||
return _get_name(data['names'], options.locales);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
if (typeof(data[next_property]) == 'undefined')
|
||||
return options['default'];
|
||||
if (typeof(data[next_property]) == 'string')
|
||||
return data[next_property];
|
||||
return geoip_detect_get_property_value(data[next_property], properties.join('.'), options);
|
||||
}
|
||||
|
||||
function geoip_detect_fill_data_attributes(el) {
|
||||
var $ = jQuery;
|
||||
el = $(el);
|
||||
// Fill in the shortcodes into the HTML
|
||||
var shortcodes = el.find('[data-geoip]');
|
||||
if (!shortcodes.length)
|
||||
return;
|
||||
|
||||
var promise = geoip_detect_ajax_promise('en');
|
||||
|
||||
promise.done(function(data) {
|
||||
shortcodes.each(function() {
|
||||
var options = $(this).data('geoip');
|
||||
var value = geoip_detect_get_property_value(data, options.property, options);
|
||||
|
||||
if ($(this).data('geoip-method') == 'class')
|
||||
$(this).addClass('geoip-' + value);
|
||||
else
|
||||
$(this).text(value);
|
||||
$(this).trigger('geoip_detect.value.success');
|
||||
});
|
||||
}).fail(function(data) {
|
||||
if (typeof(console) != 'undefined' && typeof(console.log) != 'undefined')
|
||||
console.log('Error: ' + data.error);
|
||||
shortcodes.trigger('geoip_detect.value.failure');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
jQuery(document).ready(function($) {
|
||||
geoip_detect_fill_data_attributes('html');
|
||||
});
|
||||
@@ -0,0 +1,102 @@
|
||||
import Record from './models/record';
|
||||
import Cookies from 'js-cookie';
|
||||
|
||||
if (!window.jQuery) {
|
||||
console.error('Geoip-detect: window.jQuery is missing!');
|
||||
}
|
||||
const $ = window.jQuery;
|
||||
|
||||
|
||||
if (!window.geoip_detect) {
|
||||
console.error('Geoip-detect: window.geoip_detect')
|
||||
}
|
||||
const options = window.geoip_detect.options || {};
|
||||
|
||||
let ajaxPromise = null;
|
||||
|
||||
function get_info_raw() {
|
||||
if (!ajaxPromise) {
|
||||
// Do Ajax Request only once per page load
|
||||
ajaxPromise = $.ajax(options.ajaxurl, {
|
||||
dataType: 'json',
|
||||
type: 'GET',
|
||||
data: {
|
||||
action: 'geoip_detect2_get_info_from_current_ip'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return ajaxPromise;
|
||||
}
|
||||
|
||||
async function get_info_cached() {
|
||||
let response = false;
|
||||
|
||||
// 1) Load Info from cookie cache, if possible
|
||||
if (options.cookie_name) {
|
||||
response = Cookies.getJSON(options.cookie_name)
|
||||
}
|
||||
|
||||
// 2) Get response
|
||||
try {
|
||||
response = await get_info_raw();
|
||||
} catch(err) {
|
||||
response = err.responseJSON || err;
|
||||
}
|
||||
|
||||
// 3) Save info to cookie cache
|
||||
if (options.cookie_name) {
|
||||
let cookie_options = { path: '/' };
|
||||
if (options.cookie_duration_in_days) {
|
||||
cookie_options.expires = options.cookie_duration_in_days;
|
||||
}
|
||||
Cookies.set(options.cookie_name, JSON.stringify(response), cookie_options);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
export async function get_info() {
|
||||
let response = await get_info_cached();
|
||||
|
||||
if (typeof(response) !== 'object') {
|
||||
console.error('Geoip-detect: Record should be an object, not a ' + typeof(response), response);
|
||||
response = { 'extra': { 'error': response || 'Network error, look at the original server response ...' }};
|
||||
}
|
||||
|
||||
const record = new Record(response, options.default_locales);
|
||||
return record;
|
||||
}
|
||||
|
||||
async function add_body_classes() {
|
||||
const record = await get_info();
|
||||
|
||||
if (record.error()) {
|
||||
console.error('Geodata Error (could not add CSS-classes to body): ' + record.error());
|
||||
}
|
||||
|
||||
const css_classes = {
|
||||
country: record.get('country.iso_code'),
|
||||
'country-is-in-european-union': record.get('country.is_in_european_union'),
|
||||
continent: record.get('continent.code'),
|
||||
province: record.get('most_specific_subdivision.iso_code'),
|
||||
};
|
||||
|
||||
for(let key of Object.keys(css_classes)) {
|
||||
const value = css_classes[key];
|
||||
if (value) {
|
||||
if (typeof(value) == 'string') {
|
||||
$('body').addClass(`geoip-${key}-${value}`);
|
||||
} else {
|
||||
$('body').addClass(`geoip-${key}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (options.do_body_classes) {
|
||||
add_body_classes();
|
||||
}
|
||||
|
||||
// Extend window object
|
||||
window.geoip_detect.get_info = get_info;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @license
|
||||
* Lodash (Custom Build) lodash.com/license | Underscore.js 1.8.3 underscorejs.org/LICENSE
|
||||
* Build: `lodash include="get" -o js/lodash.custom.js`
|
||||
*/
|
||||
;(function(){function t(){}function e(t){var e=-1,n=null==t?0:t.length;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function n(t){var e=-1,n=null==t?0:t.length;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function r(t){var e=-1,n=null==t?0:t.length;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function o(t,e){for(var n=t.length;n--;)if(f(t[n][0],e))return n;return-1}function i(t){if(null==t)t=t===b?"[object Undefined]":"[object Null]";else if(I&&I in Object(t)){var e=T.call(t,I),n=t[I];
|
||||
try{t[I]=b;var r=true}catch(t){}var o=P.call(t);r&&(e?t[I]=n:delete t[I]),t=o}else t=P.call(t);return t}function a(t){if(typeof t=="string")return t;if(M(t)){for(var e=-1,n=null==t?0:t.length,r=Array(n);++e<n;)r[e]=a(t[e]);return r+""}return y(t)?G?G.call(t):"":(e=t+"","0"==e&&1/t==-g?"-0":e)}function c(t,e){var n=t.__data__,r=typeof e;return("string"==r||"number"==r||"symbol"==r||"boolean"==r?"__proto__"!==e:null===e)?n[typeof e=="string"?"string":"hash"]:n.map}function u(t,e){var n=null==t?b:t[e];
|
||||
return(!h(n)||C&&C in n?0:(p(n)?k:z).test(s(n)))?n:b}function s(t){if(null!=t){try{return E.call(t)}catch(t){}return t+""}return""}function l(t,e){function n(){var r=arguments,o=e?e.apply(this,r):r[0],i=n.cache;return i.has(o)?i.get(o):(r=t.apply(this,r),n.cache=i.set(o,r)||i,r)}if(typeof t!="function"||null!=e&&typeof e!="function")throw new TypeError("Expected a function");return n.cache=new(l.Cache||r),n}function f(t,e){return t===e||t!==t&&e!==e}function p(t){return!!h(t)&&(t=i(t),"[object Function]"==t||"[object GeneratorFunction]"==t||"[object AsyncFunction]"==t||"[object Proxy]"==t);
|
||||
}function h(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}function _(t){return null!=t&&typeof t=="object"}function y(t){return typeof t=="symbol"||_(t)&&"[object Symbol]"==i(t)}function d(t){return null==t?"":a(t)}var b,g=1/0,v=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,j=/^\w*$/,m=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,O=/\\(\\)?/g,z=/^\[object .+?Constructor\]$/,S=typeof self=="object"&&self&&self.Object===Object&&self,S=typeof global=="object"&&global&&global.Object===Object&&global||S||Function("return this")(),w=typeof exports=="object"&&exports&&!exports.nodeType&&exports,x=w&&typeof module=="object"&&module&&!module.nodeType&&module,$=Array.prototype,A=Object.prototype,F=S["__core-js_shared__"],E=Function.prototype.toString,T=A.hasOwnProperty,C=function(){
|
||||
var t=/[^.]+$/.exec(F&&F.keys&&F.keys.IE_PROTO||"");return t?"Symbol(src)_1."+t:""}(),P=A.toString,k=RegExp("^"+E.call(T).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),A=S.Symbol,R=$.splice,I=A?A.toStringTag:b,N=u(S,"Map"),q=u(Object,"create"),G=($=A?A.prototype:b)?$.toString:b;e.prototype.clear=function(){this.__data__=q?q(null):{},this.size=0},e.prototype.delete=function(t){return t=this.has(t)&&delete this.__data__[t],this.size-=t?1:0,
|
||||
t},e.prototype.get=function(t){var e=this.__data__;return q?(t=e[t],"__lodash_hash_undefined__"===t?b:t):T.call(e,t)?e[t]:b},e.prototype.has=function(t){var e=this.__data__;return q?e[t]!==b:T.call(e,t)},e.prototype.set=function(t,e){var n=this.__data__;return this.size+=this.has(t)?0:1,n[t]=q&&e===b?"__lodash_hash_undefined__":e,this},n.prototype.clear=function(){this.__data__=[],this.size=0},n.prototype.delete=function(t){var e=this.__data__;return t=o(e,t),!(0>t)&&(t==e.length-1?e.pop():R.call(e,t,1),
|
||||
--this.size,true)},n.prototype.get=function(t){var e=this.__data__;return t=o(e,t),0>t?b:e[t][1]},n.prototype.has=function(t){return-1<o(this.__data__,t)},n.prototype.set=function(t,e){var n=this.__data__,r=o(n,t);return 0>r?(++this.size,n.push([t,e])):n[r][1]=e,this},r.prototype.clear=function(){this.size=0,this.__data__={hash:new e,map:new(N||n),string:new e}},r.prototype.delete=function(t){return t=c(this,t).delete(t),this.size-=t?1:0,t},r.prototype.get=function(t){return c(this,t).get(t)},r.prototype.has=function(t){
|
||||
return c(this,t).has(t)},r.prototype.set=function(t,e){var n=c(this,t),r=n.size;return n.set(t,e),this.size+=n.size==r?0:1,this};var L=function(t){t=l(t,function(t){return 500===e.size&&e.clear(),t});var e=t.cache;return t}(function(t){var e=[];return 46===t.charCodeAt(0)&&e.push(""),t.replace(m,function(t,n,r,o){e.push(r?o.replace(O,"$1"):n||t)}),e});l.Cache=r;var M=Array.isArray;t.memoize=l,t.eq=f,t.get=function(t,e,n){if(null==t)t=b;else{var r=t;if(!M(e)){if(M(e))r=false;else var o=typeof e,r=!("number"!=o&&"symbol"!=o&&"boolean"!=o&&null!=e&&!y(e))||(j.test(e)||!v.test(e)||null!=r&&e in Object(r));
|
||||
e=r?[e]:L(d(e))}for(r=0,o=e.length;null!=t&&r<o;){var i;if(i=e[r++],typeof i!="string"&&!y(i)){var a=i+"";i="0"==a&&1/i==-g?"-0":a}t=t[i]}t=r&&r==o?t:b}return t===b?n:t},t.isArray=M,t.isFunction=p,t.isObject=h,t.isObjectLike=_,t.isSymbol=y,t.toString=d,t.VERSION="4.17.5",typeof define=="function"&&typeof define.amd=="object"&&define.amd?(S._=t, define(function(){return t})):x?((x.exports=t)._=t,w._=t):S._=t}).call(this);
|
||||
@@ -0,0 +1,58 @@
|
||||
|
||||
import _ from '../lodash.custom';
|
||||
|
||||
|
||||
const _get_localized = function(ret, locales) {
|
||||
if (typeof(ret) == 'object' && typeof(ret.names) == 'object') {
|
||||
for (let locale of locales) {
|
||||
if (ret.names[locale]) {
|
||||
return ret.names[locale];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
class Record {
|
||||
data = {};
|
||||
default_locales = [];
|
||||
|
||||
constructor(data, default_locales) {
|
||||
this.data = data || {};
|
||||
this.default_locales = default_locales || ['en'];
|
||||
}
|
||||
|
||||
get(prop, default_value) {
|
||||
return this.get_with_locales(prop, this.default_locales, default_value);
|
||||
}
|
||||
|
||||
|
||||
get_with_locales(prop, locales, default_value) {
|
||||
// Treat pseudo-property 'name' as if it never existed
|
||||
if (prop.substr(-5) === '.name') {
|
||||
prop = prop.substr(0, prop.length - 5);
|
||||
}
|
||||
|
||||
// TODO handle most_specific_subdivision (here or in PHP)?
|
||||
|
||||
let ret = _.get(this.data, prop, default_value);
|
||||
|
||||
// Localize property, if possible
|
||||
ret = _get_localized(ret, locales);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get error message, if any
|
||||
* @return string Error Message
|
||||
*/
|
||||
error() {
|
||||
return _.get(this.data, 'extra.error', '');
|
||||
}
|
||||
}
|
||||
|
||||
export default Record;
|
||||
Reference in New Issue
Block a user