Completed
Push — master ( effa51...64c76e )
by Mike
12:23
created

WC_Countries::load_country_states()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * WooCommerce countries
4
 *
5
 * @package WooCommerce\l10n
6
 * @version 3.3.0
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * The WooCommerce countries class stores country/state data.
13
 */
14
class WC_Countries {
15
16
	/**
17
	 * Locales list.
18
	 *
19
	 * @var array
20
	 */
21
	public $locale = array();
22
23
	/**
24
	 * List of address formats for locales.
25
	 *
26
	 * @var array
27
	 */
28
	public $address_formats = array();
29
30
	/**
31
	 * Auto-load in-accessible properties on demand.
32
	 *
33
	 * @param  mixed $key Key.
34
	 * @return mixed
35
	 */
36 7
	public function __get( $key ) {
37 7
		if ( 'countries' === $key ) {
38 6
			return $this->get_countries();
39 2
		} elseif ( 'states' === $key ) {
40 2
			return $this->get_states();
41
		}
42
	}
43
44
	/**
45
	 * Get all countries.
46
	 *
47
	 * @return array
48
	 */
49 24
	public function get_countries() {
50 24
		if ( empty( $this->countries ) ) {
51 7
			$this->countries = apply_filters( 'woocommerce_countries', include WC()->plugin_path() . '/i18n/countries.php' );
52 7
			if ( apply_filters( 'woocommerce_sort_countries', true ) ) {
53 7
				uasort( $this->countries, 'wc_ascii_uasort_comparison' );
54
			}
55
		}
56
57 24
		return $this->countries;
58
	}
59
60
	/**
61
	 * Get all continents.
62
	 *
63
	 * @return array
64
	 */
65 21
	public function get_continents() {
66 21
		if ( empty( $this->continents ) ) {
67 1
			$this->continents = apply_filters( 'woocommerce_continents', include WC()->plugin_path() . '/i18n/continents.php' );
68
		}
69
70 21
		return $this->continents;
71
	}
72
73
	/**
74
	 * Get continent code for a country code.
75
	 *
76
	 * @since 2.6.0
77
	 * @param string $cc Country code.
78
	 * @return string
79
	 */
80 16
	public function get_continent_code_for_country( $cc ) {
81 16
		$cc                 = trim( strtoupper( $cc ) );
82 16
		$continents         = $this->get_continents();
83 16
		$continents_and_ccs = wp_list_pluck( $continents, 'countries' );
84 16
		foreach ( $continents_and_ccs as $continent_code => $countries ) {
85 16
			if ( false !== array_search( $cc, $countries, true ) ) {
86 16
				return $continent_code;
87
			}
88
		}
89
90
		return '';
91
	}
92
93
	/**
94
	 * Get continents that the store ships to.
95
	 *
96 4
	 * @since 3.6.0
97
	 * @return array
98
	 */
99
	public function get_shipping_continents() {
100
		$continents             = $this->get_continents();
101 4
		$shipping_countries     = $this->get_shipping_countries();
102
		$shipping_country_codes = array_keys( $shipping_countries );
103
		$shipping_continents    = array();
104
105
		foreach ( $continents as $continent_code => $continent ) {
106
			if ( count( array_intersect( $continent['countries'], $shipping_country_codes ) ) ) {
107
				$shipping_continents[ $continent_code ] = $continent;
108
			}
109
		}
110
111
		return $shipping_continents;
112
	}
113
114
	/**
115
	 * Load the states.
116
	 *
117
	 * @deprecated 3.6.0 This method was used to load state files, but is no longer needed. @see get_states().
118
	 */
119
	public function load_country_states() {
120
		global $states;
121
122
		$states       = include WC()->plugin_path() . '/i18n/states.php';
123
		$this->states = apply_filters( 'woocommerce_states', $states );
124
	}
125
126
	/**
127
	 * Get the states for a country.
128
	 *
129
	 * @param  string $cc Country code.
130
	 * @return false|array of states
131
	 */
132
	public function get_states( $cc = null ) {
133
		if ( ! isset( $this->states ) ) {
134
			$this->states = apply_filters( 'woocommerce_states', include WC()->plugin_path() . '/i18n/states.php' );
135
		}
136
137
		if ( ! is_null( $cc ) ) {
138
			return isset( $this->states[ $cc ] ) ? $this->states[ $cc ] : false;
139 4
		} else {
140
			return $this->states;
141 4
		}
142 4
	}
143 4
144 4
	/**
145
	 * Get the base address (first line) for the store.
146
	 *
147
	 * @since 3.1.1
148
	 * @return string
149 4
	 */
150
	public function get_base_address() {
151
		$base_address = get_option( 'woocommerce_store_address', '' );
152
		return apply_filters( 'woocommerce_countries_base_address', $base_address );
153
	}
154
155
	/**
156
	 * Get the base address (second line) for the store.
157
	 *
158 21
	 * @since 3.1.1
159 21
	 * @return string
160 4
	 */
161
	public function get_base_address_2() {
162
		$base_address_2 = get_option( 'woocommerce_store_address_2', '' );
163 21
		return apply_filters( 'woocommerce_countries_base_address_2', $base_address_2 );
164 12
	}
165
166 9
	/**
167
	 * Get the base country for the store.
168
	 *
169
	 * @return string
170
	 */
171
	public function get_base_country() {
172
		$default = wc_get_base_location();
173
		return apply_filters( 'woocommerce_countries_base_country', $default['country'] );
174
	}
175
176
	/**
177
	 * Get the base state for the store.
178
	 *
179
	 * @return string
180
	 */
181
	public function get_base_state() {
182
		$default = wc_get_base_location();
183
		return apply_filters( 'woocommerce_countries_base_state', $default['state'] );
184
	}
185
186
	/**
187
	 * Get the base city for the store.
188
	 *
189
	 * @version 3.1.1
190
	 * @return string
191
	 */
192
	public function get_base_city() {
193
		$base_city = get_option( 'woocommerce_store_city', '' );
194
		return apply_filters( 'woocommerce_countries_base_city', $base_city );
195
	}
196
197 443
	/**
198 443
	 * Get the base postcode for the store.
199 443
	 *
200
	 * @since 3.1.1
201
	 * @return string
202
	 */
203
	public function get_base_postcode() {
204
		$base_postcode = get_option( 'woocommerce_store_postcode', '' );
205
		return apply_filters( 'woocommerce_countries_base_postcode', $base_postcode );
206
	}
207 24
208 24
	/**
209 24
	 * Get countries that the store sells to.
210
	 *
211
	 * @return array
212
	 */
213
	public function get_allowed_countries() {
214
		if ( 'all' === get_option( 'woocommerce_allowed_countries' ) ) {
215
			return apply_filters( 'woocommerce_countries_allowed_countries', $this->countries );
216
		}
217
218 24
		if ( 'all_except' === get_option( 'woocommerce_allowed_countries' ) ) {
219 24
			$except_countries = get_option( 'woocommerce_all_except_countries', array() );
220 24
221
			if ( ! $except_countries ) {
222
				return $this->countries;
223
			} else {
224
				$all_except_countries = $this->countries;
225
				foreach ( $except_countries as $country ) {
226
					unset( $all_except_countries[ $country ] );
227
				}
228
				return apply_filters( 'woocommerce_countries_allowed_countries', $all_except_countries );
229 24
			}
230 24
		}
231 24
232
		$countries = array();
233
234
		$raw_countries = get_option( 'woocommerce_specific_allowed_countries', array() );
235
236
		if ( $raw_countries ) {
237
			foreach ( $raw_countries as $country ) {
238
				$countries[ $country ] = $this->countries[ $country ];
239 110
			}
240 110
		}
241 109
242
		return apply_filters( 'woocommerce_countries_allowed_countries', $countries );
243
	}
244 3
245 1
	/**
246
	 * Get countries that the store ships to.
247 1
	 *
248
	 * @return array
249
	 */
250 1
	public function get_shipping_countries() {
251 1
		if ( '' === get_option( 'woocommerce_ship_to_countries' ) ) {
252 1
			return $this->get_allowed_countries();
253
		}
254 1
255
		if ( 'all' === get_option( 'woocommerce_ship_to_countries' ) ) {
256
			return $this->countries;
257
		}
258 3
259
		$countries = array();
260 3
261
		$raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
262 3
263 3
		if ( $raw_countries ) {
264 3
			foreach ( $raw_countries as $country ) {
265
				$countries[ $country ] = $this->countries[ $country ];
266
			}
267
		}
268 3
269
		return apply_filters( 'woocommerce_countries_shipping_countries', $countries );
270
	}
271
272
	/**
273
	 * Get allowed country states.
274
	 *
275
	 * @return array
276 109
	 */
277 109
	public function get_allowed_country_states() {
278 109
		if ( get_option( 'woocommerce_allowed_countries' ) !== 'specific' ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
279
			return $this->states;
280
		}
281 1
282 1
		$states = array();
283
284
		$raw_countries = get_option( 'woocommerce_specific_allowed_countries' );
285 1
286
		if ( $raw_countries ) {
287 1
			foreach ( $raw_countries as $country ) {
288
				if ( isset( $this->states[ $country ] ) ) {
289 1
					$states[ $country ] = $this->states[ $country ];
290 1
				}
291 1
			}
292
		}
293
294
		return apply_filters( 'woocommerce_countries_allowed_country_states', $states );
295 1
	}
296
297
	/**
298
	 * Get shipping country states.
299
	 *
300
	 * @return array
301
	 */
302
	public function get_shipping_country_states() {
303 2
		if ( get_option( 'woocommerce_ship_to_countries' ) === '' ) {
304 2
			return $this->get_allowed_country_states();
305 2
		}
306
307
		if ( get_option( 'woocommerce_ship_to_countries' ) !== 'specific' ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
308 1
			return $this->states;
309
		}
310 1
311
		$states = array();
312 1
313 1
		$raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
314 1
315 1
		if ( $raw_countries ) {
316
			foreach ( $raw_countries as $country ) {
317
				if ( ! empty( $this->states[ $country ] ) ) {
318
					$states[ $country ] = $this->states[ $country ];
319
				}
320 1
			}
321
		}
322
323
		return apply_filters( 'woocommerce_countries_shipping_country_states', $states );
324
	}
325
326
	/**
327
	 * Gets an array of countries in the EU.
328 1
	 *
329 1
	 * MC (monaco) and IM (isle of man, part of UK) also use VAT.
330 1
	 *
331
	 * @param  string $type Type of countries to retrieve. Blank for EU member countries. eu_vat for EU VAT countries.
332
	 * @return string[]
333 1
	 */
334 1
	public function get_european_union_countries( $type = '' ) {
335
		$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' );
336
337 1
		if ( 'eu_vat' === $type ) {
338
			$countries[] = 'MC';
339 1
			$countries[] = 'IM';
340
		}
341 1
342 1
		return $countries;
343 1
	}
344 1
345
	/**
346
	 * Gets the correct string for shipping - either 'to the' or 'to'.
347
	 *
348
	 * @param string $country_code Country code.
349 1
	 * @return string
350
	 */
351
	public function shipping_to_prefix( $country_code = '' ) {
352
		$country_code = $country_code ? $country_code : WC()->customer->get_shipping_country();
353
		$countries    = array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' );
354
		$return       = in_array( $country_code, $countries, true ) ? __( 'to the', 'woocommerce' ) : __( 'to', 'woocommerce' );
355
356
		return apply_filters( 'woocommerce_countries_shipping_to_prefix', $return, $country_code );
357
	}
358
359
	/**
360 3
	 * Prefix certain countries with 'the'.
361 3
	 *
362
	 * @param string $country_code Country code.
363 3
	 * @return string
364 3
	 */
365 3
	public function estimated_for_prefix( $country_code = '' ) {
366
		$country_code = $country_code ? $country_code : $this->get_base_country();
367
		$countries    = array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' );
368 3
		$return       = in_array( $country_code, $countries, true ) ? __( 'the', 'woocommerce' ) . ' ' : '';
369
370
		return apply_filters( 'woocommerce_countries_estimated_for_prefix', $return, $country_code );
371
	}
372
373
	/**
374
	 * Correctly name tax in some countries VAT on the frontend.
375
	 *
376
	 * @return string
377 1
	 */
378 1
	public function tax_or_vat() {
379 1
		$return = in_array( $this->get_base_country(), array_merge( $this->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
380 1
381
		return apply_filters( 'woocommerce_countries_tax_or_vat', $return );
382 1
	}
383
384
	/**
385
	 * Include the Inc Tax label.
386
	 *
387
	 * @return string
388
	 */
389
	public function inc_tax_or_vat() {
390
		$return = in_array( $this->get_base_country(), array_merge( $this->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( '(incl. VAT)', 'woocommerce' ) : __( '(incl. tax)', 'woocommerce' );
391 1
392 1
		return apply_filters( 'woocommerce_countries_inc_tax_or_vat', $return );
393 1
	}
394 1
395
	/**
396 1
	 * Include the Ex Tax label.
397
	 *
398
	 * @return string
399
	 */
400
	public function ex_tax_or_vat() {
401
		$return = in_array( $this->get_base_country(), array_merge( $this->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( '(ex. VAT)', 'woocommerce' ) : __( '(ex. tax)', 'woocommerce' );
402
403
		return apply_filters( 'woocommerce_countries_ex_tax_or_vat', $return );
404 2
	}
405 2
406
	/**
407 2
	 * Outputs the list of countries and states for use in dropdown boxes.
408
	 *
409
	 * @param string $selected_country Selected country.
410
	 * @param string $selected_state   Selected state.
411
	 * @param bool   $escape           If we should escape HTML.
412
	 */
413
	public function country_dropdown_options( $selected_country = '', $selected_state = '', $escape = false ) {
414
		if ( $this->countries ) {
415
			foreach ( $this->countries as $key => $value ) {
416
				$states = $this->get_states( $key );
417
				if ( $states ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $states of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
418
					echo '<optgroup label="' . esc_attr( $value ) . '">';
419
					foreach ( $states as $state_key => $state_value ) {
420
						echo '<option value="' . esc_attr( $key ) . ':' . esc_attr( $state_key ) . '"';
421
422
						if ( $selected_country === $key && $selected_state === $state_key ) {
423
							echo ' selected="selected"';
424
						}
425
426 1
						echo '>' . esc_html( $value ) . ' &mdash; ' . ( $escape ? esc_js( $state_value ) : $state_value ) . '</option>'; // WPCS: XSS ok.
427 1
					}
428
					echo '</optgroup>';
429 1
				} else {
430
					echo '<option';
431
					if ( $selected_country === $key && '*' === $selected_state ) {
432
						echo ' selected="selected"';
433
					}
434
					echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value ) : $value ) . '</option>'; // WPCS: XSS ok.
435
				}
436
			}
437
		}
438
	}
439
440
	/**
441
	 * Get country address formats.
442
	 *
443
	 * These define how addresses are formatted for display in various countries.
444
	 *
445
	 * @return array
446
	 */
447
	public function get_address_formats() {
448
		if ( empty( $this->address_formats ) ) {
449
			$this->address_formats = apply_filters(
450
				'woocommerce_localisation_address_formats',
451
				array(
452
					'default' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}",
453
					'AU'      => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
454
					'AT'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
455
					'BE'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
456
					'CA'      => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
457
					'CH'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
458
					'CL'      => "{company}\n{name}\n{address_1}\n{address_2}\n{state}\n{postcode} {city}\n{country}",
459
					'CN'      => "{country} {postcode}\n{state}, {city}, {address_2}, {address_1}\n{company}\n{name}",
460
					'CZ'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
461
					'DE'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
462
					'EE'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
463
					'FI'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
464
					'DK'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
465
					'FR'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city_upper}\n{country}",
466
					'HK'      => "{company}\n{first_name} {last_name_upper}\n{address_1}\n{address_2}\n{city_upper}\n{state_upper}\n{country}",
467
					'HU'      => "{name}\n{company}\n{city}\n{address_1}\n{address_2}\n{postcode}\n{country}",
468
					'IN'      => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {postcode}\n{state}, {country}",
469
					'IS'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
470
					'IT'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode}\n{city}\n{state_upper}\n{country}",
471
					'JP'      => "{postcode}\n{state} {city} {address_1}\n{address_2}\n{company}\n{last_name} {first_name}\n{country}",
472
					'TW'      => "{company}\n{last_name} {first_name}\n{address_1}\n{address_2}\n{state}, {city} {postcode}\n{country}",
473 6
					'LI'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
474 6
					'NL'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
475 1
					'NZ'      => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {postcode}\n{country}",
476 1
					'NO'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
477
					'PL'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
478 1
					'PT'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
479
					'SK'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
480
					'SI'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
481
					'ES'      => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}",
482
					'SE'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
483
					'TR'      => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city} {state}\n{country}",
484
					'US'      => "{name}\n{company}\n{address_1}\n{address_2}\n{city}, {state_code} {postcode}\n{country}",
485
					'VN'      => "{name}\n{company}\n{address_1}\n{city}\n{country}",
486
				)
487
			);
488
		}
489
		return $this->address_formats;
490
	}
