-
Notifications
You must be signed in to change notification settings - Fork 92
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
gpml-acf-user-image-field.php: Fixed an issue with User Image getting removed on form resubmission.
#1091
Changes from all commits
bfa73b9
e02508e
f97703f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,26 +33,64 @@ 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 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $raw_json = $_POST['gform_uploaded_files'] ?? ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $field_id = $this->_args['field_id']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $key = 'input_' . $field_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( empty( $value ) && $raw_json ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $uploaded_files_array = json_decode( stripslashes( $raw_json ), true ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( isset( $uploaded_files_array[ $key ][0]['uploaded_filename'] ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Here's an example of
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $filename = $uploaded_files_array[ $key ][0]['uploaded_filename']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $attachment_id = $this->get_attachment_id_by_filename( $filename ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( $attachment_id ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $value = $attachment_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+61
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for JSON parsing and array access. The current code assumes the JSON structure and doesn't handle parsing errors or missing array keys, which could cause PHP warnings or unexpected behavior. if ( empty( $value ) && $raw_json ) {
$uploaded_files_array = json_decode( stripslashes( $raw_json ), true );
- if ( isset( $uploaded_files_array[ $key ][0]['uploaded_filename'] ) ) {
+ if ( json_last_error() === JSON_ERROR_NONE &&
+ isset( $uploaded_files_array[ $key ][0]['uploaded_filename'] ) ) {
$filename = $uploaded_files_array[ $key ][0]['uploaded_filename'];
$attachment_id = $this->get_attachment_id_by_filename( $filename );
if ( $attachment_id ) {
$value = $attachment_id;
}
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $field = GFFormsModel::get_field( $form, $field_id ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( empty( $value ) && ! $field->multipleFiles && isset( $_FILES[ $key ] ) && ! empty( $_FILES[ $key ]['name'] ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $filename = $_FILES[ $key ]['name']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $attachment_id = $this->get_attachment_id_by_filename( $filename ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( $attachment_id ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $value = $attachment_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+77
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add filename validation for security. The filename from if ( empty( $value ) && ! $field->multipleFiles && isset( $_FILES[ $key ] ) && ! empty( $_FILES[ $key ]['name'] ) ) {
$filename = $_FILES[ $key ]['name'];
+ // Validate filename
+ if ( ! preg_match( '/^[a-zA-Z0-9._-]+\.(jpg|jpeg|png|gif|webp)$/i', $filename ) ) {
+ return;
+ }
$attachment_id = $this->get_attachment_id_by_filename( $filename );
if ( $attachment_id ) {
$value = $attachment_id;
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( empty( $value ) && ! $this->_args['remove_if_empty'] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| update_field( $this->_args['meta_key'], $value, 'user_' . $user_id ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Configuration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve the filename matching logic for better reliability.
The current approach of using
LIKE '%filename%'on the GUID field may produce unreliable results since:Consider using WordPress's built-in functions or querying by
post_titleor_wp_attached_filemeta instead.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 ) . '%' - )); + global $wpdb; + + // First try exact match on _wp_attached_file meta + $attachment_id = $wpdb->get_var( $wpdb->prepare( + "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value LIKE %s LIMIT 1", + '%' . $wpdb->esc_like( basename( $filename ) ) + )); + + if ( $attachment_id ) { + return $attachment_id; + } + + // Fallback to post_title match + return $wpdb->get_var( $wpdb->prepare( + "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_title = %s LIMIT 1", + pathinfo( $filename, PATHINFO_FILENAME ) + )); }📝 Committable suggestion
🤖 Prompt for AI Agents