-
Notifications
You must be signed in to change notification settings - Fork 91
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
base: master
Are you sure you want to change the base?
Conversation
…ng removed on form resubmission.
WalkthroughThe update_user_image_field method in the GPML_ACF_User_Image_Field class was modified to better handle scenarios where the ACF image field value is empty, but uploaded files exist in the $_POST data under the 'gform_uploaded_files' key. The method now attempts to extract the relevant uploaded filename from the POST data, locate the corresponding attachment post in the WordPress database, and set the field value to the attachment ID if found. This new logic is executed before the method checks if the field value is empty and whether to remove it. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Form (POST)
participant GPML_ACF_User_Image_Field
participant WordPress DB
User->>Form (POST): Submit form with/without image
Form (POST)->>GPML_ACF_User_Image_Field: Call update_user_image_field($value, $post_id, $field, $input_name)
alt $value is empty and 'gform_uploaded_files' exists
GPML_ACF_User_Image_Field->>Form (POST): Extract uploaded filename from $_POST['gform_uploaded_files']
GPML_ACF_User_Image_Field->>WordPress DB: Query for attachment post by filename
WordPress DB-->>GPML_ACF_User_Image_Field: Return attachment ID (if found)
GPML_ACF_User_Image_Field->>GPML_ACF_User_Image_Field: Set $value = attachment ID
end
GPML_ACF_User_Image_Field-->>Form (POST): Continue with updated $value
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
gp-media-library/gpml-acf-user-image-field.php (2)
47-70
: Good fix for the image retention issue, with some security considerations.The added code effectively solves the issue by checking for uploaded files in the POST data when the ACF field value appears empty. This prevents the user's image from being lost during form resubmission.
However, there are some security considerations:
- The database query uses
LIKE
with wildcards, which could potentially match unintended files if filenames are similar.- Consider adding additional validation to ensure the attachment actually belongs to the user being updated.
- $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 ) . '%' - )); + // More precise query that also checks post_author matches the user being updated + $attachment_id = $wpdb->get_var( $wpdb->prepare( + "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND guid LIKE %s AND (post_author = %d OR post_author = 0) LIMIT 1", + '%' . $wpdb->esc_like( $filename ) . '%', + $user_id + ));
48-53
: Consider adding error handling for JSON parsing.The code assumes that
$clean_json
will always be valid JSON. If the JSON is malformed,json_decode()
will returnnull
and no error handling is in place.- $clean_json = stripslashes( $raw_json ); - - $uploaded_files_array = json_decode( $clean_json, true ); + $clean_json = stripslashes( $raw_json ); + $uploaded_files_array = json_decode( $clean_json, true ); + + // Add error handling for JSON parsing + if ( json_last_error() !== JSON_ERROR_NONE ) { + error_log( 'GPML_ACF_User_Image_Field: Error decoding uploaded files JSON: ' . json_last_error_msg() ); + $uploaded_files_array = array(); + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gp-media-library/gpml-acf-user-image-field.php
(1 hunks)
🔇 Additional comments (1)
gp-media-library/gpml-acf-user-image-field.php (1)
72-74
: The fix correctly preserves the existing logic.The original conditional that checks if the value is empty is maintained, with the new code improving detection of uploaded files before this check. This ensures backward compatibility with the existing behavior.
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2921885802/82733
Summary
File uploaded to an ACF Image field on the user profile, when user profile is updated with GFUR is removed.
https://www.loom.com/share/029fcc517e50428d81800577d1106661?sid=ac7c7d82-17ae-4161-bda6-f4c3478d5e50