| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
die( 'You are not allowed to call this page directly.' ); |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Controller for Address fields. |
| 9 |
* Provides helper methods for CSV export and country codes. |
| 10 |
* Rendering is handled by FrmFieldAddress model. |
| 11 |
* |
| 12 |
* @since 6.34 |
| 13 |
*/ |
| 14 |
class FrmAddressesController extends FrmComboFieldsController { |
| 15 |
|
| 16 |
/** |
| 17 |
* @since 6.34 |
| 18 |
* |
| 19 |
* @var array|null Country codes indexed by country name. This is stored when maybe_define_country_codes is called the first time. |
| 20 |
*/ |
| 21 |
private static $country_codes; |
| 22 |
|
| 23 |
/** |
| 24 |
* Show address field in form. |
| 25 |
* |
| 26 |
* @since 6.34 |
| 27 |
* |
| 28 |
* @param array $field |
| 29 |
* @param string $field_name |
| 30 |
* @param array $atts |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public static function show_in_form( $field, $field_name, $atts ) { |
| 35 |
$errors = $atts['errors'] ?? array(); |
| 36 |
$html_id = $atts['html_id']; |
| 37 |
$defaults = self::empty_value_array(); |
| 38 |
self::fill_values( $field['value'], $defaults ); |
| 39 |
self::fill_values( $field['default_value'], $defaults ); |
| 40 |
|
| 41 |
$sub_fields = self::get_sub_fields( $field ); |
| 42 |
|
| 43 |
include FrmAppHelper::plugin_path() . '/classes/views/combo-fields/input.php'; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add optional class to field. |
| 48 |
* |
| 49 |
* @since 6.34 |
| 50 |
* |
| 51 |
* @param string $class |
| 52 |
* @param array $field |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public static function add_optional_class( $class, $field ) { |
| 57 |
return $class . ' frm_optional'; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get empty value array for address field. |
| 62 |
* |
| 63 |
* @since 6.34 |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
private static function empty_value_array() { |
| 68 |
$default = array( |
| 69 |
'line1' => '', |
| 70 |
'line2' => '', |
| 71 |
'city' => '', |
| 72 |
'state' => '', |
| 73 |
'zip' => '', |
| 74 |
'country' => '', |
| 75 |
); |
| 76 |
|
| 77 |
/** |
| 78 |
* @since 6.34 |
| 79 |
* |
| 80 |
* @param array $empty_value_array array of empty address data. |
| 81 |
*/ |
| 82 |
/** @var array */ |
| 83 |
$result = apply_filters( 'frm_address_empty_value_array', $default ); |
| 84 |
|
| 85 |
return is_array( $result ) ? $result : $default; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get sub-fields for address field. |
| 90 |
* |
| 91 |
* @since 6.34 |
| 92 |
* |
| 93 |
* @param array $field |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
protected static function get_sub_fields( $field ) { |
| 98 |
$fields = array( |
| 99 |
'line1' => array( |
| 100 |
'type' => 'text', |
| 101 |
'classes' => '', |
| 102 |
'label' => __( 'Line 1', 'formidable' ), |
| 103 |
'atts' => array( |
| 104 |
'autocomplete' => 'address-line1', |
| 105 |
), |
| 106 |
), |
| 107 |
'line2' => array( |
| 108 |
'type' => 'text', |
| 109 |
'classes' => '', |
| 110 |
'optional' => true, |
| 111 |
'label' => __( 'Line 2', 'formidable' ), |
| 112 |
'atts' => array( |
| 113 |
'autocomplete' => 'address-line2', |
| 114 |
), |
| 115 |
), |
| 116 |
'city' => array( |
| 117 |
'type' => 'text', |
| 118 |
'classes' => 'frm_third frm_first', |
| 119 |
'label' => __( 'City', 'formidable' ), |
| 120 |
'atts' => array( |
| 121 |
'autocomplete' => 'address-level2', |
| 122 |
), |
| 123 |
), |
| 124 |
'state' => array( |
| 125 |
'type' => 'text', |
| 126 |
'classes' => 'frm_third', |
| 127 |
'label' => __( 'State/Province', 'formidable' ), |
| 128 |
'atts' => array( |
| 129 |
'autocomplete' => 'address-level1', |
| 130 |
), |
| 131 |
), |
| 132 |
'zip' => array( |
| 133 |
'type' => 'text', |
| 134 |
'classes' => 'frm_third', |
| 135 |
'label' => __( 'Zip/Postal', 'formidable' ), |
| 136 |
'atts' => array( |
| 137 |
'autocomplete' => 'postal-code', |
| 138 |
), |
| 139 |
), |
| 140 |
); |
| 141 |
|
| 142 |
$address_type = $field['address_type'] ?? 'international'; |
| 143 |
|
| 144 |
if ( 'europe' === $address_type ) { |
| 145 |
$city_field = $fields['city']; |
| 146 |
unset( $fields['state'], $fields['city'] ); |
| 147 |
$fields['city'] = $city_field; |
| 148 |
$fields['city']['classes'] = 'frm_third'; |
| 149 |
$fields['zip']['classes'] .= ' frm_first'; |
| 150 |
} |
| 151 |
|
| 152 |
if ( $address_type === 'us' ) { |
| 153 |
$fields['state']['type'] = 'select'; |
| 154 |
$fields['state']['options'] = FrmFieldsHelper::get_us_states(); |
| 155 |
} elseif ( $address_type !== 'generic' ) { |
| 156 |
$fields['country'] = array( |
| 157 |
'type' => 'select', |
| 158 |
'classes' => '', |
| 159 |
'label' => __( 'Country', 'formidable' ), |
| 160 |
'options' => FrmFieldsHelper::get_countries(), |
| 161 |
'atts' => array( |
| 162 |
'autocomplete' => 'country-name', |
| 163 |
), |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
// Include the placeholder with the sub field. |
| 168 |
foreach ( $fields as $name => $f ) { |
| 169 |
if ( isset( $field['placeholder'] ) && isset( $field['placeholder'][ $name ] ) ) { |
| 170 |
$fields[ $name ]['placeholder'] = $field['placeholder'][ $name ]; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Filter sub fields so an Address field can be customized. |
| 176 |
* |
| 177 |
* @since 6.34 |
| 178 |
* |
| 179 |
* @param array $fields |
| 180 |
* @param array $field |
| 181 |
*/ |
| 182 |
/** @var array */ |
| 183 |
$result = apply_filters( 'frm_address_sub_fields', $fields, $field ); |
| 184 |
|
| 185 |
return is_array( $result ) ? $result : $fields; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Maps Country name to Country code. |
| 190 |
* |
| 191 |
* @since 6.34 |
| 192 |
* |
| 193 |
* @param string $country Country name. |
| 194 |
* |
| 195 |
* @return string Country code or empty string if not found. |
| 196 |
*/ |
| 197 |
public static function get_country_code( $country ) { |
| 198 |
self::maybe_define_country_codes(); |
| 199 |
$code = self::$country_codes[ $country ] ?? ''; |
| 200 |
return is_string( $code ) ? $code : ''; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Define country codes mapping. |
| 205 |
* |
| 206 |
* @since 6.34 |
| 207 |
* |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
private static function maybe_define_country_codes() { // phpcs:ignore SlevomatCodingStandard.Functions.FunctionLength.FunctionLength |
| 211 |
if ( isset( self::$country_codes ) ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
self::$country_codes = array( |
| 216 |
'Afghanistan' => 'AF', |
| 217 |
'Aland Islands' => 'AX', |
| 218 |
'Albania' => 'AL', |
| 219 |
'Algeria' => 'DZ', |
| 220 |
'American Samoa' => 'AS', |
| 221 |
'Andorra' => 'AD', |
| 222 |
'Angola' => 'AO', |
| 223 |
'Anguilla' => 'AI', |
| 224 |
'Antarctica' => 'AQ', |
| 225 |
'Antigua and Barbuda' => 'AG', |
| 226 |
'Argentina' => 'AR', |
| 227 |
'Armenia' => 'AM', |
| 228 |
'Aruba' => 'AW', |
| 229 |
'Australia' => 'AU', |
| 230 |
'Austria' => 'AT', |
| 231 |
'Azerbaijan' => 'AZ', |
| 232 |
'Bahamas' => 'BS', |
| 233 |
'Bahrain' => 'BH', |
| 234 |
'Bangladesh' => 'BD', |
| 235 |
'Barbados' => 'BB', |
| 236 |
'Belarus' => 'BY', |
| 237 |
'Belgium' => 'BE', |
| 238 |
'Belize' => 'BZ', |
| 239 |
'Benin' => 'BJ', |
| 240 |
'Bermuda' => 'BM', |
| 241 |
'Bhutan' => 'BT', |
| 242 |
'Bolivia' => 'BO', |
| 243 |
'Bonaire, Saint Eustatius and Saba' => 'BQ', |
| 244 |
'Bosnia and Herzegovina' => 'BA', |
| 245 |
'Botswana' => 'BW', |
| 246 |
'Bouvet Island' => 'BV', |
| 247 |
'Brazil' => 'BR', |
| 248 |
'British Indian Ocean Territory' => 'IO', |
| 249 |
'British Virgin Islands' => 'VG', |
| 250 |
'Brunei' => 'BN', |
| 251 |
'Bulgaria' => 'BG', |
| 252 |
'Burkina Faso' => 'BF', |
| 253 |
'Burundi' => 'BI', |
| 254 |
'Cambodia' => 'KH', |
| 255 |
'Cameroon' => 'CM', |
| 256 |
'Canada' => 'CA', |
| 257 |
'Cape Verde' => 'CV', |
| 258 |
'Cayman Islands' => 'KY', |
| 259 |
'Central African Republic' => 'CF', |
| 260 |
'Chad' => 'TD', |
| 261 |
'Chile' => 'CL', |
| 262 |
'China' => 'CN', |
| 263 |
'Christmas Island' => 'CX', |
| 264 |
'Cocos Islands' => 'CC', |
| 265 |
'Colombia' => 'CO', |
| 266 |
'Comoros' => 'KM', |
| 267 |
'Cook Islands' => 'CK', |
| 268 |
'Costa Rica' => 'CR', |
| 269 |
'Croatia' => 'HR', |
| 270 |
'Cuba' => 'CU', |
| 271 |
'Curacao' => 'CW', |
| 272 |
'Cyprus' => 'CY', |
| 273 |
'Czech Republic' => 'CZ', |
| 274 |
'Democratic Republic of the Congo' => 'CD', |
| 275 |
'Denmark' => 'DK', |
| 276 |
'Djibouti' => 'DJ', |
| 277 |
'Dominica' => 'DM', |
| 278 |
'Dominican Republic' => 'DO', |
| 279 |
'East Timor' => 'TL', |
| 280 |
'Ecuador' => 'EC', |
| 281 |
'Egypt' => 'EG', |
| 282 |
'El Salvador' => 'SV', |
| 283 |
'Equatorial Guinea' => 'GQ', |
| 284 |
'Eritrea' => 'ER', |
| 285 |
'Estonia' => 'EE', |
| 286 |
'Ethiopia' => 'ET', |
| 287 |
'Falkland Islands' => 'FK', |
| 288 |
'Faroe Islands' => 'FO', |
| 289 |
'Fiji' => 'FJ', |
| 290 |
'Finland' => 'FI', |
| 291 |
'France' => 'FR', |
| 292 |
'French Guiana' => 'GF', |
| 293 |
'French Polynesia' => 'PF', |
| 294 |
'French Southern Territories' => 'TF', |
| 295 |
'Gabon' => 'GA', |
| 296 |
'Gambia' => 'GM', |
| 297 |
'Georgia' => 'GE', |
| 298 |
'Germany' => 'DE', |
| 299 |
'Ghana' => 'GH', |
| 300 |
'Gibraltar' => 'GI', |
| 301 |
'Greece' => 'GR', |
| 302 |
'Greenland' => 'GL', |
| 303 |
'Grenada' => 'GD', |
| 304 |
'Guadeloupe' => 'GP', |
| 305 |
'Guam' => 'GU', |
| 306 |
'Guatemala' => 'GT', |
| 307 |
'Guernsey' => 'GG', |
| 308 |
'Guinea' => 'GN', |
| 309 |
'Guinea-Bissau' => 'GW', |
| 310 |
'Guyana' => 'GY', |
| 311 |
'Haiti' => 'HT', |
| 312 |
'Heard Island and McDonald Islands' => 'HM', |
| 313 |
'Honduras' => 'HN', |
| 314 |
'Hong Kong' => 'HK', |
| 315 |
'Hungary' => 'HU', |
| 316 |
'Iceland' => 'IS', |
| 317 |
'India' => 'IN', |
| 318 |
'Indonesia' => 'ID', |
| 319 |
'Iran' => 'IR', |
| 320 |
'Iraq' => 'IQ', |
| 321 |
'Ireland' => 'IE', |
| 322 |
'Isle of Man' => 'IM', |
| 323 |
'Israel' => 'IL', |
| 324 |
'Italy' => 'IT', |
| 325 |
'Ivory Coast' => 'CI', |
| 326 |
'Jamaica' => 'JM', |
| 327 |
'Japan' => 'JP', |
| 328 |
'Jersey' => 'JE', |
| 329 |
'Jordan' => 'JO', |
| 330 |
'Kazakhstan' => 'KZ', |
| 331 |
'Kenya' => 'KE', |
| 332 |
'Kiribati' => 'KI', |
| 333 |
'Kosovo' => 'XK', |
| 334 |
'Kuwait' => 'KW', |
| 335 |
'Kyrgyzstan' => 'KG', |
| 336 |
'Laos' => 'LA', |
| 337 |
'Latvia' => 'LV', |
| 338 |
'Lebanon' => 'LB', |
| 339 |
'Lesotho' => 'LS', |
| 340 |
'Liberia' => 'LR', |
| 341 |
'Libya' => 'LY', |
| 342 |
'Liechtenstein' => 'LI', |
| 343 |
'Lithuania' => 'LT', |
| 344 |
'Luxembourg' => 'LU', |
| 345 |
'Macao' => 'MO', |
| 346 |
'Macedonia' => 'MK', |
| 347 |
'Madagascar' => 'MG', |
| 348 |
'Malawi' => 'MW', |
| 349 |
'Malaysia' => 'MY', |
| 350 |
'Maldives' => 'MV', |
| 351 |
'Mali' => 'ML', |
| 352 |
'Malta' => 'MT', |
| 353 |
'Marshall Islands' => 'MH', |
| 354 |
'Martinique' => 'MQ', |
| 355 |
'Mauritania' => 'MR', |
| 356 |
'Mauritius' => 'MU', |
| 357 |
'Mayotte' => 'YT', |
| 358 |
'Mexico' => 'MX', |
| 359 |
'Micronesia' => 'FM', |
| 360 |
'Moldova' => 'MD', |
| 361 |
'Monaco' => 'MC', |
| 362 |
'Mongolia' => 'MN', |
| 363 |
'Montenegro' => 'ME', |
| 364 |
'Montserrat' => 'MS', |
| 365 |
'Morocco' => 'MA', |
| 366 |
'Mozambique' => 'MZ', |
| 367 |
'Myanmar' => 'MM', |
| 368 |
'Namibia' => 'NA', |
| 369 |
'Nauru' => 'NR', |
| 370 |
'Nepal' => 'NP', |
| 371 |
'Netherlands' => 'NL', |
| 372 |
'New Caledonia' => 'NC', |
| 373 |
'New Zealand' => 'NZ', |
| 374 |
'Nicaragua' => 'NI', |
| 375 |
'Niger' => 'NE', |
| 376 |
'Nigeria' => 'NG', |
| 377 |
'Niue' => 'NU', |
| 378 |
'Norfolk Island' => 'NF', |
| 379 |
'North Korea' => 'KP', |
| 380 |
'Northern Mariana Islands' => 'MP', |
| 381 |
'Norway' => 'NO', |
| 382 |
'Oman' => 'OM', |
| 383 |
'Pakistan' => 'PK', |
| 384 |
'Palau' => 'PW', |
| 385 |
'Palestinian Territory' => 'PS', |
| 386 |
'Panama' => 'PA', |
| 387 |
'Papua New Guinea' => 'PG', |
| 388 |
'Paraguay' => 'PY', |
| 389 |
'Peru' => 'PE', |
| 390 |
'Philippines' => 'PH', |
| 391 |
'Pitcairn' => 'PN', |
| 392 |
'Poland' => 'PL', |
| 393 |
'Portugal' => 'PT', |
| 394 |
'Puerto Rico' => 'PR', |
| 395 |
'Qatar' => 'QA', |
| 396 |
'Republic of the Congo' => 'CG', |
| 397 |
'Reunion' => 'RE', |
| 398 |
'Romania' => 'RO', |
| 399 |
'Russia' => 'RU', |
| 400 |
'Rwanda' => 'RW', |
| 401 |
'Saint Barthelemy' => 'BL', |
| 402 |
'Saint Helena' => 'SH', |
| 403 |
'Saint Kitts and Nevis' => 'KN', |
| 404 |
'Saint Lucia' => 'LC', |
| 405 |
'Saint Martin' => 'MF', |
| 406 |
'Saint Pierre and Miquelon' => 'PM', |
| 407 |
'Saint Vincent and the Grenadines' => 'VC', |
| 408 |
'Samoa' => 'WS', |
| 409 |
'San Marino' => 'SM', |
| 410 |
'Sao Tome and Principe' => 'ST', |
| 411 |
'Saudi Arabia' => 'SA', |
| 412 |
'Senegal' => 'SN', |
| 413 |
'Serbia' => 'RS', |
| 414 |
'Seychelles' => 'SC', |
| 415 |
'Sierra Leone' => 'SL', |
| 416 |
'Singapore' => 'SG', |
| 417 |
'Sint Maarten' => 'SX', |
| 418 |
'Slovakia' => 'SK', |
| 419 |
'Slovenia' => 'SI', |
| 420 |
'Solomon Islands' => 'SB', |
| 421 |
'Somalia' => 'SO', |
| 422 |
'South Africa' => 'ZA', |
| 423 |
'South Georgia and the South Sandwich Islands' => 'GS', |
| 424 |
'South Korea' => 'KR', |
| 425 |
'South Sudan' => 'SS', |
| 426 |
'Spain' => 'ES', |
| 427 |
'Sri Lanka' => 'LK', |
| 428 |
'Sudan' => 'SD', |
| 429 |
'Suriname' => 'SR', |
| 430 |
'Svalbard and Jan Mayen' => 'SJ', |
| 431 |
'Swaziland' => 'SZ', |
| 432 |
'Sweden' => 'SE', |
| 433 |
'Switzerland' => 'CH', |
| 434 |
'Syria' => 'SY', |
| 435 |
'Taiwan' => 'TW', |
| 436 |
'Tajikistan' => 'TJ', |
| 437 |
'Tanzania' => 'TZ', |
| 438 |
'Thailand' => 'TH', |
| 439 |
'Togo' => 'TG', |
| 440 |
'Tokelau' => 'TK', |
| 441 |
'Tonga' => 'TO', |
| 442 |
'Trinidad and Tobago' => 'TT', |
| 443 |
'Tunisia' => 'TN', |
| 444 |
'Turkey' => 'TR', |
| 445 |
'Turkmenistan' => 'TM', |
| 446 |
'Turks and Caicos Islands' => 'TC', |
| 447 |
'Tuvalu' => 'TV', |
| 448 |
'U.S. Virgin Islands' => 'VI', |
| 449 |
'Uganda' => 'UG', |
| 450 |
'Ukraine' => 'UA', |
| 451 |
'United Arab Emirates' => 'AE', |
| 452 |
'United Kingdom' => 'GB', |
| 453 |
'United States' => 'US', |
| 454 |
'United States Minor Outlying Islands' => 'UM', |
| 455 |
'Uruguay' => 'UY', |
| 456 |
'Uzbekistan' => 'UZ', |
| 457 |
'Vanuatu' => 'VU', |
| 458 |
'Vatican' => 'VA', |
| 459 |
'Venezuela' => 'VE', |
| 460 |
'Vietnam' => 'VN', |
| 461 |
'Wallis and Futuna' => 'WF', |
| 462 |
'Western Sahara' => 'EH', |
| 463 |
'Yemen' => 'YE', |
| 464 |
'Zambia' => 'ZM', |
| 465 |
'Zimbabwe' => 'ZW', |
| 466 |
); |
| 467 |
|
| 468 |
/** |
| 469 |
* Allows modifying the list of country name to code mapping. |
| 470 |
* |
| 471 |
* @since 6.34 |
| 472 |
* |
| 473 |
* @param array $country_codes Array of country name to code mapping. |
| 474 |
*/ |
| 475 |
self::$country_codes = apply_filters( 'frm_country_codes', self::$country_codes ); |
| 476 |
} |
| 477 |
} |
| 478 |
|