Skip to content

Commit e579ea3

Browse files
authored
gpml-acf-user-image-field.php: Fixed an issue with User Image getting removed on form resubmission.
1 parent 93f28b8 commit e579ea3

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,64 @@ public function __construct( $args ) {
3333

3434
}
3535

36+
protected function get_attachment_id_by_filename( $filename ) {
37+
global $wpdb;
38+
39+
return $wpdb->get_var( $wpdb->prepare(
40+
"SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND guid LIKE %s LIMIT 1",
41+
'%' . $wpdb->esc_like( $filename ) . '%'
42+
));
43+
}
44+
3645
function update_user_image_field( $user_id, $feed, $entry ) {
3746
if ( $entry['form_id'] == $this->_args['form_id'] && is_callable( 'gp_media_library' ) ) {
3847

3948
$form = GFAPI::get_form( $entry['form_id'] );
40-
$value = gp_media_library()->acf_get_field_value( $this->_args['format'], $entry, GFFormsModel::get_field( $form, $this->_args['field_id'] ), $this->_args['is_multi'] );
49+
$value = gp_media_library()->acf_get_field_value(
50+
$this->_args['format'],
51+
$entry,
52+
GFFormsModel::get_field( $form, $this->_args['field_id'] ),
53+
$this->_args['is_multi']
54+
);
4155

4256
if ( $value && $this->_args['is_multi'] && $this->_args['append'] ) {
4357
$current_value = wp_list_pluck( (array) get_field( $this->_args['meta_key'], 'user_' . $user_id ), 'ID' );
4458
$value = array_merge( $current_value, $value );
4559
}
4660

61+
$raw_json = $_POST['gform_uploaded_files'] ?? '';
62+
$field_id = $this->_args['field_id'];
63+
$key = 'input_' . $field_id;
64+
65+
if ( empty( $value ) && $raw_json ) {
66+
$uploaded_files_array = json_decode( stripslashes( $raw_json ), true );
67+
if ( isset( $uploaded_files_array[ $key ][0]['uploaded_filename'] ) ) {
68+
$filename = $uploaded_files_array[ $key ][0]['uploaded_filename'];
69+
$attachment_id = $this->get_attachment_id_by_filename( $filename );
70+
if ( $attachment_id ) {
71+
$value = $attachment_id;
72+
}
73+
}
74+
}
75+
76+
$field = GFFormsModel::get_field( $form, $field_id );
77+
if ( empty( $value ) && ! $field->multipleFiles && isset( $_FILES[ $key ] ) && ! empty( $_FILES[ $key ]['name'] ) ) {
78+
$filename = $_FILES[ $key ]['name'];
79+
$attachment_id = $this->get_attachment_id_by_filename( $filename );
80+
if ( $attachment_id ) {
81+
$value = $attachment_id;
82+
}
83+
}
84+
4785
if ( empty( $value ) && ! $this->_args['remove_if_empty'] ) {
4886
return;
4987
}
5088

5189
update_field( $this->_args['meta_key'], $value, 'user_' . $user_id );
52-
5390
}
5491
}
5592

93+
5694
}
5795

5896
# Configuration

0 commit comments

Comments
 (0)