-
Notifications
You must be signed in to change notification settings - Fork 92
gpdtc-recalc.php: Added GravityView Support.
#1181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughRewrote parts of Changes
Sequence DiagramsequenceDiagram
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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. Comment |
There was a problem hiding this 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$calculatingby 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 ofGFAPI::update_entry_fieldand 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
📒 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.
| if ( $field['id'] !== $field_id ) { | ||
| $values[ $field['id'] ] = $value; | ||
| return $value; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$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.
| 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.
| $form = GFAPI::get_form( $entry['form_id'] ); | ||
| $_entry = $entry + $values; | ||
| $calculated_value = GFCommon::calculate( $field, $form, $_entry ); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| $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.
| // 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 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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). $entryis 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).
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/3060866235/88730
Summary
Added GravityView support.
BEFORE (Samuel):
https://www.loom.com/share/5a30f870bb42444f80d9abeaf984bf73
AFTER:
https://www.loom.com/share/82b35632ae034565a948df8da85dd102