From bfa73b9cf20154d56d268eda68a8941ed6e5937d Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Wed, 30 Apr 2025 18:36:49 +0530 Subject: [PATCH 1/3] `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; } From e02508ecb924d10eccb2c1ddf2da3d72f9d6f8f6 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Fri, 13 Jun 2025 22:59:50 +0530 Subject: [PATCH 2/3] `gpml-acf-user-image-field.php`: Fixed an issue with User Image getting removed on form resubmission. --- .../gpml-acf-user-image-field.php | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/gp-media-library/gpml-acf-user-image-field.php b/gp-media-library/gpml-acf-user-image-field.php index 99ee93d8f..50c4e0043 100644 --- a/gp-media-library/gpml-acf-user-image-field.php +++ b/gp-media-library/gpml-acf-user-image-field.php @@ -46,14 +46,14 @@ function update_user_image_field( $user_id, $feed, $entry ) { // 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 ); - $field_id = $this->_args['field_id']; - $key = 'input_' . $field_id; - + // Multi-file upload check (existing logic) if ( isset( $uploaded_files_array[ $key ][0]['uploaded_filename'] ) ) { $filename = $uploaded_files_array[ $key ][0]['uploaded_filename']; @@ -69,6 +69,22 @@ function update_user_image_field( $user_id, $feed, $entry ) { } } + $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; } From f97703fe9091cce1af69c23241c4a724aef52073 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Fri, 20 Jun 2025 17:24:58 +0530 Subject: [PATCH 3/3] `gpml-acf-user-image-field.php`: Fixed an issue with User Image getting removed on form resubmission. --- .../gpml-acf-user-image-field.php | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/gp-media-library/gpml-acf-user-image-field.php b/gp-media-library/gpml-acf-user-image-field.php index 50c4e0043..cc56a4647 100644 --- a/gp-media-library/gpml-acf-user-image-field.php +++ b/gp-media-library/gpml-acf-user-image-field.php @@ -33,53 +33,50 @@ public function __construct( $args ) { } + protected function get_attachment_id_by_filename( $filename ) { + global $wpdb; + + return $wpdb->get_var( $wpdb->prepare( + "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND guid LIKE %s LIMIT 1", + '%' . $wpdb->esc_like( $filename ) . '%' + )); + } + function update_user_image_field( $user_id, $feed, $entry ) { if ( $entry['form_id'] == $this->_args['form_id'] && is_callable( 'gp_media_library' ) ) { $form = GFAPI::get_form( $entry['form_id'] ); - $value = gp_media_library()->acf_get_field_value( $this->_args['format'], $entry, GFFormsModel::get_field( $form, $this->_args['field_id'] ), $this->_args['is_multi'] ); + $value = gp_media_library()->acf_get_field_value( + $this->_args['format'], + $entry, + GFFormsModel::get_field( $form, $this->_args['field_id'] ), + $this->_args['is_multi'] + ); if ( $value && $this->_args['is_multi'] && $this->_args['append'] ) { $current_value = wp_list_pluck( (array) get_field( $this->_args['meta_key'], 'user_' . $user_id ), 'ID' ); $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) + $uploaded_files_array = json_decode( stripslashes( $raw_json ), true ); 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 ) . '%' - )); - + $filename = $uploaded_files_array[ $key ][0]['uploaded_filename']; + $attachment_id = $this->get_attachment_id_by_filename( $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 ) . '%' - )); - + $filename = $_FILES[ $key ]['name']; + $attachment_id = $this->get_attachment_id_by_filename( $filename ); if ( $attachment_id ) { $value = $attachment_id; } @@ -90,10 +87,10 @@ function update_user_image_field( $user_id, $feed, $entry ) { } update_field( $this->_args['meta_key'], $value, 'user_' . $user_id ); - } } + } # Configuration