Skip to content
Merged
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
43 changes: 36 additions & 7 deletions gp-reload-form/gprf-clear-specific-field-values-on-reload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,41 @@
*/
// Update "123" to your form ID.
add_filter( 'gprf_disable_dynamic_reload_123', function( $return ) {
function gprf_clear_specific_field_values( $return ) {
// Update "3" to the ID of the field for whose value you would like to clear.
$_POST['input_3'] = '';
remove_filter( 'gform_pre_render', 'gprf_clear_specific_field_values' );
return $return;
}
add_filter( 'gform_pre_render', 'gprf_clear_specific_field_values' );

$form_id = 123;
$fields_to_clear = array( 2, 3, 4 ); // array of field IDs to clear

add_filter( 'gform_pre_render_' . $form_id, function( $form ) use ( $fields_to_clear ) {

foreach ( $fields_to_clear as $field_id ) {
$field = GFAPI::get_field( $form, $field_id );

if ( ! $field ) {
continue; // skip if field not found
}

if ( $field->type === 'checkbox' ) {
foreach ( $_POST as $key => $value ) {
if ( strpos( $key, "input_{$field_id}" ) === 0 ) {
$_POST[ $key ] = '';
}
}
} elseif ( $field->type === 'fileupload' ) {
Comment on lines +25 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Bug: prefix match can clear the wrong fields (e.g., field 2 matches input_20.*).

Use an exact-key or “dot-delimited” boundary check for checkbox inputs.

Apply this diff:

-			if ( $field->type === 'checkbox' ) {
+			if ( $field->type === 'checkbox' ) {
 				foreach ( $_POST as $key => $value ) {
-					if ( strpos( $key, "input_{$field_id}" ) === 0 ) {
-						$_POST[ $key ] = '';
-					}
+					// Match either "input_<id>" or "input_<id>.<choice>"
+					if ( $key === "input_{$field_id}" || strpos( $key, "input_{$field_id}." ) === 0 ) {
+						unset( $_POST[ $key ] );
+					}
 				}
📝 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.

Suggested change
if ( $field->type === 'checkbox' ) {
foreach ( $_POST as $key => $value ) {
if ( strpos( $key, "input_{$field_id}" ) === 0 ) {
$_POST[ $key ] = '';
}
}
} elseif ( $field->type === 'fileupload' ) {
if ( $field->type === 'checkbox' ) {
foreach ( $_POST as $key => $value ) {
// Match either "input_<id>" or "input_<id>.<choice>"
if ( $key === "input_{$field_id}" || strpos( $key, "input_{$field_id}." ) === 0 ) {
unset( $_POST[ $key ] );
}
}
} elseif ( $field->type === 'fileupload' ) {
🤖 Prompt for AI Agents
In gp-reload-form/gprf-clear-specific-field-values-on-reload.php around lines 25
to 31, the current prefix check strpos($key, "input_{$field_id}") === 0 can
match unintended fields like input_20 when field_id is 2; change the condition
to only clear exact keys or keys that use the field boundary (e.g., key ===
"input_{$field_id}" OR strpos($key, "input_{$field_id}_") === 0 OR strpos($key,
"input_{$field_id}.") === 0) so only the intended checkbox inputs are cleared.

if ( isset( $_POST['gform_uploaded_files'] ) ) {
$uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true );
if ( isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
unset( $uploaded_files[ "input_{$field_id}" ] );
$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
}
}
GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
} else {
Comment on lines +32 to +40
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Harden file upload clearing: unslashing, type checks, and static array init.

Avoids edge-case warnings and ensures the static cache is always an array.

Apply this diff:

-			} elseif ( $field->type === 'fileupload' ) {
-				if ( isset( $_POST['gform_uploaded_files'] ) ) {
-					$uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true );
-					if ( isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
-						unset( $uploaded_files[ "input_{$field_id}" ] );
-						$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
-					}
-				}
-				GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
+			} elseif ( $field->type === 'fileupload' ) {
+				if ( isset( $_POST['gform_uploaded_files'] ) ) {
+					$uploaded_files = json_decode( wp_unslash( $_POST['gform_uploaded_files'] ), true );
+					if ( is_array( $uploaded_files ) && isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
+						unset( $uploaded_files[ "input_{$field_id}" ] );
+						$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
+					}
+				}
+				if ( ! is_array( GFFormsModel::$uploaded_files ) ) {
+					GFFormsModel::$uploaded_files = array();
+				}
+				if ( ! isset( GFFormsModel::$uploaded_files[ $form['id'] ] ) || ! is_array( GFFormsModel::$uploaded_files[ $form['id'] ] ) ) {
+					GFFormsModel::$uploaded_files[ $form['id'] ] = array();
+				}
+				GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
📝 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.

Suggested change
if ( isset( $_POST['gform_uploaded_files'] ) ) {
$uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true );
if ( isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
unset( $uploaded_files[ "input_{$field_id}" ] );
$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
}
}
GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
} else {
} elseif ( $field->type === 'fileupload' ) {
if ( isset( $_POST['gform_uploaded_files'] ) ) {
$uploaded_files = json_decode( wp_unslash( $_POST['gform_uploaded_files'] ), true );
if ( is_array( $uploaded_files ) && isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
unset( $uploaded_files[ "input_{$field_id}" ] );
$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
}
}
// Ensure the static cache is initialized
if ( ! is_array( GFFormsModel::$uploaded_files ) ) {
GFFormsModel::$uploaded_files = array();
}
if ( ! isset( GFFormsModel::$uploaded_files[ $form['id'] ] )
|| ! is_array( GFFormsModel::$uploaded_files[ $form['id'] ] ) ) {
GFFormsModel::$uploaded_files[ $form['id'] ] = array();
}
GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
} else {
🤖 Prompt for AI Agents
In gp-reload-form/gprf-clear-specific-field-values-on-reload.php around lines 32
to 40, the code that clears uploaded files should first wp_unslash the raw
$_POST['gform_uploaded_files'] and confirm it's a string before calling
json_decode; after json_decode verify the result is an array (or fallback to an
empty array) before accessing or unsetting input keys; when writing back use
wp_json_encode of the ensured-array; and always ensure
GFFormsModel::$uploaded_files is initialized as an array and that
GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] is set to an
array (not null) to avoid warnings and preserve the static cache shape.

$_POST[ "input_{$field_id}" ] = '';
}
}

return $form;
} );

return $return;
} );