| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Text Field Class |
| 5 |
*/ |
| 6 |
class WeForms_Form_Field_Name extends WeForms_Field_Contract { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
$this->name = __( 'Name', 'weforms' ); |
| 10 |
$this->input_type = 'name_field'; |
| 11 |
$this->icon = 'user'; |
| 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 name 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 |
<li <?php $this->print_list_attributes( $field_settings ); ?>> |
| 29 |
<?php $this->print_label( $field_settings, $form_id ); ?> |
| 30 |
|
| 31 |
<div class="wpuf-fields"> |
| 32 |
<div class="wpuf-name-field-wrap format-<?php echo esc_attr( $field_settings['format'] ); ?>"> |
| 33 |
<div class="wpuf-name-field-first-name"> |
| 34 |
<input |
| 35 |
name="<?php echo esc_attr( $field_settings['name'] ) ?>[first]" |
| 36 |
type="text" |
| 37 |
placeholder="<?php echo esc_attr( $field_settings['first_name']['placeholder'] ); ?>" |
| 38 |
value="<?php echo esc_attr( $field_settings['first_name']['default'] ); ?>" |
| 39 |
size="40" |
| 40 |
data-required="<?php echo esc_attr( $field_settings['required'] ) ?>" |
| 41 |
data-type="text" |
| 42 |
class="textfield wpuf_<?php echo esc_attr( $field_settings['name'] ); ?>_<?php echo esc_attr( $form_id); ?>" |
| 43 |
autocomplete="given-name" |
| 44 |
> |
| 45 |
<?php if ( ! $field_settings['hide_subs'] ) : ?> |
| 46 |
<label class="wpuf-form-sub-label"><?php esc_html_e( 'First', 'weforms' ); ?></label> |
| 47 |
<?php endif; ?> |
| 48 |
</div> |
| 49 |
|
| 50 |
<?php if ( $field_settings['format'] != 'first-last' ) { ?> |
| 51 |
<div class="wpuf-name-field-middle-name"> |
| 52 |
<input |
| 53 |
name="<?php echo esc_attr( $field_settings['name']) ?>[middle]" |
| 54 |
type="text" class="textfield" |
| 55 |
placeholder="<?php echo esc_attr( $field_settings['middle_name']['placeholder'] ); ?>" |
| 56 |
value="<?php echo esc_attr( $field_settings['middle_name']['default'] ); ?>" |
| 57 |
size="40" |
| 58 |
autocomplete="additional-name" |
| 59 |
> |
| 60 |
<?php if ( ! $field_settings['hide_subs'] ) : ?> |
| 61 |
<label class="wpuf-form-sub-label"><?php esc_html_e( 'Middle', 'weforms' ); ?></label> |
| 62 |
<?php endif; ?> |
| 63 |
</div> |
| 64 |
<?php } else { ?> |
| 65 |
<input type="hidden" name="<?php echo esc_attr( $field_settings['name'] ) ?>[middle]" value=""> |
| 66 |
<?php } ?> |
| 67 |
|
| 68 |
<div class="wpuf-name-field-last-name"> |
| 69 |
<input |
| 70 |
name="<?php echo esc_attr( $field_settings['name'] ) ?>[last]" |
| 71 |
type="text" class="textfield" |
| 72 |
placeholder="<?php echo esc_attr( $field_settings['last_name']['placeholder'] ); ?>" |
| 73 |
value="<?php echo esc_attr( $field_settings['last_name']['default'] ); ?>" |
| 74 |
size="40" |
| 75 |
autocomplete="family-name" |
| 76 |
> |
| 77 |
<?php if ( ! $field_settings['hide_subs'] ) : ?> |
| 78 |
<label class="wpuf-form-sub-label"><?php esc_html_e( 'Last', 'weforms' ); ?></label> |
| 79 |
<?php endif; ?> |
| 80 |
</div> |
| 81 |
</div> |
| 82 |
<?php $this->help_text( $field_settings ); ?> |
| 83 |
</div> |
| 84 |
</li> |
| 85 |
<?php |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get field options setting |
| 90 |
* |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function get_options_settings() { |
| 94 |
$default_options = $this->get_default_option_settings( true, [ 'width' ] ); |
| 95 |
|
| 96 |
$name_settings = [ |
| 97 |
[ |
| 98 |
'name' => 'format', |
| 99 |
'title' => __( 'Format', 'weforms' ), |
| 100 |
'type' => 'radio', |
| 101 |
'options' => [ |
| 102 |
'first-last' => __( 'First and Last name', 'weforms' ), |
| 103 |
'first-middle-last' => __( 'First, Middle and Last name', 'weforms' ), |
| 104 |
], |
| 105 |
'selected' => 'first-last', |
| 106 |
'section' => 'advanced', |
| 107 |
'priority' => 20, |
| 108 |
'help_text' => __( 'Select format to use for the name field', 'weforms' ), |
| 109 |
], |
| 110 |
[ |
| 111 |
'name' => 'auto_populate', |
| 112 |
'title' => 'Auto-populate name for logged users', |
| 113 |
'type' => 'checkbox', |
| 114 |
'is_single_opt' => true, |
| 115 |
'options' => [ |
| 116 |
'yes' => __( 'Auto-populate Name', 'weforms' ), |
| 117 |
], |
| 118 |
'default' => '', |
| 119 |
'section' => 'advanced', |
| 120 |
'priority' => 23, |
| 121 |
'help_text' => __( 'If a user is logged into the site, this name field will be auto-populated with his first-last/display name. And form\'s name field will be hidden.', 'weforms' ), |
| 122 |
], |
| 123 |
[ |
| 124 |
'name' => 'sub-labels', |
| 125 |
'title' => __( 'Label', 'weforms' ), |
| 126 |
'type' => 'name', |
| 127 |
'section' => 'advanced', |
| 128 |
'priority' => 21, |
| 129 |
'help_text' => __( 'Select format to use for the name field', 'weforms' ), |
| 130 |
], |
| 131 |
[ |
| 132 |
'name' => 'hide_subs', |
| 133 |
'title' => '', |
| 134 |
'type' => 'checkbox', |
| 135 |
'is_single_opt' => true, |
| 136 |
'options' => [ |
| 137 |
'true' => __( 'Hide Sub Labels', 'weforms' ), |
| 138 |
], |
| 139 |
'section' => 'advanced', |
| 140 |
'priority' => 23, |
| 141 |
'help_text' => '', |
| 142 |
], |
| 143 |
[ |
| 144 |
'name' => 'inline', |
| 145 |
'title' => __( 'Show in inline list', 'weforms' ), |
| 146 |
'type' => 'radio', |
| 147 |
'options' => [ |
| 148 |
'yes' => __( 'Yes', 'weforms' ), |
| 149 |
'no' => __( 'No', 'weforms' ), |
| 150 |
], |
| 151 |
'default' => 'no', |
| 152 |
'inline' => true, |
| 153 |
'section' => 'advanced', |
| 154 |
'priority' => 23, |
| 155 |
'help_text' => __( 'Show this option in an inline list', 'weforms' ), |
| 156 |
], |
| 157 |
]; |
| 158 |
|
| 159 |
return array_merge( $default_options, $name_settings ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get the field props |
| 164 |
* |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
public function get_field_props() { |
| 168 |
$defaults = $this->default_attributes(); |
| 169 |
$props = [ |
| 170 |
'format' => 'first-last', |
| 171 |
'first_name' => [ |
| 172 |
'placeholder' => '', |
| 173 |
'default' => '', |
| 174 |
'sub' => __( 'First', 'weforms' ), |
| 175 |
], |
| 176 |
'middle_name' => [ |
| 177 |
'placeholder' => '', |
| 178 |
'default' => '', |
| 179 |
'sub' => __( 'Middle', 'weforms' ), |
| 180 |
], |
| 181 |
'last_name' => [ |
| 182 |
'placeholder' => '', |
| 183 |
'default' => '', |
| 184 |
'sub' => __( 'Last', 'weforms' ), |
| 185 |
], |
| 186 |
'inline' => 'yes', |
| 187 |
'hide_subs' => false, |
| 188 |
]; |
| 189 |
|
| 190 |
return array_merge( $defaults, $props ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Prepare entry default, can be replaced through field classes |
| 195 |
* |
| 196 |
* @param $field |
| 197 |
* |
| 198 |
* @return mixed |
| 199 |
*/ |
| 200 |
public function prepare_entry( $field, $args = [] ) { |
| 201 |
if( empty( $_POST[ '_wpnonce' ] ) ) { |
| 202 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 203 |
} |
| 204 |
|
| 205 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { |
| 206 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 207 |
} |
| 208 |
|
| 209 |
// $args = ! empty( $args ) ? $args : sanitize_text_field( wp_unslash( $_POST ) ); |
| 210 |
$args = ! empty( $args ) ? $args : weforms_clean( $_POST ); |
| 211 |
|
| 212 |
if ( isset( $field['auto_populate'] ) && $field['auto_populate'] == 'yes' && is_user_logged_in() ) { |
| 213 |
$user = wp_get_current_user(); |
| 214 |
|
| 215 |
if ( !empty( $user->ID ) ) { |
| 216 |
if ( $user->first_name || $user->last_name ) { |
| 217 |
$name = []; |
| 218 |
$name[] = $user->first_name; |
| 219 |
$name[] = $user->last_name; |
| 220 |
|
| 221 |
return implode( WeForms::$field_separator, $name ); |
| 222 |
} else { |
| 223 |
return $user->display_name; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
$value = !empty( $args[$field['name']] ) ? $args[$field['name']] : ''; |
| 229 |
|
| 230 |
if ( is_array( $value ) ) { |
| 231 |
$entry_value = sanitize_text_field( trim( implode( WeForms::$field_separator, $args[$field['name']] ) ) ); |
| 232 |
} else { |
| 233 |
$entry_value = sanitize_text_field( trim( $value ) ); |
| 234 |
} |
| 235 |
|
| 236 |
return $entry_value; |
| 237 |
} |
| 238 |
} |
| 239 |
|