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: error not showing after post form submission #1510

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

sapayth
Copy link
Member

@sapayth sapayth commented Feb 6, 2025

custom validation checking using wpuf_add_post_validate hook is not showing properly.

  • fix the add/update post validation message
  • update related AJAX response to show other messages properly

image

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling during form submissions, ensuring users receive accurate error messages.
    • Updated responses to indicate error conditions for pending payments and existing users.
  • Chores

    • Updated translation metadata with the current copyright year and revised timestamps.
    • Expanded .gitignore entries to include additional files and directories.

Copy link

coderabbitai bot commented Feb 6, 2025

Walkthrough

This pull request modifies the error handling in the formSubmit method of the WP_User_Frontend object by replacing the reference from res.error to res.data.error. Additionally, it updates the metadata in the translation file, changing the copyright year and date information. Changes are also made to the error handling in various methods across multiple files, ensuring that error responses are consistently communicated.

Changes

File(s) Change Summary
assets/js/.../frontend-form.js Updated error handling logic in formSubmit: changed error reference from res.error to res.data.error.
includes/Ajax/.../Frontend_Form_Ajax.php Changed responses in submit_post and wpuf_get_post_user methods from success to error JSON responses.
includes/Frontend/.../Frontend_Form.php Updated response in publish_guest_post method to indicate error when payment status is pending.
languages/.../wp-user-frontend.pot Updated metadata: copyright year from 2024 to 2025; revised POT-Creation-Date and PO-Revision-Date accordingly.
.gitignore Added entries for assets/js/frontend-form.min.js, languages, and exception for !languages/readme.txt.

Possibly related PRs

Suggested labels

bug, needs: developer feedback

Poem

Hoppity, hoppity, through the code I go,
Changing error paths so the feedback will flow.
Metadata refreshed like new morning dew,
From 2024 to 2025, everything feels brand new.
With a twitch of my nose and a joyful cheer,
CodeRabbit hops, bringing changes near!
🐰✨

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between accc7ae and a346304.

⛔ Files ignored due to path filters (1)
  • assets/js/frontend-form.min.js is excluded by !**/*.min.js
📒 Files selected for processing (3)
  • assets/js/frontend-form.js (3 hunks)
  • includes/Ajax/Frontend_Form_Ajax.php (2 hunks)
  • languages/wp-user-frontend.pot (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • assets/js/frontend-form.js
  • includes/Ajax/Frontend_Form_Ajax.php
  • languages/wp-user-frontend.pot

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>, 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. (Beta)
  • @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

@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)
assets/js/frontend-form.js (2)

480-494: Consider adding defensive checks for the error response structure.

While the fix addresses the immediate issue, consider adding null checks to handle cases where res.data or res.data.error might be undefined:

 if ( form.find('.g-recaptcha').length > 0 ) {
     grecaptcha.reset();
 }

 Swal.fire({
-    html: res.data.error,
+    html: res.data && res.data.error ? res.data.error : 'An error occurred during form submission',
     icon: 'warning',
     showCancelButton: false,
     confirmButtonColor: '#d54e21',
     confirmButtonText: 'OK',
     customClass: {
         cancelButton: 'btn btn-danger',
     }
 });

430-504: Consider adding unit tests for the form submission error handling.

The formSubmit method handles critical form submission logic. To ensure reliability, consider adding unit tests that cover:

  • Success and error response handling
  • Various response data structures
  • Edge cases like undefined/malformed responses
  • Integration with SweetAlert2 dialogs
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9801b41 and 658947d.

⛔ Files ignored due to path filters (1)
  • assets/js/frontend-form.min.js is excluded by !**/*.min.js
