forked from backdrop-contrib/ip_geoloc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip_geoloc.tokens.inc
71 lines (64 loc) · 2.03 KB
/
ip_geoloc.tokens.inc
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
<?php
/**
* Implements hook_token_info().
*/
function ip_geoloc_token_info() {
$types['visitor-location'] = array(
'name' => t('Visitor location'),
'description' => t("Tokens related to the visitor's location"),
);
$token_keys = array(
'country',
'country_code',
'region',
'region_code',
'city',
'locality',
'route',
'street_number',
'postal_code',
'administrative_area_level_1',
'formatted_address',
'latitude',
'longitude',
'ip_address',
'provider',
);
$token_info = array();
foreach ($token_keys as $key) {
$name = str_replace('_', ' ', $key);
$token_info[$key] = array(
'name' => $name,
'description' => t("Visitor's @name", array('@name' => $name)),
);
}
$token_info['provider']['description'] = t('Provider of location, for example <em>user</em> or <em>google</em>');
return array(
'types' => $types,
'tokens' => array('visitor-location' => $token_info),
);
}
/**
* Implements hook_tokens().
*/
function ip_geoloc_tokens($type, $tokens, array $data = array(), array $options = array()) {
$config = config('ip_geoloc.settings');
$replacements = array();
if ($type == 'visitor-location') {
$is_sanitize = !empty($options['sanitize']);
$location = ip_geoloc_get_visitor_location();
foreach ($tokens as $key => $original) {
if (isset($location[$key])) {
$replacements[$original] = $is_sanitize ? filter_xss_admin($location[$key]) : $location[$key];
}
}
if (!empty($replacements['[visitor-location:formatted_address]'])) {
$need_street = $config->get('ip_geoloc_return_address_street');
$need_locality = $config->get('ip_geoloc_return_address_locality');
$need_country = $config->get('ip_geoloc_return_address_country');
$address = _ip_geoloc_custom_formatted_address($location, $need_street, $need_locality, $need_country);
$replacements['[visitor-location:formatted_address]'] = $is_sanitize ? filter_xss_admin($address) : $address;
}
}
return $replacements;
}