491
492
	/**
493
	 * Get country address format.
494
	 *
495
	 * @param  array  $args Arguments.
496
	 * @param  string $separator How to separate address lines. @since 3.5.0.
497
	 * @return string
498
	 */
499
	public function get_formatted_address( $args = array(), $separator = '<br/>' ) {
500
		$default_args = array(
501
			'first_name' => '',
502
			'last_name'  => '',
503
			'company'    => '',
504
			'address_1'  => '',
505
			'address_2'  => '',
506
			'city'       => '',
507
			'state'      => '',
508
			'postcode'   => '',
509
			'country'    => '',
510
		);
511
512
		$args    = array_map( 'trim', wp_parse_args( $args, $default_args ) );
513
		$state   = $args['state'];
514
		$country = $args['country'];
515 6
516
		// Get all formats.
517
		$formats = $this->get_address_formats();
518
519
		// Get format for the address' country.
520
		$format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];
521
522
		// Handle full country name.
523
		$full_country = ( isset( $this->countries[ $country ] ) ) ? $this->countries[ $country ] : $country;
524
525 6
		// Country is not needed if the same as base.
526
		if ( $country === $this->get_base_country() && ! apply_filters( 'woocommerce_formatted_address_force_country_display', false ) ) {
527 6
			$format = str_replace( '{country}', '', $format );
528
		}
