PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.2.6
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.2.6
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / class-countries.php

class-countries.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.2.6, at includes/class-countries.php

319 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WeDevs\ERP;
3
4 /**
5 * WP ERP countries
6 *
7 * The WP ERP countries class stores country/state data.
8 *
9 * @category i18n
10 * @package WPERP/i18n
11 */
12 class Countries {
13
14 /** @var array Array of locales */
15 public $locale;
16
17 /** @var array Array of address formats for locales */
18 public $address_formats;
19
20 /**
21 * Initializes the Countries() class
22 *
23 * Checks for an existing Countries() instance
24 * and if it doesn't find one, creates it.
25 */
26 public static function instance() {
27 static $instance = false;
28
29 if ( ! $instance ) {
30 $instance = new self();
31 }
32
33 return $instance;
34 }
35
36 /**
37 * Constructor for the Countries class
38 *
39 * @return void
40 */
41 public function __construct() {
42 $this->load_country_states();
43 }
44
45 /**
46 * Get all countries
47 * @return array
48 */
49 public function get_countries( $default = '' ) {
50 if ( empty( $this->countries ) ) {
51 $this->countries = apply_filters( 'erp_countries', include( WPERP_PATH . '/i18n/countries.php' ) );
52
53 if ( apply_filters('erp_sort_countries', true ) ) {
54 asort( $this->countries );
55 }
56 }
57 if ( '-1' == $default ) {
58 $this->countries = array( '-1' => __( '- Select -', 'erp' ) ) + $this->countries;
59 }
60 return $this->countries;
61 }
62
63 /**
64 * Load the states
65 */
66 public function load_country_states() {
67 global $states;
68
69 // States set to array() are blank i.e. the country has no use for the state field.
70 $states = array(
71 'AF' => array(),
72 'AT' => array(),
73 'BE' => array(),
74 'BI' => array(),
75 'CZ' => array(),
76 'DE' => array(),
77 'DK' => array(),
78 'EE' => array(),
79 'FI' => array(),
80 'FR' => array(),
81 'IS' => array(),
82 'IL' => array(),
83 'KR' => array(),
84 'NL' => array(),
85 'NO' => array(),
86 'PL' => array(),
87 'PT' => array(),
88 'SG' => array(),
89 'SK' => array(),
90 'SI' => array(),
91 'LK' => array(),
92 'SE' => array(),
93 'VN' => array(),
94 );
95
96 // Load only the state files the shop owner wants/needs
97 $countries = $this->get_countries();
98
99 if ( $countries ) {
100 foreach ( $countries as $code => $country ) {
101 if ( ! isset( $states[ $code ] ) && file_exists( WPERP_PATH . '/i18n/states/' . $code . '.php' ) ) {
102 include( WPERP_PATH . '/i18n/states/' . $code . '.php' );
103 }
104 }
105 }
106
107 $this->states = apply_filters( 'erp_states', $states );
108
109 return $this->states;
110 }
111
112 /**
113 * Get the states for a country.
114 *
115 * @access public
116 * @param string $cc country code
117 * @return array of states
118 */
119 public function get_states( $cc = null ) {
120 if ( empty( $this->states ) ) {
121 $this->load_country_states();
122 }
123 if ( ! is_null( $cc ) ) {
124 return isset( $this->states[ $cc ] ) ? $this->states[ $cc ] : false;
125 } else {
126 return $this->states;
127 }
128 }
129
130 /**
131 * Get the base country for the store.
132 *
133 * @access public
134 * @return string
135 */
136 public function get_base_country() {
137 $country = '-1';
138
139 return apply_filters( 'erp_countries_base_country', $country );
140 }
141
142 /**
143 * Generate a country dropdown
144 *
145 * @param string selected country
146 *
147 * @return string the country dropdown
148 */
149 public function country_dropdown( $selected = '' ) {
150 $dropdown = sprintf( '<option value="-1">%s</option>', __( '- Select -', 'erp' ) );
151 $countries = $this->get_countries();
152 $selected = empty( $selected ) ? $this->get_base_country() : $selected;
153
154 foreach ($countries as $key => $value) {
155 $select = ( $key == $selected ) ? ' selected="selected"' : '';
156 $dropdown .= sprintf( "<option value='%s'%s>%s</option>\n", $key, $select, $value );
157 }
158
159 return $dropdown;
160 }
161
162 /**
163 * Outputs the list of countries and states for use in dropdown boxes.
164 * @param string $selected_country (default: '')
165 * @param string $selected_state (default: '')
166 * @param bool $escape (default: false)
167 * @param bool $escape (default: false)
168 */
169 public function country_dropdown_options( $selected_country = '', $selected_state = '', $escape = false ) {
170 $html = '';
171
172 if ( $this->countries ) foreach ( $this->countries as $key => $value ) :
173 if ( $states = $this->get_states( $key ) ) :
174 $html .= '<optgroup label="' . esc_attr( $value ) . '">';
175 $html .= '<option value="' . esc_attr( $key ) .'">' . $value . ' &mdash; '. __( 'Any State', 'erp' ). '</option>';
176 foreach ( $states as $state_key => $state_value ) :
177 $html .= '<option value="' . esc_attr( $key ) . ':' . $state_key . '"';
178
179 if ( $selected_country == $key && $selected_state == $state_key ) {
180 $html .= ' selected="selected"';
181 }
182
183 $html .= '>' . $value . ' &mdash; ' . ( $escape ? esc_js( $state_value ) : $state_value ) . '</option>';
184 endforeach;
185 $html .= '</optgroup>';
186 else :
187 $html .= '<option';
188 if ( $selected_country == $key && $selected_state == '*' ) {
189 $html .= ' selected="selected"';
190 }
191 $html .= ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value ) : $value ) . '</option>';
192 endif;
193 endforeach;
194
195 return $html;
196 }
197
198
199 /**
200 * Get country address formats
201 *
202 * @return array
203 */
204 public function get_address_formats() {
205
206 if ( !$this->address_formats ) :
207
208 // Common formats
209 $postcode_before_city = "{address_1}\n{address_2}\n{postcode} {city}\n{country}";
210
211 // Define address formats
212 $this->address_formats = apply_filters('woocommerce_localisation_address_formats', array(
213 'default' => "{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}",
214 'AU' => "{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
215 'AT' => $postcode_before_city,
216 'BE' => $postcode_before_city,
217 'CA' => "{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
218 'CH' => $postcode_before_city,
219 'CN' => "{country} {postcode}\n{state}, {city}, {address_2}, {address_1}\n{name}",
220 'CZ' => $postcode_before_city,
221 'DE' => $postcode_before_city,
222 'EE' => $postcode_before_city,
223 'FI' => $postcode_before_city,
224 'DK' => $postcode_before_city,
225 'FR' => "{address_1}\n{address_2}\n{postcode} {city_upper}\n{country}",
226 'HK' => "{first_name} {last_name_upper}\n{address_1}\n{address_2}\n{city_upper}\n{state_upper}\n{country}",
227 'HU' => "{city}\n{address_1}\n{address_2}\n{postcode}\n{country}",
228 'IS' => $postcode_before_city,
229 'IT' => "{address_1}\n{address_2}\n{postcode}\n{city}\n{state_upper}\n{country}",
230 'JP' => "{postcode}\n{state}{city}{address_1}\n{address_2}\n{last_name} {first_name}\n {country}",
231 'TW' => "{postcode}\n{city}{address_2}\n{address_1}\n{last_name} {first_name}\n {country}",
232 'LI' => $postcode_before_city,
233 'NL' => $postcode_before_city,
234 'NZ' => "{address_1}\n{address_2}\n{city} {postcode}\n{country}",
235 'NO' => $postcode_before_city,
236 'PL' => $postcode_before_city,
237 'SK' => $postcode_before_city,
238 'SI' => $postcode_before_city,
239 'ES' => "{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}",
240 'SE' => $postcode_before_city,
241 'TR' => "{address_1}\n{address_2}\n{postcode} {city} {state}\n{country}",
242 'US' => "{address_1}\n{address_2}\n{city}, {state_code} {postcode}\n{country}",
243 'VN' => "{address_1}\n{city}\n{country}",
244 ));
245 endif;
246
247 return $this->address_formats;
248 }
249
250 /**
251 * Get country address format
252 *
253 * @param array $args (default: array())
254 *
255 * @return string address
256 */
257 public function get_formatted_address( $args = array() ) {
258
259 $args = array_map( 'trim', $args );
260
261 extract( $args );
262
263 // Get all formats
264 $formats = $this->get_address_formats();
265
266 // Get format for the address' country
267 $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];
268
269 // Handle full country name
270 $full_country = ( isset( $this->countries[ $country ] ) ) ? $this->countries[ $country ] : $country;
271
272 // Handle full state name
273 $full_state = ( $country && $state && isset( $this->states[ $country ][ $state ] ) ) ? $this->states[ $country ][ $state ] : $state;
274
275 // Substitute address parts into the string
276 $replace = array_map( 'esc_html', apply_filters( 'erp_formatted_address_replacements', array(
277 '{address_1}' => $address_1,
278 '{address_2}' => $address_2,
279 '{city}' => $city,
280 '{state}' => $full_state,
281 '{postcode}' => $postcode,
282 '{country}' => $full_country,
283 '{address_1_upper}' => strtoupper( $address_1 ),
284 '{address_2_upper}' => strtoupper( $address_2 ),
285 '{city_upper}' => strtoupper( $city ),
286 '{state_upper}' => strtoupper( $full_state ),
287 '{state_code}' => strtoupper( $state ),
288 '{postcode_upper}' => strtoupper( $postcode ),
289 '{country_upper}' => strtoupper( $full_country ),
290 ), $args ) );
291
292 $formatted_address = str_replace( array_keys( $replace ), $replace, $format );
293
294 // Clean up white space
295 $formatted_address = preg_replace( '/ +/', ' ', trim( $formatted_address ) );
296 $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
297
298 // Break newlines apart and remove empty lines/trim commas and white space
299 $formatted_address = array_filter( array_map( array( $this, 'trim_formatted_address_line' ), explode( "\n", $formatted_address ) ) );
300
301 // Add html breaks
302 $formatted_address = implode( '<br/>', $formatted_address );
303
304 // We're done!
305 return $formatted_address;
306 }
307
308 /**
309 * trim white space and commans off a line
310 *
311 * @param string
312 *
313 * @return string
314 */
315 private function trim_formatted_address_line( $line ) {
316 return trim( $line, ", " );
317 }
318 }
319