forked from luizhguimaraes/acf-brazilian-city
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathacf-brazilian-city-field.php
More file actions
executable file
·220 lines (173 loc) · 6.26 KB
/
Copy pathacf-brazilian-city-field.php
File metadata and controls
executable file
·220 lines (173 loc) · 6.26 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
class acf_brazilian_city_field extends acf_field {
var $settings, $defaults;
public function __construct()
{
$this->name = 'COUNTRY_FIELD';
$this->label = 'Cidade Brasileira';
$this->category = __("Basic",'acf');
$this->defaults = array(
"city_name" => '',
"state_name" => '',
"city_id" => 0,
"state_id" => '',
);
parent::__construct();
$this->settings = array(
'version' => '1.1.0',
'url' => plugin_dir_url( __FILE__ ),
'path' => plugin_dir_path( __FILE__ )
);
}
function render_field( $field )
{
global $wpdb;
$field['value'] = isset($field['value']) ? $field['value'] : '';
$fieldName = $field['name'];
$city_id = (isset($field['value']['city_id'])) ? $field['value']['city_id'] : 0;
$state_id = (isset($field['value']['state_id'])) ? $field['value']['state_id'] : 0;
$cities = $this->list_cities($state_id);
//Carregando Estados
$states = array("__" => "");
$statesResults = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."states ORDER BY name ASC");
foreach ($statesResults AS $state)
{
$states[$state->id] = $state->name;
}
?>
<ul class="country-selector-list">
<li id="field-<?php echo $fieldName; ?>[state_id]">
<strong><?php _e("Selecione o estado", 'acf'); ?></strong><br />
<?php
$state_field = $field['name'] . '[state_id]';
acf_render_field(array(
'type' => 'select',
'name' => $state_field,
'value' => $state_id,
'choices' => $states,
));
?>
</li>
<li id="field-<?php echo $fieldName; ?>[city_id]">
<strong><?php _e("Selecione a cidade", 'acf'); ?></strong><br />
<?php
$city_field = $field['name'] . '[city_id]';
acf_render_field(array(
'type' => 'select',
'name' => $city_field,
'value' => $city_id,
'choices' => $cities,
));
?>
</li>
</ul>
<?php
}
function update_value($value, $post_id, $field)
{
$value['city_name'] = $this->city_name($value['city_id']);
$value['state_name'] = (isset($value['state_id']) && $value['state_id'] !== 0) ? $this->state_name($value['state_id']) : '';
return $value;
}
function format_value_for_api($value, $post_id, $field)
{
$value['city_name'] = $this->city_name($value['city_id']);
$value['state_name'] = (isset($value['state_id']) && $value['state_id'] !== 0) ? $this->state_name($value['state_id']) : '';
return $value;
}
function input_admin_enqueue_scripts()
{
wp_register_script('acf-brazilian-city', $this->settings['url'] . 'js/brazilian-city.js', array('acf-input'), $this->settings['version']);
wp_register_script('acf-input-chosen', $this->settings['url'] . 'js/chosen.jquery.min.js', array('jquery'), $this->settings['version']);
wp_register_style('acf-input-chosen', $this->settings['url'] . 'css/chosen.min.css', array(), $this->settings['version']);
wp_localize_script( 'acf-brazilian-city', "AcfBrazilianCity", array(
"ajaxurl" => admin_url("admin-ajax.php"),
) );
// scripts
wp_enqueue_script(array(
'acf-brazilian-city',
'acf-input-chosen',
));
// styles
wp_enqueue_style(array(
'acf-input-chosen',
));
}
/**
* Retorna todas as cidades de um determinado estado
* @global type $wpdb
* @param string $state_id identificador do estados Ex.: 'ES'
* @return array "ID" => "Nome"
*/
protected function list_cities($state_id)
{
global $wpdb;
$cities_results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."cities WHERE state_id ='".$state_id."' ORDER BY name ASC");
$cities = array();
foreach ($cities_results AS $city)
{
$cities[$city->id] = $city->name;
}
return $cities;
}
/**
* Retorna o nome de uma cidade especifica
* @global type $wpdb
* @param int $city_id identificador da cidade
* @return mixed
*/
protected function city_name($city_id)
{
global $wpdb;
$city = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."cities WHERE id = '".$city_id."'");
if ($city)
{
return $city->name;
}
else
{
return false;
}
}
/**
* Retorna o nome de um estado especifico
* @global type $wpdb
* @param int $state_id
* @return mixed
*/
protected function state_name($state_id)
{
global $wpdb;
$state = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."states WHERE id = '".$state_id."'");
if ($state) {
return $state->name;
} else {
return false;
}
}
}
add_action('wp_ajax_get_list_state_cities', 'get_list_state_cities');
add_action('wp_ajax_nopriv_gt_list_state_cities', 'et_list_state_cities');
/**
* Disponibilia via ajax a lista de cidades para um determinado estado
* @global type $wpdb
*/
function get_list_state_cities()
{
global $wpdb;
$state_id = substr(trim($_REQUEST['stateId']),0,2);
$cities_results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."cities WHERE state_id ='".$state_id."' ORDER BY name ASC");
$cities = array();
if ($cities_results)
{
foreach ($cities_results AS $city)
{
$cities[$city->id] = $city->name;
}
}
ob_end_clean();
header("Content-Type: application/json");
echo json_encode($cities);
die();
}
new acf_brazilian_city_field();