Skip to content

Optimize vendor selection with AJAX in quick/bulk edit to improve performance and UX #2807

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: develop
Choose a base branch
from

Conversation

kzamanbd
Copy link
Contributor

@kzamanbd kzamanbd commented Jul 23, 2025

All Submissions:

  • My code follow the WordPress' coding standards
  • My code satisfies feature requirements
  • My code is tested
  • My code passes the PHPCS tests
  • My code has proper inline documentation
  • I've included related pull request(s) (optional)
  • I've included developer documentation (optional)
  • I've added proper labels to this pull request

Changes proposed in this Pull Request:

Related Pull Request(s)

  • Full PR Link

Closes

How to test the changes in this Pull Request:

  • Steps or issue link

Changelog entry

- **update:** Improved performance by replacing vendor selection with AJAX-powered dynamic dropdown in product quick/bulk edit, and improved inline edit event handling.

Before Changes

Describe the issue before changes with screenshots(s).

After Changes

Describe the issue after changes with screenshot(s).

Feature Video (optional)

Link of detailed video if this PR is for a feature.

PR Self Review Checklist:

  • Code is not following code style guidelines
  • Bad naming: make sure you would understand your code if you read it a few months from now.
  • KISS: Keep it simple, Sweetie (not stupid!).
  • DRY: Don't Repeat Yourself.
  • Code that is not readable: too many nested 'if's are a bad sign.
  • Performance issues
  • Complicated constructions that need refactoring or comments: code should almost always be self-explanatory.
  • Grammar errors.

FOR PR REVIEWER ONLY:

As a reviewer, your feedback should be focused on the idea, not the person. Seek to understand, be respectful, and focus on constructive dialog.

As a contributor, your responsibility is to learn from suggestions and iterate your pull request should it be needed based on feedback. Seek to collaborate and produce the best possible contribution to the greater whole.

  • Correct — Does the change do what it’s supposed to? ie: code 100% fulfilling the requirements?
  • Secure — Would a nefarious party find some way to exploit this change? ie: everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities?
  • Readable — Will your future self be able to understand this change months down the road?
  • Elegant — Does the change fit aesthetically within the overall style and architecture?

Summary by CodeRabbit

  • New Features
    • Vendor selection in the quick edit and bulk edit forms is now a dynamic, searchable dropdown, making it easier to find and assign vendors.
  • Refactor
    • Improved event handling and scope differentiation for vendor selection in product editing forms.
  • Chores
    • Made internal admin product functionality accessible via the browser console for easier debugging and external integrations.

Updated the vendor selection field in the WooCommerce product quick edit to use a dynamic select input with AJAX search, improving scalability and user experience. Exposed DokanAdminProduct to the global window object for use in inline edit scripts. Minor code cleanup and improved event handling for inline edit actions.
Copy link
Contributor

coderabbitai bot commented Jul 23, 2025

Walkthrough

The changes expose the DokanAdminProduct JavaScript object globally by attaching it to the window object. In PHP, the quick edit vendor selection is refactored to use a dynamic, AJAX-powered searchable select element, with improved event handling and scope differentiation for quick and bulk edit forms.

Changes

File(s) Change Summary
assets/src/js/dokan-admin-product.js Exposed DokanAdminProduct globally via window.DokanAdminProduct assignment inside DOM ready callback.
includes/wc-template.php Refactored dokan_author_field_quick_edit() to add $scope param, switched static vendor select to AJAX search, improved event handling, and differentiated quick/bulk edit scopes.

Sequence Diagram(s)

sequenceDiagram
    participant Admin as Admin User
    participant Browser as Browser (JS)
    participant Server as Server (PHP/AJAX)
    Admin->>Browser: Opens Quick Edit for Product
    Browser->>Browser: Inline edit form appears
    Browser->>Browser: window.DokanAdminProduct.searchVendors() initializes AJAX search
    Admin->>Browser: Types in vendor field
    Browser->>Server: AJAX request for vendor search
    Server-->>Browser: Returns matching vendors
    Browser->>Admin: Displays searchable vendor dropdown
Loading

Estimated code review effort

2 (~15 minutes)

Suggested labels

QA approved, :+1: Dev Review Done, Upcoming Release

Suggested reviewers

  • mrabbani

Poem

In the meadow of code, a change takes flight,
Vendors now searchable, swift as a kite.
With AJAX they dance, no more static to see,
And DokanAdminProduct waves from the window, with glee.
Reviewers, hop in—let’s make this bug flee! 🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-07-23T07_42_21_513Z-debug-0.log

✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.

@kzamanbd kzamanbd requested a review from mrabbani July 23, 2025 07:40
@kzamanbd kzamanbd self-assigned this Jul 23, 2025
@kzamanbd kzamanbd added Needs: Testing This requires further testing Needs: Dev Review It requires a developer review and approval labels Jul 23, 2025
Copy link
Contributor

@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

🧹 Nitpick comments (2)
assets/src/js/dokan-admin-product.js (1)

89-89: Consider the implications of global exposure.

Exposing DokanAdminProduct globally enables external access to its methods, which aligns with the PR's goal of enabling AJAX vendor selection in quick edit. However, this creates a global dependency that could lead to namespace pollution.

Consider these alternatives:

  1. Use a more specific namespace like window.Dokan = window.Dokan || {}; window.Dokan.AdminProduct = DokanAdminProduct;
  2. Only expose specific methods that need external access
  3. Document this global API for maintainability
-        window.DokanAdminProduct = DokanAdminProduct;
+        // Expose DokanAdminProduct globally for external scripts (e.g., quick edit)
+        window.Dokan = window.Dokan || {};
+        window.Dokan.AdminProduct = DokanAdminProduct;
includes/wc-template.php (1)

183-193: Improved event handling with potential timing issue.

The use of delegated event binding and setTimeout is a good approach for dynamically created elements. However, the 100ms delay might not be sufficient in all cases.

Consider using a more robust approach to ensure the element is ready:

-                setTimeout(function() {
-                    const element = $(selector).find('.dokan_product_author_override_quick');
-                    if( ! $(element).hasClass('select2-hidden-accessible')) {
-                        window.DokanAdminProduct.searchVendors(element);
-                    }
-                }, 100)
+                // Poll until element is ready or timeout
+                let attempts = 0;
+                const maxAttempts = 50; // 5 seconds max
+                const checkElement = function() {
+                    const element = $(selector).find('.dokan_product_author_override_quick');
+                    if (element.length && !element.hasClass('select2-hidden-accessible')) {
+                        window.DokanAdminProduct.searchVendors(element);
+                    } else if (attempts < maxAttempts) {
+                        attempts++;
+                        setTimeout(checkElement, 100);
+                    }
+                };
+                checkElement();
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e5d4bc3 and 1ca47f7.

📒 Files selected for processing (2)
  • assets/src/js/dokan-admin-product.js (1 hunks)
  • includes/wc-template.php (2 hunks)
🔇 Additional comments (4)
includes/wc-template.php (4)

40-40: Minor text change looks good.

The text change to "Vendor Info" improves clarity and consistency.


158-158: Function signature change is well-designed.

Adding the optional $scope parameter allows differentiation between quick and bulk edit contexts while maintaining backward compatibility.


199-208: Action hook implementation is well-structured.

The separation of quick edit and bulk edit with different scope parameters is a clean approach that enables proper differentiation of behavior.


169-176: Security validation in search_vendors confirmed

The AJAX handler in includes/Admin/Hooks.php already enforces:

  • current_user_can( 'manage_woocommerce' ) capability check
  • Nonce verification via wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'dokan_admin_product' )
  • Sanitization of the search input (sanitize_text_field + wp_unslash)

No further action needed.

setTimeout(function() {
const element = $(selector).find('.dokan_product_author_override_quick');
if( ! $(element).hasClass('select2-hidden-accessible')) {
window.DokanAdminProduct.searchVendors(element);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Verify global DokanAdminProduct availability.

The code assumes window.DokanAdminProduct is always available. Add a safety check to prevent errors if the script hasn't loaded yet.

-                    if( ! $(element).hasClass('select2-hidden-accessible')) {
-                        window.DokanAdminProduct.searchVendors(element);
-                    }
+                    if( ! $(element).hasClass('select2-hidden-accessible')) {
+                        if (window.DokanAdminProduct && typeof window.DokanAdminProduct.searchVendors === 'function') {
+                            window.DokanAdminProduct.searchVendors(element);
+                        } else {
+                            console.warn('DokanAdminProduct not available for vendor search');
+                        }
+                    }
🤖 Prompt for AI Agents
In includes/wc-template.php at line 190, the code calls
window.DokanAdminProduct.searchVendors without checking if
window.DokanAdminProduct exists. Add a conditional check to verify
window.DokanAdminProduct is defined before calling searchVendors to avoid
runtime errors if the script is not loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Dev Review It requires a developer review and approval Needs: Testing This requires further testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant