Completed
Push — master ( e99798...67e7b0 )
by Claudio
18s
created

WC_Countries::get_address_formats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 47
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 39
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 47
rs 9.0303
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * WooCommerce countries
9
 *
10
 * The WooCommerce countries class stores country/state data.
11
 *
12
 * @class       WC_Countries
13
 * @version     2.3.0
14
 * @package     WooCommerce/Classes
15
 * @category    Class
16
 * @author      WooThemes
17
 */
18
class WC_Countries {
19
20
	/** @var array Array of locales */
21
	public $locale;
22
23
	/** @var array Array of address formats for locales */
24
	public $address_formats;
25
26
	/**
27
	 * Auto-load in-accessible properties on demand.
28
	 * @param  mixed $key
29
	 * @return mixed
30
	 */
31
	public function __get( $key ) {
32
		if ( 'countries' == $key ) {
33
			return $this->get_countries();
34
		} elseif ( 'states' == $key ) {
35
			return $this->get_states();
36
		}
37
	}
38
39
	/**
40
	 * Get all countries.
41
	 * @return array
42
	 */
43
	public function get_countries() {
44
		if ( empty( $this->countries ) ) {
45
			$this->countries = apply_filters( 'woocommerce_countries', include( WC()->plugin_path() . '/i18n/countries.php' ) );
46
			if ( apply_filters( 'woocommerce_sort_countries', true ) ) {
47
				asort( $this->countries );
48
			}
49
		}
50
		return $this->countries;
51
	}
52
53
	/**
54
	 * Get all continents.
55
	 * @return array
56
	 */
57
	public function get_continents() {
58
		if ( empty( $this->continents ) ) {
59
			$this->continents = apply_filters( 'woocommerce_continents', include( WC()->plugin_path() . '/i18n/continents.php' ) );
60
		}
61
		return $this->continents;
62
	}
63
64
	/**
65
	 * Get continent code for a country code.
66
	 * @since 2.6.0
67
	 * @param string $cc string
68
	 * @return string
69
	 */
70
	public function get_continent_code_for_country( $cc ) {
71
		$cc                 = trim( strtoupper( $cc ) );
72
		$continents         = $this->get_continents();
73
		$continents_and_ccs = wp_list_pluck( $continents, 'countries' );
74
		foreach ( $continents_and_ccs as $continent_code => $countries ) {
75
			if ( false !== array_search( $cc, $countries ) ) {
76
				return $continent_code;
77
			}
78
		}
79
		return '';
80
	}
81
82
	/**
83
	 * Load the states.
84
	 */
85
	public function load_country_states() {
86
		global $states;
87
88
		// States set to array() are blank i.e. the country has no use for the state field.
89
		$states = array(
90
			'AF' => array(),
91
			'AT' => array(),
92
			'AX' => array(),
93
			'BE' => array(),
94
			'BI' => array(),
95
			'CZ' => array(),
96
			'DE' => array(),
97
			'DK' => array(),
98
			'EE' => array(),
99
			'FI' => array(),
100
			'FR' => array(),
101
			'IS' => array(),
102
			'IL' => array(),
103
			'KR' => array(),
104
			'NL' => array(),
105
			'NO' => array(),
106
			'PL' => array(),
107
			'PT' => array(),
108
			'SG' => array(),
109
			'SK' => array(),
110
			'SI' => array(),
111
			'LK' => array(),
112
			'SE' => array(),
113
			'VN' => array(),
114
		);
115
116
		// Load only the state files the shop owner wants/needs.
117
		$allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() );
118
119
		if ( ! empty( $allowed ) ) {
120
			foreach ( $allowed as $code => $country ) {
121
				if ( ! isset( $states[ $code ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ) ) {
122
					include( WC()->plugin_path() . '/i18n/states/' . $code . '.php' );
123
				}
124
			}
125
		}
126
127
		$this->states = apply_filters( 'woocommerce_states', $states );
128
	}
129
130
	/**
131
	 * Get the states for a country.
132
	 * @param  string $cc country code
133
	 * @return array of states
134
	 */
135
	public function get_states( $cc = null ) {
136
		if ( empty( $this->states ) ) {
137
			$this->load_country_states();
138
		}
139
140
		if ( ! is_null( $cc ) ) {
141
			return isset( $this->states[ $cc ] ) ? $this->states[ $cc ] : false;
142
		} else {
143
			return $this->states;
144
		}
145
	}
146
147
	/**
148
	 * Get the base country for the store.
149
	 * @return string
150
	 */
151
	public function get_base_country() {
152
		$default = wc_get_base_location();
153
		return apply_filters( 'woocommerce_countries_base_country', $default['country'] );
154
	}
155
156
	/**
157
	 * Get the base state for the store.
158
	 * @return string
159
	 */
160
	public function get_base_state() {
161
		$default = wc_get_base_location();
162
		return apply_filters( 'woocommerce_countries_base_state', $default['state'] );
163
	}
164
165
	/**
166
	 * Get the base city for the store.
167
	 * @return string
168
	 */
169
	public function get_base_city() {
170
		return apply_filters( 'woocommerce_countries_base_city', '' );
171
	}
172
173
	/**
174
	 * Get the base postcode for the store.
175
	 * @return string
176
	 */
177
	public function get_base_postcode() {
178
		return apply_filters( 'woocommerce_countries_base_postcode', '' );
179
	}
180
181
	/**
182
	 * Get the allowed countries for the store.
183
	 * @return array
184
	 */
185
	public function get_allowed_countries() {
186
		if ( 'all' === get_option( 'woocommerce_allowed_countries' ) ) {
187
			return $this->countries;
188
		}
189
190
		if( 'all_except' === get_option( 'woocommerce_allowed_countries' ) ) {
191
			$except_countries = get_option( 'woocommerce_all_except_countries', array() );
192
193
			if ( ! $except_countries ) {
194
				return $this->countries;
195
			} else {
196
				$all_except_countries = $this->countries;
197
				foreach( $except_countries as $country ) {
198
					unset( $all_except_countries[ $country ] );
199
				}
200
				return apply_filters( 'woocommerce_countries_allowed_countries', $all_except_countries );
201
			}
202
		}
203
204
		$countries = array();
205
206
		$raw_countries = get_option( 'woocommerce_specific_allowed_countries', array() );
207
208
		if ( $raw_countries ) {
209
			foreach ( $raw_countries as $country ) {
210
				$countries[ $country ] = $this->countries[ $country ];
211
			}
212
		}
213
214
		return apply_filters( 'woocommerce_countries_allowed_countries', $countries );
215
	}
216
217
	/**
218
	 * Get the countries you ship to.
219
	 * @return array
220
	 */
221
	public function get_shipping_countries() {
222
		if ( '' === get_option( 'woocommerce_ship_to_countries' ) ) {
223
			return $this->get_allowed_countries();
224
		}
225
226
		if ( 'all' === get_option( 'woocommerce_ship_to_countries' ) ) {
227
			return $this->countries;
228
		}
229
230
		$countries = array();
231
232
		$raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
233
234
		if ( $raw_countries ) {
235
			foreach ( $raw_countries as $country ) {
236
				$countries[ $country ] = $this->countries[ $country ];
237
			}
238
		}
239
240
		return apply_filters( 'woocommerce_countries_shipping_countries', $countries );
241
	}
242
243
	/**
244
	 * Get allowed country states.
245
	 * @return array
246
	 */
247
	public function get_allowed_country_states() {
248
		if ( get_option( 'woocommerce_allowed_countries' ) !== 'specific' ) {
249
			return $this->states;
250
		}
251
252
		$states = array();
253
254
		$raw_countries = get_option( 'woocommerce_specific_allowed_countries' );
255
256
		if ( $raw_countries ) {
257
			foreach ( $raw_countries as $country ) {
258
				if ( isset( $this->states[ $country ] ) ) {
259
					$states[ $country ] = $this->states[ $country ];
260
				}
261
			}
262
		}
263
264
		return apply_filters( 'woocommerce_countries_allowed_country_states', $states );
265
	}
266
267
	/**
268
	 * Get shipping country states.
269
	 * @return array
270
	 */
271
	public function get_shipping_country_states() {
272
		if ( get_option( 'woocommerce_ship_to_countries' ) == '' ) {
273
			return $this->get_allowed_country_states();
274
		}
275
276
		if ( get_option( 'woocommerce_ship_to_countries' ) !== 'specific' ) {
277
			return $this->states;
278
		}
279
280
		$states = array();
281
282
		$raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
283
284
		if ( $raw_countries ) {
285
			foreach ( $raw_countries as $country ) {
286
				if ( ! empty( $this->states[ $country ] ) ) {
287
					$states[ $country ] = $this->states[ $country ];
288
				}
289
			}
290
		}
291
292
		return apply_filters( 'woocommerce_countries_shipping_country_states', $states );
293
	}
294
295
	/**
296
	 * Gets an array of countries in the EU.
297
	 *
298
	 * MC (monaco) and IM (isle of man, part of UK) also use VAT.
299
	 *
300
	 * @param  $type Type of countries to retrieve. Blank for EU member countries. eu_vat for EU VAT countries.
301
	 * @return string[]
302
	 */
303
	public function get_european_union_countries( $type = '' ) {
304
		$countries = array( 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HU', 'HR', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK' );
305
306
		if ( 'eu_vat' === $type ) {
307
			$countries[] = 'MC';
308
			$countries[] = 'IM';
309
		}
310
311
		return $countries;
312
	}
313
314
	/**
315
	 * Gets the correct string for shipping - either 'to the' or 'to'
316
	 * @return string
317
	 */
318
	public function shipping_to_prefix( $country_code = '' ) {
319
		$country_code = $country_code ? $country_code : WC()->customer->get_shipping_country();
320
		$countries    = array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' );
321
		$return       = in_array( $country_code, $countries ) ? __( 'to the', 'woocommerce' ) : __( 'to', 'woocommerce' );
322
323
		return apply_filters( 'woocommerce_countries_shipping_to_prefix', $return, $country_code );
324
	}
325
326
	/**
327
	 * Prefix certain countries with 'the'
328
	 * @return string
329
	 */
330
	public function estimated_for_prefix( $country_code = '' ) {
331
		$country_code = $country_code ? $country_code : $this->get_base_country();
332
		$countries    = array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' );
333
		$return       = in_array( $country_code, $countries ) ? __( 'the', 'woocommerce' ) . ' ' : '';
334
335
		return apply_filters( 'woocommerce_countries_estimated_for_prefix', $return, $country_code );
336
	}
337
338
	/**
339
	 * Correctly name tax in some countries VAT on the frontend.
340
	 * @return string
341
	 */
342
	public function tax_or_vat() {
343
		$return = in_array( $this->get_base_country(), $this->get_european_union_countries( 'eu_vat' ) ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
344
345
		return apply_filters( 'woocommerce_countries_tax_or_vat', $return );
346
	}
347
348
	/**
349
	 * Include the Inc Tax label.
350
	 * @return string
351
	 */
352
	public function inc_tax_or_vat() {
353
		$return = in_array( $this->get_base_country(), $this->get_european_union_countries( 'eu_vat' ) ) ? __( '(incl. VAT)', 'woocommerce' ) : __( '(incl. tax)', 'woocommerce' );
354
355
		return apply_filters( 'woocommerce_countries_inc_tax_or_vat', $return );
356
	}
357
358
	/**
359
	 * Include the Ex Tax label.
360
	 * @return string
361
	 */
362
	public function ex_tax_or_vat() {
363
		$return = in_array( $this->get_base_country(), $this->get_european_union_countries( 'eu_vat' ) ) ? __( '(ex. VAT)', 'woocommerce' ) : __( '(ex. tax)', 'woocommerce' );
364
365
		return apply_filters( 'woocommerce_countries_ex_tax_or_vat', $return );
366
	}
367
368
	/**
369
	 * Outputs the list of countries and states for use in dropdown boxes.
370
	 * @param string $selected_country (default: '')
371
	 * @param string $selected_state (default: '')
372
	 * @param bool $escape (default: false)
373
	 * @param bool   $escape (default: false)
374
	 */
375
	public function country_dropdown_options( $selected_country = '', $selected_state = '', $escape = false ) {
376
		if ( $this->countries ) foreach ( $this->countries as $key => $value ) :
377
			if ( $states = $this->get_states( $key ) ) :
378
				echo '<optgroup label="' . esc_attr( $value ) . '">';
379
					foreach ( $states as $state_key => $state_value ) :
380
						echo '<option value="' . esc_attr( $key ) . ':' . $state_key . '"';
381
382
						if ( $selected_country == $key && $selected_state == $state_key ) {
383
							echo ' selected="selected"';
384
						}
385
386
						echo '>' . $value . ' &mdash; ' . ( $escape ? esc_js( $state_value ) : $state_value ) . '</option>';
387
					endforeach;
388
				echo '</optgroup>';
389
			else :
390
				echo '<option';
391
				if ( $selected_country == $key && $selected_state == '*' ) {
392
					echo ' selected="selected"';
393
				}
394
				echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value ) : $value ) . '</option>';
395
			endif;
396
		endforeach;
397
	}
