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-email.php
416 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Email field. |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_Number class. |
| 13 | */ |
| 14 | class EVF_Field_Email extends EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->name = esc_html__( 'Email', 'everest-forms' ); |
| 21 | $this->type = 'email'; |
| 22 | $this->icon = 'evf-icon evf-icon-email'; |
| 23 | $this->order = 90; |
| 24 | $this->group = 'general'; |
| 25 | $this->settings = array( |
| 26 | 'basic-options' => array( |
| 27 | 'field_options' => array( |
| 28 | 'label', |
| 29 | 'meta', |
| 30 | 'description', |
| 31 | 'required', |
| 32 | 'required_field_message', |
| 33 | 'confirmation', |
| 34 | ), |
| 35 | ), |
| 36 | 'advanced-options' => array( |
| 37 | 'field_options' => array( |
| 38 | 'size', |
| 39 | 'placeholder', |
| 40 | 'confirmation_placeholder', |
| 41 | 'label_hide', |
| 42 | 'sublabel_hide', |
| 43 | 'default_value', |
| 44 | 'css', |
| 45 | ), |
| 46 | ), |
| 47 | ); |
| 48 | |
| 49 | parent::__construct(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Hook in tabs. |
| 54 | */ |
| 55 | public function init_hooks() { |
| 56 | add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); |
| 57 | add_filter( 'everest_forms_field_new_required', array( $this, 'field_default_required' ), 5, 3 ); |
| 58 | add_filter( 'everest_forms_builder_field_option_class', array( $this, 'field_option_class' ), 10, 2 ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Field properties. |
| 63 | * |
| 64 | * @param array $properties Field properties. |
| 65 | * @param array $field Field Data. |
| 66 | * @param array $form_data Form Data. |
| 67 | * |
| 68 | * @return array |
| 69 | */ |
| 70 | public function field_properties( $properties, $field, $form_data ) { |
| 71 | if ( empty( $field['confirmation'] ) ) { |
| 72 | return $properties; |
| 73 | } |
| 74 | |
| 75 | $form_id = absint( $form_data['id'] ); |
| 76 | $field_id = $field['id']; |
| 77 | |
| 78 | $props = array( |
| 79 | 'inputs' => array( |
| 80 | 'primary' => array( |
| 81 | 'block' => array( |
| 82 | 'everest-forms-field-row-block', |
| 83 | 'everest-forms-one-half', |
| 84 | 'everest-forms-first', |
| 85 | ), |
| 86 | 'class' => array( |
| 87 | 'everest-forms-field-email-primary', |
| 88 | ), |
| 89 | 'sublabel' => array( |
| 90 | 'hidden' => ! empty( $field['sublabel_hide'] ), |
| 91 | 'value' => esc_html__( 'Email', 'everest-forms' ), |
| 92 | ), |
| 93 | ), |
| 94 | 'secondary' => array( |
| 95 | 'attr' => array( |
| 96 | 'name' => "everest_forms[form_fields][{$field_id}][secondary]", |
| 97 | 'value' => '', |
| 98 | 'placeholder' => ! empty( $field['confirmation_placeholder'] ) ? $field['confirmation_placeholder'] : '', |
| 99 | ), |
| 100 | 'block' => array( |
| 101 | 'everest-forms-field-row-block', |
| 102 | 'everest-forms-one-half', |
| 103 | ), |
| 104 | 'class' => array( |
| 105 | 'input-text', |
| 106 | 'everest-forms-field-email-secondary', |
| 107 | ), |
| 108 | 'data' => array( |
| 109 | 'rule-confirm' => '#' . $properties['inputs']['primary']['id'], |
| 110 | ), |
| 111 | 'id' => "evf-{$form_id}-field_{$field_id}-secondary", |
| 112 | 'required' => ! empty( $field['required'] ) ? 'required' : '', |
| 113 | 'sublabel' => array( |
| 114 | 'hidden' => ! empty( $field['sublabel_hide'] ), |
| 115 | 'value' => esc_html__( 'Confirm Email', 'everest-forms' ), |
| 116 | ), |
| 117 | 'value' => '', |
| 118 | ), |
| 119 | ), |
| 120 | ); |
| 121 | $properties = array_merge_recursive( $properties, $props ); |
| 122 | |
| 123 | $properties['inputs']['primary']['attr']['name'] = "everest_forms[form_fields][{$field_id}][primary]"; |
| 124 | |
| 125 | $properties['inputs']['primary']['class'] = array_diff( |
| 126 | $properties['inputs']['primary']['class'], |
| 127 | array( |
| 128 | 'evf-error', |
| 129 | ) |
| 130 | ); |
| 131 | |
| 132 | if ( ! empty( $properties['error']['value']['primary'] ) ) { |
| 133 | $properties['inputs']['primary']['class'][] = 'evf-error'; |
| 134 | } |
| 135 | |
| 136 | if ( ! empty( $properties['error']['value']['secondary'] ) ) { |
| 137 | $properties['inputs']['secondary']['class'][] = 'evf-error'; |
| 138 | } |
| 139 | |
| 140 | if ( ! empty( $field['required'] ) ) { |
| 141 | $properties['inputs']['secondary']['class'][] = 'evf-field-required'; |
| 142 | } |
| 143 | |
| 144 | return $properties; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Field should default to being required. |
| 149 | * |
| 150 | * @since 1.5.0 |
| 151 | * |
| 152 | * @param bool $required Required status, true is required. |
| 153 | * @param array $field Field settings. |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | public function field_default_required( $required, $field ) { |
| 158 | if ( 'email' === $field['type'] ) { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | return $required; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Confirmation field option. |
| 167 | * |
| 168 | * @param array $field Field settings. |
| 169 | */ |
| 170 | public function confirmation( $field ) { |
| 171 | $fld = $this->field_element( |
| 172 | 'checkbox', |
| 173 | $field, |
| 174 | array( |
| 175 | 'slug' => 'confirmation', |
| 176 | 'value' => isset( $field['confirmation'] ) ? '1' : '0', |
| 177 | 'desc' => esc_html__( 'Enable Email Confirmation', 'everest-forms' ), |
| 178 | 'tooltip' => esc_html__( 'Check to enable email confirmation.', 'everest-forms' ), |
| 179 | ), |
| 180 | false |
| 181 | ); |
| 182 | $args = array( |
| 183 | 'slug' => 'confirmation', |
| 184 | 'content' => $fld, |
| 185 | ); |
| 186 | $this->field_element( 'row', $field, $args ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Confirmation Placeholder field option. |
| 191 | * |
| 192 | * @param array $field Field Data. |
| 193 | */ |
| 194 | public function confirmation_placeholder( $field ) { |
| 195 | $lbl = $this->field_element( |
| 196 | 'label', |
| 197 | $field, |
| 198 | array( |
| 199 | 'slug' => 'confirmation_placeholder', |
| 200 | 'value' => esc_html__( 'Confirmation Placeholder Text', 'everest-forms' ), |
| 201 | 'tooltip' => esc_html__( 'Enter text for the confirmation field placeholder.', 'everest-forms' ), |
| 202 | ), |
| 203 | false |
| 204 | ); |
| 205 | $fld = $this->field_element( |
| 206 | 'text', |
| 207 | $field, |
| 208 | array( |
| 209 | 'slug' => 'confirmation_placeholder', |
| 210 | 'value' => ! empty( $field['confirmation_placeholder'] ) ? esc_attr( $field['confirmation_placeholder'] ) : '', |
| 211 | ), |
| 212 | false |
| 213 | ); |
| 214 | $args = array( |
| 215 | 'slug' => 'confirmation_placeholder', |
| 216 | 'content' => $lbl . $fld, |
| 217 | ); |
| 218 | $this->field_element( 'row', $field, $args ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Field preview inside the builder. |
| 223 | * |
| 224 | * @since 1.0.0 |
| 225 | * |
| 226 | * @param array $field Field settings. |
| 227 | */ |
| 228 | public function field_preview( $field ) { |
| 229 | $placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; |
| 230 | $confirm_placeholder = ! empty( $field['confirmation_placeholder'] ) ? esc_attr( $field['confirmation_placeholder'] ) : ''; |
| 231 | $confirm = ! empty( $field['confirmation'] ) ? 'enabled' : 'disabled'; |
| 232 | |
| 233 | // Label. |
| 234 | $this->field_preview_option( 'label', $field ); |
| 235 | ?> |
| 236 | <div class="everest-forms-confirm everest-forms-confirm-<?php echo esc_attr( $confirm ); ?>"> |
| 237 | <div class="everest-forms-confirm-primary"> |
| 238 | <input type="email" placeholder="<?php echo esc_attr( $placeholder ); ?>" class="widefat primary-input" disabled> |
| 239 | <label class="everest-forms-sub-label"><?php esc_html_e( 'Email', 'everest-forms' ); ?></label> |
| 240 | |
| 241 | </div> |
| 242 | <div class="everest-forms-confirm-confirmation"> |
| 243 | <input type="email" placeholder="<?php echo esc_attr( $confirm_placeholder ); ?>" class="widefat secondary-input" disabled> |
| 244 | <label class="everest-forms-sub-label"><?php esc_html_e( 'Confirm Email', 'everest-forms' ); ?></label> |
| 245 | </div> |
| 246 | </div> |
| 247 | <?php |
| 248 | // Description. |
| 249 | $this->field_preview_option( 'description', $field ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Field display on the form front-end. |
| 254 | * |
| 255 | * @since 1.0.0 |
| 256 | * |
| 257 | * @param array $field Field settings. |
| 258 | * @param array $deprecated Deprecated array. |
| 259 | * @param array $form_data Form data and settings. |
| 260 | */ |
| 261 | public function field_display( $field, $deprecated, $form_data ) { |
| 262 | |
| 263 | // Define data. |
| 264 | $form_id = absint( $form_data['id'] ); |
| 265 | $confirmation = ! empty( $field['confirmation'] ); |
| 266 | $primary = $field['properties']['inputs']['primary']; |
| 267 | $secondary = ! empty( $field['properties']['inputs']['secondary'] ) ? $field['properties']['inputs']['secondary'] : ''; |
| 268 | |
| 269 | // Standard email field. |
| 270 | if ( ! $confirmation ) { |
| 271 | |
| 272 | // Primary field. |
| 273 | printf( |
| 274 | '<input type="email" %s %s>', |
| 275 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 276 | esc_attr( $primary['required'] ) |
| 277 | ); |
| 278 | |
| 279 | // Confirmation email field configuration. |
| 280 | } else { |
| 281 | |
| 282 | // Row wrapper. |
| 283 | echo '<div class="everest-forms-field-row everest-forms-field">'; |
| 284 | // Primary field. |
| 285 | echo '<div ' . evf_html_attributes( false, $primary['block'] ) . '>'; |
| 286 | printf( |
| 287 | '<input type="email" %s %s>', |
| 288 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 289 | esc_attr( $primary['required'] ) |
| 290 | ); |
| 291 | $this->field_display_sublabel( 'primary', 'after', $field ); |
| 292 | $this->field_display_error( 'primary', $field ); |
| 293 | echo '</div>'; |
| 294 | |
| 295 | // Secondary field. |
| 296 | echo '<div ' . evf_html_attributes( false, $secondary['block'] ) . '>'; |
| 297 | printf( |
| 298 | '<input type="email" %s %s>', |
| 299 | evf_html_attributes( $secondary['id'], $secondary['class'], $secondary['data'], $secondary['attr'] ), |
| 300 | esc_attr( $secondary['required'] ) |
| 301 | ); |
| 302 | $this->field_display_sublabel( 'secondary', 'after', $field ); |
| 303 | $this->field_display_error( 'secondary', $field ); |
| 304 | echo '</div>'; |
| 305 | |
| 306 | echo '</div>'; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Add class to field options wrapper to indicate if field confirmation is enabled. |
| 312 | * |
| 313 | * @param array $class Field class. |
| 314 | * @param array $field Field option data. |
| 315 | * @return array |
| 316 | */ |
| 317 | public function field_option_class( $class, $field ) { |
| 318 | if ( 'email' === $field['type'] ) { |
| 319 | if ( isset( $field['confirmation'] ) ) { |
| 320 | $class[] = 'everest-forms-confirm-enabled'; |
| 321 | } else { |
| 322 | $class[] = 'everest-forms-confirm-disabled'; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return $class; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Validates field on form submit. |
| 331 | * |
| 332 | * @param int $field_id Field ID. |
| 333 | * @param array $field_submit Submitted data. |
| 334 | * @param array $form_data Form data. |
| 335 | */ |
| 336 | public function validate( $field_id, $field_submit, $form_data ) { |
| 337 | $form_id = $form_data['id']; |
| 338 | $fields = $form_data['form_fields']; |
| 339 | $required = evf_get_required_label(); |
| 340 | $invalid_email = esc_html__( 'Please enter a valid email address.', 'everest-forms' ); |
| 341 | $conditional_status = isset( $form_data['form_fields'][ $field_id ]['conditional_logic_status'] ) ? $form_data['form_fields'][ $field_id ]['conditional_logic_status'] : 0; |
| 342 | |
| 343 | // Standard configuration, confirmation disabled. |
| 344 | if ( empty( $fields[ $field_id ]['confirmation'] ) ) { |
| 345 | |
| 346 | // Required check. |
| 347 | if ( ! empty( $fields[ $field_id ]['required'] ) && '1' !== $conditional_status && ( empty( $field_submit ) && '0' !== $field_submit ) ) { |
| 348 | evf()->task->errors[ $form_id ][ $field_id ] = $required; |
| 349 | update_option( 'evf_validation_error', 'yes' ); |
| 350 | } |
| 351 | |
| 352 | if ( isset( $field_submit ) && ! is_email( $field_submit ) ) { |
| 353 | evf()->task->errors[ $form_id ][ $field_id ] = $invalid_email; |
| 354 | update_option( 'evf_validation_error', 'yes' ); |
| 355 | return; |
| 356 | } |
| 357 | } else { |
| 358 | |
| 359 | // Required check. |
| 360 | if ( ! empty( $fields[ $field_id ]['required'] ) && '1' !== $conditional_status && ( empty( $field_submit['primary'] ) && '0' !== $field_submit ) ) { |
| 361 | evf()->task->errors[ $form_id ][ $field_id ]['primary'] = $required; |
| 362 | update_option( 'evf_validation_error', 'yes' ); |
| 363 | } |
| 364 | |
| 365 | // Required check, secondary confirmation field. |
| 366 | if ( ! empty( $fields[ $field_id ]['required'] ) && '1' !== $conditional_status && ( empty( $field_submit['secondary'] ) && '0' !== $field_submit ) ) { |
| 367 | evf()->task->errors[ $form_id ][ $field_id ]['secondary'] = $required; |
| 368 | update_option( 'evf_validation_error', 'yes' ); |
| 369 | } |
| 370 | |
| 371 | // Fields need to match. |
| 372 | if ( isset( $field_submit['primary'] ) && isset( $field_submit['secondary'] ) ) { |
| 373 | if ( $field_submit['primary'] !== $field_submit['secondary'] ) { |
| 374 | evf()->task->errors[ $form_id ][ $field_id ]['secondary'] = esc_html__( 'Confirmation Email do not match.', 'everest-forms' ); |
| 375 | update_option( 'evf_validation_error', 'yes' ); |
| 376 | return; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if ( isset( $field_submit['primary'] ) && ! is_email( $field_submit['primary'] ) ) { |
| 381 | evf()->task->errors[ $form_id ][ $field_id ]['primary'] = $invalid_email; |
| 382 | update_option( 'evf_validation_error', 'yes' ); |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Formats and sanitizes field. |
| 390 | * |
| 391 | * @param int $field_id Field ID. |
| 392 | * @param array $field_submit Submitted field value. |
| 393 | * @param array $form_data Form data and settings. |
| 394 | * @param string $meta_key Field meta key. |
| 395 | */ |
| 396 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 397 | |
| 398 | if ( is_array( $field_submit ) ) { |
| 399 | $value = ! empty( $field_submit['primary'] ) ? $field_submit['primary'] : ''; |
| 400 | } else { |
| 401 | $value = ! empty( $field_submit ) ? $field_submit : ''; |
| 402 | } |
| 403 | |
| 404 | $name = ! empty( $form_data['form_fields'][ $field_id ] ['label'] ) ? $form_data['form_fields'][ $field_id ]['label'] : ''; |
| 405 | |
| 406 | // Set final field details. |
| 407 | evf()->task->form_fields[ $field_id ] = array( |
| 408 | 'name' => sanitize_text_field( $name ), |
| 409 | 'value' => sanitize_text_field( $value ), |
| 410 | 'id' => $field_id, |
| 411 | 'type' => $this->type, |
| 412 | 'meta_key' => $meta_key, |
| 413 | ); |
| 414 | } |
| 415 | } |
| 416 |