class-wc-countries.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. <?php
  2. /**
  3. * WooCommerce countries
  4. *
  5. * @package WooCommerce\l10n
  6. * @version 3.3.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * The WooCommerce countries class stores country/state data.
  11. */
  12. class WC_Countries {
  13. /**
  14. * Locales list.
  15. *
  16. * @var array
  17. */
  18. public $locale = array();
  19. /**
  20. * List of address formats for locales.
  21. *
  22. * @var array
  23. */
  24. public $address_formats = array();
  25. /**
  26. * Auto-load in-accessible properties on demand.
  27. *
  28. * @param mixed $key 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. * Get all countries.
  40. *
  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. * Get all continents.
  54. *
  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. * Get continent code for a country code.
  65. *
  66. * @since 2.6.0
  67. * @param string $cc Continent code.
  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, true ) ) {
  76. return $continent_code;
  77. }
  78. }
  79. return '';
  80. }
  81. /**
  82. * Load the states.
  83. */
  84. public function load_country_states() {
  85. global $states;
  86. // States set to array() are blank i.e. the country has no use for the state field.
  87. $states = array(
  88. 'AF' => array(),
  89. 'AT' => array(),
  90. 'AX' => array(),
  91. 'BE' => array(),
  92. 'BH' => array(),
  93. 'BI' => array(),
  94. 'CZ' => array(),
  95. 'DE' => array(),
  96. 'DK' => array(),
  97. 'EE' => array(),
  98. 'FI' => array(),
  99. 'FR' => array(),
  100. 'GP' => array(),
  101. 'GF' => array(),
  102. 'IS' => array(),
  103. 'IL' => array(),
  104. 'IM' => array(),
  105. 'KR' => array(),
  106. 'KW' => array(),
  107. 'LB' => array(),
  108. 'LU' => array(),
  109. 'MQ' => array(),
  110. 'NL' => array(),
  111. 'NO' => array(),
  112. 'PL' => array(),
  113. 'PT' => array(),
  114. 'RE' => array(),
  115. 'SG' => array(),
  116. 'SK' => array(),
  117. 'SI' => array(),
  118. 'LK' => array(),
  119. 'SE' => array(),
  120. 'VN' => array(),
  121. 'YT' => array(),
  122. );
  123. // Load only the state files the shop owner wants/needs.
  124. $allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() );
  125. if ( ! empty( $allowed ) ) {
  126. foreach ( $allowed as $code => $country ) {
  127. if ( ! isset( $states[ $code ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ) ) {
  128. include WC()->plugin_path() . '/i18n/states/' . $code . '.php';
  129. }
  130. }
  131. }
  132. $this->states = apply_filters( 'woocommerce_states', $states );
  133. }
  134. /**
  135. * Get the states for a country.
  136. *
  137. * @param string $cc Country code.
  138. * @return false|array of states
  139. */
  140. public function get_states( $cc = null ) {
  141. if ( empty( $this->states ) ) {
  142. $this->load_country_states();
  143. }
  144. if ( ! is_null( $cc ) ) {
  145. return isset( $this->states[ $cc ] ) ? $this->states[ $cc ] : false;
  146. } else {
  147. return $this->states;
  148. }
  149. }
  150. /**
  151. * Get the base address (first line) for the store.
  152. *
  153. * @since 3.1.1
  154. * @return string
  155. */
  156. public function get_base_address() {
  157. $base_address = get_option( 'woocommerce_store_address', '' );
  158. return apply_filters( 'woocommerce_countries_base_address', $base_address );
  159. }
  160. /**
  161. * Get the base address (second line) for the store.
  162. *
  163. * @since 3.1.1
  164. * @return string
  165. */
  166. public function get_base_address_2() {
  167. $base_address_2 = get_option( 'woocommerce_store_address_2', '' );
  168. return apply_filters( 'woocommerce_countries_base_address_2', $base_address_2 );
  169. }
  170. /**
  171. * Get the base country for the store.
  172. *
  173. * @return string
  174. */
  175. public function get_base_country() {
  176. $default = wc_get_base_location();
  177. return apply_filters( 'woocommerce_countries_base_country', $default['country'] );
  178. }
  179. /**
  180. * Get the base state for the store.
  181. *
  182. * @return string
  183. */
  184. public function get_base_state() {
  185. $default = wc_get_base_location();
  186. return apply_filters( 'woocommerce_countries_base_state', $default['state'] );
  187. }
  188. /**
  189. * Get the base city for the store.
  190. *
  191. * @version 3.1.1
  192. * @return string
  193. */
  194. public function get_base_city() {
  195. $base_city = get_option( 'woocommerce_store_city', '' );
  196. return apply_filters( 'woocommerce_countries_base_city', $base_city );
  197. }
  198. /**
  199. * Get the base postcode for the store.
  200. *
  201. * @since 3.1.1
  202. * @return string
  203. */
  204. public function get_base_postcode() {
  205. $base_postcode = get_option( 'woocommerce_store_postcode', '' );
  206. return apply_filters( 'woocommerce_countries_base_postcode', $base_postcode );
  207. }
  208. /**
  209. * Get the allowed countries for the store.
  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. if ( 'all_except' === get_option( 'woocommerce_allowed_countries' ) ) {
  218. $except_countries = get_option( 'woocommerce_all_except_countries', array() );
  219. if ( ! $except_countries ) {
  220. return $this->countries;
  221. } else {
  222. $all_except_countries = $this->countries;
  223. foreach ( $except_countries as $country ) {
  224. unset( $all_except_countries[ $country ] );
  225. }
  226. return apply_filters( 'woocommerce_countries_allowed_countries', $all_except_countries );
  227. }
  228. }
  229. $countries = array();
  230. $raw_countries = get_option( 'woocommerce_specific_allowed_countries', array() );
  231. if ( $raw_countries ) {
  232. foreach ( $raw_countries as $country ) {
  233. $countries[ $country ] = $this->countries[ $country ];
  234. }
  235. }
  236. return apply_filters( 'woocommerce_countries_allowed_countries', $countries );
  237. }
  238. /**
  239. * Get the countries you ship to.
  240. *
  241. * @return array
  242. */
  243. public function get_shipping_countries() {
  244. if ( '' === get_option( 'woocommerce_ship_to_countries' ) ) {
  245. return $this->get_allowed_countries();
  246. }
  247. if ( 'all' === get_option( 'woocommerce_ship_to_countries' ) ) {
  248. return $this->countries;
  249. }
  250. $countries = array();
  251. $raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
  252. if ( $raw_countries ) {
  253. foreach ( $raw_countries as $country ) {
  254. $countries[ $country ] = $this->countries[ $country ];
  255. }
  256. }
  257. return apply_filters( 'woocommerce_countries_shipping_countries', $countries );
  258. }
  259. /**
  260. * Get allowed country states.
  261. *
  262. * @return array
  263. */
  264. public function get_allowed_country_states() {
  265. if ( get_option( 'woocommerce_allowed_countries' ) !== 'specific' ) {
  266. return $this->states;
  267. }
  268. $states = array();
  269. $raw_countries = get_option( 'woocommerce_specific_allowed_countries' );
  270. if ( $raw_countries ) {
  271. foreach ( $raw_countries as $country ) {
  272. if ( isset( $this->states[ $country ] ) ) {
  273. $states[ $country ] = $this->states[ $country ];
  274. }
  275. }
  276. }
  277. return apply_filters( 'woocommerce_countries_allowed_country_states', $states );
  278. }
  279. /**
  280. * Get shipping country states.
  281. *
  282. * @return array
  283. */
  284. public function get_shipping_country_states() {
  285. if ( get_option( 'woocommerce_ship_to_countries' ) === '' ) {
  286. return $this->get_allowed_country_states();
  287. }
  288. if ( get_option( 'woocommerce_ship_to_countries' ) !== 'specific' ) {
  289. return $this->states;
  290. }
  291. $states = array();
  292. $raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
  293. if ( $raw_countries ) {
  294. foreach ( $raw_countries as $country ) {
  295. if ( ! empty( $this->states[ $country ] ) ) {
  296. $states[ $country ] = $this->states[ $country ];
  297. }
  298. }
  299. }
  300. return apply_filters( 'woocommerce_countries_shipping_country_states', $states );
  301. }
  302. /**
  303. * Gets an array of countries in the EU.
  304. *
  305. * MC (monaco) and IM (isle of man, part of UK) also use VAT.
  306. *
  307. * @param string $type Type of countries to retrieve. Blank for EU member countries. eu_vat for EU VAT countries.
  308. * @return string[]
  309. */
  310. public function get_european_union_countries( $type = '' ) {
  311. $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' );
  312. if ( 'eu_vat' === $type ) {
  313. $countries[] = 'MC';
  314. $countries[] = 'IM';
  315. }
  316. return $countries;
  317. }
  318. /**
  319. * Gets the correct string for shipping - either 'to the' or 'to'.
  320. *
  321. * @param string $country_code Country code.
  322. * @return string
  323. */
  324. public function shipping_to_prefix( $country_code = '' ) {
  325. $country_code = $country_code ? $country_code : WC()->customer->get_shipping_country();
  326. $countries = array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' );
  327. $return = in_array( $country_code, $countries, true ) ? __( 'to the', 'woocommerce' ) : __( 'to', 'woocommerce' );
  328. return apply_filters( 'woocommerce_countries_shipping_to_prefix', $return, $country_code );
  329. }
  330. /**
  331. * Prefix certain countries with 'the'.
  332. *
  333. * @param string $country_code Country code.
  334. * @return string
  335. */
  336. public function estimated_for_prefix( $country_code = '' ) {
  337. $country_code = $country_code ? $country_code : $this->get_base_country();
  338. $countries = array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' );
  339. $return = in_array( $country_code, $countries, true ) ? __( 'the', 'woocommerce' ) . ' ' : '';
  340. return apply_filters( 'woocommerce_countries_estimated_for_prefix', $return, $country_code );
  341. }
  342. /**
  343. * Correctly name tax in some countries VAT on the frontend.
  344. *
  345. * @return string
  346. */
  347. public function tax_or_vat() {
  348. $return = in_array( $this->get_base_country(), array_merge( $this->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
  349. return apply_filters( 'woocommerce_countries_tax_or_vat', $return );
  350. }
  351. /**
  352. * Include the Inc Tax label.
  353. *
  354. * @return string
  355. */
  356. public function inc_tax_or_vat() {
  357. $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' );
  358. return apply_filters( 'woocommerce_countries_inc_tax_or_vat', $return );
  359. }
  360. /**
  361. * Include the Ex Tax label.
  362. *
  363. * @return string
  364. */
  365. public function ex_tax_or_vat() {
  366. $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' );
  367. return apply_filters( 'woocommerce_countries_ex_tax_or_vat', $return );
  368. }
  369. /**
  370. * Outputs the list of countries and states for use in dropdown boxes.
  371. *
  372. * @param string $selected_country Selected country.
  373. * @param string $selected_state Selected state.
  374. * @param bool $escape If should escape HTML.
  375. */
  376. public function country_dropdown_options( $selected_country = '', $selected_state = '', $escape = false ) {
  377. if ( $this->countries ) {
  378. foreach ( $this->countries as $key => $value ) {
  379. $states = $this->get_states( $key );
  380. if ( $states ) {
  381. echo '<optgroup label="' . esc_attr( $value ) . '">';
  382. foreach ( $states as $state_key => $state_value ) {
  383. echo '<option value="' . esc_attr( $key ) . ':' . esc_attr( $state_key ) . '"';
  384. if ( $selected_country === $key && $selected_state === $state_key ) {
  385. echo ' selected="selected"';
  386. }
  387. echo '>' . esc_html( $value ) . ' &mdash; ' . ( $escape ? esc_js( $state_value ) : $state_value ) . '</option>'; // WPCS: XSS ok.
  388. }
  389. echo '</optgroup>';
  390. } else {
  391. echo '<option';
  392. if ( $selected_country === $key && '*' === $selected_state ) {
  393. echo ' selected="selected"';
  394. }
  395. echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value ) : $value ) . '</option>'; // WPCS: XSS ok.
  396. }
  397. }
  398. }
  399. }
  400. /**
  401. * Get country address formats.
  402. *
  403. * These define how addresses are formatted for display in various countries.
  404. *
  405. * @return array
  406. */
  407. public function get_address_formats() {
  408. if ( empty( $this->address_formats ) ) {
  409. $this->address_formats = apply_filters(
  410. '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' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  414. 'BE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  415. 'CA' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
  416. 'CH' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  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' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  420. 'DE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  421. 'EE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  422. 'FI' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  423. 'DK' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  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' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  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' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  433. 'NL' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  434. 'NZ' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {postcode}\n{country}",
  435. 'NO' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  436. 'PL' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  437. 'PT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  438. 'SK' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  439. 'SI' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  440. 'ES' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}",
  441. 'SE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  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. * Get country address format.
  452. *
  453. * @param array $args Arguments.
  454. * @return string
  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. $args = array_map( 'trim', wp_parse_args( $args, $default_args ) );
  469. $state = $args['state'];
  470. $country = $args['country'];
  471. // Get all formats.
  472. $formats = $this->get_address_formats();
  473. // Get format for the address' country.
  474. $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];
  475. // Handle full country name.
  476. $full_country = ( isset( $this->countries[ $country ] ) ) ? $this->countries[ $country ] : $country;
  477. // Country is not needed if the same as base.
  478. if ( $country === $this->get_base_country() && ! apply_filters( 'woocommerce_formatted_address_force_country_display', false ) ) {
  479. $format = str_replace( '{country}', '', $format );
  480. }
  481. // Handle full state name.
  482. $full_state = ( $country && $state && isset( $this->states[ $country ][ $state ] ) ) ? $this->states[ $country ][ $state ] : $state;
  483. // Substitute address parts into the string.
  484. $replace = array_map(
  485. 'esc_html', apply_filters(
  486. 'woocommerce_formatted_address_replacements', array(
  487. '{first_name}' => $args['first_name'],
  488. '{last_name}' => $args['last_name'],
  489. '{name}' => $args['first_name'] . ' ' . $args['last_name'],
  490. '{company}' => $args['company'],
  491. '{address_1}' => $args['address_1'],
  492. '{address_2}' => $args['address_2'],
  493. '{city}' => $args['city'],
  494. '{state}' => $full_state,
  495. '{postcode}' => $args['postcode'],
  496. '{country}' => $full_country,
  497. '{first_name_upper}' => strtoupper( $args['first_name'] ),
  498. '{last_name_upper}' => strtoupper( $args['last_name'] ),
  499. '{name_upper}' => strtoupper( $args['first_name'] . ' ' . $args['last_name'] ),
  500. '{company_upper}' => strtoupper( $args['company'] ),
  501. '{address_1_upper}' => strtoupper( $args['address_1'] ),
  502. '{address_2_upper}' => strtoupper( $args['address_2'] ),
  503. '{city_upper}' => strtoupper( $args['city'] ),
  504. '{state_upper}' => strtoupper( $full_state ),
  505. '{state_code}' => strtoupper( $state ),
  506. '{postcode_upper}' => strtoupper( $args['postcode'] ),
  507. '{country_upper}' => strtoupper( $full_country ),
  508. ), $args
  509. )
  510. );
  511. $formatted_address = str_replace( array_keys( $replace ), $replace, $format );
  512. // Clean up white space.
  513. $formatted_address = preg_replace( '/ +/', ' ', trim( $formatted_address ) );
  514. $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
  515. // Break newlines apart and remove empty lines/trim commas and white space.
  516. $formatted_address = array_filter( array_map( array( $this, 'trim_formatted_address_line' ), explode( "\n", $formatted_address ) ) );
  517. // Add html breaks.
  518. $formatted_address = implode( '<br/>', $formatted_address );
  519. // We're done!
  520. return $formatted_address;
  521. }
  522. /**
  523. * Trim white space and commas off a line.
  524. *
  525. * @param string $line Line.
  526. * @return string
  527. */
  528. private function trim_formatted_address_line( $line ) {
  529. return trim( $line, ', ' );
  530. }
  531. /**
  532. * Returns the fields we show by default. This can be filtered later on.
  533. *
  534. * @return array
  535. */
  536. public function get_default_address_fields() {
  537. if ( 'optional' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ) ) {
  538. $address_2_placeholder = __( 'Apartment, suite, unit etc. (optional)', 'woocommerce' );
  539. } else {
  540. $address_2_placeholder = __( 'Apartment, suite, unit etc.', 'woocommerce' );
  541. }
  542. $fields = array(
  543. 'first_name' => array(
  544. 'label' => __( 'First name', 'woocommerce' ),
  545. 'required' => true,
  546. 'class' => array( 'form-row-first' ),
  547. 'autocomplete' => 'given-name',
  548. 'priority' => 10,
  549. ),
  550. 'last_name' => array(
  551. 'label' => __( 'Last name', 'woocommerce' ),
  552. 'required' => true,
  553. 'class' => array( 'form-row-last' ),
  554. 'autocomplete' => 'family-name',
  555. 'priority' => 20,
  556. ),
  557. 'company' => array(
  558. 'label' => __( 'Company name', 'woocommerce' ),
  559. 'class' => array( 'form-row-wide' ),
  560. 'autocomplete' => 'organization',
  561. 'priority' => 30,
  562. 'required' => 'required' === get_option( 'woocommerce_checkout_company_field', 'optional' ),
  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. 'priority' => 40,
  571. ),
  572. 'address_1' => array(
  573. 'label' => __( 'Street address', 'woocommerce' ),
  574. /* translators: use local order of street name and house number. */
  575. 'placeholder' => esc_attr__( 'House number and street name', 'woocommerce' ),
  576. 'required' => true,
  577. 'class' => array( 'form-row-wide', 'address-field' ),
  578. 'autocomplete' => 'address-line1',
  579. 'priority' => 50,
  580. ),
  581. 'address_2' => array(
  582. 'placeholder' => esc_attr( $address_2_placeholder ),
  583. 'class' => array( 'form-row-wide', 'address-field' ),
  584. 'autocomplete' => 'address-line2',
  585. 'priority' => 60,
  586. 'required' => 'required' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ),
  587. ),
  588. 'city' => array(
  589. 'label' => __( 'Town / City', 'woocommerce' ),
  590. 'required' => true,
  591. 'class' => array( 'form-row-wide', 'address-field' ),
  592. 'autocomplete' => 'address-level2',
  593. 'priority' => 70,
  594. ),
  595. 'state' => array(
  596. 'type' => 'state',
  597. 'label' => __( 'State / County', 'woocommerce' ),
  598. 'required' => true,
  599. 'class' => array( 'form-row-wide', 'address-field' ),
  600. 'validate' => array( 'state' ),
  601. 'autocomplete' => 'address-level1',
  602. 'priority' => 80,
  603. ),
  604. 'postcode' => array(
  605. 'label' => __( 'Postcode / ZIP', 'woocommerce' ),
  606. 'required' => true,
  607. 'class' => array( 'form-row-wide', 'address-field' ),
  608. 'validate' => array( 'postcode' ),
  609. 'autocomplete' => 'postal-code',
  610. 'priority' => 90,
  611. ),
  612. );
  613. if ( 'hidden' === get_option( 'woocommerce_checkout_company_field', 'optional' ) ) {
  614. unset( $fields['company'] );
  615. }
  616. $address_2_visibility = get_option( 'woocommerce_checkout_address_2_field', 'optional' );
  617. if ( 'hidden' === $address_2_visibility ) {
  618. unset( $fields['address_2'] );
  619. }
  620. if ( 'hidden' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ) ) {
  621. unset( $fields['address_2'] );
  622. }
  623. return apply_filters( 'woocommerce_default_address_fields', $fields );
  624. }
  625. /**
  626. * Get JS selectors for fields which are shown/hidden depending on the locale.
  627. *
  628. * @return array
  629. */
  630. public function get_country_locale_field_selectors() {
  631. $locale_fields = array(
  632. 'address_1' => '#billing_address_1_field, #shipping_address_1_field',
  633. 'address_2' => '#billing_address_2_field, #shipping_address_2_field',
  634. 'state' => '#billing_state_field, #shipping_state_field, #calc_shipping_state_field',
  635. 'postcode' => '#billing_postcode_field, #shipping_postcode_field, #calc_shipping_postcode_field',
  636. 'city' => '#billing_city_field, #shipping_city_field, #calc_shipping_city_field',
  637. );
  638. return apply_filters( 'woocommerce_country_locale_field_selectors', $locale_fields );
  639. }
  640. /**
  641. * Get country locale settings.
  642. *
  643. * These locales override the default country selections after a country is chosen.
  644. *
  645. * @return array
  646. */
  647. public function get_country_locale() {
  648. if ( empty( $this->locale ) ) {
  649. $this->locale = apply_filters(
  650. 'woocommerce_get_country_locale', array(
  651. 'AE' => array(
  652. 'postcode' => array(
  653. 'required' => false,
  654. 'hidden' => true,
  655. ),
  656. 'state' => array(
  657. 'required' => false,
  658. ),
  659. ),
  660. 'AF' => array(
  661. 'state' => array(
  662. 'required' => false,
  663. ),
  664. ),
  665. 'AT' => array(
  666. 'postcode' => array(
  667. 'priority' => 65,
  668. ),
  669. 'state' => array(
  670. 'required' => false,
  671. ),
  672. ),
  673. 'AU' => array(
  674. 'city' => array(
  675. 'label' => __( 'Suburb', 'woocommerce' ),
  676. ),
  677. 'postcode' => array(
  678. 'label' => __( 'Postcode', 'woocommerce' ),
  679. ),
  680. 'state' => array(
  681. 'label' => __( 'State', 'woocommerce' ),
  682. ),
  683. ),
  684. 'AX' => array(
  685. 'postcode' => array(
  686. 'priority' => 65,
  687. ),
  688. 'state' => array(
  689. 'required' => false,
  690. ),
  691. ),
  692. 'BD' => array(
  693. 'postcode' => array(
  694. 'required' => false,
  695. ),
  696. 'state' => array(
  697. 'label' => __( 'District', 'woocommerce' ),
  698. ),
  699. ),
  700. 'BE' => array(
  701. 'postcode' => array(
  702. 'priority' => 65,
  703. ),
  704. 'state' => array(
  705. 'required' => false,
  706. 'label' => __( 'Province', 'woocommerce' ),
  707. ),
  708. ),
  709. 'BH' => array(
  710. 'state' => array(
  711. 'required' => false,
  712. ),
  713. ),
  714. 'BI' => array(
  715. 'state' => array(
  716. 'required' => false,
  717. ),
  718. ),
  719. 'BO' => array(
  720. 'postcode' => array(
  721. 'required' => false,
  722. 'hidden' => true,
  723. ),
  724. ),
  725. 'BS' => array(
  726. 'postcode' => array(
  727. 'required' => false,
  728. 'hidden' => true,
  729. ),
  730. ),
  731. 'CA' => array(
  732. 'state' => array(
  733. 'label' => __( 'Province', 'woocommerce' ),
  734. ),
  735. ),
  736. 'CH' => array(
  737. 'postcode' => array(
  738. 'priority' => 65,
  739. ),
  740. 'state' => array(
  741. 'label' => __( 'Canton', 'woocommerce' ),
  742. 'required' => false,
  743. ),
  744. ),
  745. 'CL' => array(
  746. 'city' => array(
  747. 'required' => true,
  748. ),
  749. 'postcode' => array(
  750. 'required' => false,
  751. ),
  752. 'state' => array(
  753. 'label' => __( 'Region', 'woocommerce' ),
  754. ),
  755. ),
  756. 'CN' => array(
  757. 'state' => array(
  758. 'label' => __( 'Province', 'woocommerce' ),
  759. ),
  760. ),
  761. 'CO' => array(
  762. 'postcode' => array(
  763. 'required' => false,
  764. ),
  765. ),
  766. 'CZ' => array(
  767. 'state' => array(
  768. 'required' => false,
  769. ),
  770. ),
  771. 'DE' => array(
  772. 'postcode' => array(
  773. 'priority' => 65,
  774. ),
  775. 'state' => array(
  776. 'required' => false,
  777. ),
  778. ),
  779. 'DK' => array(
  780. 'postcode' => array(
  781. 'priority' => 65,
  782. ),
  783. 'state' => array(
  784. 'required' => false,
  785. ),
  786. ),
  787. 'EE' => array(
  788. 'postcode' => array(
  789. 'priority' => 65,
  790. ),
  791. 'state' => array(
  792. 'required' => false,
  793. ),
  794. ),
  795. 'FI' => array(
  796. 'postcode' => array(
  797. 'priority' => 65,
  798. ),
  799. 'state' => array(
  800. 'required' => false,
  801. ),
  802. ),
  803. 'FR' => array(
  804. 'postcode' => array(
  805. 'priority' => 65,
  806. ),
  807. 'state' => array(
  808. 'required' => false,
  809. ),
  810. ),
  811. 'GP' => array(
  812. 'state' => array(
  813. 'required' => false,
  814. ),
  815. ),
  816. 'GF' => array(
  817. 'state' => array(
  818. 'required' => false,
  819. ),
  820. ),
  821. 'HK' => array(
  822. 'postcode' => array(
  823. 'required' => false,
  824. ),
  825. 'city' => array(
  826. 'label' => __( 'Town / District', 'woocommerce' ),
  827. ),
  828. 'state' => array(
  829. 'label' => __( 'Region', 'woocommerce' ),
  830. ),
  831. ),
  832. 'HU' => array(
  833. 'state' => array(
  834. 'label' => __( 'County', 'woocommerce' ),
  835. ),
  836. ),
  837. 'ID' => array(
  838. 'state' => array(
  839. 'label' => __( 'Province', 'woocommerce' ),
  840. ),
  841. ),
  842. 'IE' => array(
  843. 'postcode' => array(
  844. 'required' => false,
  845. 'label' => __( 'Eircode', 'woocommerce' ),
  846. ),
  847. 'state' => array(
  848. 'label' => __( 'County', 'woocommerce' ),
  849. ),
  850. ),
  851. 'IS' => array(
  852. 'postcode' => array(
  853. 'priority' => 65,
  854. ),
  855. 'state' => array(
  856. 'required' => false,
  857. ),
  858. ),
  859. 'IL' => array(
  860. 'postcode' => array(
  861. 'priority' => 65,
  862. ),
  863. 'state' => array(
  864. 'required' => false,
  865. ),
  866. ),
  867. 'IM' => array(
  868. 'state' => array(
  869. 'required' => false,
  870. ),
  871. ),
  872. 'IT' => array(
  873. 'postcode' => array(
  874. 'priority' => 65,
  875. ),
  876. 'state' => array(
  877. 'required' => true,
  878. 'label' => __( 'Province', 'woocommerce' ),
  879. ),
  880. ),
  881. 'JP' => array(
  882. 'state' => array(
  883. 'label' => __( 'Prefecture', 'woocommerce' ),
  884. 'priority' => 66,
  885. ),
  886. 'postcode' => array(
  887. 'priority' => 65,
  888. ),
  889. ),
  890. 'KR' => array(
  891. 'state' => array(
  892. 'required' => false,
  893. ),
  894. ),
  895. 'KW' => array(
  896. 'state' => array(
  897. 'required' => false,
  898. ),
  899. ),
  900. 'LB' => array(
  901. 'state' => array(
  902. 'required' => false,
  903. ),
  904. ),
  905. 'MQ' => array(
  906. 'state' => array(
  907. 'required' => false,
  908. ),
  909. ),
  910. 'NL' => array(
  911. 'postcode' => array(
  912. 'priority' => 65,
  913. ),
  914. 'state' => array(
  915. 'required' => false,
  916. 'label' => __( 'Province', 'woocommerce' ),
  917. ),
  918. ),
  919. 'NZ' => array(
  920. 'postcode' => array(
  921. 'label' => __( 'Postcode', 'woocommerce' ),
  922. ),
  923. 'state' => array(
  924. 'required' => false,
  925. 'label' => __( 'Region', 'woocommerce' ),
  926. ),
  927. ),
  928. 'NO' => array(
  929. 'postcode' => array(
  930. 'priority' => 65,
  931. ),
  932. 'state' => array(
  933. 'required' => false,
  934. ),
  935. ),
  936. 'NP' => array(
  937. 'state' => array(
  938. 'label' => __( 'State / Zone', 'woocommerce' ),
  939. ),
  940. 'postcode' => array(
  941. 'required' => false,
  942. ),
  943. ),
  944. 'PL' => array(
  945. 'postcode' => array(
  946. 'priority' => 65,
  947. ),
  948. 'state' => array(
  949. 'required' => false,
  950. ),
  951. ),
  952. 'PT' => array(
  953. 'state' => array(
  954. 'required' => false,
  955. ),
  956. ),
  957. 'RE' => array(
  958. 'state' => array(
  959. 'required' => false,
  960. ),
  961. ),
  962. 'RO' => array(
  963. 'state' => array(
  964. 'label' => __( 'County', 'woocommerce' ),
  965. 'required' => false,
  966. ),
  967. ),
  968. 'SG' => array(
  969. 'state' => array(
  970. 'required' => false,
  971. ),
  972. ),
  973. 'SK' => array(
  974. 'postcode' => array(
  975. 'priority' => 65,
  976. ),
  977. 'state' => array(
  978. 'required' => false,
  979. ),
  980. ),
  981. 'SI' => array(
  982. 'postcode' => array(
  983. 'priority' => 65,
  984. ),
  985. 'state' => array(
  986. 'required' => false,
  987. ),
  988. ),
  989. 'SR' => array(
  990. 'postcode' => array(
  991. 'required' => false,
  992. 'hidden' => true,
  993. ),
  994. ),
  995. 'ES' => array(
  996. 'postcode' => array(
  997. 'priority' => 65,
  998. ),
  999. 'state' => array(
  1000. 'label' => __( 'Province', 'woocommerce' ),
  1001. ),
  1002. ),
  1003. 'LI' => array(
  1004. 'postcode' => array(
  1005. 'priority' => 65,
  1006. ),
  1007. 'state' => array(
  1008. 'label' => __( 'Municipality', 'woocommerce' ),
  1009. 'required' => false,
  1010. ),
  1011. ),
  1012. 'LK' => array(
  1013. 'state' => array(
  1014. 'required' => false,
  1015. ),
  1016. ),
  1017. 'LU' => array(
  1018. 'state' => array(
  1019. 'required' => false,
  1020. ),
  1021. ),
  1022. 'MD' => array(
  1023. 'state' => array(
  1024. 'label' => __( 'Municipality / District', 'woocommerce' ),
  1025. ),
  1026. ),
  1027. 'SE' => array(
  1028. 'postcode' => array(
  1029. 'priority' => 65,
  1030. ),
  1031. 'state' => array(
  1032. 'required' => false,
  1033. ),
  1034. ),
  1035. 'TR' => array(
  1036. 'postcode' => array(
  1037. 'priority' => 65,
  1038. ),
  1039. 'state' => array(
  1040. 'label' => __( 'Province', 'woocommerce' ),
  1041. ),
  1042. ),
  1043. 'US' => array(
  1044. 'postcode' => array(
  1045. 'label' => __( 'ZIP', 'woocommerce' ),
  1046. ),
  1047. 'state' => array(
  1048. 'label' => __( 'State', 'woocommerce' ),
  1049. ),
  1050. ),
  1051. 'GB' => array(
  1052. 'postcode' => array(
  1053. 'label' => __( 'Postcode', 'woocommerce' ),
  1054. ),
  1055. 'state' => array(
  1056. 'label' => __( 'County', 'woocommerce' ),
  1057. 'required' => false,
  1058. ),
  1059. ),
  1060. 'VN' => array(
  1061. 'state' => array(
  1062. 'required' => false,
  1063. ),
  1064. 'postcode' => array(
  1065. 'priority' => 65,
  1066. 'required' => false,
  1067. 'hidden' => false,
  1068. ),
  1069. 'address_2' => array(
  1070. 'required' => false,
  1071. 'hidden' => true,
  1072. ),
  1073. ),
  1074. 'WS' => array(
  1075. 'postcode' => array(
  1076. 'required' => false,
  1077. 'hidden' => true,
  1078. ),
  1079. ),
  1080. 'YT' => array(
  1081. 'state' => array(
  1082. 'required' => false,
  1083. ),
  1084. ),
  1085. 'ZA' => array(
  1086. 'state' => array(
  1087. 'label' => __( 'Province', 'woocommerce' ),
  1088. ),
  1089. ),
  1090. 'ZW' => array(
  1091. 'postcode' => array(
  1092. 'required' => false,
  1093. 'hidden' => true,
  1094. ),
  1095. ),
  1096. )
  1097. );
  1098. $this->locale = array_intersect_key( $this->locale, array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() ) );
  1099. // Default Locale Can be filtered to override fields in get_address_fields(). Countries with no specific locale will use default.
  1100. $this->locale['default'] = apply_filters( 'woocommerce_get_country_locale_default', $this->get_default_address_fields() );
  1101. // Filter default AND shop base locales to allow overides via a single function. These will be used when changing countries on the checkout.
  1102. if ( ! isset( $this->locale[ $this->get_base_country() ] ) ) {
  1103. $this->locale[ $this->get_base_country() ] = $this->locale['default'];
  1104. }
  1105. $this->locale['default'] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale['default'] );
  1106. $this->locale[ $this->get_base_country() ] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale[ $this->get_base_country() ] );
  1107. }
  1108. return $this->locale;
  1109. }
  1110. /**
  1111. * Apply locale and get address fields.
  1112. *
  1113. * @param mixed $country Country.
  1114. * @param string $type Address type, defaults to 'billing_'.
  1115. * @return array
  1116. */
  1117. public function get_address_fields( $country = '', $type = 'billing_' ) {
  1118. if ( ! $country ) {
  1119. $country = $this->get_base_country();
  1120. }
  1121. $fields = $this->get_default_address_fields();
  1122. $locale = $this->get_country_locale();
  1123. if ( isset( $locale[ $country ] ) ) {
  1124. $fields = wc_array_overlay( $fields, $locale[ $country ] );
  1125. }
  1126. // Prepend field keys.
  1127. $address_fields = array();
  1128. foreach ( $fields as $key => $value ) {
  1129. if ( 'state' === $key ) {
  1130. $value['country_field'] = $type . 'country';
  1131. }
  1132. $address_fields[ $type . $key ] = $value;
  1133. }
  1134. // Add email and phone fields.
  1135. if ( 'billing_' === $type ) {
  1136. if ( 'hidden' !== get_option( 'woocommerce_checkout_phone_field', 'required' ) ) {
  1137. $address_fields['billing_phone'] = array(
  1138. 'label' => __( 'Phone', 'woocommerce' ),
  1139. 'required' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ),
  1140. 'type' => 'tel',
  1141. 'class' => array( 'form-row-wide' ),
  1142. 'validate' => array( 'phone' ),
  1143. 'autocomplete' => 'tel',
  1144. 'priority' => 100,
  1145. );
  1146. }
  1147. $address_fields['billing_email'] = array(
  1148. 'label' => __( 'Email address', 'woocommerce' ),
  1149. 'required' => true,
  1150. 'type' => 'email',
  1151. 'class' => array( 'form-row-wide' ),
  1152. 'validate' => array( 'email' ),
  1153. 'autocomplete' => 'no' === get_option( 'woocommerce_registration_generate_username' ) ? 'email' : 'email username',
  1154. 'priority' => 110,
  1155. );
  1156. }
  1157. /**
  1158. * Important note on this filter: Changes to address fields can and will be overridden by
  1159. * the woocommerce_default_address_fields. The locales/default locales apply on top based
  1160. * on country selection. If you want to change things like the required status of an
  1161. * address field, filter woocommerce_default_address_fields instead.
  1162. */
  1163. return apply_filters( 'woocommerce_' . $type . 'fields', $address_fields, $country );
  1164. }
  1165. }