Skip to content
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Gravity Wiz // Gravity Forms // Consolidate Multiple List Fields into a Single List Field
* https://gravitywiz.com/
*
* Instruction Video: https://www.loom.com/share/8b45a92cf56249a982aa1aa6e1301778
*
* This snippet merges values from multiple list fields into a single list field.
*/
// Update "123" to your form ID.
add_action( 'gform_post_submission_123', function ( $entry, $form ) {
// Define source field IDs and target field ID.
$source_field_ids = array( 1, 5, 4 );
$target_field_id = 7;
Comment on lines +13 to +14
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add field validation

The snippet uses hardcoded field IDs without verifying that these fields exist or are list fields. This could lead to runtime errors if the form structure changes.

// Define source field IDs and target field ID.
$source_field_ids = array( 1, 5, 4 );
$target_field_id  = 7;

+// Validate that all required fields exist and are list fields
+foreach ( $source_field_ids as $field_id ) {
+	$field = GFAPI::get_field( $form, $field_id );
+	if ( ! $field || $field->type !== 'list' ) {
+		error_log( "GF List Consolidation: Source field {$field_id} is not a valid list field." );
+		return;
+	}
+}
+
+$target_field = GFAPI::get_field( $form, $target_field_id );
+if ( ! $target_field || $target_field->type !== 'list' ) {
+	error_log( "GF List Consolidation: Target field {$target_field_id} is not a valid list field." );
+	return;
+}
🤖 Prompt for AI Agents
In gravity-forms/gw-consolidate-multiple-list-fields-to-single-list-field.php
around lines 13 to 14, the code uses hardcoded field IDs without validating
their existence or type. Add validation to check that each source field ID
exists in the form and is a list field, and similarly verify the target field
ID. If any field is missing or not a list field, handle the error appropriately
to prevent runtime issues.


$combined = array();

// Loop through source field IDs and merge their unserialized values.
foreach ( $source_field_ids as $field_id ) {
if ( isset( $entry[ $field_id ] ) && ! empty( $entry[ $field_id ] ) ) {
$field_values = unserialize( $entry[ $field_id ] );
$combined = array_merge( $combined, $field_values );
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for unserialization

The current implementation assumes that unserialize() will always succeed, which might not be the case with corrupted data. Adding error handling would make the code more robust.

foreach ( $source_field_ids as $field_id ) {
	if ( isset( $entry[ $field_id ] ) && ! empty( $entry[ $field_id ] ) ) {
-		$field_values = unserialize( $entry[ $field_id ] );
-		$combined     = array_merge( $combined, $field_values );
+		$field_values = @unserialize( $entry[ $field_id ] );
+		if ( $field_values === false && $entry[ $field_id ] !== 'b:0;' ) {
+			// Log error but continue with other fields
+			error_log( "GF List Consolidation: Failed to unserialize data for field {$field_id}" );
+			continue;
+		}
+		
+		if ( is_array( $field_values ) ) {
+			$combined = array_merge( $combined, $field_values );
+		}
	}
}
📝 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
foreach ( $source_field_ids as $field_id ) {
if ( isset( $entry[ $field_id ] ) && ! empty( $entry[ $field_id ] ) ) {
$field_values = unserialize( $entry[ $field_id ] );
$combined = array_merge( $combined, $field_values );
}
}
foreach ( $source_field_ids as $field_id ) {
if ( isset( $entry[ $field_id ] ) && ! empty( $entry[ $field_id ] ) ) {
$field_values = @unserialize( $entry[ $field_id ] );
if ( $field_values === false && $entry[ $field_id ] !== 'b:0;' ) {
// Log error but continue with other fields
error_log( "GF List Consolidation: Failed to unserialize data for field {$field_id}" );
continue;
}
if ( is_array( $field_values ) ) {
$combined = array_merge( $combined, $field_values );
}
}
}
🤖 Prompt for AI Agents
In gravity-forms/gw-consolidate-multiple-list-fields-to-single-list-field.php
around lines 19 to 24, the code uses unserialize() without checking for errors,
which can cause issues if the data is corrupted. Modify the code to check if
unserialize() returns false or triggers an error, and handle such cases
gracefully, for example by skipping the corrupted data or logging an error, to
ensure robustness.


// Re-index the combined array.
$combined = array_values( $combined );

// Serialize the combined array and update the target field and entry.
$finalSerialized = serialize( $combined );
$entry[ $target_field_id ] = $finalSerialized;

GFAPI::update_entry( $entry );
}, 10, 2 );
Comment on lines +36 to +37
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for entry update

The code doesn't handle potential errors when updating the entry. Adding error checking will help with debugging if issues arise.

-GFAPI::update_entry( $entry );
+$result = GFAPI::update_entry( $entry );
+if ( is_wp_error( $result ) ) {
+	error_log( "GF List Consolidation: Failed to update entry. Error: " . $result->get_error_message() );
+}
📝 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
GFAPI::update_entry( $entry );
}, 10, 2 );
$result = GFAPI::update_entry( $entry );
if ( is_wp_error( $result ) ) {
error_log( "GF List Consolidation: Failed to update entry. Error: " . $result->get_error_message() );
}
}, 10, 2 );
🤖 Prompt for AI Agents
In gravity-forms/gw-consolidate-multiple-list-fields-to-single-list-field.php
around lines 33 to 34, the call to GFAPI::update_entry($entry) lacks error
handling. Modify the code to capture the return value of GFAPI::update_entry and
check if it indicates an error. If an error occurs, log or handle it
appropriately to aid debugging and ensure issues during entry update are
detected.

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add a check for GFAPI availability

The snippet assumes that Gravity Forms is active and the GFAPI class is available. Adding a check would prevent PHP errors if Gravity Forms is deactivated.

<?php
/**
 * Gravity Wiz // Gravity Forms // Consolidate Multiple List Fields into a Single List Field
 * https://gravitywiz.com/
 *
 * Instruction Video: https://www.loom.com/share/8b45a92cf56249a982aa1aa6e1301778
 *
 * This snippet merges values from multiple list fields into a single list field.
 */
+// Exit if accessed directly
+if ( ! defined( 'ABSPATH' ) ) {
+	exit;
+}
+
+// Check if Gravity Forms is active
+if ( ! class_exists( 'GFAPI' ) ) {
+	return;
+}
+
// Update "123" to your form ID.
add_action( 'gform_post_submission_123', function ( $entry, $form ) {
🤖 Prompt for AI Agents
In gravity-forms/gw-consolidate-multiple-list-fields-to-single-list-field.php
lines 1 to 34, the code uses GFAPI without verifying if the Gravity Forms plugin
is active and the GFAPI class exists. To fix this, add a conditional check
before calling GFAPI::update_entry to ensure the GFAPI class exists, preventing
PHP errors if Gravity Forms is deactivated.

Loading