398
399
	/**
400
	 * Get country address formats.
401
	 * @return array
402
	 */
403
	public function get_address_formats() {
404
		if ( empty( $this->address_formats ) ) {
405
406
			// Common formats
407
			$postcode_before_city = "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}";
408
409
			// Define address formats
410
			$this->address_formats = apply_filters( 'woocommerce_localisation_address_formats', array(
411
				'default' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}",
412
				'AU' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
413
				'AT' => $postcode_before_city,
414
				'BE' => $postcode_before_city,
415
				'CA' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
416
				'CH' => $postcode_before_city,
417
				'CL' => "{company}\n{name}\n{address_1}\n{address_2}\n{state}\n{postcode} {city}\n{country}",
418
				'CN' => "{country} {postcode}\n{state}, {city}, {address_2}, {address_1}\n{company}\n{name}",
419
				'CZ' => $postcode_before_city,
420
				'DE' => $postcode_before_city,
421
				'EE' => $postcode_before_city,
422
				'FI' => $postcode_before_city,
423
				'DK' => $postcode_before_city,
424
				'FR' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city_upper}\n{country}",
425
				'HK' => "{company}\n{first_name} {last_name_upper}\n{address_1}\n{address_2}\n{city_upper}\n{state_upper}\n{country}",
426
				'HU' => "{name}\n{company}\n{city}\n{address_1}\n{address_2}\n{postcode}\n{country}",
427
				'IN' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} - {postcode}\n{state}, {country}",
428
				'IS' => $postcode_before_city,
429
				'IT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode}\n{city}\n{state_upper}\n{country}",
430
				'JP' => "{postcode}\n{state}{city}{address_1}\n{address_2}\n{company}\n{last_name} {first_name}\n{country}",
431
				'TW' => "{company}\n{last_name} {first_name}\n{address_1}\n{address_2}\n{state}, {city} {postcode}\n{country}",
432
				'LI' => $postcode_before_city,
433
				'NL' => $postcode_before_city,
434
				'NZ' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {postcode}\n{country}",
435
				'NO' => $postcode_before_city,
436
				'PL' => $postcode_before_city,
437
				'PT' => $postcode_before_city,
438
				'SK' => $postcode_before_city,
439
				'SI' => $postcode_before_city,
440
				'ES' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}",
441
				'SE' => $postcode_before_city,
442
				'TR' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city} {state}\n{country}",
443
				'US' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}, {state_code} {postcode}\n{country}",
444
				'VN' => "{name}\n{company}\n{address_1}\n{city}\n{country}",
445
			));
446
		}
447
448
		return $this->address_formats;
449
	}
450
451
	/**
452
	 * Get country address format.
453
	 * @param  array  $args (default: array())
454
	 * @return string address
455
	 */
456
	public function get_formatted_address( $args = array() ) {
457
		$default_args = array(
458
			'first_name' => '',
459
			'last_name'  => '',
460
			'company'    => '',
461
			'address_1'  => '',
462
			'address_2'  => '',
463
			'city'       => '',
464
			'state'      => '',
465
			'postcode'   => '',
466
			'country'    => ''
467
		);
468
469
		$args = array_map( 'trim', wp_parse_args( $args, $default_args ) );
470
471
		extract( $args );
472
473
		// Get all formats
474
		$formats = $this->get_address_formats();
475
476
		// Get format for the address' country
477
		$format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];
478
479
		// Handle full country name
480
		$full_country = ( isset( $this->countries[ $country ] ) ) ? $this->countries[ $country ] : $country;
481
482
		// Country is not needed if the same as base
483
		if ( $country == $this->get_base_country() && ! apply_filters( 'woocommerce_formatted_address_force_country_display', false ) ) {
484
			$format = str_replace( '{country}', '', $format );
485
		}
