Skip to content
Merged
Changes from 1 commit
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
27 changes: 22 additions & 5 deletions gp-date-time-calculator/gpdtc-recalc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,44 @@
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;

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 ) {
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.
$calculating = true;

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

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Use array_replace to overlay runtime $values onto $entry.

$entry + $values does not override existing keys; it keeps left-hand values, so your collected $values are ignored when keys exist.

-    $form             = GFAPI::get_form( $entry['form_id'] );
-    $_entry           = $entry + $values;
+    $form             = GFAPI::get_form( rgar( $entry, 'form_id' ) );
+    $_entry           = array_replace( $entry, $values );
     $calculated_value = GFCommon::calculate( $field, $form, $_entry );
📝 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
$form = GFAPI::get_form( $entry['form_id'] );
$_entry = $entry + $values;
$calculated_value = GFCommon::calculate( $field, $form, $_entry );
$form = GFAPI::get_form( rgar( $entry, 'form_id' ) );
$_entry = array_replace( $entry, $values );
$calculated_value = GFCommon::calculate( $field, $form, $_entry );
🤖 Prompt for AI Agents
In gp-date-time-calculator/gpdtc-recalc.php around lines 35 to 38, the code uses
"$_entry = $entry + $values" which preserves $entry's values and does not
override keys from $values; replace this with an array overlay using
array_replace so runtime $values override $entry (e.g. $_entry =
array_replace($entry, $values)), and ensure both $entry and $values are arrays
(cast or default to empty arrays) before calling array_replace.

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 );
Comment on lines 47 to 54
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

GravityView condition and parameter usage are off; fix field check, form check, and entry ID access.

  • Early return should be OR (handle only when both form and field match).
  • In GV, the GF field id is usually $field->field_id (not $field->ID).
  • $entry is often an array; access ['id'] defensively.
-  // 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 );
+  // GravityView Support.
+  add_filter( 'gravityview/field/number/value', function( $value, $field, $view, $form, $entry ) use ( $field_id, $form_id ) {
+    $gv_field_id = isset( $field->field_id ) ? (int) $field->field_id : ( isset( $field->ID ) ? (int) $field->ID : null );
+    if ( $gv_field_id !== (int) $field_id || (int) $form->ID !== (int) $form_id ) {
+      return $value;
+    }
+    $entry_id  = is_array( $entry ) ? (int) rgar( $entry, 'id' ) : ( isset( $entry->ID ) ? (int) $entry->ID : 0 );
+    if ( ! $entry_id ) {
+      return $value;
+    }
+    $orig_entry = GFAPI::get_entry( $entry_id );
+    return rgar( $orig_entry, (string) $field_id );
+  }, 10, 5 );
🤖 Prompt for AI Agents
In gp-date-time-calculator/gpdtc-recalc.php around lines 47–55, the GravityView
filter callback uses the wrong field/form properties and an incorrect
early-return logic; change the early-return to use OR so the callback only
proceeds when both field and form match, use the GF field identifier property
(check $field->field_id rather than $field->ID), robustly read the form id (use
$form->id or $form['id'] depending on the form shape) when comparing to
$form_id, and treat $entry defensively as an array (use $entry['id'] or
rgar($entry,'id')) when calling GFAPI::get_entry and then return
rgar($orig_entry, $field_id).


} );