Fix File Upload sync for Gravity Forms 2.10+ JSON storage format#16
Fix File Upload sync for Gravity Forms 2.10+ JSON storage format#16curtismchale wants to merge 2 commits into
Conversation
GF 2.10.0.1 standardized the File Upload field storage so single-file
fields are now stored as a JSON array (one element, with escaped slashes)
matching multi-file fields. The previous code only json_decoded when
$field->multipleFiles was true, so single-file values were treated as a
raw URL string and strpos('gravity_forms/') failed on the escaped slashes.
The GCS sync and URL rewrite were silently skipped, leaving entry meta
pointing at the local wp-content URL.
- Detect the field's storage shape from the value itself (JSON array vs
raw string) rather than from $field->multipleFiles. Normalize to an
array, run the existing sync loop, re-encode in the original shape so
older GF releases keep storing single-file values as a plain string.
- modify_db(): the JSON branch re-encoded the original $value instead of
the rewritten $result, silently dropping URL rewrites from the
Compatibility Files Sync tool. Tighten the JSON validity check to
require an array so json_decode'd scalars don't trip a PHP 8 warning
on foreach.
- Bump to 0.0.4 and update readme/changelog.
Refs udx#15
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates the plugin for Gravity Forms 2.10+ File Upload JSON storage changes and fixes an encoding bug in DB rewrite logic, along with version/readme/changelog bumps.
Changes:
- Update File Upload value handling to detect JSON array storage shape (GF 2.10+) and re-encode consistently.
- Fix
modify_db()to re-encode the rewritten JSON array (and require decoded JSON to be an array). - Bump stable tag and update readme/changelog notes for release 0.0.4.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| readme.txt | Bumps stable tag to 0.0.4, updates “Tested with” note, adds 0.0.4 changelog entry. |
| class-gravity-forms.php | Adds GF 2.10+ JSON-shape detection for file upload values; fixes JSON decode/encode flow in modify_db(). |
| changelog.txt | Adds 0.0.4 release notes mirroring the readme. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $decoded = is_string($value) ? json_decode($value, true) : null; | ||
| $was_json = is_array($decoded); | ||
|
|
||
| if ($was_json) { | ||
| $value = $decoded; | ||
| } else { | ||
| $value = array($value); | ||
| } |
There was a problem hiding this comment.
Good catch. Added an is_array($value) short-circuit at the top so a pre-decoded array input is treated as JSON-shape directly instead of being wrapped. Pushed in 6307706.
| = Notes = | ||
|
|
||
| * Tested with Gravity Forms plugin version 2.9.16.1 | ||
| * Tested with Gravity Forms plugin version 2.10.x |
There was a problem hiding this comment.
Fair — pinned to 2.10.2 (the version this fix was validated against on a live ProudCity site). 6307706.
Address PR review feedback on udx#16: - gform_save_field_value(): if an upstream filter already decoded the value to an array, treat it as JSON-shape instead of nesting it inside another array. The inner loop's is_string() guard already prevented breakage, but this keeps the re-encoded output in the expected shape. - readme.txt: replace the vague 2.10.x compatibility note with the version the fix was validated against (2.10.2).
Fixes #15
Summary
$field->multipleFiles. GF 2.10+ stores single-file fields as a JSON array too, so the old branch never decoded them andstrpos('gravity_forms/')failed on the JSON-escaped slashes — the GCS sync action was never fired and the URL was never rewritten.modify_db(): fix a re-encode bug where the JSON branch wrotewp_json_encode($value)(the original raw string) instead ofwp_json_encode($result)(the rewritten array), which silently dropped the Sync-tab URL rewrites. Also tighten the JSON validity check to require an array so ajson_decode'd scalar does not trip a PHP 8 warning onforeach.The
post_imagebranch is unchanged — that field type uses GF's legacy|:|-delimited string and was not affected by the 2.10 storage change.Validation
Verified against a live production site running Gravity Forms 2.10.2 with WP-Stateless. After deploying the fix, new File Upload submissions are uploaded to GCS and the entry meta is rewritten to the
storage.googleapis.com/<bucket>/gravity_forms/...URL. The "view file" link on the Entries screen serves the file correctly. Pre-existing entries created before the fix still hold the local URL (expected — the save filter only fires on submission).Test plan
storage.googleapis.com/...URL and the file exists in the bucket.wp_gf_entry_meta.