-
Notifications
You must be signed in to change notification settings - Fork 92
gw-consolidate-multiple-list-fields-to-single-list-field.php: Added snippet to consolidate multiple list fields to a single list field.
#1105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
a9720e9
048e327
1c70d25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| $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 ); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for unserialization The current implementation assumes that 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
🤖 Prompt for AI AgentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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.
🤖 Prompt for AI Agents