486
487
		// Handle full state name
488
		$full_state = ( $country && $state && isset( $this->states[ $country ][ $state ] ) ) ? $this->states[ $country ][ $state ] : $state;
489
490
		// Substitute address parts into the string
491
		$replace = array_map( 'esc_html', apply_filters( 'woocommerce_formatted_address_replacements', array(
492
			'{first_name}'       => $first_name,
493
			'{last_name}'        => $last_name,
494
			'{name}'             => $first_name . ' ' . $last_name,
495
			'{company}'          => $company,
496
			'{address_1}'        => $address_1,
497
			'{address_2}'        => $address_2,
498
			'{city}'             => $city,
499
			'{state}'            => $full_state,
500
			'{postcode}'         => $postcode,
501
			'{country}'          => $full_country,
502
			'{first_name_upper}' => strtoupper( $first_name ),
503
			'{last_name_upper}'  => strtoupper( $last_name ),
504
			'{name_upper}'       => strtoupper( $first_name . ' ' . $last_name ),
505
			'{company_upper}'    => strtoupper( $company ),
506
			'{address_1_upper}'  => strtoupper( $address_1 ),
507
			'{address_2_upper}'  => strtoupper( $address_2 ),
508
			'{city_upper}'       => strtoupper( $city ),
509
			'{state_upper}'      => strtoupper( $full_state ),
510
			'{state_code}'       => strtoupper( $state ),
511
			'{postcode_upper}'   => strtoupper( $postcode ),
512
			'{country_upper}'    => strtoupper( $full_country ),
513
		), $args ) );