529
530
		// Handle full state name.
531
		$full_state = ( $country && $state && isset( $this->states[ $country ][ $state ] ) ) ? $this->states[ $country ][ $state ] : $state;
532
533
		// Substitute address parts into the string.
534
		$replace = array_map(
535
			'esc_html',
536
			apply_filters(
537
				'woocommerce_formatted_address_replacements',
538 6
				array(
539 6
					'{first_name}'       => $args['first_name'],
540 6
					'{last_name}'        => $args['last_name'],
541
					'{name}'             => $args['first_name'] . ' ' . $args['last_name'],
542
					'{company}'          => $args['company'],
543 6
					'{address_1}'        => $args['address_1'],
544
					'{address_2}'        => $args['address_2'],
545
					'{city}'             => $args['city'],
546 6
					'{state}'            => $full_state,
547
					'{postcode}'         => $args['postcode'],
548
					'{country}'          => $full_country,
549 6
					'{first_name_upper}' => wc_strtoupper( $args['first_name'] ),
550
					'{last_name_upper}'  => wc_strtoupper( $args['last_name'] ),
551
					'{name_upper}'       => wc_strtoupper( $args['first_name'] . ' ' . $args['last_name'] ),
552 6
					'{company_upper}'    => wc_strtoupper( $args['company'] ),
553
					'{address_1_upper}'  => wc_strtoupper( $args['address_1'] ),
554
					'{address_2_upper}'  => wc_strtoupper( $args['address_2'] ),
555
					'{city_upper}'       => wc_strtoupper( $args['city'] ),
556
					'{state_upper}'      => wc_strtoupper( $full_state ),
557 6
					'{state_code}'       => wc_strtoupper( $state ),
558
					'{postcode_upper}'   => wc_strtoupper( $args['postcode'] ),
559
					'{country_upper}'    => wc_strtoupper( $full_country ),
560 6
				),
561 6
				$args
562 6
			)
563 6
		);