📒 Files selected for processing (2)
  • assets/js/frontend-form.js (1 hunks)
  • languages/wp-user-frontend.pot (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • languages/wp-user-frontend.pot
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Run PHPCS inspection
🔇 Additional comments (1)
assets/js/frontend-form.js (1)

484-493: LGTM! The error handling fix looks correct.

The change from res.error to res.data.error properly accesses the error message from the server response structure. This fix ensures that validation errors are correctly displayed to users after form submission.

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 comments (3)
includes/Frontend/Frontend_Form.php (2)

122-122: Replace shorthand list syntax for PHP 7.0 compatibility

The shorthand list syntax [...] is not supported in PHP 7.0 or earlier. This needs to be updated to maintain backward compatibility.

Apply this change:

-[ $post_vars, $taxonomy_vars, $meta_vars ] = $this->get_input_fields( $this->form_fields );
+list( $post_vars, $taxonomy_vars, $meta_vars ) = $this->get_input_fields( $this->form_fields );

38-38: Remove error silencing and add proper error handling

Using the @ operator to silence errors is discouraged. Instead, implement proper error handling.

Apply this change:

-@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
+if (!headers_sent()) {
+    header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
+}
includes/Ajax/Frontend_Form_Ajax.php (1)

38-38: Remove error silencing and add proper error handling

Using the @ operator to silence errors is discouraged. Implement proper error handling instead.

Apply this change:

-@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
+if (!headers_sent()) {
+    header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 658947d and accc7ae.

⛔ Files ignored due to path filters (1)
  • assets/js/frontend-form.min.js is excluded by !**/*.min.js
📒 Files selected for processing (4)
  • .gitignore (1 hunks)
  • includes/Ajax/Frontend_Form_Ajax.php (2 hunks)
  • includes/Frontend/Frontend_Form.php (1 hunks)
  • languages/wp-user-frontend.pot (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .gitignore
🚧 Files skipped from review as they are similar to previous changes (1)
  • languages/wp-user-frontend.pot
🧰 Additional context used
🪛 GitHub Actions: Inspections
includes/Frontend/Frontend_Form.php

[warning] 1-1: Silencing errors is strongly discouraged. Use proper error checking instead.


[error] 1-1: The shorthand list syntax '[]' to destructure arrays is not available in PHP 7.0 or earlier.

includes/Ajax/Frontend_Form_Ajax.php

[warning] 1-1: Silencing errors is strongly discouraged. Use proper error checking instead. Found: @Header( 'Content-Type: application/json; charset=' ...

🔇 Additional comments (2)
includes/Ajax/Frontend_Form_Ajax.php (2)

512-519: Consistent error handling in user registration

The change to wp_send_json_error maintains consistency in error handling. The response includes a redirect URL for login, which is a good UX practice.


339-339: Consistent error handling with wp_send_json_error

The change aligns the error handling with the rest of the codebase by using wp_send_json_error. However, ensure that the frontend code is updated to handle the modified response structure.

Let's verify the frontend error handling:


wp_redirect( $response['redirect_to'] );
wpuf_clear_buffer();
wp_send_json( $response );
wp_send_json_error( $response );
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

⚠️ Potential issue

🧩 Analysis chain

Breaking change: Response structure modified

The change from wp_send_json to wp_send_json_error modifies the response structure. Clients expecting the response data at the root level will need to access it via the data property instead. This change requires corresponding updates in the frontend code that handles the response.

Let's verify if the frontend code has been updated to handle this change:


🏁 Script executed:

#!/bin/bash
# Search for JavaScript code that handles the response from publish_guest_post
rg -A 5 "publish_guest_post|formSubmit|wpuf_form_submit" --type js

Length of output: 22151


Based on our review, there’s a breaking change on the server side: using wp_send_json_error() returns an object of the form

{
  success: false,
  data: {}
}

instead of just a raw response at the root level. This means that any client code handling the response must now look for the result under the data property. Our initial search of the frontend JS (in assets/js/frontend-form.js) did not reveal any clear handling of the updated structure (for example, checks looking for response.data when processing error responses).

Please verify that the JavaScript handling of the AJAX response (especially in the formSubmit function in assets/js/frontend-form.js) has been updated to expect the payload inside the data property. Any code that was previously accessing the response directly (e.g. via response instead of response.data) will need updating to avoid breaking error handling downstream.

Actionable items:

  • Review and update the AJAX success/error handling in assets/js/frontend-form.js so that error responses properly extract the error details from response.data.
  • Test the guest post submission flow to ensure errors are handled correctly after this backend change.

@sapayth sapayth self-assigned this Feb 6, 2025
@Rubaiyat-E-Mohammad Rubaiyat-E-Mohammad added QA In Progress QA Approved This PR is approved by the QA team Ready to Merge This PR is now ready to be merged and removed needs: testing QA In Progress labels Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
QA Approved This PR is approved by the QA team Ready to Merge This PR is now ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants