Skip to content

gpml-acf-user-image-field.php: Fixed an issue with User Image getting removed on form resubmission. #1091

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions gp-media-library/gpml-acf-user-image-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,47 @@ 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'] ?? '';
$field_id = $this->_args['field_id'];
$key = 'input_' . $field_id;

if ( empty( $value ) && $raw_json ) {
$clean_json = stripslashes( $raw_json );
$uploaded_files_array = json_decode( $clean_json, true );

// Multi-file upload check (existing logic)
if ( isset( $uploaded_files_array[ $key ][0]['uploaded_filename'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@saifsultanc does this need to work with single file upload fields as well?

If so, you'll likely need to do a check on $_FILES if it's a single file upload field. You can check the field type like so: $field->multipleFiles

Here's an example of $_FILES with a file for a single file upload field.

image

$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;
}
}
}

$form = GFAPI::get_form( $entry['form_id'] );
$field = GFFormsModel::get_field( $form, $field_id );
if ( empty( $value ) && ! $field->multipleFiles && isset( $_FILES[ $key ] ) && ! empty( $_FILES[ $key ]['name'] ) ) {
$filename = $_FILES[ $key ]['name'];

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;
}
Expand Down
Loading