564
565 6
		$formatted_address = str_replace( array_keys( $replace ), $replace, $format );
566 6
567 6
		// Clean up white space.
568 6
		$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
569 6
		$formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
570 6
571 6
		// Break newlines apart and remove empty lines/trim commas and white space.
572 6
		$formatted_address = array_filter( array_map( array( $this, 'trim_formatted_address_line' ), explode( "\n", $formatted_address ) ) );
573 6
574 6
		// Add html breaks.
575 6
		$formatted_address = implode( $separator, $formatted_address );
576 6
577 6
		// We're done!
578 6
		return $formatted_address;
579 6
	}
580 6
581 6
	/**
582 6
	 * Trim white space and commas off a line.
583 6
	 *
584 6
	 * @param  string $line Line.
585 6
	 * @return string
586
	 */
587 6
	private function trim_formatted_address_line( $line ) {
588
		return trim( $line, ', ' );
589
	}
590
591 6
	/**
592
	 * Returns the fields we show by default. This can be filtered later on.
593
	 *
594 6
	 * @return array
595 6
	 */
596
	public function get_default_address_fields() {
597
		if ( 'optional' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ) ) {
598 6
			$address_2_placeholder = __( 'Apartment, suite, unit etc. (optional)', 'woocommerce' );
599
		} else {
600
			$address_2_placeholder = __( 'Apartment, suite, unit etc.', 'woocommerce' );
601 6
		}
