-
Notifications
You must be signed in to change notification settings - Fork 0
/
states-cities-and-places-for-woocommerce.php
335 lines (279 loc) · 11.6 KB
/
states-cities-and-places-for-woocommerce.php
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* Plugin Name: Myanmar States & Cities for WooCommerce
* Original Plugin URI: https://github.com/chitezh/woocommerce_states_places
* Description: WooCommerce plugin for listing states, cities, places, local government areas and towns in Myanmar Country.
* Version: 9.0
* Author: Hein Htet Kyaw
* Author URI: https://github.com/h2kyaw
* Developer: Hein Htet Kyaw
* Developer URI: https://linkedin.com/in/h2kyaw/
* Main Contributors: yordansoares, luisurrutiaf
* Re-Contributor: HeinHtetKyaw
* Text Domain: myanmar-states-and-cities-for-woocommerce
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Requires at least: 4.0
* Tested up to: 5.6
* WC requires at least: 3.0.x
* WC tested up to: 4.8
*/
/**
* Die if accessed directly
*/
defined( 'ABSPATH' ) or die( __('You can not access this file directly!', 'myanmar-states-and-cities-for-woocommerce') );
/**
* Check if WooCommerce is active
*/
if(in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
class WC_States_Places {
const VERSION = '1.3.2';
private $states;
private $places;
/**
* Construct class
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'init') );
}
/**
* WC init
*/
public function init() {
$this->init_textdomain();
$this->init_fields();
$this->init_states();
$this->init_places();
}
/**
* Load text domain for internationalitation
*/
public function init_textdomain() {
load_plugin_textdomain('myanmar-states-and-cities-for-woocommerce', FALSE, dirname(plugin_basename(__FILE__)) . '/languages' );
}
/**
* WC Fields init
*/
public function init_fields() {
add_filter('woocommerce_default_address_fields', array($this, 'wc_change_state_and_city_order'));
}
/**
* WC States init
*/
public function init_states() {
add_filter('woocommerce_states', array($this, 'wc_states'));
}
/**
* WC Places init
*/
public function init_places() {
add_filter( 'woocommerce_billing_fields', array( $this, 'wc_billing_fields' ), 10, 2 );
add_filter( 'woocommerce_shipping_fields', array( $this, 'wc_shipping_fields' ), 10, 2 );
add_filter( 'woocommerce_form_field_city', array( $this, 'wc_form_field_city' ), 10, 4 );
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
}
/**
* Change the order of State and City fields to have more sense with the steps of form
* @param mixed $fields
* @return mixed
*/
public function wc_change_state_and_city_order($fields) {
$fields['state']['priority'] = 70;
$fields['city']['priority'] = 80;
/* translators: Translate it to the name of the State level territory division, e.g. "State", "Province", "Department" */
$fields['state']['label'] = __('State', 'myanmar-states-and-cities-for-woocommerce');
/* translators: Translate it to the name of the City level territory division, e.g. "City, "Municipality", "District" */
$fields['city']['label'] = __('City', 'myanmar-states-and-cities-for-woocommerce');
return $fields;
}
/**
* Implement WC States
* @param mixed $states
* @return mixed
*/
public function wc_states() {
//get countries allowed by store owner
$allowed = $this->get_store_allowed_countries();
$states = array();
if (!empty( $allowed ) ) {
foreach ($allowed as $code => $country) {
if (! isset( $states[$code] ) && file_exists($this->get_plugin_path() . '/states/' . $code . '.php')) {
include($this->get_plugin_path() . '/states/' . $code . '.php');
}
}
}
return $states;
}
/**
* Modify billing field
* @param mixed $fields
* @param mixed $country
* @return mixed
*/
public function wc_billing_fields( $fields, $country ) {
$fields['billing_city']['type'] = 'city';
return $fields;
}
/**
* Modify shipping field
* @param mixed $fields
* @param mixed $country
* @return mixed
*/
public function wc_shipping_fields( $fields, $country ) {
$fields['shipping_city']['type'] = 'city';
return $fields;
}
/**
* Implement places/city field
* @param mixed $field
* @param string $key
* @param mixed $args
* @param string $value
* @return mixed
*/
public function wc_form_field_city($field, $key, $args, $value ) {
// Do we need a clear div?
if ( ( ! empty( $args['clear'] ) ) ) {
$after = '<div class="clear"></div>';
} else {
$after = '';
}
// Required markup
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
} else {
$required = '';
}
// Custom attribute handling
$custom_attributes = array();
if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
// Validate classes
if ( ! empty( $args['validate'] ) ) {
foreach( $args['validate'] as $validate ) {
$args['class'][] = 'validate-' . $validate;
}
}
// field p and label
$field = '<p class="form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $args['id'] ) . '_field">';
if ( $args['label'] ) {
$field .= '<label for="' . esc_attr( $args['id'] ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) .'">' . $args['label']. $required . '</label>';
}
// Get Country
$country_key = $key == 'billing_city' ? 'billing_country' : 'shipping_country';
$current_cc = WC()->checkout->get_value( $country_key );
$state_key = $key == 'billing_city' ? 'billing_state' : 'shipping_state';
$current_sc = WC()->checkout->get_value( $state_key );
// Get country places
$places = $this->get_places( $current_cc );
if ( is_array( $places ) ) {
$field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="city_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) .'" ' . implode( ' ', $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '">';
$field .= '<option value="">'. __( 'Select an option…', 'woocommerce' ) .'</option>';
if ( $current_sc && array_key_exists( $current_sc, $places ) ) {
$dropdown_places = $places[ $current_sc ];
} else if ( is_array($places) && isset($places[0])) {
$dropdown_places = $places;
sort( $dropdown_places );
} else {
$dropdown_places = $places;
}
foreach ( $dropdown_places as $city_name ) {
if(!is_array($city_name)) {
$field .= '<option value="' . esc_attr( $city_name ) . '" '.selected( $value, $city_name, false ) . '>' . $city_name .'</option>';
}
}
$field .= '</select>';
} else {
$field .= '<input type="text" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) .'" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
}
// field description and close wrapper
if ( $args['description'] ) {
$field .= '<span class="description">' . esc_attr( $args['description'] ) . '</span>';
}
$field .= '</p>' . $after;
return $field;
}
/**
* Get places
* @param string $p_code(default:)
* @return mixed
*/
public function get_places( $p_code = null ) {
if ( empty( $this->places ) ) {
$this->load_country_places();
}
if ( ! is_null( $p_code ) ) {
return isset( $this->places[ $p_code ] ) ? $this->places[ $p_code ] : false;
} else {
return $this->places;
}
}
/**
* Get country places
* @return mixed
*/
public function load_country_places() {
global $places;
$allowed = $this->get_store_allowed_countries();
if ( $allowed ) {
foreach ( $allowed as $code => $country ) {
if ( ! isset( $places[ $code ] ) && file_exists( $this->get_plugin_path() . '/places/' . $code . '.php' ) ) {
include( $this->get_plugin_path() . '/places/' . $code . '.php' );
}
}
}
$this->places = $places;
}
/**
* Load scripts
*/
public function load_scripts() {
if ( is_cart() || is_checkout() || is_wc_endpoint_url( 'edit-address' ) ) {
$city_select_path = $this->get_plugin_url() . 'js/place-select.js';
wp_enqueue_script( 'wc-city-select', $city_select_path, array( 'jquery', 'woocommerce' ), self::VERSION, true );
$places = json_encode( $this->get_places() );
wp_localize_script( 'wc-city-select', 'wc_city_select_params', array(
'cities' => $places,
'i18n_select_city_text' => esc_attr__( 'Select an option…', 'woocommerce' )
) );
}
}
/**
* Get plugin root path
* @return mixed
*/
private function get_plugin_path() {
if (isset($this->plugin_path)) {
return $this->plugin_path;
}
$path = $this->plugin_path = plugin_dir_path( __FILE__ );
return untrailingslashit($path);
}
/**
* Get Store allowed countries
* @return mixed
*/
private function get_store_allowed_countries() {
return array_merge( WC()->countries->get_allowed_countries(), WC()->countries->get_shipping_countries() );
}
/**
* Get plugin url
* @return mixed
*/
public function get_plugin_url() {
if (isset($this->plugin_url)) {
return $this->plugin_url;
}
return $this->plugin_url = plugin_dir_url( __FILE__ );
}
}
/**
* Instantiate class
*/
$GLOBALS['wc_states_places'] = new WC_States_Places();
};