514
515
		$formatted_address = str_replace( array_keys( $replace ), $replace, $format );
516
517
		// Clean up white space
518
		$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
519
		$formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
520
521
		// Break newlines apart and remove empty lines/trim commas and white space
522
		$formatted_address = array_filter( array_map( array( $this, 'trim_formatted_address_line' ), explode( "\n", $formatted_address ) ) );
523
524
		// Add html breaks
525
		$formatted_address = implode( '<br/>', $formatted_address );
526
527
		// We're done!
528
		return $formatted_address;
529
	}
530
531
	/**
532
	 * Trim white space and commas off a line.
533
	 * @param  string $line
534
	 * @return string
535
	 */
536
	private function trim_formatted_address_line( $line ) {
537
		return trim( $line, ", " );
538
	}
539
540
	/**
541
	 * Returns the fields we show by default. This can be filtered later on.
542
	 * @return array
543
	 */
544
	public function get_default_address_fields() {
545
		$fields = array(
546
			'first_name' => array(
547
				'label'        => __( 'First Name', 'woocommerce' ),
548
				'required'     => true,
549
				'class'        => array( 'form-row-first' ),
550
				'autocomplete' => 'given-name',
551
			),
552
			'last_name' => array(
553
				'label'        => __( 'Last Name', 'woocommerce' ),
554
				'required'     => true,
555
				'class'        => array( 'form-row-last' ),
556
				'clear'        => true,
557
				'autocomplete' => 'family-name',
558
			),
559
			'company' => array(
560
				'label'        => __( 'Company Name', 'woocommerce' ),
561
				'class'        => array( 'form-row-wide' ),
562
				'autocomplete' => 'organization',
563
			),
564
			'country' => array(
565
				'type'         => 'country',
566
				'label'        => __( 'Country', 'woocommerce' ),
567
				'required'     => true,
568
				'class'        => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
569
				'autocomplete' => 'country',
570
			),
571
			'address_1' => array(
572
				'label'        => __( 'Address', 'woocommerce' ),
573
				'placeholder'  => _x( 'Street address', 'placeholder', 'woocommerce' ),
574
				'required'     => true,
575
				'class'        => array( 'form-row-wide', 'address-field' ),
576
				'autocomplete' => 'address-line1',
577
			),
578
			'address_2' => array(
579
				'placeholder'  => _x( 'Apartment, suite, unit etc. (optional)', 'placeholder', 'woocommerce' ),
580
				'class'        => array( 'form-row-wide', 'address-field' ),
581
				'required'     => false,
582
				'autocomplete' => 'address-line2',
583
			),
584
			'city' => array(
585
				'label'        => __( 'Town / City', 'woocommerce' ),
586
				'required'     => true,
587
				'class'        => array( 'form-row-wide', 'address-field' ),
588
				'autocomplete' => 'address-level2',
589
			),
590
			'state' => array(
591
				'type'         => 'state',
592
				'label'        => __( 'State / County', 'woocommerce' ),
593
				'required'     => true,
594
				'class'        => array( 'form-row-first', 'address-field' ),
595
				'validate'     => array( 'state' ),
596
				'autocomplete' => 'address-level1',
597
			),
598
			'postcode' => array(
599
				'label'        => __( 'Postcode / ZIP', 'woocommerce' ),
600
				'required'     => true,
601
				'class'        => array( 'form-row-last', 'address-field' ),
602
				'clear'        => true,
603
				'validate'     => array( 'postcode' ),
604
				'autocomplete' => 'postal-code',
605
			),
606
		);
607
608
		return apply_filters( 'woocommerce_default_address_fields', $fields );
609
	}