602
603
		$fields = array(
604 6
			'first_name' => array(
605
				'label'        => __( 'First name', 'woocommerce' ),
606
				'required'     => true,
607
				'class'        => array( 'form-row-first' ),
608
				'autocomplete' => 'given-name',
609
				'priority'     => 10,
610
			),
611
			'last_name'  => array(
612
				'label'        => __( 'Last name', 'woocommerce' ),
613 6
				'required'     => true,
614 6
				'class'        => array( 'form-row-last' ),
615
				'autocomplete' => 'family-name',
616
				'priority'     => 20,
617
			),
618
			'company'    => array(
619
				'label'        => __( 'Company name', 'woocommerce' ),
620
				'class'        => array( 'form-row-wide' ),
621
				'autocomplete' => 'organization',
622 1
				'priority'     => 30,
623 1
				'required'     => 'required' === get_option( 'woocommerce_checkout_company_field', 'optional' ),
624 1
			),
625
			'country'    => array(
626
				'type'         => 'country',
627
				'label'        => __( 'Country', 'woocommerce' ),
628
				'required'     => true,
629
				'class'        => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
630 1
				'autocomplete' => 'country',
631 1
				'priority'     => 40,
632
			),
633
			'address_1'  => array(
634 1
				'label'        => __( 'Street address', 'woocommerce' ),
635 1
				/* translators: use local order of street name and house number. */
636
				'placeholder'  => esc_attr__( 'House number and street name', 'woocommerce' ),
637
				'required'     => true,
638 1
				'class'        => array( 'form-row-wide', 'address-field' ),
639
				'autocomplete' => 'address-line1',
640
				'priority'     => 50,
641 1
			),
642 1
			'address_2'  => array(
643
				'label'        => __( 'Apartment, suite, unit etc.', 'woocommerce' ),
644
				'label_class'  => array( 'screen-reader-text' ),
645 1
				'placeholder'  => esc_attr( $address_2_placeholder ),
646
				'class'        => array( 'form-row-wide', 'address-field' ),
647 1
				'autocomplete' => 'address-line2',
648 1
				'priority'     => 60,
649 1
				'required'     => 'required' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ),
650
			),
651
			'city'       => array(
652 1
				'label'        => __( 'Town / City', 'woocommerce' ),
653 1
				'required'     => true,
654
				'class'        => array( 'form-row-wide', 'address-field' ),
655
				'autocomplete' => 'address-level2',
656 1
				'priority'     => 70,
657 1
			),
658
			'state'      => array(
659
				'type'         => 'state',
660 1
				'label'        => __( 'State / County', 'woocommerce' ),
661
				'required'     => true,
662 1
				'class'        => array( 'form-row-wide', 'address-field' ),
663
				'validate'     => array( 'state' ),
664
				'autocomplete' => 'address-level1',
665 1
				'priority'     => 80,
666 1
			),
667
			'postcode'   => array(
668
				'label'        => __( 'Postcode / ZIP', 'woocommerce' ),
669 1
				'required'     => true,
670
				'class'        => array( 'form-row-wide', 'address-field' ),
671 1
				'validate'     => array( 'postcode' ),
672
				'autocomplete' => 'postal-code',
673 1
				'priority'     => 90,
674 1
			),
675 1
		);
676
677
		if ( 'hidden' === get_option( 'woocommerce_checkout_company_field', 'optional' ) ) {
678 1
			unset( $fields['company'] );
679
		}
680
681 1
		if ( 'hidden' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ) ) {
682 1
			unset( $fields['address_2'] );
683
		}
684
685 1
		$default_address_fields = apply_filters( 'woocommerce_default_address_fields', $fields );
686 1
		// Sort each of the fields based on priority.
687
		uasort( $default_address_fields, 'wc_checkout_fields_uasort_comparison' );
688
689
		return $default_address_fields;
690 1
	}
691 1
692
	/**
693
	 * Get JS selectors for fields which are shown/hidden depending on the locale.
694 1
	 *
695
	 * @return array
696
	 */
697
	public function get_country_locale_field_selectors() {
698 1
		$locale_fields = array(
699 1
			'address_1' => '#billing_address_1_field, #shipping_address_1_field',
700
			'address_2' => '#billing_address_2_field, #shipping_address_2_field',
701
			'state'     => '#billing_state_field, #shipping_state_field, #calc_shipping_state_field',
702
			'postcode'  => '#billing_postcode_field, #shipping_postcode_field, #calc_shipping_postcode_field',
703 1
			'city'      => '#billing_city_field, #shipping_city_field, #calc_shipping_city_field',
704
		);
705
		return apply_filters( 'woocommerce_country_locale_field_selectors', $locale_fields );
706
	}
707 1
708
	/**
709
	 * Get country locale settings.
710
	 *
711 1
	 * These locales override the default country selections after a country is chosen.
712
	 *
713 1
	 * @return array
714
	 */
