Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
21 changes: 15 additions & 6 deletions class-gravity-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ 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. 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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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']);

Expand All @@ -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']);

Expand Down
8 changes: 6 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.2

= Support, Feedback, & Contribute =

Expand All @@ -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

Expand Down