class-evf-field-address.php
7 years ago
class-evf-field-ai.php
2 years ago
class-evf-field-captcha.php
2 years ago
class-evf-field-checkbox.php
3 years ago
class-evf-field-color.php
3 years ago
class-evf-field-country.php
7 years ago
class-evf-field-credit-card.php
2 years ago
class-evf-field-date-time.php
2 years ago
class-evf-field-divider.php
4 years ago
class-evf-field-email.php
2 years ago
class-evf-field-file-upload.php
6 years ago
class-evf-field-first-name.php
2 years ago
class-evf-field-hidden.php
7 years ago
class-evf-field-html.php
7 years ago
class-evf-field-image-upload.php
7 years ago
class-evf-field-last-name.php
2 years ago
class-evf-field-likert.php
2 years ago
class-evf-field-lookup.php
2 years ago
class-evf-field-number.php
2 years ago
class-evf-field-password.php
7 years ago
class-evf-field-payment-authorize-net.php
2 years ago
class-evf-field-payment-checkbox.php
7 years ago
class-evf-field-payment-coupon.php
2 years ago
class-evf-field-payment-quantity.php
5 years ago
class-evf-field-payment-radio.php
7 years ago
class-evf-field-payment-single.php
7 years ago
class-evf-field-payment-subtotal.php
3 years ago
class-evf-field-payment-total.php
6 years ago
class-evf-field-phone.php
7 years ago
class-evf-field-privacy-policy.php
4 years ago
class-evf-field-progress.php
3 years ago
class-evf-field-radio.php
3 years ago
class-evf-field-range-slider.php
6 years ago
class-evf-field-rating.php
7 years ago
class-evf-field-repeater.php
2 years ago
class-evf-field-reset.php
3 years ago
class-evf-field-scale-rating.php
2 years ago
class-evf-field-select.php
3 years ago
class-evf-field-signature.php
7 years ago
class-evf-field-text.php
2 years ago
class-evf-field-textarea.php
3 years ago
class-evf-field-title.php
6 years ago
class-evf-field-url.php
2 years ago
class-evf-field-wysiwyg.php
4 years ago
class-evf-field-yes-no.php
2 years ago
class-evf-field-select.php
411 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Select Dropdown field. |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_Select class. |
| 13 | */ |
| 14 | class EVF_Field_Select extends EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->name = esc_html__( 'Dropdown', 'everest-forms' ); |
| 21 | $this->type = 'select'; |
| 22 | $this->icon = 'evf-icon evf-icon-dropdown'; |
| 23 | $this->order = 50; |
| 24 | $this->group = 'general'; |
| 25 | $this->defaults = array( |
| 26 | 1 => array( |
| 27 | 'label' => esc_html__( 'Option 1', 'everest-forms' ), |
| 28 | 'value' => '', |
| 29 | 'default' => '', |
| 30 | ), |
| 31 | 2 => array( |
| 32 | 'label' => esc_html__( 'Option 2', 'everest-forms' ), |
| 33 | 'value' => '', |
| 34 | 'default' => '', |
| 35 | ), |
| 36 | 3 => array( |
| 37 | 'label' => esc_html__( 'Option 3', 'everest-forms' ), |
| 38 | 'value' => '', |
| 39 | 'default' => '', |
| 40 | ), |
| 41 | ); |
| 42 | $this->settings = array( |
| 43 | 'basic-options' => array( |
| 44 | 'field_options' => array( |
| 45 | 'label', |
| 46 | 'meta', |
| 47 | 'choices', |
| 48 | 'enhanced_select', |
| 49 | 'description', |
| 50 | 'required', |
| 51 | 'required_field_message_setting', |
| 52 | 'required_field_message', |
| 53 | ), |
| 54 | ), |
| 55 | 'advanced-options' => array( |
| 56 | 'field_options' => array( |
| 57 | 'size', |
| 58 | 'placeholder', |
| 59 | 'label_hide', |
| 60 | 'css', |
| 61 | ), |
| 62 | ), |
| 63 | ); |
| 64 | |
| 65 | parent::__construct(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Hook in tabs. |
| 70 | */ |
| 71 | public function init_hooks() { |
| 72 | add_action( 'everest_forms_shortcode_scripts', array( $this, 'load_assets' ) ); |
| 73 | add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Define additional field properties. |
| 78 | * |
| 79 | * @since 1.7.0 |
| 80 | * |
| 81 | * @param array $properties Field properties. |
| 82 | * @param array $field Field settings. |
| 83 | * @param array $form_data Form data and settings. |
| 84 | * |
| 85 | * @return array of additional field properties. |
| 86 | */ |
| 87 | public function field_properties( $properties, $field, $form_data ) { |
| 88 | // Define data. |
| 89 | $form_id = absint( $form_data['id'] ); |
| 90 | $field_id = $field['id']; |
| 91 | $field = apply_filters( 'everest_forms_dropdown_options', $field ); |
| 92 | $choices = $field['choices']; |
| 93 | |
| 94 | // Remove primary input. |
| 95 | unset( $properties['inputs']['primary'] ); |
| 96 | |
| 97 | // Set input container (<select>) properties. |
| 98 | $properties['input_container'] = array( |
| 99 | 'class' => array( 'input-text' ), |
| 100 | 'data' => array(), |
| 101 | 'id' => "evf-{$form_id}-field_{$field_id}", |
| 102 | 'attr' => array( |
| 103 | 'name' => "everest_forms[form_fields][{$field_id}]", |
| 104 | ), |
| 105 | ); |
| 106 | |
| 107 | // Set input properties. |
| 108 | foreach ( $choices as $key => $choice ) { |
| 109 | $depth = isset( $choice['depth'] ) ? absint( $choice['depth'] ) : 1; |
| 110 | |
| 111 | $properties['inputs'][ $key ] = array( |
| 112 | 'container' => array( |
| 113 | 'attr' => array(), |
| 114 | 'class' => array( "choice-{$key}", "depth-{$depth}" ), |
| 115 | 'data' => array(), |
| 116 | 'id' => '', |
| 117 | ), |
| 118 | 'label' => array( |
| 119 | 'attr' => array( |
| 120 | 'for' => "evf-{$form_id}-field_{$field_id}_{$key}", |
| 121 | ), |
| 122 | 'class' => array( 'everest-forms-field-label-inline' ), |
| 123 | 'data' => array(), |
| 124 | 'id' => '', |
| 125 | 'text' => evf_string_translation( $form_id, $field_id, $choice['label'], '-choice-' . $key ), |
| 126 | ), |
| 127 | 'attr' => array( |
| 128 | 'name' => "everest_forms[form_fields][{$field_id}][]", |
| 129 | 'value' => isset( $field['show_values'] ) ? $choice['value'] : $choice['label'], |
| 130 | ), |
| 131 | 'class' => array(), |
| 132 | 'data' => array(), |
| 133 | 'id' => "evf-{$form_id}-field_{$field_id}_{$key}", |
| 134 | 'required' => ! empty( $field['required'] ) ? 'required' : '', |
| 135 | 'default' => isset( $choice['default'] ), |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | // Required class for validation. |
| 140 | if ( ! empty( $field['required'] ) ) { |
| 141 | $properties['input_container']['class'][] = 'evf-field-required'; |
| 142 | } |
| 143 | |
| 144 | return $properties; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Register/queue frontend scripts. |
| 149 | * |
| 150 | * @param array $atts Shortcode attributes. |
| 151 | */ |
| 152 | public static function load_assets( $atts ) { |
| 153 | $form_data = evf()->form->get( $atts['id'], array( 'content_only' => true ) ); |
| 154 | |
| 155 | if ( ! empty( $form_data['form_fields'] ) ) { |
| 156 | $is_enhanced_select = wp_list_filter( |
| 157 | $form_data['form_fields'], |
| 158 | array( |
| 159 | 'type' => 'select', |
| 160 | 'enhanced_select' => 1, |
| 161 | ) |
| 162 | ); |
| 163 | |
| 164 | if ( ! empty( $is_enhanced_select ) ) { |
| 165 | wp_enqueue_style( 'evf_select2' ); |
| 166 | wp_enqueue_script( 'selectWoo' ); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Enable enhanced select field option. |
| 173 | * |
| 174 | * @param array $field Field Data. |
| 175 | */ |
| 176 | public function enhanced_select( $field ) { |
| 177 | $plan = evf_get_license_plan(); |
| 178 | $value = isset( $field['enhanced_select'] ) && false !== $plan ? $field['enhanced_select'] : '0'; |
| 179 | $tooltip = esc_html__( 'Check this option to enable enhanced select. It enables you to search items in the dropdown field.', 'everest-forms' ); |
| 180 | |
| 181 | // Enable enhanced select toggle field. |
| 182 | $enhanced_select = $this->field_element( |
| 183 | 'checkbox', |
| 184 | $field, |
| 185 | array( |
| 186 | 'slug' => 'enhanced_select', |
| 187 | 'value' => $value, |
| 188 | 'class' => ( false === $plan ) ? 'disabled' : '', |
| 189 | 'desc' => esc_html__( 'Enable Enhanced Select', 'everest-forms' ), |
| 190 | 'tooltip' => $tooltip, |
| 191 | ), |
| 192 | false |
| 193 | ); |
| 194 | $this->field_element( |
| 195 | 'row', |
| 196 | $field, |
| 197 | array( |
| 198 | 'slug' => 'enhanced_select', |
| 199 | 'content' => $enhanced_select, |
| 200 | 'class' => ( false === $plan ) ? 'upgrade-modal' : '', |
| 201 | 'data' => array( |
| 202 | 'feature' => esc_html__( 'Enhanced select', 'everest-forms' ), |
| 203 | ), |
| 204 | ) |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Field preview inside the builder. |
| 210 | * |
| 211 | * @since 1.0.0 |
| 212 | * |
| 213 | * @param array $field Field data and settings. |
| 214 | */ |
| 215 | public function field_preview( $field ) { |
| 216 | $args = array(); |
| 217 | |
| 218 | if ( |
| 219 | ! empty( $field['enhanced_select'] ) |
| 220 | && ! empty( $field['multiple_choices'] ) && '1' === $field['multiple_choices'] |
| 221 | ) { |
| 222 | $args['class'] = 'evf-enhanced-select'; |
| 223 | } |
| 224 | |
| 225 | // Label. |
| 226 | $this->field_preview_option( 'label', $field ); |
| 227 | |
| 228 | // Choices. |
| 229 | $this->field_preview_option( 'choices', $field, $args ); |
| 230 | |
| 231 | // Description. |
| 232 | $this->field_preview_option( 'description', $field ); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Field display on the form front-end. |
| 237 | * |
| 238 | * @since 1.0.0 |
| 239 | * |
| 240 | * @param array $field Field Data. |
| 241 | * @param array $field_atts Field attributes. |
| 242 | * @param array $form_data All Form Data. |
| 243 | */ |
| 244 | public function field_display( $field, $field_atts, $form_data ) { |
| 245 | // Define data. |
| 246 | $container = $field['properties']['input_container']; |
| 247 | $choices = $field['properties']['inputs']; |
| 248 | $field = apply_filters( 'everest_forms_select_field_display', $field, $field_atts, $form_data ); |
| 249 | $field_placeholder = ! empty( $field['placeholder'] ) ? evf_string_translation( $form_data['id'], $field['id'], $field['placeholder'], '-placeholder' ) : ''; |
| 250 | $plan = evf_get_license_plan(); |
| 251 | $has_default = false; |
| 252 | $is_multiple = false; |
| 253 | $select_all = isset( $field['select_all'] ) ? $field['select_all'] : '0'; |
| 254 | |
| 255 | if ( ! empty( $field['required'] ) ) { |
| 256 | $container['attr']['required'] = 'required'; |
| 257 | } |
| 258 | |
| 259 | // Enable enhanced select. |
| 260 | if ( false !== $plan && ! empty( $field['enhanced_select'] ) && '1' === $field['enhanced_select'] ) { |
| 261 | $container['class'][] = 'evf-enhanced-select'; |
| 262 | |
| 263 | if ( empty( $field_placeholder ) ) { |
| 264 | $first_choices = reset( $choices ); |
| 265 | $field_placeholder = $first_choices['label']['text']; |
| 266 | } |
| 267 | |
| 268 | // Set placeholder for select2. |
| 269 | $container['data']['placeholder'] = esc_attr( $field_placeholder ); |
| 270 | } |
| 271 | |
| 272 | // Enable multiple choices selection. |
| 273 | if ( false !== $plan && ! empty( $field['multiple_choices'] ) && '1' === $field['multiple_choices'] ) { |
| 274 | $is_multiple = true; |
| 275 | $container['attr']['multiple'] = 'multiple'; |
| 276 | |
| 277 | // Change a name attribute. |
| 278 | if ( ! empty( $container['attr']['name'] ) ) { |
| 279 | $container['attr']['name'] .= '[]'; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Check to see if any of the options have selected by default. |
| 284 | foreach ( $choices as $choice ) { |
| 285 | if ( ! empty( $choice['default'] ) ) { |
| 286 | $has_default = true; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Conditional logic. |
| 292 | if ( isset( $choices['primary'] ) ) { |
| 293 | $container['attr']['conditional_id'] = $choices['primary']['attr']['conditional_id']; |
| 294 | |
| 295 | if ( isset( $choices['primary']['attr']['conditional_rules'] ) ) { |
| 296 | $container['attr']['conditional_rules'] = $choices['primary']['attr']['conditional_rules']; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // Select All checkbox. |
| 301 | if ( '1' === $select_all ) { |
| 302 | if ( isset( $container['attr']['multiple'] ) && 'multiple' === $container['attr']['multiple'] ) { |
| 303 | $container['attr']['select_all_unselect_all'] = 'true'; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // Primary select field. |
| 308 | printf( |
| 309 | '<select %s >', |
| 310 | evf_html_attributes( $container['id'], $container['class'], $container['data'], $container['attr'] ) |
| 311 | ); |
| 312 | |
| 313 | // Optional placeholder. |
| 314 | if ( ! empty( $field_placeholder ) ) { |
| 315 | printf( '<option value="" class="placeholder" disabled %s>%s</option>', selected( false, $has_default || $is_multiple, false ), esc_html( $field_placeholder ) ); |
| 316 | } |
| 317 | |
| 318 | // Build the select options. |
| 319 | foreach ( $choices as $choice ) { |
| 320 | if ( empty( $choice['container'] ) ) { |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | printf( |
| 325 | '<option value="%s" %s>%s</option>', |
| 326 | esc_attr( $choice['attr']['value'] ), |
| 327 | selected( true, ! empty( $choice['default'] ), false ), |
| 328 | esc_html( $choice['label']['text'] ) |
| 329 | ); |
| 330 | } |
| 331 | |
| 332 | echo '</select>'; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Edit form field display on the entry back-end. |
| 337 | * |
| 338 | * @since 1.7.0 |
| 339 | * |
| 340 | * @param array $entry_field Entry field data. |
| 341 | * @param array $field Field data. |
| 342 | * @param array $form_data Form data and settings. |
| 343 | */ |
| 344 | public function edit_form_field_display( $entry_field, $field, $form_data ) { |
| 345 | $value_choices = ! empty( $entry_field['value_raw'] ) ? $entry_field['value_raw'] : array(); |
| 346 | |
| 347 | $this->remove_field_choices_defaults( $field, $field['properties'] ); |
| 348 | |
| 349 | if ( is_array( $value_choices ) ) { |
| 350 | foreach ( $value_choices as $input => $single_value ) { |
| 351 | $field['properties'] = $this->get_single_field_property_value( $single_value, sanitize_key( $input ), $field['properties'], $field ); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | $this->field_display( $field, null, $form_data ); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Formats and sanitizes field. |
| 360 | * |
| 361 | * @since 1.0.0 |
| 362 | * |
| 363 | * @param int $field_id Field ID. |
| 364 | * @param mixed $field_submit Submitted field value. |
| 365 | * @param array $form_data Form data and settings. |
| 366 | * @param string $meta_key Field meta key. |
| 367 | */ |
| 368 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 369 | $field = $form_data['form_fields'][ $field_id ]; |
| 370 | $name = make_clickable( $field['label'] ); |
| 371 | $value = array(); |
| 372 | |
| 373 | // Convert field value into to array. |
| 374 | if ( ! is_array( $field_submit ) ) { |
| 375 | $field_submit = array( $field_submit ); |
| 376 | } |
| 377 | |
| 378 | $value_raw = evf_sanitize_array_combine( $field_submit ); |
| 379 | |
| 380 | $data = array( |
| 381 | 'name' => $name, |
| 382 | 'value' => '', |
| 383 | 'value_raw' => $value_raw, |
| 384 | 'id' => $field_id, |
| 385 | 'type' => $this->type, |
| 386 | 'meta_key' => $meta_key, |
| 387 | ); |
| 388 | |
| 389 | // Normal processing, dynamic population is off. |
| 390 | // If show_values is true, that means values posted are the raw values |
| 391 | // and not the labels. So we need to get the label values. |
| 392 | if ( ! empty( $field['show_values'] ) && '1' === $field['show_values'] ) { |
| 393 | foreach ( $field_submit as $item ) { |
| 394 | foreach ( $field['choices'] as $choice ) { |
| 395 | if ( $item === $choice['value'] ) { |
| 396 | $value[] = $choice['label']; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | $data['value'] = ! empty( $value ) ? evf_sanitize_array_combine( $value ) : ''; |
| 403 | } else { |
| 404 | $data['value'] = $value_raw; |
| 405 | } |
| 406 | |
| 407 | // Push field details to be saved. |
| 408 | evf()->task->form_fields[ $field_id ] = $data; |
| 409 | } |
| 410 | } |
| 411 |