Skip to content

Commit 2e2a7bd

Browse files
committed
feat: wpforms full support checks
1 parent f494960 commit 2e2a7bd

2 files changed

Lines changed: 91 additions & 27 deletions

File tree

forms-bridge/integrations/formidable/class-formidable-integration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ private function serialize_field( $field ) {
331331
$type = 'mixed';
332332
break;
333333
case 'checkbox':
334-
if ( is_array( $field->options ) && 1 === count( $field->options ) && '1' === ( $field->options[0]['value'] ?? false ) ) {
334+
if ( 1 === count( $options ) && '1' === $options[0]['value'] ) {
335335
$type = 'checkbox';
336336
} else {
337337
$type = 'select';
@@ -638,7 +638,7 @@ private function format_value( $value, $field ) {
638638
case 'number':
639639
return (float) $value;
640640
case 'checkbox':
641-
$value = maybe_unserialize( $value );
641+
$value = maybe_unserialize( $value );
642642
$is_boolean = 'checkbox' === $field['type'];
643643

644644
if ( is_array( $value ) ) {

forms-bridge/integrations/wpforms/class-wpforms-integration.php

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,15 @@ private function serialize_field( $field, $fields = array(), $all_fields = array
362362
case 'payment-multiple':
363363
case 'payment-checkbox':
364364
case 'select':
365-
case 'checkbox':
366365
$type = 'select';
367366
break;
367+
case 'checkbox':
368+
if ( 1 === count( $options ) && '1' === $options[0]['value'] ) {
369+
$type = 'checkbox';
370+
} else {
371+
$type = 'select';
372+
}
373+
break;
368374
case 'number-slider':
369375
case 'number':
370376
$type = 'number';
@@ -402,11 +408,11 @@ private function serialize_field( $field, $fields = array(), $all_fields = array
402408
'required' => '1' === ( $field['required'] ?? '' ),
403409
'options' => $options,
404410
'is_file' => 'file-upload' === $field['type'],
405-
'is_multi' => $this->is_multi_field( $field ),
411+
'is_multi' => $this->is_multi_field( $field, $type ),
406412
'conditional' => false,
407413
'format' => $format,
408414
'children' => array_values( $children ),
409-
'schema' => $this->field_value_schema( $field, $children ),
415+
'schema' => $this->field_value_schema( $field, $children, $type ),
410416
'basetype' => $field['type'],
411417
),
412418
$field,
@@ -417,12 +423,17 @@ private function serialize_field( $field, $fields = array(), $all_fields = array
417423
/**
418424
* Checks if a filed is multi value field.
419425
*
420-
* @param array $field Target field instance.
426+
* @param array $field Target field instance.
427+
* @param string $norm_type Normalized field type.
421428
*
422429
* @return boolean
423430
*/
424-
private function is_multi_field( $field ) {
425-
if ( 'checkbox' === $field['type'] || 'repeater' === $field['type'] ) {
431+
private function is_multi_field( $field, $norm_type ) {
432+
if ( 'checkbox' === $field['type'] && 'select' === $norm_type ) {
433+
return true;
434+
}
435+
436+
if ( 'repeater' === $field['type'] ) {
426437
return true;
427438
}
428439

@@ -443,12 +454,13 @@ private function is_multi_field( $field ) {
443454
/**
444455
* Gets the field value JSON schema.
445456
*
446-
* @param array $field Field instance.
447-
* @param array $children Children fields.
457+
* @param array $field Field instance.
458+
* @param array $children Children fields.
459+
* @param string $norm_type Normalized field type.
448460
*
449461
* @return array JSON schema of the value of the field.
450462
*/
451-
private function field_value_schema( $field, $children = array() ) {
463+
private function field_value_schema( $field, $children = array(), $norm_type ) {
452464
switch ( $field['type'] ) {
453465
case 'name':
454466
case 'email':
@@ -483,6 +495,10 @@ private function field_value_schema( $field, $children = array() ) {
483495

484496
return array( 'type' => 'string' );
485497
case 'checkbox':
498+
if ( 'checkbox' === $norm_type ) {
499+
return array( 'type' => 'boolean' );
500+
}
501+
486502
$items = array();
487503
$count = count( $field['choices'] );
488504
for ( $i = 0; $i < $count; $i++ ) {
@@ -582,18 +598,28 @@ public function serialize_submission( $submission, $form_data ) {
582598
}
583599
}
584600

585-
foreach ( $submission['fields'] as $field ) {
586-
if ( 'file-upload' === $field['type'] ) {
601+
$submission_fields = array_values( $submission['fields'] );
602+
603+
foreach ( $form_data['fields'] as $field_data ) {
604+
if ( 'file' === $field_data['type'] ) {
587605
continue;
588606
}
589607

590-
$i = array_search(
591-
trim( $field['name'] ),
592-
array_column( $form_data['fields'], 'name' ),
608+
$index = array_search(
609+
trim( $field_data['name'] ),
610+
array_column( $submission_fields, 'name' ),
593611
true
594612
);
595613

596-
$field_data = $form_data['fields'][ $i ];
614+
if ( false === $index ) {
615+
if ( 'checkbox' === $field_data['type'] ) {
616+
$data[ $field_data['name'] ] = false;
617+
}
618+
619+
continue;
620+
}
621+
622+
$field = $submission_fields[ $index ];
597623

598624
$field['id'] = preg_replace( '/_\d+$/', '', $field['id'] );
599625
if ( isset( $fields_in_repeaters[ $field['id'] ] ) ) {
@@ -661,7 +687,7 @@ private function format_value( $field, $field_data ) {
661687

662688
if (
663689
'select' === $field_data['basetype'] ||
664-
'checkbox' === $field_data['basetype']
690+
'checkbox' === $field_data['basetype'] && 'select' === $field_data['type']
665691
) {
666692
if ( $field_data['is_multi'] ) {
667693
return array_map(
@@ -673,6 +699,10 @@ function ( $value ) {
673699
}
674700
}
675701

702+
if ( 'checkbox' === $field_data['basetype'] && 'checkbox' === $field_data['type'] ) {
703+
return (bool) $field['value'];
704+
}
705+
676706
if ( 'address' === $field_data['basetype'] ) {
677707
$post_values = $_POST['wpforms']['fields'][ $field['id'] ] ?? array();
678708
$field_values = array();
@@ -768,12 +798,13 @@ private function encode_form_data( $data ) {
768798
$wp_fields[ strval( $id ) ] = $this->textarea_field( ...$args );
769799
break;
770800
case 'hidden':
771-
if ( isset( $field['value'] ) ) {
801+
if ( $this->full_support() && isset( $field['value'] ) ) {
772802
if ( is_bool( $field['value'] ) ) {
773803
$field['value'] = $field['value'] ? '1' : '0';
774804
}
775805

776-
$args[] = (string) $field['value'];
806+
$args[] = (string) $field['value'];
807+
777808
$wp_fields[ strval( $id ) ] = $this->hidden_field( ...$args );
778809
}
779810

@@ -787,8 +818,11 @@ private function encode_form_data( $data ) {
787818
$wp_fields[ strval( $id ) ] = $this->checkbox_field( ...$args );
788819
break;
789820
case 'file':
790-
$args[] = $field['filetypes'] ?? '';
791-
$wp_fields[ strval( $id ) ] = $this->file_field( ...$args );
821+
if ( $this->full_support() ) {
822+
$args[] = $field['filetypes'] ?? '';
823+
$wp_fields[ strval( $id ) ] = $this->file_field( ...$args );
824+
}
825+
792826
break;
793827
case 'date':
794828
$wp_fields[ strval( $id ) ] = $this->date_field( ...$args );
@@ -808,12 +842,26 @@ private function encode_form_data( $data ) {
808842
$wp_fields[ strval( $id ) ] = $this->number_field( ...$args );
809843
break;
810844
case 'tel':
811-
$wp_fields[ strval( $id ) ] = $this->field_template(
812-
'phone',
813-
...$args
814-
);
845+
if ( ! $this->full_support() ) {
846+
$wp_field = $this->field_template( 'text', ...$args );
847+
} else {
848+
$wp_field = $this->field_template( 'phone', ...$args );
849+
}
850+
851+
$wp_fields[ strval( $id ) ] = $wp_field;
815852
break;
816853
case 'url':
854+
if ( ! $this->full_support() ) {
855+
$wp_field = array_merge(
856+
$this->field_template( 'text', ...$args ),
857+
array( 'input_mask' => 'alias:url' ),
858+
);
859+
} else {
860+
$wp_field = $this->field_template( 'url', ...$args );
861+
}
862+
863+
$wp_fields[ strval( $id ) ] = $wp_field;
864+
break;
817865
case 'email':
818866
default:
819867
$wp_fields[ strval( $id ) ] = $this->field_template(
@@ -918,7 +966,7 @@ private function checkbox_field( $id, $name, $required ) {
918966
'choices' => array(
919967
array(
920968
'label' => $name,
921-
'value' => __( 'Checked', 'forms-bridge' ),
969+
'value' => '1',
922970
'image' => '',
923971
'icon' => '',
924972
'icon_style' => 'regular',
@@ -956,6 +1004,13 @@ private function number_field( $id, $name, $required, $constraints ) {
9561004
* @return array
9571005
*/
9581006
private function date_field( $id, $name, $required ) {
1007+
if ( ! $this->full_support() ) {
1008+
return array_merge(
1009+
$this->field_template( 'text', $id, $name, $required ),
1010+
array( 'input_mask' => 'date:yyyy-mm-dd' ),
1011+
);
1012+
}
1013+
9591014
return array_merge(
9601015
$this->field_template( 'date-time', $id, $name, $required ),
9611016
array(
@@ -1084,6 +1139,15 @@ private function file_field( $id, $name, $required, $filetypes ) {
10841139
)
10851140
);
10861141
}
1142+
1143+
/**
1144+
* Returns true if the wpforms pro version is installed.
1145+
*
1146+
* @return boolean
1147+
*/
1148+
private function full_support() {
1149+
return (bool) wpforms()->is_pro();
1150+
}
10871151
}
10881152

10891153
WPForms_Integration::setup();

0 commit comments

Comments
 (0)