forked from yellowtree/geoip-detect
/
shortcode.php
114 lines (89 loc) · 3.66 KB
/
shortcode.php
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
<?php
/**
* @deprecated
*/
function geoip_detect_shortcode($attr)
{
$userInfo = geoip_detect_get_info_from_current_ip();
$defaultValue = isset($attr['default']) ? $attr['default'] : '';
if (!is_object($userInfo))
return $defaultValue . '<!-- GeoIP Detect: No info found for this IP. -->';
$propertyName = $attr['property'];
if (property_exists($userInfo, $propertyName)) {
if ($userInfo->$propertyName)
return $userInfo->$propertyName;
else
return $defaultValue;
}
return $defaultValue . '<!-- GeoIP Detect: Invalid property name. -->';
}
add_shortcode('geoip_detect', 'geoip_detect_shortcode');
/**
* Short Code
*
* Examples:
* `[geoip_detect2 property="country"]` -> Germany
* `[geoip_detect2 property="country.isoCode"]` -> de
*
* `[geoip_detect2 property="country" lang="de"]` -> Deutschland
* `[geoip_detect2 property="country" lang="fr,de"]` -> Allemagne
* `[geoip_detect2 property="country.confidence" default="default value"]` -> default value
*
* @param string $property Property to read. Instead of '->', use '.'
* @param string $lang Language(s) (optional. If not set, current site language is used.)
* @param string $default Default Value that will be shown if value not set (optional)
* @param string $skipCache if 'true': Do not cache value
*/
function geoip_detect2_shortcode($attr)
{
$skipCache = isset($attr['skip_cache']) && (strtolower($attr['skip_cache']) == 'true' || $attr['skip_cache'] == '1');
$locales = isset($attr['lang']) ? $attr['lang'] . ',en' : 'en';
$locales = apply_filters('geoip_detect2_locales', $locales);
$defaultValue = isset($attr['default']) ? $attr['default'] : '';
$properties = explode('.', $attr['property']);
$options = array('skipCache' => $skipCache);
$userInfo = geoip_detect2_get_info_from_current_ip($locales, $options);
if ($userInfo->isEmpty)
return $defaultValue . '<!-- GeoIP Detect: No information found for this IP (' . geoip_detect2_get_client_ip() . ') -->';
$return = '';
try {
if (count($properties) == 1) {
$return = $userInfo->{$properties[0]};
} else if (count($properties) == 2) {
$return = $userInfo->{$properties[0]};
if (!is_object($return))
throw new \RuntimeException('Invalid property name.');
$return = $return->{$properties[1]};
} else {
throw new \RuntimeException('Only 1 dot supported. Please send a bug report to show me the shortcode you used if you need it ...');
}
} catch (\RuntimeException $e) {
return $defaultValue . '<!-- GeoIP Detect: Invalid property name. -->';
}
if (is_object($return) && $return instanceof \GeoIp2\Record\AbstractPlaceRecord)
$return = $return->name;
if (is_object($return) || is_array($return)) {
return $defaultValue . '<!-- GeoIP Detect: Invalid property name (sub-property missing). -->';
}
if ($return)
return (string) $return;
else
return $defaultValue;
}
add_shortcode('geoip_detect2', 'geoip_detect2_shortcode');
function geoip_detect2_shortcode_client_ip($attr) {
$client_ip = geoip_detect2_get_client_ip();
geoip_detect_normalize_ip($client_ip);
return $client_ip;
}
add_shortcode('geoip_detect2_get_client_ip', 'geoip_detect2_shortcode_client_ip');
function geoip_detect2_shortcode_get_external_ip_adress($attr) {
$external_ip = geoip_detect2_get_external_ip_adress();
return $external_ip;
}
add_shortcode('geoip_detect2_get_external_ip_adress', 'geoip_detect2_shortcode_get_external_ip_adress');
function geoip_detect2_shortcode_get_current_source_description($attr) {
$external_ip = geoip_detect2_get_current_source_description();
return $external_ip;
}
add_shortcode('geoip_detect2_get_current_source_description', 'geoip_detect2_shortcode_get_current_source_description');