715 1
	public function get_country_locale() {
716
		if ( empty( $this->locale ) ) {
717
			$this->locale = apply_filters(
718
				'woocommerce_get_country_locale',
719
				array(
720
					'AE' => array(
721
						'postcode' => array(
722
							'required' => false,
723
							'hidden'   => true,
724
						),
725
						'state'    => array(
726
							'required' => false,
727
						),
728
					),
729
					'AF' => array(
730
						'state' => array(
731
							'required' => false,
732
						),
733
					),
734
					'AO' => array(
735
						'postcode' => array(
736
							'required' => false,
737
							'hidden'   => true,
738
						),
739
						'state'    => array(
740
							'label' => __( 'Province', 'woocommerce' ),
741 1
						),
742 1
					),
743 1
					'AT' => array(
744 1
						'postcode' => array(
745
							'priority' => 65,
746 1
						),
747
						'state'    => array(
748
							'required' => false,
749
						),
750
					),
751
					'AU' => array(
752
						'city'     => array(
753
							'label' => __( 'Suburb', 'woocommerce' ),
754
						),
755
						'postcode' => array(
756
							'label' => __( 'Postcode', 'woocommerce' ),
757
						),
758
						'state'    => array(
759
							'label' => __( 'State', 'woocommerce' ),
760
						),
761
					),
762
					'AX' => array(
763
						'postcode' => array(
764
							'priority' => 65,
765
						),
766 1
						'state'    => array(
767
							'required' => false,
768
						),
769
					),
770
					'BD' => array(
771
						'postcode' => array(
772
							'required' => false,
773
						),
774
						'state'    => array(
775
							'label' => __( 'District', 'woocommerce' ),
776
						),
777
					),
778
					'BE' => array(
779 1
						'postcode' => array(
780
							'priority' => 65,
781
						),
782 1
						'state'    => array(
783
							'required' => false,
784
							'label'    => __( 'Province', 'woocommerce' ),
785 1
						),
786
					),
787
					'BH' => array(
788
						'postcode' => array(
789
							'required' => false,
790
						),
791
						'state'    => array(
792
							'required' => false,
793
						),
794
					),
795
					'BI' => array(
796
						'state' => array(
797
							'required' => false,
798
						),
799
					),
800
					'BO' => array(
801 1
						'postcode' => array(
802
							'required' => false,
803
							'hidden'   => true,
804
						),
805
					),
806
					'BS' => array(
807
						'postcode' => array(
808
							'required' => false,
809
							'hidden'   => true,
810 1
						),
811
					),
812
					'CA' => array(
813
						'state' => array(
814
							'label' => __( 'Province', 'woocommerce' ),
815
						),
816
					),
817
					'CH' => array(
818
						'postcode' => array(
819
							'priority' => 65,
820
						),
821
						'state'    => array(
822
							'label'    => __( 'Canton', 'woocommerce' ),
823
							'required' => false,
824
						),
825
					),
826
					'CL' => array(
827
						'city'     => array(
828
							'required' => true,
829
						),
830
						'postcode' => array(
831
							'required' => false,
832
						),
833
						'state'    => array(
834
							'label' => __( 'Region', 'woocommerce' ),
835
						),
836
					),
837
					'CN' => array(
838
						'state' => array(
839
							'label' => __( 'Province', 'woocommerce' ),
840 1
						),
841
					),
842
					'CO' => array(
843
						'postcode' => array(
844
							'required' => false,
845
						),
846
					),
847
					'CZ' => array(
848 1
						'state' => array(
849
							'required' => false,
850
						),
851
					),
852
					'DE' => array(
853
						'postcode' => array(
854
							'priority' => 65,
855
						),
856
						'state'    => array(
857
							'required' => false,
858
						),
859
					),
860 1
					'DK' => array(
861
						'postcode' => array(
862
							'priority' => 65,
863
						),
864
						'state'    => array(
865 1
							'required' => false,
866
						),
867
					),
868
					'EE' => array(
869
						'postcode' => array(
870
							'priority' => 65,
871
						),
872
						'state'    => array(
873
							'required' => false,
874
						),
875
					),
876
					'FI' => array(
877
						'postcode' => array(
878
							'priority' => 65,
879
						),
880
						'state'    => array(
881
							'required' => false,
882
						),
883
					),
884
					'FR' => array(
885
						'postcode' => array(
886
							'priority' => 65,
887
						),
888
						'state'    => array(
889
							'required' => false,
890
						),
891
					),
892
					'GP' => array(
893
						'state' => array(
894
							'required' => false,
895
						),
896
					),
897
					'GF' => array(
898
						'state' => array(
899
							'required' => false,
900
						),
901
					),
902
					'HK' => array(
903
						'postcode' => array(
904
							'required' => false,
905
						),
906
						'city'     => array(
907
							'label' => __( 'Town / District', 'woocommerce' ),
908
						),
909
						'state'    => array(
910
							'label' => __( 'Region', 'woocommerce' ),
911
						),
912
					),
913
					'HU' => array(
914
						'state' => array(
915
							'label' => __( 'County', 'woocommerce' ),
916
						),
917
					),
918
					'ID' => array(
919
						'state' => array(
920
							'label' => __( 'Province', 'woocommerce' ),
921
						),
922
					),
923
					'IE' => array(
924
						'postcode' => array(
925
							'required' => false,
926
							'label'    => __( 'Eircode', 'woocommerce' ),
927
						),
928
						'state'    => array(
929
							'label' => __( 'County', 'woocommerce' ),
930
						),
931
					),
932
					'IS' => array(
933 1
						'postcode' => array(
934
							'priority' => 65,
935
						),
936 1
						'state'    => array(
937
							'required' => false,
938
						),
939
					),
940
					'IL' => array(
941 1
						'postcode' => array(
942
							'priority' => 65,
943
						),
944
						'state'    => array(
945
							'required' => false,
946 1
						),
947
					),
948
					'IM' => array(
949
						'state' => array(
950
							'required' => false,
951
						),
952 1
					),
953
					'IT' => array(
954
						'postcode' => array(
955 1
							'priority' => 65,
956
						),
957
						'state'    => array(
958
							'required' => true,
959
							'label'    => __( 'Province', 'woocommerce' ),
960
						),
961
					),
962
					'JP' => array(
963
						'state'    => array(
964
							'label'    => __( 'Prefecture', 'woocommerce' ),
965
							'priority' => 66,
966
						),
967
						'postcode' => array(
968
							'priority' => 65,
969
						),
970
					),
971
					'KR' => array(
972
						'state' => array(
973
							'required' => false,
974
						),
975
					),
976
					'KW' => array(
977
						'state' => array(
978
							'required' => false,
979
						),
980
					),
981
					'LV' => array(
982
						'state' => array(
983
							'label'    => __( 'Municipality', 'woocommerce' ),
984
							'required' => false,
985 1
						),
986
					),
987
					'LB' => array(
988
						'state' => array(
989
							'required' => false,
990 1
						),
991 1
					),
992
					'MQ' => array(
993
						'state' => array(
994
							'required' => false,
995
						),
996
					),
997
					'MT' => array(
998
						'state' => array(
999
							'required' => false,
1000
						),
1001
					),
1002
					'NL' => array(
1003
						'postcode' => array(
1004
							'priority' => 65,
1005
						),
1006
						'state'    => array(
1007
							'required' => false,
1008
							'label'    => __( 'Province', 'woocommerce' ),
1009
						),
1010
					),
1011
					'NG' => array(
1012
						'postcode' => array(
1013
							'label'    => __( 'Postcode', 'woocommerce' ),
1014
							'required' => false,
1015
							'hidden'   => true,
1016
						),
1017
						'state'    => array(
1018
							'label' => __( 'State', 'woocommerce' ),
1019
						),
1020
					),
1021
					'NZ' => array(
1022
						'postcode' => array(
1023
							'label' => __( 'Postcode', 'woocommerce' ),
1024
						),
1025
						'state'    => array(
1026
							'required' => false,
1027
							'label'    => __( 'Region', 'woocommerce' ),
1028 1
						),
1029
					),
1030
					'NO' => array(
1031
						'postcode' => array(
1032
							'priority' => 65,
1033 1
						),
1034
						'state'    => array(
1035
							'required' => false,
1036
						),
1037
					),
1038 1
					'NP' => array(
1039
						'state'    => array(
1040
							'label' => __( 'State / Zone', 'woocommerce' ),
1041
						),
1042
						'postcode' => array(
1043 1
							'required' => false,
1044
						),
1045
					),
1046
					'PL' => array(
1047 1
						'postcode' => array(
1048
							'priority' => 65,
1049
						),
1050
						'state'    => array(
1051
							'required' => false,
1052
						),
1053
					),
1054
					'PT' => array(
1055
						'state' => array(
1056
							'required' => false,
1057
						),
1058
					),
1059
					'RE' => array(
1060 1
						'state' => array(
1061
							'required' => false,
1062
						),
1063
					),
1064
					'RO' => array(
1065
						'state' => array(
1066
							'label'    => __( 'County', 'woocommerce' ),
1067
							'required' => true,
1068
						),
1069
					),
1070
					'SG' => array(
1071
						'state' => array(
1072
							'required' => false,
1073
						),
1074
						'city'  => array(
1075
							'required' => false,
1076
							'hidden'   => true,
1077
						),
1078
					),
1079
					'SK' => array(
1080
						'postcode' => array(
1081
							'priority' => 65,
1082
						),
1083
						'state'    => array(
1084
							'required' => false,
1085
						),
1086 1
					),
1087
					'SI' => array(
1088
						'postcode' => array(
1089
							'priority' => 65,
1090
						),
1091
						'state'    => array(
1092
							'required' => false,
1093
						),
1094
					),
1095
					'SR' => array(
1096
						'postcode' => array(
1097
							'required' => false,
1098
							'hidden'   => true,
1099
						),
1100
					),
1101
					'ES' => array(
1102
						'postcode' => array(
1103
							'priority' => 65,
1104
						),
1105
						'state'    => array(
1106
							'label' => __( 'Province', 'woocommerce' ),
1107
						),
1108
					),
1109
					'LI' => array(
1110
						'postcode' => array(
1111
							'priority' => 65,
1112
						),
1113
						'state'    => array(
1114
							'label'    => __( 'Municipality', 'woocommerce' ),
1115
							'required' => false,
1116
						),
1117
					),
1118
					'LK' => array(
1119
						'state' => array(
1120
							'required' => false,
1121
						),
1122
					),
1123
					'LU' => array(
1124
						'state' => array(
1125
							'required' => false,
1126 1
						),
1127
					),
1128
					'MD' => array(
1129
						'state' => array(
1130
							'label' => __( 'Municipality / District', 'woocommerce' ),
1131
						),
1132
					),
1133
					'SE' => array(
1134 1
						'postcode' => array(
1135
							'priority' => 65,
1136
						),
1137
						'state'    => array(
1138
							'required' => false,
1139
						),
1140
					),
1141
					'TR' => array(
1142
						'postcode' => array(
1143
							'priority' => 65,
1144
						),
1145
						'state'    => array(
1146
							'label' => __( 'Province', 'woocommerce' ),
1147
						),
1148
					),
1149
					'US' => array(
1150 1
						'postcode' => array(
1151
							'label' => __( 'ZIP', 'woocommerce' ),
1152
						),
1153
						'state'    => array(
1154
							'label' => __( 'State', 'woocommerce' ),
1155
						),
1156
					),
1157
					'GB' => array(
1158
						'postcode' => array(
1159
							'label' => __( 'Postcode', 'woocommerce' ),
1160
						),
1161
						'state'    => array(
1162
							'label'    => __( 'County', 'woocommerce' ),
1163
							'required' => false,
1164
						),
1165
					),
1166 1
					'ST' => array(
1167
						'postcode' => array(
1168
							'required' => false,
1169
							'hidden'   => true,
1170
						),
1171 1
						'state'    => array(
1172
							'label' => __( 'District', 'woocommerce' ),
1173
						),
1174 1
					),
1175
					'VN' => array(
1176
						'state'     => array(
1177
							'required' => false,
1178
						),
1179 1
						'postcode'  => array(
1180
							'priority' => 65,
1181
							'required' => false,
1182 1
							'hidden'   => false,
1183
						),
1184
						'address_2' => array(
1185
							'required' => false,
1186
							'hidden'   => true,
1187
						),
1188
					),
1189
					'WS' => array(
1190
						'postcode' => array(
1191
							'required' => false,
1192 1
							'hidden'   => true,
1193
						),
1194
					),
1195
					'YT' => array(
1196
						'state' => array(
1197
							'required' => false,
1198
						),
1199
					),
1200
					'ZA' => array(
1201
						'state' => array(
1202
							'label' => __( 'Province', 'woocommerce' ),
1203
						),
1204
					),
1205
					'ZW' => array(
1206
						'postcode' => array(
1207
							'required' => false,
1208
							'hidden'   => true,
1209
						),
1210
					),
1211
				)
1212
			);
1213
1214
			$this->locale = array_intersect_key( $this->locale, array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() ) );
1215
1216
			// Default Locale Can be filtered to override fields in get_address_fields(). Countries with no specific locale will use default.
1217
			$this->locale['default'] = apply_filters( 'woocommerce_get_country_locale_default', $this->get_default_address_fields() );
1218
1219
			// Filter default AND shop base locales to allow overides via a single function. These will be used when changing countries on the checkout.
1220
			if ( ! isset( $this->locale[ $this->get_base_country() ] ) ) {
1221
				$this->locale[ $this->get_base_country() ] = $this->locale['default'];
1222 1
			}
1223
1224
			$this->locale['default']                   = apply_filters( 'woocommerce_get_country_locale_base', $this->locale['default'] );
1225
			$this->locale[ $this->get_base_country() ] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale[ $this->get_base_country() ] );
