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
17 changes: 16 additions & 1 deletion gp-populate-anything/gppa-preserve-selections.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@
*/
// Update "123" to your form ID and "4" to your field ID.
add_filter( 'gppa_input_choices_123_4', function( $choices, $field, $objects ) {
$selected_values = (array) rgar( gp_populate_anything()->get_field_values_from_request(), $field->id );
$field_values = gp_populate_anything()->get_field_values_from_request();
$selected_values = (array) rgar( $field_values, $field->id );
if ( $field->type == 'checkbox' && ! empty( $selected_values ) ) {
// look in field_values for values of index like 5.1, 5.2 etc. where 5 is the field id
$selected_values = array();
foreach ( $field_values as $key => $value ) {
if ( strpos( (string) $key, $field->id . '.' ) === 0 ) {
$selected_values[ $key ] = $value;
}
}
}
Comment on lines +10 to +18
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Logic issue: Inconsistent array structure for checkbox processing

The checkbox handling logic changes the structure of $selected_values from a simple array to an associative array with keys like "5.1" and their corresponding values. This creates a mismatch with the in_array() check on line 21.

if ( $field->type == 'checkbox' && ! empty( $selected_values ) ) {
-	// look in field_values for values of index like 5.1, 5.2 etc. where 5 is the field id
-	$selected_values = array();
-	foreach ( $field_values as $key => $value ) {
-		if ( strpos( (string) $key, $field->id . '.' ) === 0 ) {
-			$selected_values[ $key ] = $value;
-		}
-	}
+	// look in field_values for values of index like 5.1, 5.2 etc. where 5 is the field id
+	$checkbox_values = array();
+	foreach ( $field_values as $key => $value ) {
+		if ( strpos( (string) $key, $field->id . '.' ) === 0 ) {
+			$checkbox_values[] = $value;
+		}
+	}
+	$selected_values = $checkbox_values;
}

This ensures $selected_values remains a simple array of values, maintaining compatibility with the in_array() check below.

🤖 Prompt for AI Agents
In gp-populate-anything/gppa-preserve-selections.php around lines 10 to 18, the
checkbox handling code changes $selected_values from a simple array to an
associative array with keys like "5.1", causing incompatibility with the
in_array() check later. To fix this, modify the loop to append values to
$selected_values as a simple indexed array instead of using keys, preserving the
original array structure and ensuring in_array() works correctly.


foreach ( $choices as &$choice ) {
if ( in_array( $choice['value'], $selected_values, true ) ) {
$choice['isSelected'] = true;
}
}

return $choices;
}, 10, 3 );

// Update "123" to your form ID and "4" to your field ID.
add_filter( 'gppa_field_choices_posted_value_123_4', '__return_false' );
Loading