610
611
	/**
612
	 * Get JS selectors for fields which are shown/hidden depending on the locale.
613
	 * @return array
614
	 */
615
	public function get_country_locale_field_selectors() {
616
		$locale_fields = array (
617
			'address_1' => '#billing_address_1_field, #shipping_address_1_field',
618
			'address_2' => '#billing_address_2_field, #shipping_address_2_field',
619
			'state'     => '#billing_state_field, #shipping_state_field, #calc_shipping_state_field',
620
			'postcode'  => '#billing_postcode_field, #shipping_postcode_field, #calc_shipping_postcode_field',
621
			'city'      => '#billing_city_field, #shipping_city_field, #calc_shipping_city_field',
622
		);
623
		return apply_filters( 'woocommerce_country_locale_field_selectors', $locale_fields );
624
	}
625
626
	/**
627
	 * Get country locale settings.
628
	 * @return array
629
	 * @todo  [2.4] Check select2 4.0.0 compatibility with `placeholder` attribute and uncomment relevant lines. https://github.com/woothemes/woocommerce/issues/7729
630
	 */
631
	public function get_country_locale() {
632
		if ( empty( $this->locale ) ) {
633
634
			// Locale information used by the checkout
635
			$this->locale = apply_filters( 'woocommerce_get_country_locale', array(
636
				'AE' => array(
637
					'postcode' => array(
638
						'required' => false,
639
						'hidden'   => true
640
					),
641
				),
642
				'AF' => array(
643
					'state' => array(
644
						'required' => false,
645
					),
646
				),
647
				'AT' => array(
648
					'postcode_before_city' => true,
649
					'state' => array(
650
						'required' => false
651
					)
652
				),
653
				'AU' => array(
654
					'city'      => array(
655
						'label'       => __( 'Suburb', 'woocommerce' ),
656
					),
657
					'postcode'  => array(
658
						'label'       => __( 'Postcode', 'woocommerce' ),
659
					),
660
					'state'     => array(
661
						'label'       => __( 'State', 'woocommerce' ),
662
					)
663
				),
664
				'AX' => array(
665
					'postcode_before_city' => true,
666
					'state' => array(
667
						'required' => false,
668
					),
669
				),
670
				'BD' => array(
671
					'postcode' => array(
672
						'required' => false
673
					),
674
					'state' => array(
675
						'label'       => __( 'District', 'woocommerce' ),
676
					)
677
				),
678
				'BE' => array(
679
					'postcode_before_city' => true,
680
					'state' => array(
681
						'required'    => false,
682
						'label'       => __( 'Province', 'woocommerce' ),
683
					),
684
				),
685
				'BI' => array(
686
					'state' => array(
687
						'required' => false,
688
					),
689
				),
690
				'BO' => array(
691
					'postcode' => array(
692
						'required' => false,
693
						'hidden'   => true
694
					),
695
				),
696
				'BS' => array(
697
					'postcode' => array(
698
						'required' => false,
699
						'hidden'   => true
700
					),
701
				),
702
				'CA' => array(
703
					'state' => array(
704
						'label'       => __( 'Province', 'woocommerce' ),
705
					)
706
				),
707
				'CH' => array(
708
					'postcode_before_city' => true,
709
					'state' => array(
710
						'label'       => __( 'Canton', 'woocommerce' ),
711
						'required'    => false
712
					)
713
				),
714
				'CL' => array(
715
					'city'      => array(
716
						'required' 	=> true,
717
					),
718
					'postcode'  => array(
719
						'required' => false
720
					),
721
					'state'     => array(
722
						'label'       => __( 'Region', 'woocommerce' ),
723
					)
724
				),
725
				'CN' => array(
726
					'state' => array(
727
						'label'       => __( 'Province', 'woocommerce' ),
728
					)
729
				),
730
				'CO' => array(
731
					'postcode' => array(
732
						'required' => false
733
					)
734
				),
735
				'CZ' => array(
736
					'state' => array(
737
						'required' => false
738
					)
739
				),
740
				'DE' => array(
741
					'postcode_before_city' => true,
742
					'state' => array(
743
						'required' => false
744
					)
745
				),
746
				'DK' => array(
747
					'postcode_before_city' => true,
748
					'state' => array(
749
						'required' => false
750
					)
751
				),
752
				'EE' => array(
753
					'postcode_before_city' => true,
754
					'state' => array(
755
						'required' => false
756
					)
757
				),
758
				'FI' => array(
759
					'postcode_before_city' => true,
760
					'state' => array(
761
						'required' => false
762
					)
763
				),
764
				'FR' => array(
765
					'postcode_before_city' => true,
766
					'state' => array(
767
						'required' => false
768
					)
769
				),
770
				'HK' => array(
771
					'postcode' => array(
772
						'required' => false
773
					),
774
					'city'  => array(
775
						'label'       => __( 'Town / District', 'woocommerce' ),
776
					),
777
					'state' => array(
778
						'label'       => __( 'Region', 'woocommerce' ),
779
					)
780
				),
781
				'HU' => array(
782
					'state' => array(
783
						'label'       => __( 'County', 'woocommerce' ),
784
					)
785
				),
786
				'ID' => array(
787
					'state' => array(
788
						'label'       => __( 'Province', 'woocommerce' ),
789
					)
790
				),
791
				'IE' => array(
792
					'postcode' => array(
793
						'required' => false,
794
						'label'    => __( 'Postcode', 'woocommerce' ),
795
					),
796
				),
797
				'IS' => array(
798
					'postcode_before_city' => true,
799
					'state' => array(
800
						'required' => false
801
					)
802
				),
803
				'IL' => array(
804
					'postcode_before_city' => true,
805
					'state' => array(
806
						'required' => false
807
					)
808
				),
809
				'IT' => array(
810
					'postcode_before_city' => true,
811
					'state' => array(
812
						'required'    => true,
813
						'label'       => __( 'Province', 'woocommerce' ),
814
					)
815
				),
816
				'JP' => array(
817
					'state' => array(
818
						'label' => __( 'Prefecture', 'woocommerce' )
819
					)
820
				),
821
				'KR' => array(
822
					'state' => array(
823
						'required' => false
824
					)
825
				),
826
				'NL' => array(
827
					'postcode_before_city' => true,
828
					'state' => array(
829
						'required'    => false,
830
						'label'       => __( 'Province', 'woocommerce' ),
831
					)
832
				),
833
				'NZ' => array(
834
					'postcode' => array(
835
						'label' => __( 'Postcode', 'woocommerce' )
836
					),
837
					'state' => array(
838
						'required' => false,
839
						'label'    => __( 'Region', 'woocommerce' )
840
					)
841
				),
842
				'NO' => array(
843
					'postcode_before_city' => true,
844
					'state' => array(
845
						'required' => false
846
					)
847
				),
848
				'NP' => array(
849
					'state' => array(
850
						'label'       => __( 'State / Zone', 'woocommerce' ),
851
					),
852
					'postcode' => array(
853
						'required' => false
854
					)
855
				),
856
				'PL' => array(
857
					'postcode_before_city' => true,
858
					'state' => array(
859
						'required' => false
860
					)
861
				),
862
				'PT' => array(
863
					'state' => array(
864
						'required' => false
865
					)
866
				),
867
				'RO' => array(
868
					'state' => array(
869
						'required' => false
870
					)
871
				),
872
				'SG' => array(
873
					'state' => array(
874
						'required' => false
875
					)
876
				),
877
				'SK' => array(
878
					'postcode_before_city' => true,
879
					'state' => array(
880
						'required' => false
881
					)
882
				),
883
				'SI' => array(
884
					'postcode_before_city' => true,
885
					'state' => array(
886
						'required' => false
887
					)
888
				),
889
				'ES' => array(
890
					'postcode_before_city' => true,
891
					'state' => array(
892
						'label'       => __( 'Province', 'woocommerce' ),
893
					)
894
				),
895
				'LI' => array(
896
					'postcode_before_city' => true,
897
					'state' => array(
898
						'label'       => __( 'Municipality', 'woocommerce' ),
899
						'required'    => false
900
					)
901
				),
902
				'LK' => array(
903
					'state' => array(
904
						'required' => false
905
					)
906
				),
907
				'SE' => array(
908
					'postcode_before_city' => true,
909
					'state' => array(
910
						'required' => false
911
					)
912
				),
913
				'TR' => array(
914
					'postcode_before_city' => true,
915
					'state' => array(
916
						'label'       => __( 'Province', 'woocommerce' ),
917
					)
918
				),
919
				'US' => array(
920
					'postcode'  => array(
921
						'label'       => __( 'ZIP', 'woocommerce' ),
922
					),
923
					'state'     => array(
924
						'label'       => __( 'State', 'woocommerce' ),
925
					)
926
				),
927
				'GB' => array(
928
					'postcode'  => array(
929
						'label'       => __( 'Postcode', 'woocommerce' ),
930
					),
931
					'state'     => array(
932
						'label'       => __( 'County', 'woocommerce' ),
933
						'required'    => false
934
					)
935
				),
936
				'VN' => array(
937
					'postcode_before_city' => true,
938
					'state' => array(
939
						'required' => false
940
					),
941
					'postcode' => array(
942
						'required' => false,
943
						'hidden'   => false
944
					),
945
					'address_2' => array(
946
						'required' => false,
947
						'hidden'   => true
948
					)
949
				),
950
				'WS' => array(
951
					'postcode' => array(
952
						'required' => false,
953
						'hidden'   => true
954
					),
955
				),
956
				'ZA' => array(
957
					'state' => array(
958
						'label'       => __( 'Province', 'woocommerce' ),
959
					)
960
				),
961
				'ZW' => array(
962
					'postcode' => array(
963
						'required' => false,
964
						'hidden'   => true
965
					),
966
				),
967
			));
968
969
			$this->locale = array_intersect_key( $this->locale, array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() ) );
970
971
			// Default Locale Can be filtered to override fields in get_address_fields().
972
			// Countries with no specific locale will use default.
973
			$this->locale['default'] = apply_filters('woocommerce_get_country_locale_default', $this->get_default_address_fields() );
974
975
			// Filter default AND shop base locales to allow overides via a single function. These will be used when changing countries on the checkout
976
			if ( ! isset( $this->locale[ $this->get_base_country() ] ) ) {
977
				$this->locale[ $this->get_base_country() ] = $this->locale['default'];
978
			}
979
980
			$this->locale['default']                   = apply_filters( 'woocommerce_get_country_locale_base', $this->locale['default'] );
981
			$this->locale[ $this->get_base_country() ] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale[ $this->get_base_country() ] );
