Skip to content

Conversation

@saifsultanc
Copy link
Contributor

@coderabbitai
Copy link

coderabbitai bot commented Oct 23, 2025

Walkthrough

Rewrote parts of gp-date-time-calculator/gpdtc-recalc.php to guard against recursive recalculation using an in-progress flag, compute the target field via GFCommon::calculate and return the computed value, and add a GravityView-specific filter to bypass recalculation and return the original field value when rendering.

Changes

Cohort / File(s) Summary
Recalculation flow & GravityView handling
gp-date-time-calculator/gpdtc-recalc.php
Added an in-progress recursion guard (by-reference flag) with early return; when target field matches, use GFCommon::calculate(entry, field) to obtain the value, update the entry field, and return the calculated result; added a GravityView filter to return the original pre-calculation value when rendering.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant Recalc as gpdtc-recalc.php
    participant GF as GFCommon
    participant GV as GravityViewFilter

    Caller->>Recalc: request recalculation (entry, field)
    alt recursion flag set
        Recalc-->>Caller: return early (already calculating)
    else normal path
        Recalc->>Recalc: set in-progress flag
        Recalc->>Recalc: is this the target field?
        alt is target
            rect rgb(230,245,250)
                Recalc->>GF: GFCommon::calculate(entry, field)
                GF-->>Recalc: calculated value
            end
            Recalc->>Recalc: update entry[field] with value
            Recalc-->>Caller: return calculated value
        else not target
            Recalc-->>Caller: continue other flow (unchanged)
        end
        Recalc->>Recalc: clear in-progress flag
    end

    rect rgb(245,235,255)
        Note over GV: On GravityView render the filter returns original pre-calc value
        GV->>GV: filter intercepts rendering for specific field/form
        GV-->>Caller: return original entry value
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review recursion guard correctness (scope/lifetime of the by-reference flag).
  • Verify GFCommon::calculate usage and entry field update semantics.
  • Confirm GravityView filter conditions correctly target intended field/form and do not bypass other needed logic.

Possibly related PRs

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: adding GravityView support to gpdtc-recalc.php, which directly corresponds to the changeset.
Description check ✅ Passed The description includes the required Context section with ticket link and Summary section with brief explanation. However, it lacks the optional Notion and Slack fields, though these appear non-critical.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch saif/fix/88730-fix-gview-support

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 73f2cdd and 083d492.

📒 Files selected for processing (1)
  • gp-date-time-calculator/gpdtc-recalc.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • gp-date-time-calculator/gpdtc-recalc.php

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Oct 23, 2025

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against 083d492

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (2)
gp-date-time-calculator/gpdtc-recalc.php (2)

22-26: Make recursion guard explicit by capturing $calculating by reference.

Today it relies on closure state; pass by reference to avoid ambiguity and ensure re-entrancy protection is shared across calls.

-add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values, $calculating ) {
+add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values, &$calculating ) {
   if ( $calculating ) {
     return $value;
   }

39-39: Check result of GFAPI::update_entry_field and fail safely.

Avoid silent failures; log or bail on errors.

-    GFAPI::update_entry_field( $_entry['id'], $field_id, $calculated_value );
+    $update = GFAPI::update_entry_field( rgar( $_entry, 'id' ), $field_id, $calculated_value );
+    if ( is_wp_error( $update ) ) {
+      if ( method_exists( 'GFCommon', 'log_error' ) ) {
+        GFCommon::log_error( 'GP DTC Recalc: update_entry_field failed: ' . $update->get_error_message() );
+      }
+      // Return original value on failure.
+      $calculating = false;
+      return $value;
+    }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 99b672c and 73f2cdd.

📒 Files selected for processing (1)
  • gp-date-time-calculator/gpdtc-recalc.php (1 hunks)
🔇 Additional comments (1)
gp-date-time-calculator/gpdtc-recalc.php (1)

44-44: Returning the calculated value is correct here.

This ensures the view gets the fresh value without re-triggering calculation.

Comment on lines +27 to 30
if ( $field['id'] !== $field_id ) {
$values[ $field['id'] ] = $value;
return $value;
}
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.

Comment on lines 35 to 38
$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.

Comment on lines 47 to 55
// 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 );
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).

@saifsultanc saifsultanc merged commit 3324f97 into master Nov 5, 2025
4 checks passed
@saifsultanc saifsultanc deleted the saif/fix/88730-fix-gview-support branch November 5, 2025 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants