|
14 | 14 | add_action( 'wp_loaded', function() { |
15 | 15 |
|
16 | 16 | $form_id = 123; // Change this to the form's ID |
17 | | - $field_id = 4; // Change this to the Calculation field's ID. |
| 17 | + $field_id = 4; // Change this to the Calculation field's ID. |
18 | 18 |
|
19 | 19 | $values = array(); |
20 | | - $calculating = false; |
| 20 | + $calculating = false; // Flag to prevent infinite recursion |
21 | 21 |
|
22 | | - add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values, $calculating ) { |
23 | | - if ( $calculating || $field['id'] !== $field_id ) { |
| 22 | + add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values, &$calculating ) { |
| 23 | + // Prevent infinite recursion |
| 24 | + if ( $calculating ) { |
| 25 | + return $value; |
| 26 | + } |
| 27 | + |
| 28 | + if ( $field['id'] !== $field_id ) { |
24 | 29 | $values[ $field['id'] ] = $value; |
25 | 30 | return $value; |
26 | 31 | } |
27 | 32 |
|
28 | | - // Set flag to prevent infinite recursion. |
| 33 | + // Set flag to prevent recursion |
29 | 34 | $calculating = true; |
30 | 35 |
|
31 | 36 | $form = GFAPI::get_form( $entry['form_id'] ); |
32 | 37 | $_entry = $entry + $values; |
| 38 | + $calculated_value = GFCommon::calculate( $field, $form, $_entry ); |
33 | 39 |
|
34 | | - // Reset flag. |
| 40 | + GFAPI::update_entry_field( $_entry['id'], $field_id, $calculated_value ); |
| 41 | + // Reset flag |
35 | 42 | $calculating = false; |
36 | 43 |
|
37 | | - return GFCommon::calculate( $field, $form, $_entry ); |
| 44 | + return $calculated_value; |
38 | 45 | }, 10, 4 ); |
39 | 46 |
|
| 47 | + // GravityView Support. |
| 48 | + add_filter( 'gravityview/field/number/value', function( $value, $field, $view, $form, $entry ) use ( $field_id, $form_id ) { |
| 49 | + if ( $field->ID != $field_id && $form->ID != $form_id ) { |
| 50 | + return $value; |
| 51 | + } |
| 52 | + $orig_entry = GFAPI::get_entry( $entry->ID ); |
| 53 | + return rgar( $orig_entry, $field_id ); |
| 54 | + }, 10, 5 ); |
| 55 | + |
40 | 56 | } ); |
0 commit comments