class-base.php
4 months ago
class-checkbox.php
6 months ago
class-email.php
8 months ago
class-gdpr-checkbox.php
8 months ago
class-internal-information.php
10 months ago
class-name.php
10 months ago
class-number-slider.php
1 year ago
class-number.php
1 year ago
class-radio.php
6 months ago
class-select.php
5 months ago
class-text.php
1 year ago
class-textarea.php
1 year ago
class-name.php
682 lines
| 1 | <?php |
| 2 | |
| 3 | // phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 4 | /** @noinspection AutoloadingIssuesInspection */ |
| 5 | |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Name text field. |
| 12 | * |
| 13 | * @since 1.0.0 |
| 14 | */ |
| 15 | class WPForms_Field_Name extends WPForms_Field { |
| 16 | |
| 17 | /** |
| 18 | * Primary class constructor. |
| 19 | * |
| 20 | * @since 1.0.0 |
| 21 | */ |
| 22 | public function init() { |
| 23 | |
| 24 | // Define field type information. |
| 25 | $this->name = esc_html__( 'Name', 'wpforms-lite' ); |
| 26 | $this->keywords = esc_html__( 'user, first, last', 'wpforms-lite' ); |
| 27 | $this->type = 'name'; |
| 28 | $this->icon = 'fa-user'; |
| 29 | $this->order = 150; |
| 30 | |
| 31 | $this->hooks(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Hooks. |
| 36 | * |
| 37 | * @since 1.8.1 |
| 38 | */ |
| 39 | private function hooks(): void { |
| 40 | |
| 41 | // Define additional field properties. |
| 42 | add_filter( 'wpforms_field_properties_name', [ $this, 'field_properties' ], 5, 3 ); |
| 43 | |
| 44 | // Set field to default required. |
| 45 | add_filter( 'wpforms_field_new_required', [ $this, 'default_required' ], 10, 2 ); |
| 46 | |
| 47 | // This field requires fieldset+legend instead of the field label. |
| 48 | add_filter( "wpforms_frontend_modern_is_field_requires_fieldset_{$this->type}", [ $this, 'is_field_requires_fieldset' ], PHP_INT_MAX, 2 ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Define additional field properties. |
| 53 | * |
| 54 | * @since 1.3.7 |
| 55 | * |
| 56 | * @param array|mixed $properties Field properties. |
| 57 | * @param array $field Field data and settings. |
| 58 | * @param array $form_data Form data and settings. |
| 59 | * |
| 60 | * @return array |
| 61 | */ |
| 62 | public function field_properties( $properties, $field, $form_data ): array { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded |
| 63 | |
| 64 | $properties = (array) $properties; |
| 65 | |
| 66 | $format = ! empty( $field['format'] ) ? esc_attr( $field['format'] ) : 'first-last'; |
| 67 | |
| 68 | // Simple format. |
| 69 | if ( $format === 'simple' ) { |
| 70 | $properties['inputs']['primary']['attr']['placeholder'] = ! empty( $field['simple_placeholder'] ) |
| 71 | ? $field['simple_placeholder'] : |
| 72 | ''; |
| 73 | $properties['inputs']['primary']['attr']['value'] = ! empty( $field['simple_default'] ) |
| 74 | ? wpforms_process_smart_tags( $field['simple_default'], $form_data, [], '', 'field-properties' ) |
| 75 | : ''; |
| 76 | |
| 77 | return $properties; |
| 78 | } |
| 79 | |
| 80 | // Expanded formats. |
| 81 | // Remove primary for expanded formats since we have first, middle, last. |
| 82 | unset( $properties['inputs']['primary'] ); |
| 83 | |
| 84 | // Remove reference to an input element to prevent duplication. |
| 85 | if ( empty( $field['sublabel_hide'] ) ) { |
| 86 | unset( $properties['label']['attr']['for'] ); |
| 87 | } |
| 88 | |
| 89 | $form_id = absint( $form_data['id'] ); |
| 90 | $field_id = wpforms_validate_field_id( $field['id'] ); |
| 91 | |
| 92 | $props = [ |
| 93 | 'inputs' => [ |
| 94 | 'first' => [ |
| 95 | 'attr' => [ |
| 96 | 'name' => "wpforms[fields][{$field_id}][first]", |
| 97 | 'value' => ! empty( $field['first_default'] ) |
| 98 | ? wpforms_process_smart_tags( $field['first_default'], $form_data, [], '', 'field-properties' ) |
| 99 | : '', |
| 100 | 'placeholder' => ! empty( $field['first_placeholder'] ) ? $field['first_placeholder'] : '', |
| 101 | ], |
| 102 | 'block' => [ |
| 103 | 'wpforms-field-row-block', |
| 104 | 'wpforms-first', |
| 105 | ], |
| 106 | 'class' => [ |
| 107 | 'wpforms-field-name-first', |
| 108 | ], |
| 109 | 'data' => [], |
| 110 | 'id' => "wpforms-{$form_id}-field_{$field_id}", |
| 111 | 'required' => ! empty( $field['required'] ) ? 'required' : '', |
| 112 | 'sublabel' => [ |
| 113 | 'hidden' => ! empty( $field['sublabel_hide'] ), |
| 114 | 'value' => esc_html__( 'First', 'wpforms-lite' ), |
| 115 | ], |
| 116 | ], |
| 117 | 'middle' => [ |
| 118 | 'attr' => [ |
| 119 | 'name' => "wpforms[fields][{$field_id}][middle]", |
| 120 | 'value' => ! empty( $field['middle_default'] ) |
| 121 | ? wpforms_process_smart_tags( $field['middle_default'], $form_data, [], '', 'field-properties' ) |
| 122 | : '', |
| 123 | 'placeholder' => ! empty( $field['middle_placeholder'] ) ? $field['middle_placeholder'] : '', |
| 124 | ], |
| 125 | 'block' => [ |
| 126 | 'wpforms-field-row-block', |
| 127 | 'wpforms-one-fifth', |
| 128 | ], |
| 129 | 'class' => [ |
| 130 | 'wpforms-field-name-middle', |
| 131 | ], |
| 132 | 'data' => [], |
| 133 | 'id' => "wpforms-{$form_id}-field_{$field_id}-middle", |
| 134 | 'required' => '', |
| 135 | 'sublabel' => [ |
| 136 | 'hidden' => ! empty( $field['sublabel_hide'] ), |
| 137 | 'value' => esc_html__( 'Middle', 'wpforms-lite' ), |
| 138 | ], |
| 139 | ], |
| 140 | 'last' => [ |
| 141 | 'attr' => [ |
| 142 | 'name' => "wpforms[fields][{$field_id}][last]", |
| 143 | 'value' => ! empty( $field['last_default'] ) |
| 144 | ? wpforms_process_smart_tags( $field['last_default'], $form_data, [], '', 'field-properties' ) |
| 145 | : '', |
| 146 | 'placeholder' => ! empty( $field['last_placeholder'] ) ? $field['last_placeholder'] : '', |
| 147 | ], |
| 148 | 'block' => [ |
| 149 | 'wpforms-field-row-block', |
| 150 | |
| 151 | ], |
| 152 | 'class' => [ |
| 153 | 'wpforms-field-name-last', |
| 154 | ], |
| 155 | 'data' => [], |
| 156 | 'id' => "wpforms-{$form_id}-field_{$field_id}-last", |
| 157 | 'required' => ! empty( $field['required'] ) ? 'required' : '', |
| 158 | 'sublabel' => [ |
| 159 | 'hidden' => ! empty( $field['sublabel_hide'] ), |
| 160 | 'value' => esc_html__( 'Last', 'wpforms-lite' ), |
| 161 | ], |
| 162 | ], |
| 163 | ], |
| 164 | ]; |
| 165 | |
| 166 | $properties = array_merge_recursive( $properties, $props ); |
| 167 | |
| 168 | $has_common_error = ! empty( $properties['error']['value'] ) && is_string( $properties['error']['value'] ); |
| 169 | |
| 170 | // Input First: add error class if needed. |
| 171 | if ( ! empty( $properties['error']['value']['first'] ) || $has_common_error ) { |
| 172 | $properties['inputs']['first']['class'][] = 'wpforms-error'; |
| 173 | } |
| 174 | |
| 175 | // Input First: add required class if needed. |
| 176 | if ( ! empty( $field['required'] ) ) { |
| 177 | $properties['inputs']['first']['class'][] = 'wpforms-field-required'; |
| 178 | } |
| 179 | |
| 180 | // Input First: add column class. |
| 181 | $properties['inputs']['first']['block'][] = $format === 'first-last' ? 'wpforms-one-half' : 'wpforms-two-fifths'; |
| 182 | |
| 183 | // Input Middle: add error class if needed. |
| 184 | if ( $has_common_error ) { |
| 185 | $properties['inputs']['middle']['class'][] = 'wpforms-error'; |
| 186 | } |
| 187 | |
| 188 | // Input Last: add error class if needed. |
| 189 | if ( ! empty( $properties['error']['value']['last'] ) || $has_common_error ) { |
| 190 | $properties['inputs']['last']['class'][] = 'wpforms-error'; |
| 191 | } |
| 192 | |
| 193 | // Input Last: add required class if needed. |
| 194 | if ( ! empty( $field['required'] ) ) { |
| 195 | $properties['inputs']['last']['class'][] = 'wpforms-field-required'; |
| 196 | } |
| 197 | |
| 198 | // Input Last: add column class. |
| 199 | $properties['inputs']['last']['block'][] = $format === 'first-last' ? 'wpforms-one-half' : 'wpforms-two-fifths'; |
| 200 | |
| 201 | return $properties; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Name fields should default to being required. |
| 206 | * |
| 207 | * @since 1.0.8 |
| 208 | * |
| 209 | * @param bool|mixed $required Whether the field is required. |
| 210 | * @param array $field Field data. |
| 211 | * |
| 212 | * @return bool |
| 213 | */ |
| 214 | public function default_required( $required, $field ): bool { |
| 215 | |
| 216 | if ( $field['type'] === 'name' ) { |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | return (bool) $required; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Field options panel inside the builder. |
| 225 | * |
| 226 | * @since 1.0.0 |
| 227 | * |
| 228 | * @param array $field Field information. |
| 229 | */ |
| 230 | public function field_options( $field ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh |
| 231 | |
| 232 | // Define data. |
| 233 | $format = ! empty( $field['format'] ) ? esc_attr( $field['format'] ) : 'first-last'; |
| 234 | |
| 235 | /* |
| 236 | * Basic field options. |
| 237 | */ |
| 238 | |
| 239 | // Options open markup. |
| 240 | $args = [ |
| 241 | 'markup' => 'open', |
| 242 | ]; |
| 243 | |
| 244 | $this->field_option( 'basic-options', $field, $args ); |
| 245 | |
| 246 | // Label. |
| 247 | $this->field_option( 'label', $field ); |
| 248 | |
| 249 | // Format. |
| 250 | $lbl = $this->field_element( |
| 251 | 'label', |
| 252 | $field, |
| 253 | [ |
| 254 | 'slug' => 'format', |
| 255 | 'value' => esc_html__( 'Format', 'wpforms-lite' ), |
| 256 | 'tooltip' => esc_html__( 'Select format to use for the name form field', 'wpforms-lite' ), |
| 257 | ], |
| 258 | false |
| 259 | ); |
| 260 | |
| 261 | $fld = $this->field_element( |
| 262 | 'select', |
| 263 | $field, |
| 264 | [ |
| 265 | 'slug' => 'format', |
| 266 | 'value' => $format, |
| 267 | 'options' => [ |
| 268 | 'simple' => esc_html__( 'Simple', 'wpforms-lite' ), |
| 269 | 'first-last' => esc_html__( 'First Last', 'wpforms-lite' ), |
| 270 | 'first-middle-last' => esc_html__( 'First Middle Last', 'wpforms-lite' ), |
| 271 | ], |
| 272 | ], |
| 273 | false |
| 274 | ); |
| 275 | |
| 276 | $args = [ |
| 277 | 'slug' => 'format', |
| 278 | 'content' => $lbl . $fld, |
| 279 | ]; |
| 280 | |
| 281 | $this->field_element( 'row', $field, $args ); |
| 282 | |
| 283 | // Description. |
| 284 | $this->field_option( 'description', $field ); |
| 285 | |
| 286 | // Required toggle. |
| 287 | $this->field_option( 'required', $field ); |
| 288 | |
| 289 | // Options close markup. |
| 290 | $args = [ |
| 291 | 'markup' => 'close', |
| 292 | ]; |
| 293 | |
| 294 | $this->field_option( 'basic-options', $field, $args ); |
| 295 | |
| 296 | /* |
| 297 | * Advanced field options. |
| 298 | */ |
| 299 | |
| 300 | // Options open markup. |
| 301 | $args = [ |
| 302 | 'markup' => 'open', |
| 303 | ]; |
| 304 | |
| 305 | $this->field_option( 'advanced-options', $field, $args ); |
| 306 | |
| 307 | // Size. |
| 308 | $this->field_option( 'size', $field ); |
| 309 | |
| 310 | echo '<div class="format-selected-' . esc_attr( $format ) . ' format-selected">'; |
| 311 | |
| 312 | // Simple. |
| 313 | $simple_placeholder = ! empty( $field['simple_placeholder'] ) ? esc_attr( $field['simple_placeholder'] ) : ''; |
| 314 | $simple_default = ! empty( $field['simple_default'] ) ? esc_attr( $field['simple_default'] ) : ''; |
| 315 | |
| 316 | printf( '<div class="wpforms-clear wpforms-field-option-row wpforms-field-option-row-simple" id="wpforms-field-option-row-%d-simple" data-subfield="simple" data-field-id="%d">', esc_attr( $field['id'] ), esc_attr( $field['id'] ) ); |
| 317 | $this->field_element( |
| 318 | 'label', |
| 319 | $field, |
| 320 | [ |
| 321 | 'slug' => 'simple_placeholder', |
| 322 | 'value' => esc_html__( 'Name', 'wpforms-lite' ), |
| 323 | 'tooltip' => esc_html__( 'Name field advanced options.', 'wpforms-lite' ), |
| 324 | ] |
| 325 | ); |
| 326 | echo '<div class="wpforms-field-options-columns-2 wpforms-field-options-columns">'; |
| 327 | echo '<div class="placeholder wpforms-field-options-column">'; |
| 328 | printf( '<input type="text" class="placeholder" id="wpforms-field-option-%d-simple_placeholder" name="fields[%d][simple_placeholder]" value="%s">', (int) $field['id'], (int) $field['id'], esc_attr( $simple_placeholder ) ); |
| 329 | printf( '<label for="wpforms-field-option-%d-simple_placeholder" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Placeholder', 'wpforms-lite' ) ); |
| 330 | echo '</div>'; |
| 331 | echo '<div class="default wpforms-field-options-column">'; |
| 332 | printf( '<input type="text" class="default wpforms-smart-tags-enabled" id="wpforms-field-option-%d-simple_default" name="fields[%d][simple_default]" data-type="other" value="%s">', (int) $field['id'], (int) $field['id'], esc_attr( $simple_default ) ); |
| 333 | printf( '<label for="wpforms-field-option-%d-simple_default" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Default Value', 'wpforms-lite' ) ); |
| 334 | echo '</div>'; |
| 335 | echo '</div>'; |
| 336 | echo '</div>'; |
| 337 | |
| 338 | // First. |
| 339 | $first_placeholder = ! empty( $field['first_placeholder'] ) ? esc_attr( $field['first_placeholder'] ) : ''; |
| 340 | $first_default = ! empty( $field['first_default'] ) ? esc_attr( $field['first_default'] ) : ''; |
| 341 | |
| 342 | printf( '<div class="wpforms-clear wpforms-field-option-row wpforms-field-option-row-first" id="wpforms-field-option-row-%d-first" data-subfield="first-name" data-field-id="%d">', esc_attr( $field['id'] ), esc_attr( $field['id'] ) ); |
| 343 | $this->field_element( |
| 344 | 'label', |
| 345 | $field, |
| 346 | [ |
| 347 | 'slug' => 'first_placeholder', |
| 348 | 'value' => esc_html__( 'First Name', 'wpforms-lite' ), |
| 349 | 'tooltip' => esc_html__( 'First name field advanced options.', 'wpforms-lite' ), |
| 350 | ] |
| 351 | ); |
| 352 | echo '<div class="wpforms-field-options-columns-2 wpforms-field-options-columns">'; |
| 353 | echo '<div class="placeholder wpforms-field-options-column">'; |
| 354 | printf( '<input type="text" class="placeholder" id="wpforms-field-option-%1$d-first_placeholder" name="fields[%1$d][first_placeholder]" value="%2$s">', (int) $field['id'], esc_attr( $first_placeholder ) ); |
| 355 | printf( '<label for="wpforms-field-option-%d-first_placeholder" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Placeholder', 'wpforms-lite' ) ); |
| 356 | echo '</div>'; |
| 357 | echo '<div class="default wpforms-field-options-column">'; |
| 358 | printf( '<input type="text" class="default wpforms-smart-tags-enabled" id="wpforms-field-option-%1$d-first_default" name="fields[%1$d][first_default]" data-type="other" value="%2$s">', (int) $field['id'], esc_attr( $first_default ) ); |
| 359 | printf( '<label for="wpforms-field-option-%d-first_default" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Default Value', 'wpforms-lite' ) ); |
| 360 | echo '</div>'; |
| 361 | echo '</div>'; |
| 362 | echo '</div>'; |
| 363 | |
| 364 | // Middle. |
| 365 | $middle_placeholder = ! empty( $field['middle_placeholder'] ) ? esc_attr( $field['middle_placeholder'] ) : ''; |
| 366 | $middle_default = ! empty( $field['middle_default'] ) ? esc_attr( $field['middle_default'] ) : ''; |
| 367 | |
| 368 | printf( '<div class="wpforms-clear wpforms-field-option-row wpforms-field-option-row-middle" id="wpforms-field-option-row-%d-middle" data-subfield="middle-name" data-field-id="%d">', esc_attr( $field['id'] ), esc_attr( $field['id'] ) ); |
| 369 | $this->field_element( |
| 370 | 'label', |
| 371 | $field, |
| 372 | [ |
| 373 | 'slug' => 'middle_placeholder', |
| 374 | 'value' => esc_html__( 'Middle Name', 'wpforms-lite' ), |
| 375 | 'tooltip' => esc_html__( 'Middle name field advanced options.', 'wpforms-lite' ), |
| 376 | ] |
| 377 | ); |
| 378 | echo '<div class="wpforms-field-options-columns-2 wpforms-field-options-columns">'; |
| 379 | echo '<div class="placeholder wpforms-field-options-column">'; |
| 380 | printf( '<input type="text" class="placeholder" id="wpforms-field-option-%1$d-middle_placeholder" name="fields[%1$d][middle_placeholder]" value="%2$s">', (int) $field['id'], esc_attr( $middle_placeholder ) ); |
| 381 | printf( '<label for="wpforms-field-option-%d-middle_placeholder" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Placeholder', 'wpforms-lite' ) ); |
| 382 | echo '</div>'; |
| 383 | echo '<div class="default wpforms-field-options-column">'; |
| 384 | printf( '<input type="text" class="default wpforms-smart-tags-enabled" id="wpforms-field-option-%1$d-middle_default" name="fields[%1$d][middle_default]" data-type="other" value="%2$s">', (int) $field['id'], esc_attr( $middle_default ) ); |
| 385 | printf( '<label for="wpforms-field-option-%d-middle_default" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Default Value', 'wpforms-lite' ) ); |
| 386 | echo '</div>'; |
| 387 | echo '</div>'; |
| 388 | echo '</div>'; |
| 389 | |
| 390 | // Last. |
| 391 | $last_placeholder = ! empty( $field['last_placeholder'] ) ? esc_attr( $field['last_placeholder'] ) : ''; |
| 392 | $last_default = ! empty( $field['last_default'] ) ? esc_attr( $field['last_default'] ) : ''; |
| 393 | |
| 394 | printf( '<div class="wpforms-clear wpforms-field-option-row wpforms-field-option-row-last" id="wpforms-field-option-row-%d-last" data-subfield="last-name" data-field-id="%d">', esc_attr( $field['id'] ), esc_attr( $field['id'] ) ); |
| 395 | $this->field_element( |
| 396 | 'label', |
| 397 | $field, |
| 398 | [ |
| 399 | 'slug' => 'last_placeholder', |
| 400 | 'value' => esc_html__( 'Last Name', 'wpforms-lite' ), |
| 401 | 'tooltip' => esc_html__( 'Last name field advanced options.', 'wpforms-lite' ), |
| 402 | ] |
| 403 | ); |
| 404 | echo '<div class="wpforms-field-options-columns-2 wpforms-field-options-columns">'; |
| 405 | echo '<div class="placeholder wpforms-field-options-column">'; |
| 406 | printf( '<input type="text" class="placeholder" id="wpforms-field-option-%1$d-last_placeholder" name="fields[%1$d][last_placeholder]" value="%2$s">', (int) $field['id'], esc_attr( $last_placeholder ) ); |
| 407 | printf( '<label for="wpforms-field-option-%d-last_placeholder" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Placeholder', 'wpforms-lite' ) ); |
| 408 | echo '</div>'; |
| 409 | echo '<div class="default wpforms-field-options-column">'; |
| 410 | printf( '<input type="text" class="default wpforms-smart-tags-enabled" id="wpforms-field-option-%1$d-last_default" name="fields[%1$d][last_default]" data-type="other" value="%2$s">', (int) $field['id'], esc_attr( $last_default ) ); |
| 411 | printf( '<label for="wpforms-field-option-%d-last_default" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Default Value', 'wpforms-lite' ) ); |
| 412 | echo '</div>'; |
| 413 | echo '</div>'; |
| 414 | echo '</div>'; |
| 415 | |
| 416 | echo '</div>'; |
| 417 | |
| 418 | // Custom CSS classes. |
| 419 | $this->field_option( 'css', $field ); |
| 420 | |
| 421 | // Hide Label. |
| 422 | $this->field_option( 'label_hide', $field ); |
| 423 | |
| 424 | // Hide sublabels. |
| 425 | $sublabel_class = isset( $field['format'] ) && ! in_array( $field['format'], [ 'first-last', 'first-middle-last' ], true ) ? 'wpforms-hidden' : ''; |
| 426 | |
| 427 | $this->field_option( 'sublabel_hide', $field, [ 'class' => $sublabel_class ] ); |
| 428 | |
| 429 | // Options close markup. |
| 430 | $args = [ |
| 431 | 'markup' => 'close', |
| 432 | ]; |
| 433 | |
| 434 | $this->field_option( 'advanced-options', $field, $args ); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Field preview inside the builder. |
| 439 | * |
| 440 | * @since 1.0.0 |
| 441 | * |
| 442 | * @param array $field Field information. |
| 443 | */ |
| 444 | public function field_preview( $field ) { |
| 445 | |
| 446 | // Define data. |
| 447 | $simple_placeholder = ! empty( $field['simple_placeholder'] ) ? $field['simple_placeholder'] : ''; |
| 448 | $first_placeholder = ! empty( $field['first_placeholder'] ) ? $field['first_placeholder'] : ''; |
| 449 | $middle_placeholder = ! empty( $field['middle_placeholder'] ) ? $field['middle_placeholder'] : ''; |
| 450 | $last_placeholder = ! empty( $field['last_placeholder'] ) ? $field['last_placeholder'] : ''; |
| 451 | $simple_default = ! empty( $field['simple_default'] ) ? $field['simple_default'] : ''; |
| 452 | $first_default = ! empty( $field['first_default'] ) ? $field['first_default'] : ''; |
| 453 | $middle_default = ! empty( $field['middle_default'] ) ? $field['middle_default'] : ''; |
| 454 | $last_default = ! empty( $field['last_default'] ) ? $field['last_default'] : ''; |
| 455 | $format = ! empty( $field['format'] ) ? $field['format'] : 'first-last'; |
| 456 | |
| 457 | // Label. |
| 458 | $this->field_preview_option( 'label', $field ); |
| 459 | ?> |
| 460 | |
| 461 | <div class="format-selected-<?php echo sanitize_html_class( $format ); ?> format-selected wpforms-clear"> |
| 462 | |
| 463 | <div class="wpforms-simple"> |
| 464 | <input type="text" placeholder="<?php echo esc_attr( $simple_placeholder ); ?>" value="<?php echo esc_attr( $simple_default ); ?>" class="primary-input" readonly> |
| 465 | </div> |
| 466 | |
| 467 | <div class="wpforms-first-name"> |
| 468 | <input type="text" placeholder="<?php echo esc_attr( $first_placeholder ); ?>" value="<?php echo esc_attr( $first_default ); ?>" class="primary-input" readonly> |
| 469 | <label class="wpforms-sub-label"><?php esc_html_e( 'First', 'wpforms-lite' ); ?></label> |
| 470 | </div> |
| 471 | |
| 472 | <div class="wpforms-middle-name"> |
| 473 | <input type="text" placeholder="<?php echo esc_attr( $middle_placeholder ); ?>" value="<?php echo esc_attr( $middle_default ); ?>" class="primary-input" readonly> |
| 474 | <label class="wpforms-sub-label"><?php esc_html_e( 'Middle', 'wpforms-lite' ); ?></label> |
| 475 | </div> |
| 476 | |
| 477 | <div class="wpforms-last-name"> |
| 478 | <input type="text" placeholder="<?php echo esc_attr( $last_placeholder ); ?>" value="<?php echo esc_attr( $last_default ); ?>" class="primary-input" readonly> |
| 479 | <label class="wpforms-sub-label"><?php esc_html_e( 'Last', 'wpforms-lite' ); ?></label> |
| 480 | </div> |
| 481 | |
| 482 | </div> |
| 483 | |
| 484 | <?php |
| 485 | // Description. |
| 486 | $this->field_preview_option( 'description', $field ); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Field display on the form front-end. |
| 491 | * |
| 492 | * @since 1.0.0 |
| 493 | * |
| 494 | * @param array $field Field information. |
| 495 | * @param array $deprecated Deprecated parameter, not used anymore. |
| 496 | * @param array $form_data Form data and settings. |
| 497 | * |
| 498 | * @noinspection HtmlUnknownAttribute |
| 499 | */ |
| 500 | public function field_display( $field, $deprecated, $form_data ) { |
| 501 | |
| 502 | // Define data. |
| 503 | $format = ! empty( $field['format'] ) ? esc_attr( $field['format'] ) : 'first-last'; |
| 504 | $primary = ! empty( $field['properties']['inputs']['primary'] ) ? $field['properties']['inputs']['primary'] : ''; |
| 505 | $first = ! empty( $field['properties']['inputs']['first'] ) ? $field['properties']['inputs']['first'] : ''; |
| 506 | $middle = ! empty( $field['properties']['inputs']['middle'] ) ? $field['properties']['inputs']['middle'] : ''; |
| 507 | $last = ! empty( $field['properties']['inputs']['last'] ) ? $field['properties']['inputs']['last'] : ''; |
| 508 | |
| 509 | // Simple format. |
| 510 | if ( $format === 'simple' ) { |
| 511 | |
| 512 | // Primary field (Simple). |
| 513 | printf( |
| 514 | '<input type="text" %s %s>', |
| 515 | wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 516 | esc_attr( $primary['required'] ) |
| 517 | ); |
| 518 | |
| 519 | // Expanded formats. |
| 520 | } else { |
| 521 | |
| 522 | // Row wrapper. |
| 523 | echo '<div class="wpforms-field-row wpforms-field-' . sanitize_html_class( $field['size'] ) . '">'; |
| 524 | |
| 525 | // First name. |
| 526 | echo '<div ' . wpforms_html_attributes( false, $first['block'] ) . '>'; |
| 527 | $this->field_display_sublabel( 'first', 'before', $field ); |
| 528 | printf( |
| 529 | '<input type="text" %s %s>', |
| 530 | wpforms_html_attributes( $first['id'], $first['class'], $first['data'], $first['attr'] ), |
| 531 | esc_attr( $first['required'] ) |
| 532 | ); |
| 533 | $this->field_display_sublabel( 'first', 'after', $field ); |
| 534 | $this->field_display_error( 'first', $field ); |
| 535 | echo '</div>'; |
| 536 | |
| 537 | // Middle name. |
| 538 | if ( $format === 'first-middle-last' ) { |
| 539 | echo '<div ' . wpforms_html_attributes( false, $middle['block'] ) . '>'; |
| 540 | $this->field_display_sublabel( 'middle', 'before', $field ); |
| 541 | printf( |
| 542 | '<input type="text" %s %s>', |
| 543 | wpforms_html_attributes( $middle['id'], $middle['class'], $middle['data'], $middle['attr'] ), |
| 544 | esc_attr( $middle['required'] ) |
| 545 | ); |
| 546 | $this->field_display_sublabel( 'middle', 'after', $field ); |
| 547 | $this->field_display_error( 'middle', $field ); |
| 548 | echo '</div>'; |
| 549 | } |
| 550 | |
| 551 | // Last name. |
| 552 | echo '<div ' . wpforms_html_attributes( false, $last['block'] ) . '>'; |
| 553 | $this->field_display_sublabel( 'last', 'before', $field ); |
| 554 | printf( |
| 555 | '<input type="text" %s %s>', |
| 556 | wpforms_html_attributes( $last['id'], $last['class'], $last['data'], $last['attr'] ), |
| 557 | esc_attr( $last['required'] ) |
| 558 | ); |
| 559 | $this->field_display_sublabel( 'last', 'after', $field ); |
| 560 | $this->field_display_error( 'last', $field ); |
| 561 | echo '</div>'; |
| 562 | |
| 563 | echo '</div>'; |
| 564 | |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Validate field on submitting a form. |
| 570 | * |
| 571 | * @since 1.0.0 |
| 572 | * |
| 573 | * @param int $field_id Field id. |
| 574 | * @param array|string $field_submit Submitted field value (raw data). |
| 575 | * @param array $form_data Form data. |
| 576 | */ |
| 577 | public function validate( $field_id, $field_submit, $form_data ) { |
| 578 | |
| 579 | if ( empty( $form_data['fields'][ $field_id ]['required'] ) ) { |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | // Extended validation needed for the different name fields. |
| 584 | $form_id = $form_data['id']; |
| 585 | $format = $form_data['fields'][ $field_id ]['format']; |
| 586 | $required = wpforms_get_required_label(); |
| 587 | $process = wpforms()->obj( 'process' ); |
| 588 | |
| 589 | if ( $format === 'simple' && wpforms_is_empty_string( $field_submit ) ) { |
| 590 | $process->errors[ $form_id ][ $field_id ] = $required; |
| 591 | |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | if ( ! ( $format === 'first-last' || $format === 'first-middle-last' ) ) { |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | $this->validate_complicated_formats( $process, $form_id, $field_id, $field_submit, $required ); |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Format and sanitize field. |
| 604 | * |
| 605 | * @since 1.0.0 |
| 606 | * |
| 607 | * @param int $field_id Field ID. |
| 608 | * @param mixed $field_submit Field value that was submitted. |
| 609 | * @param array $form_data Form data and settings. |
| 610 | */ |
| 611 | public function format( $field_id, $field_submit, $form_data ) { |
| 612 | // Define data. |
| 613 | $name = isset( $form_data['fields'][ $field_id ]['label'] ) && ! wpforms_is_empty_string( $form_data['fields'][ $field_id ]['label'] ) ? $form_data['fields'][ $field_id ]['label'] : ''; |
| 614 | $first = isset( $field_submit['first'] ) && ! wpforms_is_empty_string( $field_submit['first'] ) ? $field_submit['first'] : ''; |
| 615 | $middle = isset( $field_submit['middle'] ) && ! wpforms_is_empty_string( $field_submit['middle'] ) ? $field_submit['middle'] : ''; |
| 616 | $last = isset( $field_submit['last'] ) && ! wpforms_is_empty_string( $field_submit['last'] ) ? $field_submit['last'] : ''; |
| 617 | |
| 618 | if ( is_array( $field_submit ) ) { |
| 619 | $value = implode( ' ', array_filter( [ $first, $middle, $last ] ) ); |
| 620 | } else { |
| 621 | $value = $field_submit; |
| 622 | } |
| 623 | |
| 624 | // Set final field details. |
| 625 | wpforms()->obj( 'process' )->fields[ $field_id ] = [ |
| 626 | 'name' => sanitize_text_field( $name ), |
| 627 | 'value' => sanitize_text_field( $value ), |
| 628 | 'id' => wpforms_validate_field_id( $field_id ), |
| 629 | 'type' => $this->type, |
| 630 | 'first' => sanitize_text_field( $first ), |
| 631 | 'middle' => sanitize_text_field( $middle ), |
| 632 | 'last' => sanitize_text_field( $last ), |
| 633 | ]; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Determine if the field requires fieldset+legend instead of the regular field label. |
| 638 | * |
| 639 | * @since 1.8.1 |
| 640 | * |
| 641 | * @param bool $requires_fieldset True if it requires fieldset. |
| 642 | * @param array $field Field data. |
| 643 | * |
| 644 | * @return bool |
| 645 | * |
| 646 | * @noinspection PhpUnusedParameterInspection |
| 647 | */ |
| 648 | public function is_field_requires_fieldset( $requires_fieldset, $field ) { |
| 649 | |
| 650 | return isset( $field['format'] ) && $field['format'] !== 'simple'; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Validate complicated formats. |
| 655 | * |
| 656 | * @since 1.8.2.3 |
| 657 | * |
| 658 | * @param WPForms_Process $process Process class instance. |
| 659 | * @param int|string $form_id Form id. |
| 660 | * @param int|string $field_id Field id. |
| 661 | * @param array $field_submit Field submit. |
| 662 | * @param string $required Required message text. |
| 663 | */ |
| 664 | private function validate_complicated_formats( $process, $form_id, $field_id, $field_submit, $required ) { |
| 665 | |
| 666 | // Prevent PHP Warning: Illegal string offset ‘first’ or 'last'. |
| 667 | if ( isset( $process->errors[ $form_id ][ $field_id ] ) ) { |
| 668 | $process->errors[ $form_id ][ $field_id ] = (array) $process->errors[ $form_id ][ $field_id ]; |
| 669 | } |
| 670 | |
| 671 | if ( isset( $field_submit['first'] ) && wpforms_is_empty_string( $field_submit['first'] ) ) { |
| 672 | $process->errors[ $form_id ][ $field_id ]['first'] = $required; |
| 673 | } |
| 674 | |
| 675 | if ( isset( $field_submit['last'] ) && wpforms_is_empty_string( $field_submit['last'] ) ) { |
| 676 | $process->errors[ $form_id ][ $field_id ]['last'] = $required; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | new WPForms_Field_Name(); |
| 682 |