Skip to content
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

Fix mail body replace text bug #1460

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

egushinnosuke
Copy link

@egushinnosuke egushinnosuke commented Jun 11, 2024

Bug detail

When sending emails for creating or updating posts, the function prepare_mail_body() in the Frontend_Form_Ajax class sometimes causes the email body to display the output of wp_get_attachment_url() for related posts instead of showing the meta_value.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of email body preparation to correctly manage attachment URLs based on input type.

# Bug detail
When sending emails for creating or updating posts, the function prepare_mail_body() in the Frontend_Form_Ajax class sometimes causes the email body to display the output of wp_get_attachment_url() for related posts instead of showing the meta_value.
Copy link

coderabbitai bot commented Jun 11, 2024

Walkthrough

The prepare_mail_body function in the Frontend_Form_Ajax.php file has been enhanced to retrieve wpuf_input posts and apply conditions based on the input_type. This determines whether to use attachment URLs. The logic for handling values and attachments has been updated accordingly, ensuring more accurate email body preparation.

Changes

File Path Summary
includes/Ajax/Frontend_Form_Ajax.php Updated prepare_mail_body to retrieve wpuf_input posts and determine attachment URLs based on input_type.

Poem

In the code where emails brew,
New logic now shines through.
Attachments find their proper place,
With input_type, they embrace.
Forms refined, more precise,
User joy will now suffice.
🎉✨🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

@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: 1

Outside diff range and nitpick comments (1)
includes/Ajax/Frontend_Form_Ajax.php (1)

739-739: Validate the handling of the address_field meta key.

It appears there is special handling for an address_field meta key, but this is done outside the main loop and might not apply correctly if $use_attachment_url is true. Ensure this special case is integrated properly into the main logic flow to avoid unexpected behavior.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b7474d7 and db5f580.

Files selected for processing (1)
  • includes/Ajax/Frontend_Form_Ajax.php (3 hunks)
Additional comments not posted (1)
includes/Ajax/Frontend_Form_Ajax.php (1)

672-694: Refactor the retrieval and processing of custom fields to improve performance.
[REFACTOR_SUGGESTion]
The current implementation retrieves all posts of type wpuf_input and then iterates through them to find a match for each custom field. This can be inefficient, especially with a large number of posts or custom fields. Consider refactoring this to use a more direct query or caching mechanism to reduce database load.

// Suggested refactor to use a direct query with caching
$meta_key = 'your_meta_key_here'; // This should be dynamically set based on context
$cached_input_types = wp_cache_get($meta_key, 'wpuf_input_types');
if (false === $cached_input_types) {
    $input_types = new WP_Query([
        'post_type' => 'wpuf_input',
        'posts_per_page' => -1,
        'fields' => 'ids',
        'meta_query' => [
            [
                'key' => 'name',
                'value' => $meta_key,
                'compare' => '='
            ]
        ]
    ]);
    $cached_input_types = $input_types->posts;
    wp_cache_set($meta_key, $cached_input_types, 'wpuf_input_types');
}
// Use $cached_input_types for further processing

Comment on lines +712 to +726
if ( $use_attachment_url ) {
$meta_val = wp_get_attachment_url( $val );
} else {
$meta_val = $val;
}
$is_first = false;
} else {
if ( get_post_mime_type( (int) $val ) ) {
if ( $use_attachment_url ) {
$meta_val = $meta_val . ', ' . wp_get_attachment_url( $val );
} else {
$meta_val = $meta_val . ', ' . $val;
}
}

if ( get_post_mime_type( (int) $val ) ) {
if ( $use_attachment_url ) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure consistent handling of attachment URLs.

The logic for appending attachment URLs seems inconsistent. In some cases, you use a comma for separation, and in others, you do not. This could lead to malformed strings when multiple values are present. Ensure consistency in how values are concatenated.

// Corrected logic for consistent comma usage
if ($use_attachment_url) {
    $meta_val = $meta_val . (empty($meta_val) ? '' : ', ') . wp_get_attachment_url($val);
} else {
    $meta_val = $meta_val . (empty($meta_val) ? '' : ', ') . $val;
}

@sapayth
Copy link
Member

sapayth commented Jun 11, 2024

there could be an optimised approach. can you share the bug details in a video please @egushinnosuke ?

@egushinnosuke
Copy link
Author

@sapayth please check below
https://gyazo.com/31e6925bffeb918dd99b05c6f12ae03d
PW:bug0708

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

Successfully merging this pull request may close these issues.

2 participants