Skip to content

Conversation

@mralaminahamed
Copy link
Contributor

@mralaminahamed mralaminahamed commented Jun 27, 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:

This PR enhances the product brand integration in Dokan by refactoring the brand management system with a more structured approach. The changes include:

  1. Unified Brand Management: Moved brand handling logic from the Products template to dedicated Manager and Hooks classes following WordPress/WooCommerce coding standards
  2. Template Separation: Created a dedicated product-brand.php template for better code organization and reusability
  3. Method Standardization: Added proper methods in the Product Manager for brand operations (save_brands(), get_brands(), get_brand_ids())
  4. Hook-based Architecture: Implemented proper WordPress hooks for brand template rendering and data saving
  5. Security Improvements: Enhanced capability checks and data sanitization following WordPress security best practices

Related Pull Request(s)

Closes

How to test the changes in this Pull Request:

  1. Navigate to Dokan vendor dashboard → Products → Edit any product
  2. Verify the Brand field is displayed correctly using the new template
  3. Select one or multiple brands and save the product
  4. Confirm brands are saved properly and displayed on product edit
  5. Test with different user capabilities to ensure proper permission checks
  6. Verify the brand data is properly sanitized and escaped

Changelog entry

Title: Enhanced Product Brand Integration with Dedicated Template and Manager Methods

Detailed Description:

Previous Behavior: Brand management logic was directly embedded in the Products template class with inline HTML and basic functionality.

New Behavior: Brand management now uses a dedicated template system with proper Manager methods for data handling. The code follows WordPress coding standards with improved security, better organization, and enhanced maintainability. Brand operations are now handled through dedicated methods in the Product Manager class with proper hooks integration.

Before Changes

  • Brand management logic was mixed with product template rendering
  • Direct database operations without proper abstraction
  • Inline HTML in PHP classes reducing code maintainability
  • Limited reusability of brand-related functionality

After Changes

  • Clean separation of concerns with dedicated template and manager methods
  • Proper abstraction layer for brand operations through Manager class
  • Reusable template system following WordPress standards
  • Enhanced security with capability checks and data sanitization
  • Better code organization following WooCommerce extension development guidelines

Feature Video (optional)

N/A - This is a code refactoring enhancement

PR Self Review Checklist:

  • Code is following WordPress coding style guidelines
  • Good naming: methods and variables are self-explanatory and follow WordPress conventions
  • KISS: Simple, clean approach using WordPress/WooCommerce standard patterns
  • DRY: Eliminated code duplication by centralizing brand operations
  • Code is readable: proper method organization and clear separation of concerns
  • No performance issues: efficient use of WordPress functions
  • Self-explanatory constructions with proper PHPDoc comments
  • Grammar and documentation are correct

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

Summary by CodeRabbit

  • New Features

    • Added support for managing product brands in the product editing interface, allowing selection and assignment of multiple brands to a product.
    • Introduced a new template for displaying and selecting product brands.
  • Refactor

    • Updated the product editing form to use a dynamic hook for inserting the product brand selection UI, replacing the previous static form group.
  • Bug Fixes

    • Improved handling and saving of product brand data during product creation and updates.

@mralaminahamed mralaminahamed requested a review from mrabbani June 27, 2025 11:43
@mralaminahamed mralaminahamed self-assigned this Jun 27, 2025
@mralaminahamed mralaminahamed added Needs: Testing This requires further testing Needs: Dev Review It requires a developer review and approval labels Jun 27, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 27, 2025

"""

Walkthrough

This change refactors the handling of product brands in the product management workflow. Brand selection is moved from a static form field to a dynamic, hook-based approach, with new methods for saving and retrieving brand data. A dedicated template for brand selection is introduced, and legacy brand-handling code is removed.

Changes

File(s) Change Summary
includes/Dashboard/Templates/Products.php Removed direct handling of product brand assignment during product update.
includes/Product/Hooks.php Added hooks and methods for displaying and saving product brands using WooCommerce taxonomy and template integration.
includes/Product/Manager.php Added methods for saving, retrieving, and getting IDs of product brands for a product.
templates/products/edit-product-single.php Replaced in-template brand selection field with a dynamic action hook after pricing fields.
templates/products/product-brand.php Added a new template for rendering a multiple-select product brand dropdown.

Sequence Diagram(s)

sequenceDiagram
    participant Vendor as Vendor
    participant Dashboard as Vendor Dashboard
    participant Hooks as Product Hooks
    participant Manager as Product Manager

    Vendor->>Dashboard: Edit Product
    Dashboard->>Hooks: do_action('dokan_product_edit_after_pricing_fields')
    Hooks->>Manager: get_brands(product_id)
    Manager-->>Hooks: brand terms
    Hooks->>Dashboard: include product-brand.php template with brand data

    Vendor->>Dashboard: Submit Product Form
    Dashboard->>Hooks: do_action('dokan_new_product_added' or 'dokan_product_updated')
    Hooks->>Manager: save_brands(product_id, brand_ids)
    Manager-->>Hooks: brands saved
Loading

Assessment against linked issues

Objective Addressed Explanation
Prevent "Brand" field from appearing twice in Dokan Vendor Dashboard due to overlap with YITH WooCommerce Brands Add-On (#4515)
Ensure only one "Brand" option is shown in product creation/edit forms (#4515)
Refactor brand field rendering and saving to avoid duplication (#4515)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Suggested labels

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

Suggested reviewers

  • mrabbani

Poem

A brand new way for brands to show,
Only once, not two in a row!
With hooks and templates, clean and neat,
The dashboard now is quite a treat.
One dropdown shines, the clutter gone—
The rabbit hops, the work is done! 🐇✨
"""

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.

🔧 phpcs (3.7.2)
includes/Product/Manager.php

📜 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 849a9fc and d40485f.

📒 Files selected for processing (1)
  • includes/Product/Manager.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • includes/Product/Manager.php
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: e2e tests (1, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: api tests (1, 1)
✨ 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.

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: 0

🧹 Nitpick comments (1)
includes/Product/Manager.php (1)

805-821: Consider simplifying the error handling logic.

The method has redundant error checking since get_brands() already handles WP_Error cases by returning an empty array.

 public function get_brand_ids( int $product_id ): array {
     $brands = $this->get_brands( $product_id, 'ids' );
-    if ( is_wp_error( $brands ) || count( $brands ) === 0 ) {
+    if ( count( $brands ) === 0 ) {
         return [];
     }

     return $brands;
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 97b88e3 and 849a9fc.

📒 Files selected for processing (5)
  • includes/Dashboard/Templates/Products.php (0 hunks)
  • includes/Product/Hooks.php (2 hunks)
  • includes/Product/Manager.php (1 hunks)
  • templates/products/edit-product-single.php (1 hunks)
  • templates/products/product-brand.php (1 hunks)
💤 Files with no reviewable changes (1)
  • includes/Dashboard/Templates/Products.php
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: e2e tests (1, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: api tests (1, 1)
🔇 Additional comments (7)
includes/Product/Manager.php (2)

771-784: LGTM! Brand saving method follows WordPress standards.

The save_brands() method correctly sanitizes input and uses the appropriate WordPress function wp_set_object_terms() for taxonomy management. The absint() sanitization ensures brand IDs are valid integers.


786-803: Excellent error handling and flexibility in brand retrieval.

The get_brands() method provides good flexibility with the $fields parameter and properly handles WP_Error cases by returning an empty array. The use of type hints enhances code reliability.

templates/products/product-brand.php (1)

1-24: Well-structured template with proper security measures.

The template correctly implements:

  • Proper escaping functions (esc_html_e, esc_attr_e, esc_attr, esc_html)
  • Internationalization with appropriate text domain
  • Semantic HTML structure with proper labels and form controls
  • Accessibility considerations with data-placeholder attribute
templates/products/edit-product-single.php (1)

392-392: Excellent modularization with hook-based architecture.

The replacement of static brand selection UI with the dokan_product_edit_after_pricing_fields hook promotes extensibility and separation of concerns. The hook placement after pricing fields is logical and maintains good UX flow.

includes/Product/Hooks.php (3)

36-39: Well-integrated hook registration for brand management.

The hooks are properly registered for the complete product lifecycle:

  • Brand saving on product creation and updates
  • Template rendering via dedicated hook
    This ensures consistent brand handling across all product operations.

577-594: Robust template rendering with proper security checks.

The add_product_brand_template() method correctly:

  • Validates user capabilities with dokan_edit_product
  • Retrieves brand data using the Manager class
  • Uses proper template loading with dokan_get_template_part()
  • Follows WordPress naming conventions

596-613: Secure brand updating with proper data handling.

The update_product_brands_by_id() method implements:

  • Capability verification for product editing
  • Safe array access with null coalescing operator
  • Integration with Manager's brand saving method
  • Proper parameter typing

@mrabbani mrabbani added Needs: Author Reply and removed Needs: Dev Review It requires a developer review and approval labels Jul 4, 2025
@mralaminahamed mralaminahamed added Needs: Dev Review It requires a developer review and approval and removed Needs: Author Reply labels Jul 6, 2025
@mrabbani mrabbani added Dev Review Done and removed Needs: Dev Review It requires a developer review and approval labels Jul 10, 2025
@dev-shahed dev-shahed added QA In Progress and removed Needs: Testing This requires further testing labels Jul 14, 2025
@dev-shahed
Copy link
Member

@mralaminahamed All YITH brand moved into the WooCommerce brand field - OK
Please have a look at this button, this button does not work: https://prnt.sc/4SNnTQDkVl6V

@dev-shahed dev-shahed added 🎉 QA Approved This PR is approved by the QA team QA In Progress and removed QA In Progress labels Jul 14, 2025
@Aunshon Aunshon merged commit 73779e2 into develop Jul 15, 2025
2 of 6 checks passed
@mralaminahamed mralaminahamed deleted the refactor/unified-product-brands-integration branch July 23, 2025 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants