Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions gp-date-time-calculator/gpdtc-recalc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,43 @@
add_action( 'wp_loaded', function() {

$form_id = 123; // Change this to the form's ID
$field_id = 4; // Change this to the Calculation field's ID.
$field_id = 4; // Change this to the Calculation field's ID.

$values = array();
$calculating = false;
$calculating = false; // Flag to prevent infinite recursion

add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values, $calculating ) {
if ( $calculating || $field['id'] !== $field_id ) {
add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values, &$calculating ) {
// Prevent infinite recursion
if ( $calculating ) {
return $value;
}

if ( $field['id'] !== $field_id ) {
$values[ $field['id'] ] = $value;
return $value;
}
Comment on lines +28 to 31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

$field is a GF_Field; use object property access and key $values correctly.

Array access can throw warnings and skip your branch. Also ensure we key by the GF field id.

-    if ( $field['id'] !== $field_id ) {
-      $values[ $field['id'] ] = $value;
+    if ( (int) $field->id !== (int) $field_id ) {
+      $values[ (string) $field->id ] = $value;
       return $value;
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if ( $field['id'] !== $field_id ) {
$values[ $field['id'] ] = $value;
return $value;
}
if ( (int) $field->id !== (int) $field_id ) {
$values[ (string) $field->id ] = $value;
return $value;
}
🤖 Prompt for AI Agents
In gp-date-time-calculator/gpdtc-recalc.php around lines 27–30, $field is a
GF_Field object so using array access ($field['id']) can emit warnings and skip
the branch; change the comparisons and indexing to use the object property
($field->id) and key the $values array by that id (e.g. $values[ $field->id ] =
$value), leaving the return $value intact.


// Set flag to prevent infinite recursion.
// Set flag to prevent recursion
$calculating = true;

$form = GFAPI::get_form( $entry['form_id'] );
$_entry = $entry + $values;
$calculated_value = GFCommon::calculate( $field, $form, $_entry );

// Reset flag.
GFAPI::update_entry_field( $_entry['id'], $field_id, $calculated_value );
// Reset flag
$calculating = false;

return GFCommon::calculate( $field, $form, $_entry );
return $calculated_value;
}, 10, 4 );

// GravityView Support.
add_filter( 'gravityview/field/number/value', function( $value, $field, $view, $form, $entry ) use ( $field_id, $form_id ) {
if ( $field->ID != $field_id && $form->ID != $form_id ) {
return $value;
}
$orig_entry = GFAPI::get_entry( $entry->ID );
return rgar( $orig_entry, $field_id );
}, 10, 5 );

} );