forked from gravitywiz/snippet-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpcc-copy-to-conditionally-hidden-fields.php
53 lines (41 loc) · 1.51 KB
/
gpcc-copy-to-conditionally-hidden-fields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* Gravity Perks // Copy Cat // Copy Values for Conditionally Hidden Fields
* https://gravitywiz.com/documentation/gravity-forms-copy-cat/
*
* By default, Gravity Forms does not capture values for fields hidden by conditional logic. Use this snippet to "copy"
* values to hidden fields on submission.
*/
add_action( 'gform_entry_post_save', function( $entry, $form ) {
if ( ! is_callable( 'gp_copy_cat' ) ) {
return $entry;
}
$orig_entry = $entry;
$triggers = gp_copy_cat()->get_copy_cat_fields( $form );
foreach ( $triggers as $trigger_field_id => $targets ) {
$is_trigger_hidden = GFFormsModel::is_field_hidden( $form, GFAPI::get_field( $form, $trigger_field_id ), array(), $entry );
if ( $is_trigger_hidden ) {
continue;
}
foreach ( $targets as $target ) {
$is_target_hidden = GFFormsModel::is_field_hidden( $form, GFAPI::get_field( $form, $target['target'] ), array(), $entry );
if ( ! $is_target_hidden ) {
continue;
}
$source_field = GFAPI::get_field( $form, $target['source'] );
$source_values = $source_field->get_value_submission( array() );
if ( is_array( $source_values ) ) {
foreach ( $source_values as $input_id => $source_value ) {
$target_input_id = str_replace( "{$source_field->id}.", "{$target['target']}.", $input_id );
$entry[ $target_input_id ] = $source_value;
}
} else {
$entry[ $target['target'] ] = $source_values;
}
}
}
if ( $orig_entry !== $entry ) {
GFAPI::update_entry( $entry );
}
return $entry;
}, 9, 2 );