| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Text Field Class |
| 5 |
*/ |
| 6 |
class WeForms_Form_Field_Email extends WeForms_Form_Field_Text { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
$this->name = __( 'Email Address', 'weforms' ); |
| 10 |
$this->input_type = 'email_address'; |
| 11 |
$this->icon = 'envelope-o'; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Render the text field |
| 16 |
* |
| 17 |
* @param array $field_settings |
| 18 |
* @param int $form_id |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function render( $field_settings, $form_id ) { |
| 23 |
|
| 24 |
// let's not show the email field if user choose to auto populate for logged users |
| 25 |
if ( isset( $field_settings['auto_populate'] ) && $field_settings['auto_populate'] == 'yes' && is_user_logged_in() ) { |
| 26 |
return; |
| 27 |
} |
| 28 |
$form_settings = weforms()->form->get( $form_id )->get_settings(); |
| 29 |
$use_theme_css = isset( $form_settings['use_theme_css'] ) ? $form_settings['use_theme_css'] : 'wpuf-style'; |
| 30 |
$value = $field_settings['default']; ?> |
| 31 |
<li <?php $this->print_list_attributes( $field_settings ); ?>> |
| 32 |
<?php $this->print_label( $field_settings, $form_id ); ?> |
| 33 |
|
| 34 |
<div class="wpuf-fields"> |
| 35 |
<input |
| 36 |
id="<?php echo esc_attr( $field_settings['name'] ) . '_' . esc_attr( $form_id ); ?>" |
| 37 |
type="email" |
| 38 |
class="email <?php echo ' wpuf_'. esc_attr( $field_settings['name'] ).'_'. esc_attr( $form_id ); ?>" |
| 39 |
data-duplicate="<?php echo isset( $field_settings['duplicate'] ) ? esc_attr( $field_settings['duplicate'] ) : 'no'; ?>" |
| 40 |
data-required="<?php echo esc_attr( $field_settings['required'] ) ?>" |
| 41 |
data-type="email" |
| 42 |
data-style="<?php echo esc_attr( $use_theme_css ); ?>" |
| 43 |
name="<?php echo esc_attr( $field_settings['name'] ); ?>" |
| 44 |
placeholder="<?php echo esc_attr( $field_settings['placeholder'] ); ?>" |
| 45 |
value="<?php echo esc_attr( $value ); ?>" |
| 46 |
size="<?php echo esc_attr( $field_settings['size'] ); ?>" |
| 47 |
autocomplete="email" |
| 48 |
/> |
| 49 |
<?php $this->help_text( $field_settings ); ?> |
| 50 |
</div> |
| 51 |
</li> |
| 52 |
<?php |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get field options setting |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function get_options_settings() { |
| 61 |
$default_options = $this->get_default_option_settings(); |
| 62 |
$default_text_options = $this->get_default_text_option_settings(); |
| 63 |
$check_duplicate = [ |
| 64 |
[ |
| 65 |
'name' => 'duplicate', |
| 66 |
'title' => 'No Duplicates', |
| 67 |
'type' => 'checkbox', |
| 68 |
'is_single_opt' => true, |
| 69 |
'options' => [ |
| 70 |
'no' => __( 'Unique Values Only', 'weforms' ), |
| 71 |
], |
| 72 |
'default' => '', |
| 73 |
'section' => 'advanced', |
| 74 |
'priority' => 23, |
| 75 |
'help_text' => __( 'Select this option to limit user input to unique values only. This will require that a value entered in a field does not currently exist in the entry database for that field.', 'weforms' ), |
| 76 |
], |
| 77 |
[ |
| 78 |
'name' => 'auto_populate', |
| 79 |
'title' => 'Auto-populate email for logged users', |
| 80 |
'type' => 'checkbox', |
| 81 |
'is_single_opt' => true, |
| 82 |
'options' => [ |
| 83 |
'yes' => __( 'Auto-populate Email', 'weforms' ), |
| 84 |
], |
| 85 |
'default' => '', |
| 86 |
'section' => 'advanced', |
| 87 |
'priority' => 23, |
| 88 |
'help_text' => __( 'If a user is logged into the site, this email field will be auto-populated with his email. And form\'s email field will be hidden.', 'weforms' ), |
| 89 |
], |
| 90 |
]; |
| 91 |
|
| 92 |
return array_merge( $default_options, $default_text_options, $check_duplicate ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Prepare entry default, can be replaced through field classes |
| 97 |
* |
| 98 |
* @param $field |
| 99 |
* |
| 100 |
* @return mixed |
| 101 |
*/ |
| 102 |
public function prepare_entry( $field, $args = [] ) { |
| 103 |
if( empty( $_POST['_wpnonce'])) { |
| 104 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { |
| 108 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 109 |
} |
| 110 |
|
| 111 |
$args = ! empty( $args ) ? $args : weforms_clean( $_POST ); |
| 112 |
|
| 113 |
if ( isset( $field['auto_populate'] ) && $field['auto_populate'] == 'yes' && is_user_logged_in() ) { |
| 114 |
$user = wp_get_current_user(); |
| 115 |
|
| 116 |
if ( !empty( $user->user_email ) ) { |
| 117 |
return $user->user_email; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$value = !empty( $args[ $field[ 'name' ] ] ) ? $args[ $field[ 'name' ] ] : ''; |
| 122 |
|
| 123 |
return sanitize_text_field( trim( $value ) ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get the field props |
| 128 |
* |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public function get_field_props() { |
| 132 |
$defaults = $this->default_attributes(); |
| 133 |
$defaults['duplicate'] = ''; |
| 134 |
|
| 135 |
return $defaults; |
| 136 |
} |
| 137 |
} |
| 138 |
|