class-evf-field-address.php
7 years ago
class-evf-field-checkbox.php
6 years ago
class-evf-field-country.php
7 years ago
class-evf-field-credit-card.php
7 years ago
class-evf-field-date-time.php
6 years ago
class-evf-field-email.php
6 years ago
class-evf-field-file-upload.php
6 years ago
class-evf-field-first-name.php
6 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
6 years ago
class-evf-field-likert.php
7 years ago
class-evf-field-number.php
6 years ago
class-evf-field-password.php
7 years ago
class-evf-field-payment-checkbox.php
7 years ago
class-evf-field-payment-quantity.php
6 years ago
class-evf-field-payment-radio.php
7 years ago
class-evf-field-payment-single.php
7 years ago
class-evf-field-payment-total.php
6 years ago
class-evf-field-phone.php
7 years ago
class-evf-field-radio.php
6 years ago
class-evf-field-rating.php
7 years ago
class-evf-field-scale-rating.php
7 years ago
class-evf-field-select.php
6 years ago
class-evf-field-signature.php
7 years ago
class-evf-field-text.php
6 years ago
class-evf-field-textarea.php
6 years ago
class-evf-field-title.php
6 years ago
class-evf-field-url.php
6 years ago
class-evf-field-select.php
184 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 | 'description', |
| 49 | 'required', |
| 50 | 'required_field_message', |
| 51 | ), |
| 52 | ), |
| 53 | 'advanced-options' => array( |
| 54 | 'field_options' => array( |
| 55 | 'size', |
| 56 | 'placeholder', |
| 57 | 'label_hide', |
| 58 | 'css', |
| 59 | ), |
| 60 | ), |
| 61 | ); |
| 62 | |
| 63 | parent::__construct(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Field preview inside the builder. |
| 68 | * |
| 69 | * @since 1.0.0 |
| 70 | * |
| 71 | * @param array $field Field settings. |
| 72 | */ |
| 73 | public function field_preview( $field ) { |
| 74 | // Label. |
| 75 | $this->field_preview_option( 'label', $field ); |
| 76 | |
| 77 | // Choices. |
| 78 | $this->field_preview_option( 'choices', $field ); |
| 79 | |
| 80 | // Description. |
| 81 | $this->field_preview_option( 'description', $field ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Field display on the form front-end. |
| 86 | * |
| 87 | * @since 1.0.0 |
| 88 | * |
| 89 | * @param array $field Field Data. |
| 90 | * @param array $field_atts Field attributes. |
| 91 | * @param array $form_data All Form Data. |
| 92 | */ |
| 93 | public function field_display( $field, $field_atts, $form_data ) { |
| 94 | // Setup and sanitize the necessary data. |
| 95 | $primary = $field['properties']['inputs']['primary']; |
| 96 | $field = apply_filters( 'everest_forms_select_field_display', $field, $field_atts, $form_data ); |
| 97 | $field_placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; |
| 98 | $field_data = ''; |
| 99 | $choices = $field['choices']; |
| 100 | $has_default = false; |
| 101 | |
| 102 | if ( ! empty( $field_atts['input_data'] ) ) { |
| 103 | foreach ( $field_atts['input_data'] as $key => $val ) { |
| 104 | $field_data .= ' data-' . $key . '="' . $val . '"'; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Check to see if any of the options have selected by default. |
| 109 | foreach ( $choices as $choice ) { |
| 110 | if ( isset( $choice['default'] ) ) { |
| 111 | $has_default = true; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Primary select field. |
| 117 | printf( |
| 118 | "<select type='select' %s %s>", |
| 119 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 120 | $primary['required'] // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 121 | ); |
| 122 | |
| 123 | // Optional placeholder. |
| 124 | if ( ! empty( $field_placeholder ) ) { |
| 125 | printf( '<option value="" class="placeholder" disabled %s>%s</option>', selected( false, $has_default, false ), esc_html( $field_placeholder ) ); |
| 126 | } |
| 127 | |
| 128 | // Build the select options. |
| 129 | foreach ( $choices as $key => $choice ) { |
| 130 | $selected = isset( $choice['default'] ) && empty( $field_placeholder ) ? '1' : '0'; |
| 131 | $val = isset( $field['show_values'] ) ? esc_attr( $choice['value'] ) : esc_attr( $choice['label'] ); |
| 132 | |
| 133 | printf( '<option value="%s" %s>%s</option>', esc_attr( $val ), selected( '1', $selected, false ), esc_html( $choice['label'] ) ); |
| 134 | } |
| 135 | |
| 136 | echo '</select>'; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Formats and sanitizes field. |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | * |
| 144 | * @param int $field_id Field ID. |
| 145 | * @param mixed $field_submit Submitted field value. |
| 146 | * @param array $form_data Form data and settings. |
| 147 | * @param string $meta_key Field meta key. |
| 148 | */ |
| 149 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 150 | $field = $form_data['form_fields'][ $field_id ]; |
| 151 | $name = sanitize_text_field( $field['label'] ); |
| 152 | $value_raw = sanitize_text_field( $field_submit ); |
| 153 | $value = ''; |
| 154 | |
| 155 | $data = array( |
| 156 | 'name' => $name, |
| 157 | 'value' => '', |
| 158 | 'value_raw' => $value_raw, |
| 159 | 'id' => $field_id, |
| 160 | 'type' => $this->type, |
| 161 | 'meta_key' => $meta_key, |
| 162 | ); |
| 163 | |
| 164 | // Normal processing, dynamic population is off. |
| 165 | // If show_values is true, that means values posted are the raw values |
| 166 | // and not the labels. So we need to get the label values. |
| 167 | if ( ! empty( $field['show_values'] ) && '1' === $field['show_values'] ) { |
| 168 | foreach ( $field['choices'] as $choice ) { |
| 169 | if ( $choice['value'] === $field_submit ) { |
| 170 | $value = $choice['label']; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | $data['value'] = sanitize_text_field( $value ); |
| 176 | } else { |
| 177 | $data['value'] = $value_raw; |
| 178 | } |
| 179 | |
| 180 | // Push field details to be saved. |
| 181 | evf()->task->form_fields[ $field_id ] = $data; |
| 182 | } |
| 183 | } |
| 184 |