1226
		}
1227
1228
		return $this->locale;
1229
	}
1230
1231
	/**
1232
	 * Apply locale and get address fields.
1233
	 *
1234 1
	 * @param  mixed  $country Country.
1235
	 * @param  string $type    Address type, defaults to 'billing_'.
1236
	 * @return array
1237 1
	 */
1238
	public function get_address_fields( $country = '', $type = 'billing_' ) {
1239
		if ( ! $country ) {
1240 1
			$country = $this->get_base_country();
1241 1
		}
1242
1243
		$fields = $this->get_default_address_fields();
1244 1
		$locale = $this->get_country_locale();
1245 1
1246
		if ( isset( $locale[ $country ] ) ) {
1247
			$fields = wc_array_overlay( $fields, $locale[ $country ] );
1248 1
		}
1249
1250
		// Prepend field keys.
1251
		$address_fields = array();
1252
1253
		foreach ( $fields as $key => $value ) {
1254
			if ( 'state' === $key ) {
1255
				$value['country_field'] = $type . 'country';
1256
			}
1257
			$address_fields[ $type . $key ] = $value;
1258
		}
1259
1260
		// Add email and phone fields.
1261
		if ( 'billing_' === $type ) {
1262
			if ( 'hidden' !== get_option( 'woocommerce_checkout_phone_field', 'required' ) ) {
1263
				$address_fields['billing_phone'] = array(
1264
					'label'        => __( 'Phone', 'woocommerce' ),
1265
					'required'     => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ),
1266
					'type'         => 'tel',
1267
					'class'        => array( 'form-row-wide' ),
1268
					'validate'     => array( 'phone' ),
1269
					'autocomplete' => 'tel',
1270
					'priority'     => 100,
1271
				);
1272
			}
1273
			$address_fields['billing_email'] = array(
1274
				'label'        => __( 'Email address', 'woocommerce' ),
1275
				'required'     => true,
1276
				'type'         => 'email',
1277
				'class'        => array( 'form-row-wide' ),
1278
				'validate'     => array( 'email' ),
1279
				'autocomplete' => 'no' === get_option( 'woocommerce_registration_generate_username' ) ? 'email' : 'email username',
1280
				'priority'     => 110,
1281
			);
1282
		}
1283
1284
		/**
1285
		 * Important note on this filter: Changes to address fields can and will be overridden by
1286
		 * the woocommerce_default_address_fields. The locales/default locales apply on top based
1287
		 * on country selection. If you want to change things like the required status of an
1288
		 * address field, filter woocommerce_default_address_fields instead.
1289
		 */
1290
		$address_fields = apply_filters( 'woocommerce_' . $type . 'fields', $address_fields, $country );
1291
		// Sort each of the fields based on priority.
1292
		uasort( $address_fields, 'wc_checkout_fields_uasort_comparison' );
1293
1294
		return $address_fields;
1295
	}
1296
}
1297