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
|
<?php
class CountryStatesHooks {
// Register any render callbacks with the parser
public static function onParserFirstCallInit( Parser $parser ) {
$parser->setFunctionHook( 'countries', [ self::class, 'renderCountries' ] );
$parser->setFunctionHook( 'states', [ self::class, 'renderStates' ] );
}
public static function renderCountries( Parser $parser, $param1 = '', $param2 = '', $param3 = '' ) {
$path = realpath(dirname(__FILE__));
$str = file_get_contents($path . '/countries-states.json');
$json = json_decode($str);
$end = '';
// echo '<pre>' . print_r($json, true) . '</pre>';
// return $output;
foreach($json->countries as $item)
{
if ($param1) {
if($item->code == $param1)
{
// echo $item->name;
$countries[] = $item->name;
}
} else {
// $countries[] = $item->name;
$countries[] = $item->code;
}
}
foreach($countries as $item) {
$end .= $item . ',';
}
return $end;
}
public static function renderStates( Parser $parser, $param1 = '', $param2 = '', $param3 = '' ) {
$path = realpath(dirname(__FILE__));
$str = file_get_contents($path . '/countries-states.json');
$json = json_decode($str);
$end = '';
// echo '<pre>' . print_r($json, true) . '</pre>';
// return $output;
foreach($json->countries as $item)
{
if($item->code == $param1)
{
// echo $item->name;
foreach($item->states as $state) {
$states[] = $state->name;
}
}
}
// return $states;
foreach($states as $item) {
$end .= $item . ',';
}
return $end;
}
}
|