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 1 commit into
base: master
Choose a base branch
from

Conversation

saifsultanc
Copy link
Contributor

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

Copy link

coderabbitai bot commented Apr 30, 2025

Walkthrough

The 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

File(s) Change Summary
gp-media-library/gpml-acf-user-image-field.php Enhanced update_user_image_field to extract uploaded file information from POST data and set attachment ID when ACF image field value is empty. No changes to public API or method signatures.

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
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against bfa73b9

Copy link

@coderabbitai coderabbitai bot left a 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:

  1. The database query uses LIKE with wildcards, which could potentially match unintended files if filenames are similar.
  2. 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 return null 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

📥 Commits

Reviewing files that changed from the base of the PR and between b547df4 and bfa73b9.

📒 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant