From bfa73b9cf20154d56d268eda68a8941ed6e5937d Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Wed, 30 Apr 2025 18:36:49 +0530 Subject: [PATCH] `gpml-acf-user-image-field.php`: Fixed an issue with User Image getting removed on form resubmission. --- .../gpml-acf-user-image-field.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/gp-media-library/gpml-acf-user-image-field.php b/gp-media-library/gpml-acf-user-image-field.php index c9d41c5a5..99ee93d8f 100644 --- a/gp-media-library/gpml-acf-user-image-field.php +++ b/gp-media-library/gpml-acf-user-image-field.php @@ -44,6 +44,31 @@ function update_user_image_field( $user_id, $feed, $entry ) { $value = array_merge( $current_value, $value ); } + // Check for uploaded files in the $_POST data, we may need to prevent the ACF field from being set to empty. + $raw_json = $_POST['gform_uploaded_files'] ?? ''; + if ( empty( $value ) && $raw_json ) { + $clean_json = stripslashes( $raw_json ); + + $uploaded_files_array = json_decode( $clean_json, true ); + + $field_id = $this->_args['field_id']; + $key = 'input_' . $field_id; + + if ( isset( $uploaded_files_array[ $key ][0]['uploaded_filename'] ) ) { + $filename = $uploaded_files_array[ $key ][0]['uploaded_filename']; + + global $wpdb; + $attachment_id = $wpdb->get_var( $wpdb->prepare( + "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND guid LIKE %s LIMIT 1", + '%' . $wpdb->esc_like( $filename ) . '%' + )); + + if ( $attachment_id ) { + $value = $attachment_id; + } + } + } + if ( empty( $value ) && ! $this->_args['remove_if_empty'] ) { return; }