-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclass.country-select.php
More file actions
67 lines (48 loc) · 1.68 KB
/
class.country-select.php
File metadata and controls
67 lines (48 loc) · 1.68 KB
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
<?php
namespace WPGMZA;
class CountrySelect extends DOMDocument {
private static $cachedJson;
public function __construct($options=null) {
if(empty(CountrySelect::$cachedJson)) {
$str = file_get_contents( plugin_dir_path(WPGMZA_FILE) . 'js/countries.json' );
$json = json_decode($str);
CountrySelect::$cachedJson = $json;
}
DOMDocument::__construct();
if(!$options)
$options = array();
$this->loadHTML('<select></select>');
$select = $this->querySelector('select');
if(!empty($options['name']))
$select->setAttribute('name', $options['name']);
$option = $this->createElement('option');
$option->setAttribute('value', '');
$option->appendText(__('Please select','wp-google-maps'));
$select->appendChild($option);
foreach(CountrySelect::$cachedJson as $country) {
$code = false;
if(!empty($country->alpha2Code)){
/* We have the alpha 2 code, which will work far more consistently */
$code = strtolower($country->alpha2Code);
} else {
if(!empty($country->topLevelDomain[0])){
/* Fallback to the TLD */
$code = str_replace('.', '', $country->topLevelDomain[0]);
}
}
if(empty($code)){
continue;
}
$name = $country->name;
$option = $this->createElement('option');
$option->setAttribute('value', $code);
$option->appendText($name);
if(!empty($code) && !empty($options['value']) && $code == $options['value']){
$option->setAttribute("selected", "selected");
}
$select->appendChild($option);
}
/* Developer Hook (Action) - Alter the country select output, passes DOMElement for mutation */
do_action("wpgmza_country_select_created", $select);
}
}