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