From 12da6afc1fbbc4b4922940a96d78436367577b93 Mon Sep 17 00:00:00 2001 From: Curtis McHale Date: Wed, 3 Jun 2026 11:54:07 -0700 Subject: [PATCH 1/2] Fix File Upload sync for Gravity Forms 2.10+ JSON storage format 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/wp-stateless-gravity-forms-addon#15 --- changelog.txt | 4 ++++ class-gravity-forms.php | 19 ++++++++++++++----- readme.txt | 8 ++++++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/changelog.txt b/changelog.txt index 155665a..05031dd 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,8 @@ == Changelog == += 0.0.4 = +* FIX: compatibility with Gravity Forms 2.10+ JSON storage format for File Upload fields (single-file fields are now also stored as JSON arrays). +* FIX: modify_db() re-encoded the original value instead of the rewritten one during Compatibility Files Sync, and the JSON validity check now requires an array. + = 0.0.3 = * Tested with Gravity Forms version 2.9.16.1 diff --git a/class-gravity-forms.php b/class-gravity-forms.php index fb04dbc..3584248 100644 --- a/class-gravity-forms.php +++ b/class-gravity-forms.php @@ -83,8 +83,15 @@ public function gform_save_field_value($value, $lead, $field, $form, $input_id) if ($type == 'fileupload') { $dir = wp_upload_dir(); - if ($field->multipleFiles) { - $value = json_decode($value, true); + // GF 2.10+ standardized the File Upload storage format so single-file + // fields are also stored as a JSON array. Detect the shape from the + // value itself rather than from $field->multipleFiles so we work on + // both old and new GF versions. + $decoded = is_string($value) ? json_decode($value, true) : null; + $was_json = is_array($decoded); + + if ($was_json) { + $value = $decoded; } else { $value = array($value); } @@ -109,7 +116,9 @@ public function gform_save_field_value($value, $lead, $field, $form, $input_id) } } - if ($field->multipleFiles) { + // Re-encode in the same shape we received so older GF versions keep + // storing single-file values as a plain string. + if ($was_json) { $value = wp_json_encode($value); } else { $value = array_pop($value); @@ -205,7 +214,7 @@ public function modify_db($file_path, $fullsizepath, $media) { // Check if value is json encoded, if so, cycle through array and replace URLs. $result = json_decode($value); - if ( json_last_error() === 0 ) { + if ( json_last_error() === JSON_ERROR_NONE && is_array($result) ) { foreach ($result as $k => $v) { $position = strpos($v, $dir['baseurl']); @@ -214,7 +223,7 @@ public function modify_db($file_path, $fullsizepath, $media) { } } - $result = wp_json_encode($value); + $result = wp_json_encode($result); } else { $position = strpos($value, $dir['baseurl']); diff --git a/readme.txt b/readme.txt index 3a8aae5..a0e31d5 100644 --- a/readme.txt +++ b/readme.txt @@ -6,7 +6,7 @@ License: GPLv2 or later Requires PHP: 8.0 Requires at least: 5.0 Tested up to: 6.8 -Stable tag: 0.0.3 +Stable tag: 0.0.4 Provides compatibility between the Gravity Forms and the WP-Stateless plugins. @@ -21,7 +21,7 @@ Provides compatibility between the [Gravity Forms](https://www.gravityforms.com/ = Notes = -* Tested with Gravity Forms plugin version 2.9.16.1 +* Tested with Gravity Forms plugin version 2.10.x = Support, Feedback, & Contribute = @@ -44,6 +44,10 @@ To ensure new releases cause as little disruption as possible, we rely on early == Upgrade Notice == == Changelog == += 0.0.4 = +* FIX: compatibility with Gravity Forms 2.10+ JSON storage format for File Upload fields (single-file fields are now also stored as JSON arrays). +* FIX: modify_db() re-encoded the original value instead of the rewritten one during Compatibility Files Sync, and the JSON validity check now requires an array. + = 0.0.3 = * Tested with Gravity Forms version 2.9.16.1 From 63077062d9972bff11b2dd2264b22d7a47e67f97 Mon Sep 17 00:00:00 2001 From: Curtis McHale Date: Wed, 3 Jun 2026 14:49:36 -0700 Subject: [PATCH 2/2] Tolerate pre-decoded array input and pin tested GF version to 2.10.2 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). --- class-gravity-forms.php | 14 +++++++------- readme.txt | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/class-gravity-forms.php b/class-gravity-forms.php index 3584248..224bda5 100644 --- a/class-gravity-forms.php +++ b/class-gravity-forms.php @@ -86,14 +86,14 @@ public function gform_save_field_value($value, $lead, $field, $form, $input_id) // GF 2.10+ standardized the File Upload storage format so single-file // fields are also stored as a JSON array. Detect the shape from the // value itself rather than from $field->multipleFiles so we work on - // both old and new GF versions. - $decoded = is_string($value) ? json_decode($value, true) : null; - $was_json = is_array($decoded); - - if ($was_json) { - $value = $decoded; + // both old and new GF versions. Also tolerate an already-decoded array + // in case another filter ran ahead of us. + if (is_array($value)) { + $was_json = true; } else { - $value = array($value); + $decoded = is_string($value) ? json_decode($value, true) : null; + $was_json = is_array($decoded); + $value = $was_json ? $decoded : array($value); } foreach ($value as $k => $v) { diff --git a/readme.txt b/readme.txt index a0e31d5..2790129 100644 --- a/readme.txt +++ b/readme.txt @@ -21,7 +21,7 @@ Provides compatibility between the [Gravity Forms](https://www.gravityforms.com/ = Notes = -* Tested with Gravity Forms plugin version 2.10.x +* Tested with Gravity Forms plugin version 2.10.2 = Support, Feedback, & Contribute =