class-evf-field-address.php
1 week ago
class-evf-field-ai.php
1 year ago
class-evf-field-captcha.php
1 month ago
class-evf-field-checkbox.php
3 months ago
class-evf-field-color.php
1 month ago
class-evf-field-country.php
3 months ago
class-evf-field-credit-card.php
1 month ago
class-evf-field-date-time.php
1 month ago
class-evf-field-divider.php
2 years ago
class-evf-field-email.php
1 year ago
class-evf-field-file-upload.php
1 year ago
class-evf-field-first-name.php
1 year ago
class-evf-field-hcaptcha.php
5 months ago
class-evf-field-hidden.php
1 year ago
class-evf-field-html.php
10 months ago
class-evf-field-image-upload.php
1 year ago
class-evf-field-last-name.php
1 year ago
class-evf-field-likert.php
1 year ago
class-evf-field-lookup.php
1 month ago
class-evf-field-number.php
1 year ago
class-evf-field-password.php
1 month ago
class-evf-field-payment-authorize-net.php
1 month ago
class-evf-field-payment-checkbox.php
1 month ago
class-evf-field-payment-coupon.php
1 month ago
class-evf-field-payment-gateway-selector.php
1 month ago
class-evf-field-payment-quantity.php
1 month ago
class-evf-field-payment-radio.php
1 month ago
class-evf-field-payment-single.php
1 month ago
class-evf-field-payment-square.php
2 years ago
class-evf-field-payment-subscription-plan.php
1 month ago
class-evf-field-payment-subtotal.php
1 month ago
class-evf-field-payment-total.php
1 month ago
class-evf-field-phone.php
1 year ago
class-evf-field-privacy-policy.php
3 months ago
class-evf-field-private-note.php
1 year ago
class-evf-field-progress.php
1 month ago
class-evf-field-radio.php
3 months ago
class-evf-field-range-slider.php
1 month ago
class-evf-field-rating.php
3 months ago
class-evf-field-recaptcha.php
5 months ago
class-evf-field-repeater.php
1 month ago
class-evf-field-reset.php
1 month ago
class-evf-field-scale-rating.php
1 year ago
class-evf-field-select.php
1 year ago
class-evf-field-signature.php
1 month ago
class-evf-field-text.php
1 year ago
class-evf-field-textarea.php
1 year ago
class-evf-field-title.php
2 years ago
class-evf-field-turnstile.php
5 months ago
class-evf-field-url.php
1 year ago
class-evf-field-wysiwyg.php
3 months ago
class-evf-field-yes-no.php
1 year ago
payment-amount-helpers.php
1 month ago
class-evf-field-country.php
316 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Country field |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_Country Class. |
| 13 | */ |
| 14 | class EVF_Field_Country extends EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->name = esc_html__( 'Country', 'everest-forms' ); |
| 21 | $this->type = 'country'; |
| 22 | $this->icon = 'evf-icon evf-icon-flag'; |
| 23 | $this->order = 120; |
| 24 | $this->group = 'advanced'; |
| 25 | $this->settings = array( |
| 26 | 'basic-options' => array( |
| 27 | 'field_options' => array( |
| 28 | 'label', |
| 29 | 'description', |
| 30 | 'required', |
| 31 | 'required_field_message_setting', |
| 32 | 'required_field_message', |
| 33 | 'enable_country_flag', |
| 34 | ), |
| 35 | ), |
| 36 | 'advanced-options' => array( |
| 37 | 'field_options' => array( |
| 38 | 'placeholder', |
| 39 | 'meta', |
| 40 | 'default_country', |
| 41 | 'label_hide', |
| 42 | 'css', |
| 43 | ), |
| 44 | ), |
| 45 | ); |
| 46 | |
| 47 | parent::__construct(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Hook in tabs. |
| 52 | */ |
| 53 | public function init_hooks() { |
| 54 | add_filter( 'everest_forms_html_field_value', array( $this, 'field_value' ), 10, 4 ); |
| 55 | add_filter( 'everest_forms_plaintext_field_value', array( $this, 'field_value' ), 10, 4 ); |
| 56 | add_filter( 'everest_forms_field_exporter_' . $this->type, array( $this, 'field_exporter' ) ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Enable country flag field option. |
| 61 | * |
| 62 | * @param array $field Field Data. |
| 63 | */ |
| 64 | public function enable_country_flag( $field ) { |
| 65 | $value = isset( $field['enable_country_flag'] ) ? $field['enable_country_flag'] : '0'; |
| 66 | $tooltip = esc_html__( 'Check this option to show country flag.', 'everest-forms' ); |
| 67 | |
| 68 | // Enabled country flag. |
| 69 | $enable_country_flag = $this->field_element( |
| 70 | 'toggle', |
| 71 | $field, |
| 72 | array( |
| 73 | 'slug' => 'enable_country_flag', |
| 74 | 'value' => $value, |
| 75 | 'desc' => esc_html__( 'Enable Country Flag', 'everest-forms' ), |
| 76 | 'tooltip' => $tooltip, |
| 77 | ), |
| 78 | false |
| 79 | ); |
| 80 | $this->field_element( |
| 81 | 'row', |
| 82 | $field, |
| 83 | array( |
| 84 | 'slug' => 'enable_country_flag', |
| 85 | 'content' => $enable_country_flag, |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Default country. |
| 92 | * |
| 93 | * @param array $field Field Data. |
| 94 | */ |
| 95 | public function default_country( $field ) { |
| 96 | $default = isset( $field['default'] ) ? $field['default'] : array(); |
| 97 | |
| 98 | $lbl = $this->field_element( |
| 99 | 'label', |
| 100 | $field, |
| 101 | array( |
| 102 | 'slug' => 'default', |
| 103 | 'value' => esc_html__( 'Default Country', 'everest-forms' ), |
| 104 | 'tooltip' => sprintf( esc_html__( 'Enter the country to be displayed on frontend.', 'everest-forms' ) ), |
| 105 | ), |
| 106 | false |
| 107 | ); |
| 108 | $fld = $this->field_element( |
| 109 | 'select', |
| 110 | $field, |
| 111 | array( |
| 112 | 'slug' => 'default', |
| 113 | 'value' => $default, |
| 114 | 'multiple' => true, |
| 115 | 'class' => 'evf-select2-multiple evf-select2-country', |
| 116 | 'options' => array_merge( |
| 117 | array( |
| 118 | '' => esc_html__( '--- Select a country ---', 'everest-forms' ), |
| 119 | ), |
| 120 | evf_get_countries() |
| 121 | ), |
| 122 | 'data' => array( |
| 123 | 'select_all_unselect_all' => true, |
| 124 | 'placeholder' => __( 'Select Country(s)', 'everest-forms' ), |
| 125 | 'selected_msg' => __( 'Selected %qty% Country(s)', 'everest-forms' ), |
| 126 | ), |
| 127 | ), |
| 128 | false |
| 129 | ); |
| 130 | |
| 131 | $args = array( |
| 132 | 'slug' => 'default', |
| 133 | 'content' => $lbl . $fld, |
| 134 | ); |
| 135 | $this->field_element( 'row', $field, $args ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Customize format for field value. |
| 140 | * |
| 141 | * @param string $val Field value. |
| 142 | * @param array $field_val Field settings. |
| 143 | * @param array $form_data Form data. |
| 144 | * @param string $context Value display context. |
| 145 | * @return string $val Formatted country name. |
| 146 | */ |
| 147 | public function field_value( $val, $field_val, $form_data = array(), $context = '' ) { |
| 148 | $countries = evf_get_countries(); |
| 149 | |
| 150 | if ( is_serialized( $field_val ) || in_array( $context, array( 'email-plain', 'email-html', 'export-csv', 'export-pdf' ), true ) ) { |
| 151 | $value = evf_maybe_unserialize( $field_val ); |
| 152 | |
| 153 | if ( isset( $value['type'], $value['country_code'] ) && $value['type'] === $this->type ) { |
| 154 | $country_code = sanitize_text_field( wp_unslash( (string) $value['country_code'] ) ); |
| 155 | |
| 156 | if ( isset( $countries[ $country_code ] ) ) { |
| 157 | return sanitize_text_field( wp_unslash( (string) $countries[ $country_code ] ) ); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return $val; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Filter callback for outputting formatted data. |
| 167 | * |
| 168 | * @param array $field Field Data. |
| 169 | */ |
| 170 | public function field_exporter( $field ) { |
| 171 | $countries = evf_get_countries(); |
| 172 | $country_code = false; |
| 173 | |
| 174 | if ( ! empty( $field['value']['country_code'] ) ) { |
| 175 | $country_code = isset( $countries[ $field['value']['country_code'] ] ) ? $countries[ $field['value']['country_code'] ] : $field['value']['country_code'] . '.'; |
| 176 | } |
| 177 | |
| 178 | return array( |
| 179 | 'label' => ! empty( $field['name'] ) ? $field['name'] : ucfirst( str_replace( '_', ' ', $field['type'] ) ) . " - {$field['id']}", |
| 180 | 'value' => ! empty( $field['value']['country'] ) ? $field['value']['country'] : $country_code, |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Field preview inside the builder. |
| 186 | * |
| 187 | * @param array $field Field settings. |
| 188 | */ |
| 189 | public function field_preview( $field ) { |
| 190 | $countries = evf_get_countries(); |
| 191 | |
| 192 | $default = isset( $field['default'] ) && ! empty( $field['default'] ) ? ( is_array( $field['default'] ) ? reset( $field['default'] ) : $field['default'] ) : ''; |
| 193 | |
| 194 | // Label. |
| 195 | $this->field_preview_option( 'label', $field ); |
| 196 | |
| 197 | // Field select element. |
| 198 | echo '<select class="widefat" disabled>'; |
| 199 | |
| 200 | // Optional placeholder. |
| 201 | if ( empty( $default ) ) { |
| 202 | printf( '<option value="" class="placeholder" selected>%s</option>', ! empty( $field['placeholder'] ) ? esc_html( $field['placeholder'] ) : esc_html__( '--- Select a country ---', 'everest-forms' ) ); |
| 203 | } |
| 204 | |
| 205 | foreach ( $countries as $country_code => $country_name ) { |
| 206 | printf( '<option value="%s" %s>%s</option>', esc_attr( $country_code ), selected( $country_code, $default, false ), esc_html( $country_name ) ); |
| 207 | } |
| 208 | |
| 209 | echo '</select>'; |
| 210 | |
| 211 | // Description. |
| 212 | $this->field_preview_option( 'description', $field ); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Field display on the form front-end. |
| 217 | * |
| 218 | * @since 1.0.0 |
| 219 | * |
| 220 | * @param array $field Field Data. |
| 221 | * @param array $field_atts Field attributes. |
| 222 | * @param array $form_data All Form Data. |
| 223 | */ |
| 224 | public function field_display( $field, $field_atts, $form_data ) { |
| 225 | // Setup and sanitize the necessary data. |
| 226 | $primary = $field['properties']['inputs']['primary']; |
| 227 | $field = apply_filters( 'everest_forms_select_field_display', $field, $field_atts, $form_data ); |
| 228 | $field_default = ! empty( $field['default'] ) ? $field['default'] : array(); |
| 229 | $selected = ! empty( $primary['attr']['value'] ) ? evf_clean( $primary['attr']['value'] ) : ''; |
| 230 | $all_countries = evf_get_countries(); |
| 231 | $countries = array(); |
| 232 | |
| 233 | if ( ! empty( $field_default ) ) { |
| 234 | foreach ( $field_default as $key => $value ) { |
| 235 | if ( array_key_exists( $value, $all_countries ) ) { |
| 236 | $countries[ $value ] = $all_countries[ $value ]; |
| 237 | } |
| 238 | } |
| 239 | } else { |
| 240 | $countries = $all_countries; |
| 241 | } |
| 242 | |
| 243 | // Enable country flag. |
| 244 | if ( isset( $field['enable_country_flag'] ) ) { |
| 245 | $primary['class'][] = esc_attr( 'evf-country-flag-selector' ); |
| 246 | |
| 247 | if ( empty( $field['placeholder'] ) ) { |
| 248 | $primary['data']['placeholder'] = esc_html__( '--- Select a country ---', 'everest-forms' ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Primary select field. |
| 253 | printf( |
| 254 | '<select %s %s >', |
| 255 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 256 | esc_attr( $primary['required'] ) |
| 257 | ); |
| 258 | |
| 259 | // Optional placeholder. |
| 260 | if ( count( $countries ) ) { |
| 261 | printf( '<option value="" class="placeholder" disabled selected>%s</option>', ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : esc_html__( '--- Select a country ---', 'everest-forms' ) ); |
| 262 | } else { |
| 263 | printf( '<option value="" class="placeholder" disabled selected>%s</option>', ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : esc_html__( '--- No countries to select ---', 'everest-forms' ) ); |
| 264 | } |
| 265 | |
| 266 | foreach ( $countries as $country_code => $country_name ) { |
| 267 | printf( '<option value="%s" %s>%s</option>', esc_attr( $country_code ), selected( $country_code, $selected, false ), esc_html( $country_name ) ); |
| 268 | } |
| 269 | |
| 270 | echo '</select>'; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Edit form field display on the entry back-end. |
| 275 | * |
| 276 | * @since 1.7.0 |
| 277 | * |
| 278 | * @param array $entry_field Entry field data. |
| 279 | * @param array $field Field data. |
| 280 | * @param array $form_data Form data and settings. |
| 281 | */ |
| 282 | public function edit_form_field_display( $entry_field, $field, $form_data ) { |
| 283 | $country_code = isset( $entry_field['value']['country_code'] ) ? $entry_field['value']['country_code'] : ''; |
| 284 | |
| 285 | if ( '' !== $country_code ) { |
| 286 | $field['properties'] = $this->get_single_field_property_value( $country_code, 'primary', $field['properties'], $field ); |
| 287 | } |
| 288 | |
| 289 | $this->field_display( $field, null, $form_data ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Formats and sanitizes field. |
| 294 | * |
| 295 | * @param string $field_id Field Id. |
| 296 | * @param array $field_submit Submitted Field. |
| 297 | * @param array $form_data All Form Data. |
| 298 | * @param string $meta_key Field Meta Key. |
| 299 | */ |
| 300 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 301 | $name = ! empty( $form_data['form_fields'][ $field_id ]['label'] ) ? make_clickable( $form_data['form_fields'][ $field_id ]['label'] ) : ''; |
| 302 | |
| 303 | // Set final field details. |
| 304 | EVF()->task->form_fields[ $field_id ] = array( |
| 305 | 'name' => $name, |
| 306 | 'value' => array( |
| 307 | 'type' => $this->type, |
| 308 | 'country_code' => sanitize_text_field( $field_submit ), |
| 309 | ), |
| 310 | 'id' => $field_id, |
| 311 | 'type' => $this->type, |
| 312 | 'meta_key' => $meta_key, |
| 313 | ); |
| 314 | } |
| 315 | } |
| 316 |