name = __( 'Name', 'weforms' );
$this->input_type = 'name_field';
$this->icon = 'user';
}
/**
* Render the text field
*
* @param array $field_settings
* @param integer $form_id
*
* @return void
*/
public function render( $field_settings, $form_id ) {
// let's not show the name field if user choose to auto populate for logged users
if ( isset( $field_settings['auto_populate'] ) && $field_settings['auto_populate'] == 'yes' && is_user_logged_in() ) {
return;
}
?>
print_list_attributes( $field_settings ); ?>>
print_label( $field_settings, $form_id ); ?>
help_text( $field_settings ); ?>
get_default_option_settings( true, array( 'width' ) );
$name_settings = array(
array(
'name' => 'format',
'title' => __( 'Format', 'weforms' ),
'type' => 'radio',
'options' => array(
'first-last' => __( 'First and Last name', 'weforms' ),
'first-middle-last' => __( 'First, Middle and Last name', 'weforms' )
),
'selected' => 'first-last',
'section' => 'advanced',
'priority' => 20,
'help_text' => __( 'Select format to use for the name field', 'weforms' ),
),
array(
'name' => 'auto_populate',
'title' => 'Auto-populate name for logged users',
'type' => 'checkbox',
'is_single_opt' => true,
'options' => array(
'yes' => __( 'Auto-populate Name', 'weforms' )
),
'default' => '',
'section' => 'advanced',
'priority' => 23,
'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' ),
),
array(
'name' => 'sub-labels',
'title' => __( 'Label', 'weforms' ),
'type' => 'name',
'section' => 'advanced',
'priority' => 21,
'help_text' => __( 'Select format to use for the name field', 'weforms' ),
),
array(
'name' => 'hide_subs',
'title' => '',
'type' => 'checkbox',
'is_single_opt' => true,
'options' => array(
'true' => __( 'Hide Sub Labels', 'weforms' )
),
'section' => 'advanced',
'priority' => 23,
'help_text' => '',
),
array(
'name' => 'inline',
'title' => __( 'Show in inline list', 'weforms' ),
'type' => 'radio',
'options' => array(
'yes' => __( 'Yes', 'weforms' ),
'no' => __( 'No', 'weforms' ),
),
'default' => 'no',
'inline' => true,
'section' => 'advanced',
'priority' => 23,
'help_text' => __( 'Show this option in an inline list', 'weforms' ),
)
);
return array_merge( $default_options, $name_settings );
}
/**
* Get the field props
*
* @return array
*/
public function get_field_props() {
$defaults = $this->default_attributes();
$props = array(
'format' => 'first-last',
'first_name' => array(
'placeholder' => '',
'default' => '',
'sub' => __( 'First', 'weforms' )
),
'middle_name' => array(
'placeholder' => '',
'default' => '',
'sub' => __( 'Middle', 'weforms' )
),
'last_name' => array(
'placeholder' => '',
'default' => '',
'sub' => __( 'Last', 'weforms' )
),
'inline' => 'yes',
'hide_subs' => false,
);
return array_merge( $defaults, $props );
}
/**
* Prepare entry default, can be replaced through field classes
*
* @param $field
*
* @return mixed
*/
public function prepare_entry( $field ) {
if ( isset( $field['auto_populate'] ) && $field['auto_populate'] == 'yes' && is_user_logged_in() ) {
$user = wp_get_current_user();
if ( ! empty( $user->ID ) ) {
if ( $user->first_name || $user->last_name ) {
$name = array();
$name[] = $user->first_name;
$name[] = $user->last_name;
return implode( WeForms::$field_separator, $name );
} else {
return $user->display_name;
}
}
}
$value = !empty( $_POST[$field['name']] ) ? $_POST[$field['name']] : '';
if ( is_array( $value ) ) {
$entry_value = implode( WeForms::$field_separator, $_POST[$field['name']] );
} else {
$entry_value = trim( $value );
}
return $entry_value;
}
}