Keine Beschreibung

class-name.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php
  2. /**
  3. * Name text field.
  4. *
  5. * @since 1.0.0
  6. */
  7. class WPForms_Field_Name extends WPForms_Field {
  8. /**
  9. * Primary class constructor.
  10. *
  11. * @since 1.0.0
  12. */
  13. public function init() {
  14. // Define field type information.
  15. $this->name = esc_html__( 'Name', 'wpforms-lite' );
  16. $this->type = 'name';
  17. $this->icon = 'fa-user';
  18. $this->order = 150;
  19. // Define additional field properties.
  20. add_filter( 'wpforms_field_properties_name', array( $this, 'field_properties' ), 5, 3 );
  21. // Set field to default to required.
  22. add_filter( 'wpforms_field_new_required', array( $this, 'default_required' ), 10, 2 );
  23. }
  24. /**
  25. * Define additional field properties.
  26. *
  27. * @since 1.3.7
  28. *
  29. * @param array $properties Field properties.
  30. * @param array $field Field data and settings.
  31. * @param array $form_data Form data and settings.
  32. *
  33. * @return array
  34. */
  35. public function field_properties( $properties, $field, $form_data ) {
  36. $format = ! empty( $field['format'] ) ? esc_attr( $field['format'] ) : 'first-last';
  37. // Simple format.
  38. if ( 'simple' === $format ) {
  39. $properties['inputs']['primary']['attr']['placeholder'] = ! empty( $field['simple_placeholder'] ) ? $field['simple_placeholder'] : '';
  40. $properties['inputs']['primary']['attr']['value'] = ! empty( $field['simple_default'] ) ? wpforms_process_smart_tags( $field['simple_default'], $form_data ) : '';
  41. return $properties;
  42. }
  43. // Expanded formats.
  44. // Remove primary for expanded formats since we have first, middle, last.
  45. unset( $properties['inputs']['primary'] );
  46. $form_id = absint( $form_data['id'] );
  47. $field_id = absint( $field['id'] );
  48. $props = array(
  49. 'inputs' => array(
  50. 'first' => array(
  51. 'attr' => array(
  52. 'name' => "wpforms[fields][{$field_id}][first]",
  53. 'value' => ! empty( $field['first_default'] ) ? wpforms_process_smart_tags( $field['first_default'], $form_data ) : '',
  54. 'placeholder' => ! empty( $field['first_placeholder'] ) ? $field['first_placeholder'] : '',
  55. ),
  56. 'block' => array(
  57. 'wpforms-field-row-block',
  58. 'wpforms-first',
  59. ),
  60. 'class' => array(
  61. 'wpforms-field-name-first',
  62. ),
  63. 'data' => array(),
  64. 'id' => "wpforms-{$form_id}-field_{$field_id}",
  65. 'required' => ! empty( $field['required'] ) ? 'required' : '',
  66. 'sublabel' => array(
  67. 'hidden' => ! empty( $field['sublabel_hide'] ),
  68. 'value' => esc_html__( 'First', 'wpforms-lite' ),
  69. ),
  70. ),
  71. 'middle' => array(
  72. 'attr' => array(
  73. 'name' => "wpforms[fields][{$field_id}][middle]",
  74. 'value' => ! empty( $field['middle_default'] ) ? wpforms_process_smart_tags( $field['middle_default'], $form_data ) : '',
  75. 'placeholder' => ! empty( $field['middle_placeholder'] ) ? $field['middle_placeholder'] : '',
  76. ),
  77. 'block' => array(
  78. 'wpforms-field-row-block',
  79. 'wpforms-one-fifth',
  80. ),
  81. 'class' => array(
  82. 'wpforms-field-name-middle',
  83. ),
  84. 'data' => array(),
  85. 'id' => "wpforms-{$form_id}-field_{$field_id}-middle",
  86. 'required' => '',
  87. 'sublabel' => array(
  88. 'hidden' => ! empty( $field['sublabel_hide'] ),
  89. 'value' => esc_html__( 'Middle', 'wpforms-lite' ),
  90. ),
  91. ),
  92. 'last' => array(
  93. 'attr' => array(
  94. 'name' => "wpforms[fields][{$field_id}][last]",
  95. 'value' => ! empty( $field['last_default'] ) ? wpforms_process_smart_tags( $field['last_default'], $form_data ) : '',
  96. 'placeholder' => ! empty( $field['last_placeholder'] ) ? $field['last_placeholder'] : '',
  97. ),
  98. 'block' => array(
  99. 'wpforms-field-row-block',
  100. ),
  101. 'class' => array(
  102. 'wpforms-field-name-last',
  103. ),
  104. 'data' => array(),
  105. 'id' => "wpforms-{$form_id}-field_{$field_id}-last",
  106. 'required' => ! empty( $field['required'] ) ? 'required' : '',
  107. 'sublabel' => array(
  108. 'hidden' => ! empty( $field['sublabel_hide'] ),
  109. 'value' => esc_html__( 'Last', 'wpforms-lite' ),
  110. ),
  111. ),
  112. ),
  113. );
  114. $properties = array_merge_recursive( $properties, $props );
  115. $has_common_error = ! empty( $properties['error']['value'] ) && is_string( $properties['error']['value'] );
  116. // Input First: add error class if needed.
  117. if ( ! empty( $properties['error']['value']['first'] ) || $has_common_error ) {
  118. $properties['inputs']['first']['class'][] = 'wpforms-error';
  119. }
  120. // Input First: add required class if needed.
  121. if ( ! empty( $field['required'] ) ) {
  122. $properties['inputs']['first']['class'][] = 'wpforms-field-required';
  123. }
  124. // Input First: add column class.
  125. $properties['inputs']['first']['block'][] = 'first-last' === $format ? 'wpforms-one-half' : 'wpforms-two-fifths';
  126. // Input Middle: add error class if needed.
  127. if ( $has_common_error ) {
  128. $properties['inputs']['middle']['class'][] = 'wpforms-error';
  129. }
  130. // Input Last: add error class if needed.
  131. if ( ! empty( $properties['error']['value']['last'] ) || $has_common_error ) {
  132. $properties['inputs']['last']['class'][] = 'wpforms-error';
  133. }
  134. // Input Last: add required class if needed.
  135. if ( ! empty( $field['required'] ) ) {
  136. $properties['inputs']['last']['class'][] = 'wpforms-field-required';
  137. }
  138. // Input Last: add column class.
  139. $properties['inputs']['last']['block'][] = 'first-last' === $format ? 'wpforms-one-half' : 'wpforms-two-fifths';
  140. return $properties;
  141. }
  142. /**
  143. * Name fields should default to being required.
  144. *
  145. * @since 1.0.8
  146. *
  147. * @param bool $required
  148. * @param array $field
  149. *
  150. * @return bool
  151. */
  152. public function default_required( $required, $field ) {
  153. if ( 'name' === $field['type'] ) {
  154. return true;
  155. }
  156. return $required;
  157. }
  158. /**
  159. * Field options panel inside the builder.
  160. *
  161. * @since 1.0.0
  162. *
  163. * @param array $field
  164. */
  165. public function field_options( $field ) {
  166. // Define data.
  167. $format = ! empty( $field['format'] ) ? esc_attr( $field['format'] ) : 'first-last';
  168. /*
  169. * Basic field options.
  170. */
  171. // Options open markup.
  172. $args = array(
  173. 'markup' => 'open',
  174. );
  175. $this->field_option( 'basic-options', $field, $args );
  176. // Label.
  177. $this->field_option( 'label', $field );
  178. // Format.
  179. $lbl = $this->field_element(
  180. 'label',
  181. $field,
  182. array(
  183. 'slug' => 'format',
  184. 'value' => esc_html__( 'Format', 'wpforms-lite' ),
  185. 'tooltip' => esc_html__( 'Select format to use for the name form field', 'wpforms-lite' ),
  186. ),
  187. false
  188. );
  189. $fld = $this->field_element(
  190. 'select',
  191. $field,
  192. array(
  193. 'slug' => 'format',
  194. 'value' => $format,
  195. 'options' => array(
  196. 'simple' => esc_html__( 'Simple', 'wpforms-lite' ),
  197. 'first-last' => esc_html__( 'First Last', 'wpforms-lite' ),
  198. 'first-middle-last' => esc_html__( 'First Middle Last', 'wpforms-lite' ),
  199. ),
  200. ),
  201. false
  202. );
  203. $args = array(
  204. 'slug' => 'format',
  205. 'content' => $lbl . $fld,
  206. );
  207. $this->field_element( 'row', $field, $args );
  208. // Description.
  209. $this->field_option( 'description', $field );
  210. // Required toggle.
  211. $this->field_option( 'required', $field );
  212. // Options close markup.
  213. $args = array(
  214. 'markup' => 'close',
  215. );
  216. $this->field_option( 'basic-options', $field, $args );
  217. /*
  218. * Advanced field options.
  219. */
  220. // Options open markup.
  221. $args = array(
  222. 'markup' => 'open',
  223. );
  224. $this->field_option( 'advanced-options', $field, $args );
  225. // Size.
  226. $this->field_option( 'size', $field );
  227. echo '<div class="format-selected-' . $format . ' format-selected">';
  228. // Simple.
  229. $simple_placeholder = ! empty( $field['simple_placeholder'] ) ? esc_attr( $field['simple_placeholder'] ) : '';
  230. $simple_default = ! empty( $field['simple_default'] ) ? esc_attr( $field['simple_default'] ) : '';
  231. 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">', $field['id'], $field['id'] );
  232. $this->field_element( 'label', $field, array( 'slug' => 'simple_placeholder', 'value' => esc_html__( 'Name', 'wpforms-lite' ), 'tooltip' => esc_html__( 'Name field advanced options.', 'wpforms-lite' ) ) );
  233. echo '<div class="wpforms-field-options-columns-2 wpforms-field-options-columns">';
  234. echo '<div class="placeholder wpforms-field-options-column">';
  235. 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 ) );
  236. printf( '<label for="wpforms-field-option-%d-simple_placeholder" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Placeholder', 'wpforms-lite' ) );
  237. echo '</div>';
  238. echo '<div class="default wpforms-field-options-column">';
  239. printf( '<input type="text" class="default" id="wpforms-field-option-%d-simple_default" name="fields[%d][simple_default]" value="%s">', (int) $field['id'], (int) $field['id'], esc_attr( $simple_default ) );
  240. printf( '<label for="wpforms-field-option-%d-simple_default" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Default Value', 'wpforms-lite' ) );
  241. echo '</div>';
  242. echo '</div>';
  243. echo '</div>';
  244. // First.
  245. $first_placeholder = ! empty( $field['first_placeholder'] ) ? esc_attr( $field['first_placeholder'] ) : '';
  246. $first_default = ! empty( $field['first_default'] ) ? esc_attr( $field['first_default'] ) : '';
  247. 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">', $field['id'], $field['id'] );
  248. $this->field_element( 'label', $field, array( 'slug' => 'first_placeholder', 'value' => esc_html__( 'First Name', 'wpforms-lite' ), 'tooltip' => esc_html__( 'First name field advanced options.', 'wpforms-lite' ) ) );
  249. echo '<div class="wpforms-field-options-columns-2 wpforms-field-options-columns">';
  250. echo '<div class="placeholder wpforms-field-options-column">';
  251. 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 ) );
  252. printf( '<label for="wpforms-field-option-%d-first_placeholder" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Placeholder', 'wpforms-lite' ) );
  253. echo '</div>';
  254. echo '<div class="default wpforms-field-options-column">';
  255. printf( '<input type="text" class="default" id="wpforms-field-option-%1$d-first_default" name="fields[%1$d][first_default]" value="%2$s">', (int) $field['id'], esc_attr( $first_default ) );
  256. printf( '<label for="wpforms-field-option-%d-first_default" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Default Value', 'wpforms-lite' ) );
  257. echo '</div>';
  258. echo '</div>';
  259. echo '</div>';
  260. // Middle.
  261. $middle_placeholder = ! empty( $field['middle_placeholder'] ) ? esc_attr( $field['middle_placeholder'] ) : '';
  262. $middle_default = ! empty( $field['middle_default'] ) ? esc_attr( $field['middle_default'] ) : '';
  263. 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">', $field['id'], $field['id'] );
  264. $this->field_element( 'label', $field, array( 'slug' => 'middle_placeholder', 'value' => esc_html__( 'Middle Name', 'wpforms-lite' ), 'tooltip' => esc_html__( 'Middle name field advanced options.', 'wpforms-lite' ) ) );
  265. echo '<div class="wpforms-field-options-columns-2 wpforms-field-options-columns">';
  266. echo '<div class="placeholder wpforms-field-options-column">';
  267. 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 ) );
  268. printf( '<label for="wpforms-field-option-%d-middle_placeholder" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Placeholder', 'wpforms-lite' ) );
  269. echo '</div>';
  270. echo '<div class="default wpforms-field-options-column">';
  271. printf( '<input type="text" class="default" id="wpforms-field-option-%1$d-middle_default" name="fields[%1$d][middle_default]" value="%2$s">', (int) $field['id'], esc_attr( $middle_default ) );
  272. printf( '<label for="wpforms-field-option-%d-middle_default" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Default Value', 'wpforms-lite' ) );
  273. echo '</div>';
  274. echo '</div>';
  275. echo '</div>';
  276. // Last.
  277. $last_placeholder = ! empty( $field['last_placeholder'] ) ? esc_attr( $field['last_placeholder'] ) : '';
  278. $last_default = ! empty( $field['last_default'] ) ? esc_attr( $field['last_default'] ) : '';
  279. 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">', $field['id'], $field['id'] );
  280. $this->field_element( 'label', $field, array( 'slug' => 'last_placeholder', 'value' => esc_html__( 'Last Name', 'wpforms-lite' ), 'tooltip' => esc_html__( 'Last name field advanced options.', 'wpforms-lite' ) ) );
  281. echo '<div class="wpforms-field-options-columns-2 wpforms-field-options-columns">';
  282. echo '<div class="placeholder wpforms-field-options-column">';
  283. 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 ) );
  284. printf( '<label for="wpforms-field-option-%d-last_placeholder" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Placeholder', 'wpforms-lite' ) );
  285. echo '</div>';
  286. echo '<div class="default wpforms-field-options-column">';
  287. printf( '<input type="text" class="default" id="wpforms-field-option-%1$d-last_default" name="fields[%1$d][last_default]" value="%2$s">', (int) $field['id'], esc_attr( $last_default ) );
  288. printf( '<label for="wpforms-field-option-%d-last_default" class="sub-label">%s</label>', (int) $field['id'], esc_html__( 'Default Value', 'wpforms-lite' ) );
  289. echo '</div>';
  290. echo '</div>';
  291. echo '</div>';
  292. echo '</div>';
  293. // Custom CSS classes.
  294. $this->field_option( 'css', $field );
  295. // Hide Label.
  296. $this->field_option( 'label_hide', $field );
  297. // Hide sublabels.
  298. $this->field_option( 'sublabel_hide', $field );
  299. // Options close markup.
  300. $args = [
  301. 'markup' => 'close',
  302. ];
  303. $this->field_option( 'advanced-options', $field, $args );
  304. }
  305. /**
  306. * Field preview inside the builder.
  307. *
  308. * @since 1.0.0
  309. *
  310. * @param array $field Field information.
  311. */
  312. public function field_preview( $field ) {
  313. // Define data.
  314. $simple_placeholder = ! empty( $field['simple_placeholder'] ) ? esc_attr( $field['simple_placeholder'] ) : '';
  315. $first_placeholder = ! empty( $field['first_placeholder'] ) ? esc_attr( $field['first_placeholder'] ) : '';
  316. $middle_placeholder = ! empty( $field['middle_placeholder'] ) ? esc_attr( $field['middle_placeholder'] ) : '';
  317. $last_placeholder = ! empty( $field['last_placeholder'] ) ? esc_attr( $field['last_placeholder'] ) : '';
  318. $format = ! empty( $field['format'] ) ? esc_attr( $field['format'] ) : 'first-last';
  319. // Label.
  320. $this->field_preview_option( 'label', $field );
  321. ?>
  322. <div class="format-selected-<?php echo sanitize_html_class( $format ); ?> format-selected wpforms-clear">
  323. <div class="wpforms-simple">
  324. <input type="text" placeholder="<?php echo esc_attr( $simple_placeholder ); ?>" class="primary-input" readonly>
  325. </div>
  326. <div class="wpforms-first-name">
  327. <input type="text" placeholder="<?php echo esc_attr( $first_placeholder ); ?>" class="primary-input" readonly>
  328. <label class="wpforms-sub-label"><?php esc_html_e( 'First', 'wpforms-lite' ); ?></label>
  329. </div>
  330. <div class="wpforms-middle-name">
  331. <input type="text" placeholder="<?php echo esc_attr( $middle_placeholder ); ?>" class="primary-input" readonly>
  332. <label class="wpforms-sub-label"><?php esc_html_e( 'Middle', 'wpforms-lite' ); ?></label>
  333. </div>
  334. <div class="wpforms-last-name">
  335. <input type="text" placeholder="<?php echo esc_attr( $last_placeholder ); ?>" class="primary-input" readonly>
  336. <label class="wpforms-sub-label"><?php esc_html_e( 'Last', 'wpforms-lite' ); ?></label>
  337. </div>
  338. </div>
  339. <?php
  340. // Description.
  341. $this->field_preview_option( 'description', $field );
  342. }
  343. /**
  344. * Field display on the form front-end.
  345. *
  346. * @since 1.0.0
  347. *
  348. * @param array $field Field information.
  349. * @param array $deprecated Deprecated parameter, not used anymore.
  350. * @param array $form_data Form data and settings.
  351. */
  352. public function field_display( $field, $deprecated, $form_data ) {
  353. // Define data.
  354. $format = ! empty( $field['format'] ) ? esc_attr( $field['format'] ) : 'first-last';
  355. $primary = ! empty( $field['properties']['inputs']['primary'] ) ? $field['properties']['inputs']['primary'] : '';
  356. $first = ! empty( $field['properties']['inputs']['first'] ) ? $field['properties']['inputs']['first'] : '';
  357. $middle = ! empty( $field['properties']['inputs']['middle'] ) ? $field['properties']['inputs']['middle'] : '';
  358. $last = ! empty( $field['properties']['inputs']['last'] ) ? $field['properties']['inputs']['last'] : '';
  359. // Simple format.
  360. if ( 'simple' === $format ) {
  361. // Primary field (Simple).
  362. printf(
  363. '<input type="text" %s %s>',
  364. wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ),
  365. $primary['required']
  366. );
  367. // Expanded formats.
  368. } else {
  369. // Row wrapper.
  370. echo '<div class="wpforms-field-row wpforms-field-' . sanitize_html_class( $field['size'] ) . '">';
  371. // First name.
  372. echo '<div ' . wpforms_html_attributes( false, $first['block'] ) . '>';
  373. $this->field_display_sublabel( 'first', 'before', $field );
  374. printf(
  375. '<input type="text" %s %s>',
  376. wpforms_html_attributes( $first['id'], $first['class'], $first['data'], $first['attr'] ),
  377. $first['required']
  378. );
  379. $this->field_display_sublabel( 'first', 'after', $field );
  380. $this->field_display_error( 'first', $field );
  381. echo '</div>';
  382. // Middle name.
  383. if ( 'first-middle-last' === $format ) {
  384. echo '<div ' . wpforms_html_attributes( false, $middle['block'] ) . '>';
  385. $this->field_display_sublabel( 'middle', 'before', $field );
  386. printf(
  387. '<input type="text" %s %s>',
  388. wpforms_html_attributes( $middle['id'], $middle['class'], $middle['data'], $middle['attr'] ),
  389. $middle['required']
  390. );
  391. $this->field_display_sublabel( 'middle', 'after', $field );
  392. $this->field_display_error( 'middle', $field );
  393. echo '</div>';
  394. }
  395. // Last name.
  396. echo '<div ' . wpforms_html_attributes( false, $last['block'] ) . '>';
  397. $this->field_display_sublabel( 'last', 'before', $field );
  398. printf(
  399. '<input type="text" %s %s>',
  400. wpforms_html_attributes( $last['id'], $last['class'], $last['data'], $last['attr'] ),
  401. $last['required']
  402. );
  403. $this->field_display_sublabel( 'last', 'after', $field );
  404. $this->field_display_error( 'last', $field );
  405. echo '</div>';
  406. echo '</div>';
  407. }
  408. }
  409. /**
  410. * Validate field on form submit.
  411. *
  412. * @since 1.0.0
  413. *
  414. * @param int $field_id
  415. * @param array $field_submit
  416. * @param array $form_data
  417. */
  418. public function validate( $field_id, $field_submit, $form_data ) {
  419. // Extended validation needed for the different name fields.
  420. if ( ! empty( $form_data['fields'][ $field_id ]['required'] ) ) {
  421. $form_id = $form_data['id'];
  422. $format = $form_data['fields'][ $field_id ]['format'];
  423. $required = wpforms_get_required_label();
  424. if ( 'simple' === $format && empty( $field_submit ) ) {
  425. wpforms()->process->errors[ $form_id ][ $field_id ] = $required;
  426. }
  427. if ( ( 'first-last' === $format || 'first-middle-last' === $format ) && empty( $field_submit['first'] ) ) {
  428. wpforms()->process->errors[ $form_id ][ $field_id ]['first'] = $required;
  429. }
  430. if ( ( 'first-last' === $format || 'first-middle-last' === $format ) && empty( $field_submit['last'] ) ) {
  431. wpforms()->process->errors[ $form_id ][ $field_id ]['last'] = $required;
  432. }
  433. }
  434. }
  435. /**
  436. * Format and sanitize field.
  437. *
  438. * @since 1.0.0
  439. *
  440. * @param int $field_id
  441. * @param array $field_submit
  442. * @param array $form_data
  443. */
  444. public function format( $field_id, $field_submit, $form_data ) {
  445. // Define data.
  446. $name = ! empty( $form_data['fields'][ $field_id ]['label'] ) ? $form_data['fields'][ $field_id ]['label'] : '';
  447. $first = ! empty( $field_submit['first'] ) ? $field_submit['first'] : '';
  448. $middle = ! empty( $field_submit['middle'] ) ? $field_submit['middle'] : '';
  449. $last = ! empty( $field_submit['last'] ) ? $field_submit['last'] : '';
  450. if ( is_array( $field_submit ) ) {
  451. $value = implode( ' ', array_filter( array( $first, $middle, $last ) ) );
  452. } else {
  453. $value = $field_submit;
  454. }
  455. // Set final field details.
  456. wpforms()->process->fields[ $field_id ] = array(
  457. 'name' => sanitize_text_field( $name ),
  458. 'value' => sanitize_text_field( $value ),
  459. 'id' => absint( $field_id ),
  460. 'type' => $this->type,
  461. 'first' => sanitize_text_field( $first ),
  462. 'middle' => sanitize_text_field( $middle ),
  463. 'last' => sanitize_text_field( $last ),
  464. );
  465. }
  466. }
  467. new WPForms_Field_Name();