PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.13.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.13.0
5.13.0 5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / plugins / UserCountry / functions.php
matomo / app / plugins / UserCountry Last commit date
Categories 5 years ago Columns 4 years ago Commands 5 years ago Diagnostic 5 years ago LocationProvider 3 years ago ProfileSummary 5 years ago Reports 3 years ago config 6 years ago lang 4 years ago stylesheets 4 years ago templates 4 years ago vue 4 years ago API.php 5 years ago Archiver.php 4 years ago Controller.php 4 years ago LocationProvider.php 4 years ago Menu.php 5 years ago UserCountry.php 4 years ago VisitorDetails.php 5 years ago VisitorGeolocator.php 4 years ago functions.php 4 years ago
functions.php
262 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9
10 namespace Piwik\Plugins\UserCountry;
11
12 use Piwik\DataTable;
13 use Piwik\Piwik;
14 use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2;
15 use Piwik\Tracker\Visit;
16
17 /**
18 * Return the flag image path for a given country
19 *
20 * @param string $code ISO country code
21 * @return string Flag image path
22 */
23 function getFlagFromCode($code)
24 {
25 if (strtolower($code) == 'ti') {
26 $code = 'cn';
27 }
28
29 $pathInPiwik = 'plugins/Morpheus/icons/dist/flags/%s.png';
30 $pathWithCode = sprintf($pathInPiwik, $code);
31 $absolutePath = PIWIK_INCLUDE_PATH . '/' . $pathWithCode;
32 if (file_exists($absolutePath)) {
33 return $pathWithCode;
34 }
35 return sprintf($pathInPiwik, Visit::UNKNOWN_CODE);
36 }
37
38 /**
39 * Returns the translated continent name for a given continent code
40 *
41 * @param string $label Continent code
42 * @return string Continent name
43 */
44 function continentTranslate($label)
45 {
46 if ($label == 'unk' || $label == '') {
47 return Piwik::translate('General_Unknown');
48 }
49 return Piwik::translate('Intl_Continent_' . $label);
50 }
51
52 /**
53 * Returns the translated country name for a given country code
54 *
55 * @param string $label country code
56 * @return string Country name
57 */
58 function countryTranslate($label)
59 {
60 if ($label == Visit::UNKNOWN_CODE || $label == '') {
61 return Piwik::translate('General_Unknown');
62 }
63
64 if (strtolower($label) == 'ti') {
65 $label = 'cn';
66 }
67
68 // Try to get name from Intl plugin
69 $key = 'Intl_Country_' . strtoupper($label);
70 $country = Piwik::translate($key);
71
72 if ($country != $key) {
73 return $country;
74 }
75
76 // Handle special country codes
77 $key = 'UserCountry_country_' . $label;
78 $country = Piwik::translate($key);
79
80 if ($country != $key) {
81 return $country;
82 }
83
84 return Piwik::translate('General_Unknown');
85 }
86
87 /**
88 * Splits a label by a certain separator and returns the N-th element.
89 *
90 * @param string $label
91 * @param string $separator eg. ',' or '|'
92 * @param int $index The element index to extract.
93 * @param mixed $emptyValue The value to remove if the element is absent. Defaults to false,
94 * so no new metadata/column is added.
95 * @return string|false Returns false if $label == DataTable::LABEL_SUMMARY_ROW, otherwise
96 * explode($separator, $label)[$index].
97 */
98 function getElementFromStringArray($label, $separator, $index, $emptyValue = false)
99 {
100 if ($label == DataTable::LABEL_SUMMARY_ROW) {
101 return false; // so no metadata/column is added
102 }
103
104 $segments = explode($separator, $label);
105 return empty($segments[$index]) ? $emptyValue : $segments[$index];
106 }
107
108 /**
109 * Returns region name for the given regionCode / countryCode combination
110 * using the currently set location provider
111 *
112 * @param string $countryCode
113 * @param string $regionCode
114 * @return string
115 */
116 function getRegionNameFromCodes($countryCode, $regionCode)
117 {
118 $name = GeoIp2::getRegionNameFromCodes($countryCode, $regionCode);
119
120 // fallback if no translation with GeoIP2
121 if ($name == Piwik::translate('General_Unknown')) {
122 $name = getLegacyRegionNameFromCodes($countryCode, $regionCode);
123 }
124
125 return $name;
126 }
127
128
129 /**
130 * Returns a region name for a country code + region code.
131 *
132 * @param string $countryCode
133 * @param string $regionCode
134 * @return string The region name or 'Unknown' (translated).
135 */
136 function getLegacyRegionNameFromCodes($countryCode, $regionCode)
137 {
138 $regionNames = getRegionNames();
139
140 $countryCode = strtoupper($countryCode);
141 $regionCode = strtoupper($regionCode);
142
143 // ensure tibet is shown as region of china
144 if ($countryCode == 'TI' && $regionCode == '1') {
145 $regionCode = '14';
146 $countryCode = 'CN';
147 }
148
149 if (isset($regionNames[$countryCode][$regionCode])) {
150 return $regionNames[$countryCode][$regionCode];
151 } else {
152 return Piwik::translate('General_Unknown');
153 }
154 }
155
156 /**
157 * Returns an array of region names mapped by country code & region code.
158 *
159 * @return array
160 */
161 function getRegionNames()
162 {
163 static $regionNames;
164
165 if (is_null($regionNames)) {
166 $GEOIP_REGION_NAME = array();
167 require_once PIWIK_INCLUDE_PATH . '/libs/MaxMindGeoIP/geoipregionvars.php';
168 $regionNames = $GEOIP_REGION_NAME;
169 }
170
171 return $regionNames;
172 }
173
174
175 /**
176 * Returns the region name using the label of a Visits by Region report.
177 *
178 * @param string $label A label containing a region code followed by '|' and a country code, eg,
179 * 'P3|GB'.
180 * @return string|false The region name or false if $label == DataTable::LABEL_SUMMARY_ROW.
181 */
182 function getRegionName($label)
183 {
184 if ($label == DataTable::LABEL_SUMMARY_ROW) {
185 return false; // so no metadata/column is added
186 }
187
188 if ($label == '') {
189 return Piwik::translate('General_Unknown');
190 }
191
192 list($regionCode, $countryCode) = explode(Archiver::LOCATION_SEPARATOR, $label);
193 return getRegionNameFromCodes($countryCode, $regionCode);
194 }
195
196 /**
197 * Returns the name of a region + the name of the region's country using the label of
198 * a Visits by Region report.
199 *
200 * @param string $label A label containing a region code followed by '|' and a country code, eg,
201 * 'P3|GB'.
202 * @return string|false eg. 'Ile de France, France' or false if $label == DataTable::LABEL_SUMMARY_ROW.
203 */
204 function getPrettyRegionName($label)
205 {
206 if ($label == DataTable::LABEL_SUMMARY_ROW) {
207 return $label;
208 }
209
210 if ($label == '') {
211 return Piwik::translate('General_Unknown');
212 }
213
214 list($regionCode, $countryCode) = explode(Archiver::LOCATION_SEPARATOR, $label);
215
216 $result = getRegionNameFromCodes($countryCode, $regionCode);
217 if ($countryCode != Visit::UNKNOWN_CODE && $countryCode != '') {
218 $result .= ', ' . countryTranslate($countryCode);
219 }
220 return $result;
221 }
222
223 /**
224 * Returns the name of a city + the name of its region + the name of its country using
225 * the label of a Visits by City report.
226 *
227 * @param string $label A label containing a city name, region code + country code,
228 * separated by two '|' chars: 'Paris|A8|FR'
229 * @return string|false eg. 'Paris, Ile de France, France' or false if $label ==
230 * DataTable::LABEL_SUMMARY_ROW.
231 */
232 function getPrettyCityName($label)
233 {
234 if ($label == DataTable::LABEL_SUMMARY_ROW) {
235 return $label;
236 }
237
238 if ($label == '') {
239 return Piwik::translate('General_Unknown');
240 }
241
242 // get city name, region code & country code
243 $parts = explode(Archiver::LOCATION_SEPARATOR, $label);
244 $cityName = $parts[0];
245 $regionCode = $parts[1];
246 $countryCode = @$parts[2];
247
248 if ($cityName == Visit::UNKNOWN_CODE || $cityName == '') {
249 $cityName = Piwik::translate('General_Unknown');
250 }
251
252 $result = $cityName;
253 if ($countryCode != Visit::UNKNOWN_CODE && $countryCode != '') {
254 if ($regionCode != '' && $regionCode != Visit::UNKNOWN_CODE) {
255 $regionName = getRegionNameFromCodes($countryCode, $regionCode);
256 $result .= ', ' . $regionName;
257 }
258 $result .= ', ' . countryTranslate($countryCode);
259 }
260 return $result;
261 }
262