982
		}
983
984
		return $this->locale;
985
	}
986
987
	/**
988
	 * Apply locale and get address fields.
989
	 * @param  mixed  $country (default: '')
990
	 * @param  string $type (default: 'billing_')
991
	 * @return array
992
	 */
993
	public function get_address_fields( $country = '', $type = 'billing_' ) {
994
		if ( ! $country ) {
995
			$country = $this->get_base_country();
996
		}
997
998
		$fields = $this->get_default_address_fields();
999
		$locale = $this->get_country_locale();
1000
1001
		if ( isset( $locale[ $country ] ) ) {
1002
			$fields = wc_array_overlay( $fields, $locale[ $country ] );
1003
		}
1004
1005
		// Prepend field keys
1006
		$address_fields = array();
1007
1008
		foreach ( $fields as $key => $value ) {
1009
			$keys = array_keys( $fields );
1010
			$address_fields[ $type . $key ] = $value;
1011
1012
			// Add email and phone after company or last
1013
			if ( 'billing_' === $type && ( 'company' === $key || ( ! array_key_exists( 'company', $fields ) && $key === end( $keys ) ) ) ) {
1014
				$address_fields['billing_email'] = array(
1015
					'label'        => __( 'Email Address', 'woocommerce' ),
1016
					'required'     => true,
1017
					'type'         => 'email',
1018
					'class'        => array( 'form-row-first' ),
1019
					'validate'     => array( 'email' ),
1020
					'autocomplete' => 'email',
1021
				);
1022
				$address_fields['billing_phone'] = array(
1023
					'label'        => __( 'Phone', 'woocommerce' ),
1024
					'required'     => true,
1025
					'type'         => 'tel',
1026
					'class'        => array( 'form-row-last' ),
1027
					'clear'        => true,
1028
					'validate'     => array( 'phone' ),
1029
					'autocomplete' => 'tel',
1030
				);
1031
			}
1032
		}
1033
1034
		$address_fields = apply_filters( 'woocommerce_' . $type . 'fields', $address_fields, $country );
1035
1036
		return $address_fields;
1037
	}
1038
}
1039