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