Skip to content

Commit bfa73b9

Browse files
committed
gpml-acf-user-image-field.php: Fixed an issue with User Image getting removed on form resubmission.
1 parent b547df4 commit bfa73b9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

gp-media-library/gpml-acf-user-image-field.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@ function update_user_image_field( $user_id, $feed, $entry ) {
4444
$value = array_merge( $current_value, $value );
4545
}
4646

47+
// Check for uploaded files in the $_POST data, we may need to prevent the ACF field from being set to empty.
48+
$raw_json = $_POST['gform_uploaded_files'] ?? '';
49+
if ( empty( $value ) && $raw_json ) {
50+
$clean_json = stripslashes( $raw_json );
51+
52+
$uploaded_files_array = json_decode( $clean_json, true );
53+
54+
$field_id = $this->_args['field_id'];
55+
$key = 'input_' . $field_id;
56+
57+
if ( isset( $uploaded_files_array[ $key ][0]['uploaded_filename'] ) ) {
58+
$filename = $uploaded_files_array[ $key ][0]['uploaded_filename'];
59+
60+
global $wpdb;
61+
$attachment_id = $wpdb->get_var( $wpdb->prepare(
62+
"SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND guid LIKE %s LIMIT 1",
63+
'%' . $wpdb->esc_like( $filename ) . '%'
64+
));
65+
66+
if ( $attachment_id ) {
67+
$value = $attachment_id;
68+
}
69+
}
70+
}
71+
4772
if ( empty( $value ) && ! $this->_args['remove_if_empty'] ) {
4873
return;
4974
}

0 commit comments

Comments
 (0)