-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // 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 ); | ||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| $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.
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).
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.
$fieldis a GF_Field; use object property access and key$valuescorrectly.Array access can throw warnings and skip your branch. Also ensure we key by the GF field id.
📝 Committable suggestion
🤖 Prompt for AI Agents