-
Notifications
You must be signed in to change notification settings - Fork 152
Fix: Display term names instead of term IDs for product_cat and product_shipping_class on product page #1649
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
base: develop
Are you sure you want to change the base?
Conversation
…ct_shipping_class on product page
WalkthroughThe update refactors the Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~35 minutes Suggested labels
Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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/Traits/FieldableTrait.php (1)
512-512
: Fix formatting: Add space beforeis_wp_error
.-if ( !is_wp_error($new_term) ) { +if ( ! is_wp_error( $new_term ) ) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
includes/Traits/FieldableTrait.php
(11 hunks)
🧰 Additional context used
🪛 GitHub Actions: Inspections
includes/Traits/FieldableTrait.php
[warning] 1-1: This comment is 64% valid code; is this commented out code?
[error] 1-1: Opening parenthesis of a multi-line function call must be the last content on the line
🔇 Additional comments (8)
includes/Traits/FieldableTrait.php (8)
119-183
: Good implementation of WooCommerce taxonomy handling.The strict type comparisons and WooCommerce-specific taxonomy normalization are well implemented. The normalization of
product_category
toproduct_cat
correctly handles variations in taxonomy naming, and the addition of theis_woocommerce
flag enables proper downstream processing.
222-225
: Appropriate use of strict comparisons for reCAPTCHA type checking.The change from loose to strict comparisons for reCAPTCHA type checking improves type safety and prevents potential type coercion issues.
279-311
: Excellent security improvement with strict comparison for reCAPTCHA validation.The strict comparisons, particularly
true !== $response['success']
at line 311, ensure proper boolean validation without type coercion, which is crucial for security checks.
458-661
: Excellent refactoring of taxonomy handling that addresses the PR objective.This comprehensive refactoring properly handles the conversion of term IDs to term names for display, which directly addresses the issue mentioned in the PR title. The implementation correctly:
- Handles both numeric IDs and string names for product categories
- Converts term IDs to names for product tags
- Provides special handling for shipping classes including empty value scenarios
- Maintains compatibility with both hierarchical and non-hierarchical taxonomies
721-807
: Consistent application of strict comparisons in meta field preparation.The strict type comparisons ensure accurate field type checking and configuration value comparisons.
860-908
: Well-implemented term ID to name conversion for WooCommerce attributes.The rewritten method correctly:
- Skips product categories which are handled elsewhere
- Converts term IDs to their display names
- Creates missing terms when necessary
- Returns properly formatted attribute data for WooCommerce
This directly supports the PR objective of displaying term names instead of IDs.
839-839
: Appropriate use of strict comparison in array search.Using strict comparison ensures exact value matching without type coercion, which is important for accurate search results.
453-908
: Verify the fix displays term names correctly on the product page.The implementation looks solid and should resolve the issue of displaying term IDs instead of names. Please ensure to test the following scenarios:
- Product categories display names instead of IDs
- Shipping classes display names instead of IDs
- Product tags and attributes show proper names
- Terms are correctly created when they don't exist
includes/Traits/FieldableTrait.php
Outdated
$tax = [ $tax ]; | ||
// Handle other WooCommerce taxonomies if WooCommerce is enabled | ||
if ( $is_woo_taxonomy && ( | ||
'pa_' === substr( $taxonomy_name, 0, 3 ) || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not check this way. use This taxonomy is a WooCommerce attribute
option from form builder field setting
There was a problem hiding this 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
♻️ Duplicate comments (1)
includes/Traits/FieldableTrait.php (1)
474-474
: Consider using form builder field setting for WooCommerce attribute detection.Based on a previous review comment, consider using the "This taxonomy is a WooCommerce attribute" option from the form builder field setting instead of checking the
woo_attr
field directly.
🧹 Nitpick comments (1)
includes/Traits/FieldableTrait.php (1)
453-661
: Comprehensive WooCommerce taxonomy improvements with refactoring opportunity.The enhanced taxonomy handling significantly improves WooCommerce support with proper term creation, ID/name conversion, and attribute management. The logic is sound and handles edge cases well.
However, the method has become quite complex. Consider breaking it into smaller, focused methods for better maintainability:
private function handle_woo_product_categories($post_id, $posted_terms) { /* ... */ } private function handle_woo_product_tags($post_id, $posted_terms) { /* ... */ } private function handle_standard_taxonomy($post_id, $taxonomy, $posted_terms) { /* ... */ }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
includes/Traits/FieldableTrait.php
(11 hunks)
🔇 Additional comments (5)
includes/Traits/FieldableTrait.php (5)
119-189
: Excellent improvements to WooCommerce taxonomy handling!The changes enhance type safety with strict comparisons and add comprehensive WooCommerce taxonomy support. The normalization of
product_category
toproduct_cat
and the automatic flagging of WooCommerce taxonomies will improve consistency.
222-231
: LGTM - Type safety improvements.The strict equality comparisons for reCAPTCHA type checking enhance type safety and prevent potential type coercion issues.
279-314
: LGTM - Enhanced type safety in reCAPTCHA validation.The strict integer comparisons and boolean checks improve the reliability of reCAPTCHA validation logic.
721-721
: LGTM - Consistent strict type comparisons.The strict equality checks for string values improve type safety and consistency throughout the method.
Also applies to: 805-807
860-908
: Excellent rewrite of WooCommerce attribute handling!The method now properly:
- Handles term ID to name conversion
- Creates missing terms automatically
- Skips product categories appropriately
- Returns properly formatted WooCommerce attribute arrays
The logic is robust with good error handling and edge case management.
There was a problem hiding this 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/Traits/FieldableTrait.php (1)
474-521
: Robust product category handling with term creation.The logic properly handles both term IDs and names for product categories, creating missing terms automatically. This addresses the PR objective of improving WooCommerce taxonomy support.
Minor formatting issue on Line 512:
- if ( !is_wp_error($new_term) ) { + if ( ! is_wp_error( $new_term ) ) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
includes/Traits/FieldableTrait.php
(11 hunks)
🔇 Additional comments (12)
includes/Traits/FieldableTrait.php (12)
119-119
: LGTM! Improved type safety with strict comparisons.The change from loose (
==
) to strict (===
) equality comparisons enhances type safety and prevents unexpected type coercion issues.Also applies to: 131-131, 136-136, 159-159
164-183
: Excellent WooCommerce taxonomy handling enhancement.The new logic properly normalizes WooCommerce taxonomy names and sets appropriate flags:
- Normalizes
product_category
toproduct_cat
for consistency- Correctly identifies WooCommerce taxonomies with flags
- Handles shipping class with proper hierarchical setting and default input type
This aligns well with the PR objectives to improve WooCommerce taxonomy support.
222-222
: LGTM! Consistent type safety improvement.The strict string comparisons for reCAPTCHA type validation maintain consistency with the broader type safety enhancements throughout the file.
Also applies to: 225-225
279-279
: LGTM! Proper integer flag validation.The strict integer comparisons for reCAPTCHA flags ensure exact value matching and prevent issues with truthy/falsy value interpretation.
Also applies to: 295-295, 304-304, 311-311
458-471
: LGTM! Clean initialization and default term handling.The new structure properly initializes variables and handles default terms when no terms are posted, improving the robustness of the taxonomy assignment process.
524-550
: Excellent product tag handling with ID-to-name conversion.The implementation correctly converts term IDs to names for product tags and handles both single and array inputs. This ensures proper WooCommerce tag assignment.
553-580
: Comprehensive WooCommerce taxonomy and attribute handling.The logic properly handles product attributes (pa_ prefix), shipping classes, and other WooCommerce taxonomies. The attribute metadata preparation for WooCommerce is correctly structured.
582-652
: Well-structured regular taxonomy processing.The implementation correctly differentiates between hierarchical and non-hierarchical taxonomies, handles text vs select inputs appropriately, and includes proper term creation logic. The WooCommerce attribute integration is seamless.
655-661
: Proper WooCommerce attributes persistence.The final step correctly updates the
_product_attributes
post meta when WooCommerce attributes are present, ensuring proper integration with WooCommerce's attribute system.
721-721
: LGTM! Consistent type safety enhancement.The strict string comparison for the multiple field check maintains consistency with the type safety improvements throughout the file.
805-805
: LGTM! Type safety for ACF compatibility checks.The strict string comparisons for address input type and ACF compatibility checks are appropriate and maintain consistency with the overall type safety improvements.
Also applies to: 807-807
862-908
: WooCommerce attribute integration aligns with form builder settings
- Field definitions in includes/Fields/Field_Contract.php declare the
woo_attr
("This taxonomy is a WooCommerce attribute") andwoo_attr_vis
("Visible on product page") options.- Admin template (includes/Admin/Forms/Template_Post.php) and front-end handler (includes/class-frontend-render-form.php) both check
woo_attr === 'yes'
before callingwoo_attribute()
.- In FieldableTrait (includes/Traits/FieldableTrait.php),
$is_woo_taxonomy
is derived viawpuf_is_checkbox_or_toggle_on( $taxonomy['woo_attr'] )
, andwoo_attribute()
respectswoo_attr_vis
for visibility.The rewrite correctly leverages the form builder’s WooCommerce flag; no further changes required.
Could not built properly @arifulhoque7 vai |
There was a problem hiding this 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/Frontend/Frontend_Form.php (1)
23-23
: Remove trailing whitespace.Static analysis detected trailing whitespace at the end of this line.
- +
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
includes/Ajax/Frontend_Form_Ajax.php
(1 hunks)includes/Frontend/Frontend_Form.php
(1 hunks)includes/Traits/FieldableTrait.php
(11 hunks)
✅ Files skipped from review due to trivial changes (1)
- includes/Ajax/Frontend_Form_Ajax.php
🧰 Additional context used
🧬 Code Graph Analysis (1)
includes/Frontend/Frontend_Form.php (1)
includes/Traits/FieldableTrait.php (1)
init_woocommerce_hooks
(984-988)
🪛 GitHub Check: Run PHPCS inspection
includes/Frontend/Frontend_Form.php
[failure] 23-23:
Whitespace found at end of line
🔇 Additional comments (8)
includes/Frontend/Frontend_Form.php (1)
24-27
: LGTM! Proper conditional initialization of WooCommerce hooks.The conditional check ensures that WooCommerce hooks are only initialized when the method exists, following good defensive programming practices. This aligns well with the enhanced WooCommerce taxonomy support mentioned in the PR objectives.
includes/Traits/FieldableTrait.php (7)
119-119
: LGTM! Consistent use of strict type comparisons.The changes from loose to strict equality comparisons (
===
) improve type safety and reduce potential bugs from unexpected type coercion.Also applies to: 131-131, 136-136, 159-159, 164-164
165-184
: Excellent WooCommerce taxonomy normalization.The added logic properly handles WooCommerce-specific taxonomies by:
- Normalizing
product_category
to the correctproduct_cat
taxonomy name- Flagging WooCommerce taxonomies with appropriate metadata
- Setting correct defaults for shipping classes
This addresses the PR objective of improving WooCommerce taxonomy support.
222-222
: LGTM! Improved reCAPTCHA validation with strict comparisons.The changes from loose to strict equality comparisons in reCAPTCHA validation enhance type safety and make the conditional logic more predictable.
Also applies to: 225-225, 279-279, 295-295, 304-304, 311-311
453-707
: Comprehensive refactor of taxonomy handling with excellent WooCommerce integration.This is a significant improvement that:
- Adds support for default terms when no terms are posted
- Implements automatic term creation for missing WooCommerce terms
- Provides specialized handling for each WooCommerce taxonomy type
- Maintains proper hierarchical vs non-hierarchical taxonomy handling
- Integrates with WooCommerce's product attributes system
The logic is well-structured and handles edge cases appropriately.
767-767
: LGTM! Consistent strict type comparisons in meta field processing.The changes maintain the pattern of using strict equality comparisons for better type safety throughout the meta field processing logic.
Also applies to: 851-851, 853-853
906-976
: Excellent rewrite of thewoo_attribute
method.The rewritten method provides significant improvements:
- Excludes main WooCommerce taxonomies from additional information display unless explicitly visible
- Handles both numeric term IDs and text inputs properly
- Creates missing terms automatically for text inputs
- Prioritizes form-level labels over WooCommerce defaults
- Returns detailed attribute metadata with proper formatting
This addresses the core issue mentioned in the PR title about displaying term names instead of IDs.
978-1172
: Excellent WooCommerce integration with comprehensive attribute filtering.The new methods provide robust WooCommerce integration:
init_woocommerce_hooks()
- Safely initializes WooCommerce filters only when WooCommerce is activefilter_woocommerce_product_attributes()
- Comprehensive filtering that:
- Removes unwanted taxonomies from product display
- Ensures proper label prioritization (form-level → WooCommerce → formatted fallback)
- Converts term IDs to human-readable names
- Handles special cases for shipping classes and visibility terms
- Strips HTML tags from values
This directly addresses the PR objective of displaying term names instead of term IDs and provides excellent user experience improvements.
done vai check this @Rubaiyat-E-Mohammad |
Detected issues in the following video! @arifulhoque7 vai CleanShot.2025-08-01.at.16.04.38.1.mp4 |
please check now it is okay @Rubaiyat-E-Mohammad vai |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🔭 Outside diff range comments (1)
assets/css/admin/form-builder.css (1)
1219-1246
: Four identical rule blocks inflate bundle size.The block that styles
.wpuf-btn-outline.wpuf-btn-primary:hover
appears four times (1219-1246, 1231-1247, 1243-1249 etc.). Besides increasing CSS size ~140 bytes per copy, it makes maintenance error-prone.Consolidate them into a single block:
-/* three duplicated blocks removed for brevity */ .wpuf-btn-outline.wpuf-btn-primary:hover { --tw-text-opacity: 1; color: var(--fallback-pc,oklch(var(--pc)/var(--tw-text-opacity))); } @supports (color: color-mix(in oklab, black, black)) { .wpuf-btn-outline.wpuf-btn-primary:hover { background-color: color-mix(in oklab, var(--fallback-p,oklch(var(--p)/1)) 90%, black); border-color: color-mix(in oklab, var(--fallback-p,oklch(var(--p)/1)) 90%, black); } }
Removing the redundant copies shaves ~0.5 KB off the file and simplifies future edits.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
admin/form-builder/assets/js/form-builder.js
(1 hunks)assets/css/admin/form-builder.css
(35 hunks)assets/js-templates/form-components.php
(1 hunks)assets/js/wpuf-form-builder.js
(1 hunks)
🧰 Additional context used
🪛 Biome (2.1.2)
assets/css/admin/form-builder.css
[error] 851-851: Duplicate properties can lead to unexpected behavior and may override previous declarations unintentionally.
transition-property is already defined here.
Remove or rename the duplicate property to ensure consistent styling.
(lint/suspicious/noDuplicateProperties)
[error] 1799-1799: Duplicate properties can lead to unexpected behavior and may override previous declarations unintentionally.
transition-property is already defined here.
Remove or rename the duplicate property to ensure consistent styling.
(lint/suspicious/noDuplicateProperties)
🔇 Additional comments (3)
assets/js-templates/form-components.php (1)
1439-1449
: LGTM! Improved UX with controlled taxonomy selection.The change from a text input to a select dropdown for the 'ajax' type taxonomy field is well-implemented. This provides better user experience by offering predefined options instead of free text entry, which aligns with the backend WooCommerce taxonomy handling improvements. The implementation correctly uses Vue.js data binding and maintains consistency with other select elements in the template.
Key benefits:
- More controlled and predictable user input
- Better data validation and consistency
- Improved accessibility with semantic select element
- Consistent styling with other form selects
admin/form-builder/assets/js/form-builder.js (1)
139-143
: LGTM! Simplified field update logic.The removal of the
is_new
property check streamlines the field update logic, allowing more consistent updates regardless of field state. This aligns well with the WooCommerce taxonomy enhancements mentioned in the PR.assets/js/wpuf-form-builder.js (1)
139-143
: LGTM! Consistent with admin form builder changes.This change mirrors the identical modification in the admin form builder, ensuring consistent field update behavior across different contexts. The removal of the
is_new
property check maintains synchronization between form builder implementations.
There was a problem hiding this 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 (1)
assets/css/admin/form-builder.css (1)
1000-1014
: Collapse repeated transition rules in.wpuf-dropdown-content
transition-property
andtransition-timing-function
are each declared three times, creating the same duplicate-property error that breaks the build.- transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); - transition-timing-function: cubic-bezier(0, 0, 0.2, 1); - visibility: hidden; - opacity: 0; - transform-origin: top; - --tw-scale-x: .95; - --tw-scale-y: .95; - transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y)); - transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, transform, filter, -webkit-backdrop-filter; - transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter; - transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter, -webkit-backdrop-filter; - transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); - transition-timing-function: cubic-bezier(0, 0, 0.2, 1); + transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); + visibility: hidden; + opacity: 0; + transform-origin: top; + --tw-scale-x: .95; + --tw-scale-y: .95; + transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate)) + skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) + scaleY(var(--tw-scale-y)); + transition-property: color, background-color, border-color, text-decoration-color, + fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter, + -webkit-backdrop-filter;
♻️ Duplicate comments (2)
assets/css/admin/form-builder.css (2)
850-853
: Deduplicatetransition-property
declarations
Three identicaltransition-property
rules are declared back-to-back. Keep one canonical version to avoid Biome’snoDuplicateProperties
error.- transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, transform, filter, -webkit-backdrop-filter; - transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter; - transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter, -webkit-backdrop-filter; + transition-property: color, background-color, border-color, text-decoration-color, + fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter, + -webkit-backdrop-filter;
1798-1801
: Duplicate transition directives in.wpuf-modal-box
transition-property
andtransition-timing-function
are specified twice, triggering the same linter failure flagged earlier. Keep one of each.- transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter, -webkit-backdrop-filter; - transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); - transition-timing-function: cubic-bezier(0, 0, 0.2, 1); + transition-property: color, background-color, border-color, text-decoration-color, + fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter, + -webkit-backdrop-filter; + transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
assets/css/admin/form-builder.css
(34 hunks)
🧰 Additional context used
🪛 Biome (2.1.2)
assets/css/admin/form-builder.css
[error] 851-851: Duplicate properties can lead to unexpected behavior and may override previous declarations unintentionally.
transition-property is already defined here.
Remove or rename the duplicate property to ensure consistent styling.
(lint/suspicious/noDuplicateProperties)
[error] 1800-1800: Duplicate properties can lead to unexpected behavior and may override previous declarations unintentionally.
transition-timing-function is already defined here.
Remove or rename the duplicate property to ensure consistent styling.
(lint/suspicious/noDuplicateProperties)
transition-timing-function: cubic-bezier(0, 0, 0.2, 1); | ||
transition-duration: 200ms; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove duplicated transition-timing-function
lines
Two successive transition-timing-function
declarations differ only in easing; the first one will be ignored. Pick one and drop the other to silence the linter.
- transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
- transition-timing-function: cubic-bezier(0, 0, 0.2, 1);
+ transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In assets/css/admin/form-builder.css around lines 854 to 855, there are two
successive transition-timing-function declarations with different easing values,
causing the first to be ignored. Remove one of the duplicate
transition-timing-function lines, keeping only the desired easing value to fix
the linter warning.
There was a problem hiding this 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 (1)
includes/Traits/FieldableTrait.php (1)
561-1200
: Well-designed specialized handlers for WooCommerce taxonomies.The handler methods are comprehensive and address specific requirements:
- Product type handler with extensive ID/slug mapping
- Proper term creation for missing terms
- Cache clearing for WooCommerce compatibility
- Extensive logging for debugging
However, there are unused parameters that should be cleaned up:
Apply this diff to remove unused parameters:
- protected function handle_product_type( $post_id, $taxonomy_name, $posted_terms ) { + protected function handle_product_type( $post_id, $posted_terms ) { // Log incoming data error_log( 'WPUF Product Type - Input: ' . print_r( $posted_terms, true ) ); - protected function handle_shipping_class( $post_id, $taxonomy_name, $posted_terms ) { + protected function handle_shipping_class( $post_id, $posted_terms ) { // Clear existing shipping classes wp_set_object_terms( $post_id, [], 'product_shipping_class' ); - protected function handle_product_visibility( $post_id, $taxonomy_name, $posted_terms ) { + protected function handle_product_visibility( $post_id, $posted_terms ) { error_log( 'WPUF Product Visibility - Input: ' . print_r( $posted_terms, true ) );And update the method calls accordingly in the main handler method.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
includes/Ajax/Frontend_Form_Ajax.php
(2 hunks)includes/Traits/FieldableTrait.php
(11 hunks)includes/functions/woocommerce-attribute-labels.php
(1 hunks)wpuf-functions.php
(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- wpuf-functions.php
🚧 Files skipped from review as they are similar to previous changes (1)
- includes/Ajax/Frontend_Form_Ajax.php
🧰 Additional context used
🪛 GitHub Check: Run PHPCS inspection
includes/functions/woocommerce-attribute-labels.php
[failure] 53-53:
Expected 1 newline at end of file; 0 found
[failure] 51-51:
Whitespace found at end of line
[failure] 47-47:
Whitespace found at end of line
[failure] 39-39:
Whitespace found at end of line
[failure] 34-34:
Whitespace found at end of line
[failure] 26-26:
Whitespace found at end of line
[failure] 18-18:
Whitespace found at end of line
🪛 PHPMD (2.15.0)
includes/Traits/FieldableTrait.php
623-623: Avoid unused parameters such as '$taxonomy_name'. (Unused Code Rules)
(UnusedFormalParameter)
766-766: Avoid unused parameters such as '$taxonomy_name'. (Unused Code Rules)
(UnusedFormalParameter)
981-981: Avoid unused parameters such as '$taxonomy_name'. (Unused Code Rules)
(UnusedFormalParameter)
🔇 Additional comments (5)
includes/functions/woocommerce-attribute-labels.php (1)
30-52
: LGTM! Well-structured label customization logic.The function follows a logical fallback hierarchy:
- Custom labels from product meta (highest priority)
- Predefined defaults for known WooCommerce taxonomies
- Original label (fallback)
The parameter validation and internationalization are properly implemented.
includes/Traits/FieldableTrait.php (4)
119-189
: LGTM! Improved type safety and WooCommerce taxonomy handling.The changes enhance the code in several important ways:
- Strict type comparisons (
===
) improve reliability and prevent type coercion issues- WooCommerce taxonomy normalization ensures consistent naming (e.g.,
product_category
→product_cat
)- Adding
is_woocommerce
flags enables specialized handling downstream- Setting
hierarchical=true
for shipping classes with default select type is appropriate
453-550
: Excellent refactor that properly separates WooCommerce from regular taxonomy handling.This refactor addresses the core PR objectives by:
- Storing form field labels in post meta for consistent display
- Delegating WooCommerce taxonomies to specialized handlers
- Adding comprehensive product data synchronization
- Including extensive logging for debugging taxonomy assignments
- Providing fallback logic for missing product types
The separation of concerns makes the code more maintainable and reliable.
1431-1501
: Perfect! This directly addresses the PR's core objective.The changes successfully fix the "Display term names instead of term IDs" issue by:
- Converting numeric term IDs to human-readable names
- Creating missing terms automatically for text inputs
- Prioritizing form field labels for consistent display
- Properly excluding main taxonomies from additional information display
The logic properly handles both numeric IDs and text values, ensuring reliable term name display.
1503-1644
: Comprehensive WooCommerce integration that completes the attribute display fixes.The hook methods provide:
- Proper integration with WooCommerce display filters
- Removal of unwanted taxonomies from additional information
- Extensive term ID to name conversion with special handling for shipping classes and visibility
- Form field label prioritization for consistent display
- Proper handling of edge cases and fallbacks
This ensures that WooCommerce product attributes display correctly with human-readable names instead of IDs.
|
||
add_filter( 'woocommerce_attribute_label', 'wpuf_customize_woocommerce_attribute_labels', 20, 3 ); | ||
} | ||
|
||
/** | ||
* Customize WooCommerce attribute labels based on form field labels | ||
* | ||
* @param string $label The attribute label | ||
* @param string $name The attribute name | ||
* @param WC_Product|null $product The product object | ||
* @return string Modified label | ||
*/ | ||
function wpuf_customize_woocommerce_attribute_labels( $label, $name, $product ) { | ||
// First check if we have stored labels for this specific product | ||
if ( $product && is_a( $product, 'WC_Product' ) ) { | ||
$taxonomy_labels = get_post_meta( $product->get_id(), '_wpuf_taxonomy_labels', true ); | ||
|
||
if ( ! empty( $taxonomy_labels ) && isset( $taxonomy_labels[ $name ] ) ) { | ||
return $taxonomy_labels[ $name ]; | ||
} | ||
} | ||
|
||
// If this is being displayed in admin or frontend without a product context, | ||
// or if no stored label exists, use proper default labels for known taxonomies | ||
$default_labels = [ | ||
'product_shipping_class' => __( 'Product shipping class', 'wp-user-frontend' ), | ||
'product_visibility' => __( 'Product visibility', 'wp-user-frontend' ), | ||
'product_type' => __( 'Product Type', 'wp-user-frontend' ), | ||
]; | ||
|
||
if ( isset( $default_labels[ $name ] ) ) { | ||
return $default_labels[ $name ]; | ||
} | ||
|
||
return $label; | ||
} No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix formatting issues identified by static analysis.
The code logic is correct, but there are several formatting issues that need to be addressed:
Apply this diff to fix the formatting issues:
- }
+ }
-
+
add_filter( 'woocommerce_attribute_label', 'wpuf_customize_woocommerce_attribute_labels', 20, 3 );
}
/**
* Customize WooCommerce attribute labels based on form field labels
*
* @param string $label The attribute label
- * @param string $name The attribute name
+ * @param string $name The attribute name
* @param WC_Product|null $product The product object
* @return string Modified label
*/
function wpuf_customize_woocommerce_attribute_labels( $label, $name, $product ) {
// First check if we have stored labels for this specific product
if ( $product && is_a( $product, 'WC_Product' ) ) {
$taxonomy_labels = get_post_meta( $product->get_id(), '_wpuf_taxonomy_labels', true );
-
+
if ( ! empty( $taxonomy_labels ) && isset( $taxonomy_labels[ $name ] ) ) {
return $taxonomy_labels[ $name ];
}
}
-
+
// If this is being displayed in admin or frontend without a product context,
// or if no stored label exists, use proper default labels for known taxonomies
$default_labels = [
'product_shipping_class' => __( 'Product shipping class', 'wp-user-frontend' ),
'product_visibility' => __( 'Product visibility', 'wp-user-frontend' ),
'product_type' => __( 'Product Type', 'wp-user-frontend' ),
];
-
+
if ( isset( $default_labels[ $name ] ) ) {
return $default_labels[ $name ];
}
-
+
return $label;
}
+
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
add_filter( 'woocommerce_attribute_label', 'wpuf_customize_woocommerce_attribute_labels', 20, 3 ); | |
} | |
/** | |
* Customize WooCommerce attribute labels based on form field labels | |
* | |
* @param string $label The attribute label | |
* @param string $name The attribute name | |
* @param WC_Product|null $product The product object | |
* @return string Modified label | |
*/ | |
function wpuf_customize_woocommerce_attribute_labels( $label, $name, $product ) { | |
// First check if we have stored labels for this specific product | |
if ( $product && is_a( $product, 'WC_Product' ) ) { | |
$taxonomy_labels = get_post_meta( $product->get_id(), '_wpuf_taxonomy_labels', true ); | |
if ( ! empty( $taxonomy_labels ) && isset( $taxonomy_labels[ $name ] ) ) { | |
return $taxonomy_labels[ $name ]; | |
} | |
} | |
// If this is being displayed in admin or frontend without a product context, | |
// or if no stored label exists, use proper default labels for known taxonomies | |
$default_labels = [ | |
'product_shipping_class' => __( 'Product shipping class', 'wp-user-frontend' ), | |
'product_visibility' => __( 'Product visibility', 'wp-user-frontend' ), | |
'product_type' => __( 'Product Type', 'wp-user-frontend' ), | |
]; | |
if ( isset( $default_labels[ $name ] ) ) { | |
return $default_labels[ $name ]; | |
} | |
return $label; | |
} | |
add_filter( 'woocommerce_attribute_label', 'wpuf_customize_woocommerce_attribute_labels', 20, 3 ); | |
} | |
/** | |
* Customize WooCommerce attribute labels based on form field labels | |
* | |
* @param string $label The attribute label | |
* @param string $name The attribute name | |
* @param WC_Product|null $product The product object | |
* @return string Modified label | |
*/ | |
function wpuf_customize_woocommerce_attribute_labels( $label, $name, $product ) { | |
// First check if we have stored labels for this specific product | |
if ( $product && is_a( $product, 'WC_Product' ) ) { | |
$taxonomy_labels = get_post_meta( $product->get_id(), '_wpuf_taxonomy_labels', true ); | |
if ( ! empty( $taxonomy_labels ) && isset( $taxonomy_labels[ $name ] ) ) { | |
return $taxonomy_labels[ $name ]; | |
} | |
} | |
// If this is being displayed in admin or frontend without a product context, | |
// or if no stored label exists, use proper default labels for known taxonomies | |
$default_labels = [ | |
'product_shipping_class' => __( 'Product shipping class', 'wp-user-frontend' ), | |
'product_visibility' => __( 'Product visibility', 'wp-user-frontend' ), | |
'product_type' => __( 'Product Type', 'wp-user-frontend' ), | |
]; | |
if ( isset( $default_labels[ $name ] ) ) { | |
return $default_labels[ $name ]; | |
} | |
return $label; | |
} |
🧰 Tools
🪛 GitHub Check: Run PHPCS inspection
[failure] 53-53:
Expected 1 newline at end of file; 0 found
[failure] 51-51:
Whitespace found at end of line
[failure] 47-47:
Whitespace found at end of line
[failure] 39-39:
Whitespace found at end of line
[failure] 34-34:
Whitespace found at end of line
[failure] 26-26:
Whitespace found at end of line
[failure] 18-18:
Whitespace found at end of line
🤖 Prompt for AI Agents
In includes/functions/woocommerce-attribute-labels.php between lines 18 and 53,
fix the formatting issues by ensuring consistent indentation, proper spacing
around array elements and operators, and correct alignment of comments and code
blocks. Make sure the function and array syntax follow WordPress PHP coding
standards for readability and maintainability.
There was a problem hiding this 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)
includes/Traits/FieldableTrait.php (2)
453-546
: Excellent modularization of taxonomy handling!The refactor significantly improves the code organization by separating WooCommerce and regular taxonomy handling. The addition of taxonomy label storage and comprehensive WooCommerce product data synchronization is well-implemented.
Consider adding error handling for the WooCommerce method calls to make the code more robust against edge cases.
970-1210
: Solid implementation of visibility and attribute handling.The product visibility handling properly maps term IDs to slugs, and the attribute processing correctly converts terms and builds WooCommerce-compatible attribute arrays. The text taxonomy handling with automatic term creation is a nice touch.
Clean up the unused variables flagged by static analysis (lines 1030, 1065) to improve code quality:
- $result = wp_set_object_terms( $post_id, $terms_to_set, 'product_visibility', false ); + wp_set_object_terms( $post_id, $terms_to_set, 'product_visibility', false ); - $saved_terms = wp_get_object_terms( $post_id, 'product_visibility', array( 'fields' => 'slugs' ) ); + // Terms saved successfully - no need to verify here
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
includes/Ajax/Frontend_Form_Ajax.php
(2 hunks)includes/Traits/FieldableTrait.php
(11 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- includes/Ajax/Frontend_Form_Ajax.php
🧰 Additional context used
🪛 PHPMD (2.15.0)
includes/Traits/FieldableTrait.php
521-521: Avoid unused local variables such as '$saved_visibility'. (Unused Code Rules)
(UnusedLocalVariable)
619-619: Avoid unused parameters such as '$taxonomy_name'. (Unused Code Rules)
(UnusedFormalParameter)
678-678: Avoid unused local variables such as '$result'. (Unused Code Rules)
(UnusedLocalVariable)
700-700: Avoid unused local variables such as '$saved_types'. (Unused Code Rules)
(UnusedLocalVariable)
755-755: Avoid unused parameters such as '$taxonomy_name'. (Unused Code Rules)
(UnusedFormalParameter)
970-970: Avoid unused parameters such as '$taxonomy_name'. (Unused Code Rules)
(UnusedFormalParameter)
1030-1030: Avoid unused local variables such as '$result'. (Unused Code Rules)
(UnusedLocalVariable)
1065-1065: Avoid unused local variables such as '$saved_terms'. (Unused Code Rules)
(UnusedLocalVariable)
🔇 Additional comments (5)
includes/Traits/FieldableTrait.php (5)
119-119
: Excellent improvements to type safety and WooCommerce integration!The strict type comparisons (
===
) improve type safety, and the new WooCommerce taxonomy normalization logic is well-implemented. The normalization ofproduct_category
toproduct_cat
and the special handling for shipping class with hierarchical flag and default type are appropriate.Also applies to: 131-131, 136-136, 159-159, 164-183
222-225
: Good improvement to type safety.The strict comparison operators (
===
) prevent potential type coercion issues in reCAPTCHA validation, making the code more reliable.Also applies to: 279-280, 295-296, 304-305, 311-312
781-965
: Excellent comprehensive WooCommerce product data synchronization!The implementation provides thorough synchronization of product data with WooCommerce standards, including product type-specific meta handling, visibility synchronization, and comprehensive meta field initialization. This ensures data consistency and prevents missing meta issues.
1270-1270
: Good type safety improvements.The strict comparisons prevent type coercion issues in the meta field processing logic, particularly for the multiple field check and ACF compatibility handling.
Also applies to: 1354-1356
1409-1622
: Excellent refactor of WooCommerce attribute handling with comprehensive integration!The updated
woo_attribute
method provides much better term handling with proper ID-to-name conversion, and the new hook methods enable seamless WooCommerce integration. The label prioritization logic (form field → WooCommerce → formatted name) is well-designed.The
filter_woocommerce_product_attributes
method comprehensively handles edge cases for different taxonomy types and ensures consistent display.Note regarding past review comment: While there was a previous suggestion to use the "This taxonomy is a WooCommerce attribute" option from form builder field settings, the current implementation provides more comprehensive and automatic handling that covers edge cases and ensures consistency across different scenarios. The current approach appears more robust.
protected function handle_woocommerce_taxonomy( $post_id, $taxonomy_name, $posted_terms, $taxonomy, &$woo_attr ) { | ||
// Map of core WooCommerce taxonomies and their handling methods | ||
$core_woo_taxonomies = [ | ||
'product_type' => 'handle_product_type', | ||
'product_cat' => 'handle_hierarchical_terms', | ||
'product_tag' => 'handle_non_hierarchical_terms', | ||
'product_shipping_class' => 'handle_shipping_class', | ||
'product_visibility' => 'handle_product_visibility', | ||
]; | ||
|
||
// Check if it's a product attribute (starts with pa_) | ||
if ( strpos( $taxonomy_name, 'pa_' ) === 0 ) { | ||
$this->handle_product_attribute( $post_id, $taxonomy_name, $posted_terms, $taxonomy, $woo_attr ); | ||
return; | ||
} | ||
|
||
$taxonomy_name = array_filter( $terms ); | ||
// Handle known core WooCommerce taxonomies | ||
if ( isset( $core_woo_taxonomies[$taxonomy_name] ) ) { | ||
$method = $core_woo_taxonomies[$taxonomy_name]; | ||
$this->$method( $post_id, $taxonomy_name, $posted_terms ); | ||
|
||
// For system taxonomies, always add to attributes if visibility is enabled | ||
// This ensures product_type, shipping_class, visibility show in Additional Information | ||
if ( ! in_array( $taxonomy_name, ['product_cat', 'product_tag'] ) ) { | ||
// Check if this should be visible in Additional Information | ||
$should_show = false; | ||
|
||
// Always show core WooCommerce taxonomies if they have the woo_attr flag | ||
if ( isset( $taxonomy['woo_attr'] ) && wpuf_is_checkbox_or_toggle_on( $taxonomy['woo_attr'] ) ) { | ||
$should_show = true; | ||
} elseif ( isset( $taxonomy['woo_attr_vis'] ) && wpuf_is_checkbox_or_toggle_on( $taxonomy['woo_attr_vis'] ) ) { | ||
$should_show = true; | ||
} | ||
|
||
if ( $should_show ) { | ||
$woo_attr[$taxonomy_name] = $this->build_woo_attribute( $taxonomy_name, $taxonomy ); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
// At this point $taxonomy_name should be a single id or array of ids | ||
if ( isset( $taxonomy_name ) && $taxonomy_name != 0 && $taxonomy_name != -1 ) { | ||
if ( is_object_in_taxonomy( $this->form_settings['post_type'], $taxonomy['name'] ) ) { | ||
$tax = $taxonomy_name; | ||
// if it's not an array, make it one | ||
if ( ! is_array( $tax ) ) { | ||
$tax = [ $tax ]; | ||
} | ||
// For any other taxonomy on a product (like custom brands, etc.) | ||
// Treat as potential WooCommerce attribute | ||
if ( taxonomy_exists( $taxonomy_name ) ) { | ||
$is_hierarchical = is_taxonomy_hierarchical( $taxonomy_name ); | ||
|
||
if ( $is_hierarchical ) { | ||
$this->handle_hierarchical_terms( $post_id, $taxonomy_name, $posted_terms ); | ||
} else { | ||
$this->handle_non_hierarchical_terms( $post_id, $taxonomy_name, $posted_terms ); | ||
} | ||
|
||
// Add to WooCommerce attributes if enabled | ||
if ( isset( $taxonomy['woo_attr'] ) && wpuf_is_checkbox_or_toggle_on( $taxonomy['woo_attr'] ) ) { | ||
$woo_attr[$taxonomy_name] = $this->build_woo_attribute( $taxonomy_name, $taxonomy ); | ||
} | ||
} | ||
} | ||
|
||
if ( $taxonomy['type'] == 'text' ) { | ||
wp_set_object_terms( $post_id, $taxonomy_name, $taxonomy['name'] ); | ||
/** | ||
* Handle product type taxonomy | ||
*/ | ||
protected function handle_product_type( $post_id, $taxonomy_name, $posted_terms ) { | ||
// Log incoming data | ||
|
||
// First, completely clear ALL existing product types | ||
$existing_types = wp_get_object_terms( $post_id, 'product_type', array( 'fields' => 'ids' ) ); | ||
if ( ! empty( $existing_types ) ) { | ||
wp_remove_object_terms( $post_id, $existing_types, 'product_type' ); | ||
} | ||
|
||
if ( empty( $posted_terms ) ) { | ||
wp_set_object_terms( $post_id, 'simple', 'product_type', false ); | ||
$this->sync_product_type_data( $post_id, 'simple' ); | ||
return; | ||
} | ||
|
||
// woocommerce check | ||
if ( isset( $taxonomy['woo_attr'] ) && $taxonomy['woo_attr'] == 'yes' && ! empty( $taxonomy_name ) ) { | ||
$woo_attr[ $taxonomy['name'] ] = $this->woo_attribute( $taxonomy ); | ||
} | ||
} else { | ||
if ( is_taxonomy_hierarchical( $taxonomy['name'] ) ) { | ||
wp_set_post_terms( $post_id, $taxonomy_name, $taxonomy['name'] ); | ||
$product_type = is_array( $posted_terms ) ? reset( $posted_terms ) : $posted_terms; | ||
|
||
// Map common term IDs to slugs | ||
// These are the standard WooCommerce product type term IDs | ||
$type_map = array( | ||
'6' => 'simple', | ||
'7' => 'grouped', | ||
'8' => 'variable', | ||
'9' => 'external' | ||
); | ||
|
||
// Check if it's a known term ID | ||
if ( isset( $type_map[ strval( $product_type ) ] ) ) { | ||
$product_type = $type_map[ strval( $product_type ) ]; | ||
} elseif ( is_numeric( $product_type ) ) { | ||
// Convert any other term ID to slug | ||
$term = get_term( $product_type, 'product_type' ); | ||
if ( $term && ! is_wp_error( $term ) ) { | ||
$product_type = $term->slug; | ||
} | ||
} else { | ||
// Handle term name (like "Variable" or "variable") | ||
// First try to get by slug | ||
$term = get_term_by( 'slug', strtolower( $product_type ), 'product_type' ); | ||
if ( ! $term ) { | ||
// Try by name | ||
$term = get_term_by( 'name', $product_type, 'product_type' ); | ||
} | ||
|
||
if ( $term && ! is_wp_error( $term ) ) { | ||
$product_type = $term->slug; | ||
} else { | ||
// Sanitize to create slug | ||
$product_type = strtolower( sanitize_title( $product_type ) ); | ||
} | ||
} | ||
|
||
|
||
// Remove all existing product types first | ||
wp_remove_object_terms( $post_id, array( 'simple', 'variable', 'grouped', 'external' ), 'product_type' ); | ||
|
||
// Set the product type using term ID for better reliability | ||
$term = get_term_by( 'slug', $product_type, 'product_type' ); | ||
if ( $term && ! is_wp_error( $term ) ) { | ||
$result = wp_set_object_terms( $post_id, intval( $term->term_id ), 'product_type', false ); | ||
} else { | ||
// Fallback to slug | ||
$result = wp_set_object_terms( $post_id, $product_type, 'product_type', false ); | ||
} | ||
|
||
// Sync product type specific data with WooCommerce | ||
$this->sync_product_type_data( $post_id, $product_type ); | ||
|
||
// Clear caches to ensure WooCommerce recognizes the change | ||
if ( function_exists( 'wc_get_product' ) ) { | ||
wp_cache_delete( 'product-' . $post_id, 'products' ); | ||
wp_cache_delete( $post_id, 'post_meta' ); | ||
clean_object_term_cache( $post_id, 'product_type' ); | ||
} | ||
|
||
// Clear caches again | ||
if ( function_exists( 'wc_delete_product_transients' ) ) { | ||
wc_delete_product_transients( $post_id ); | ||
} | ||
|
||
// Final verification | ||
$saved_types = wp_get_object_terms( $post_id, 'product_type', array( 'fields' => 'slugs' ) ); | ||
|
||
// Store the intended product type in meta to preserve it | ||
update_post_meta( $post_id, '_wpuf_intended_product_type', $product_type ); | ||
} | ||
|
||
// woocommerce check | ||
if ( isset( $taxonomy['woo_attr'] ) && $taxonomy['woo_attr'] == 'yes' && ! empty( $taxonomy_name ) ) { | ||
$woo_attr[ $taxonomy['name'] ] = $this->woo_attribute( $taxonomy ); | ||
} | ||
} else { | ||
if ( $tax ) { | ||
$non_hierarchical = []; | ||
/** | ||
* Handle hierarchical terms (like product categories) | ||
*/ | ||
protected function handle_hierarchical_terms( $post_id, $taxonomy_name, $posted_terms ) { | ||
// Clear existing terms first | ||
wp_set_object_terms( $post_id, [], $taxonomy_name ); | ||
|
||
if ( empty( $posted_terms ) ) { | ||
return; | ||
} | ||
|
||
$term_ids = $this->process_terms_to_ids( $posted_terms, $taxonomy_name ); | ||
|
||
if ( ! empty( $term_ids ) ) { | ||
wp_set_object_terms( $post_id, $term_ids, $taxonomy_name, false ); | ||
} | ||
} | ||
|
||
foreach ( $tax as $value ) { | ||
$term = get_term_by( 'id', $value, $taxonomy['name'] ); | ||
/** | ||
* Handle non-hierarchical terms (like product tags) | ||
*/ | ||
protected function handle_non_hierarchical_terms( $post_id, $taxonomy_name, $posted_terms ) { | ||
if ( empty( $posted_terms ) ) { | ||
wp_set_object_terms( $post_id, [], $taxonomy_name ); | ||
return; | ||
} | ||
|
||
if ( $term && ! is_wp_error( $term ) ) { | ||
$non_hierarchical[] = $term->name; | ||
} | ||
} | ||
$terms_array = is_array( $posted_terms ) ? $posted_terms : [ $posted_terms ]; | ||
$term_names = []; | ||
|
||
wp_set_post_terms( $post_id, $non_hierarchical, $taxonomy['name'] ); | ||
foreach ( $terms_array as $term ) { | ||
if ( is_numeric( $term ) ) { | ||
$term_obj = get_term( $term, $taxonomy_name ); | ||
if ( $term_obj && ! is_wp_error( $term_obj ) ) { | ||
$term_names[] = $term_obj->name; | ||
} | ||
} else { | ||
$term_names[] = $term; | ||
} | ||
} | ||
|
||
// woocommerce check | ||
if ( isset( $taxonomy['woo_attr'] ) && $taxonomy['woo_attr'] == 'yes' && ! empty( $_POST[ $taxonomy['name'] ] ) ) { | ||
$woo_attr[ $taxonomy['name'] ] = $this->woo_attribute( $taxonomy ); | ||
} | ||
} | ||
} // hierarchical | ||
} // is text | ||
} // is object tax | ||
} // isset tax | ||
if ( ! empty( $term_names ) ) { | ||
wp_set_post_terms( $post_id, $term_names, $taxonomy_name, false ); | ||
} | ||
} | ||
|
||
else { | ||
if ( isset( $taxonomy_name ) && 0 === absint( $taxonomy_name ) ) { | ||
wp_set_post_terms( $post_id, $taxonomy_name, $taxonomy['name'] ); | ||
/** | ||
* Handle shipping class | ||
*/ | ||
protected function handle_shipping_class( $post_id, $taxonomy_name, $posted_terms ) { | ||
// Clear existing shipping classes | ||
wp_set_object_terms( $post_id, [], 'product_shipping_class' ); | ||
|
||
if ( empty( $posted_terms ) || '0' === $posted_terms || '-1' === $posted_terms ) { | ||
// No shipping class selected | ||
return; | ||
} | ||
|
||
// Handle both IDs and slugs | ||
if ( is_numeric( $posted_terms ) ) { | ||
$term = get_term( $posted_terms, 'product_shipping_class' ); | ||
if ( $term && ! is_wp_error( $term ) ) { | ||
wp_set_object_terms( $post_id, $term->slug, 'product_shipping_class', false ); | ||
} | ||
} else { | ||
// It's already a slug or name | ||
wp_set_object_terms( $post_id, $posted_terms, 'product_shipping_class', false ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Comprehensive WooCommerce taxonomy handling with good edge case coverage.
The implementation properly handles the complexity of WooCommerce taxonomies, including product type mapping, hierarchical distinctions, and special cases for shipping class. The code follows WooCommerce conventions well.
Address the unused parameters flagged by static analysis:
🏁 Script executed:
#!/bin/bash
# Check for actual usage of the flagged parameters to confirm if they're truly unused
rg -A 5 -B 5 "handle_product_type|handle_shipping_class|handle_product_visibility" --type php
Length of output: 3840
Address unused parameters flagged by static analysis
A few of the newly-added handlers never actually use the $taxonomy_name
parameter (and may have other unused locals), which is triggering warnings. Please remove or properly annotate these unused parameters/variables to clean up the code:
• In handle_product_type( $post_id, $taxonomy_name, $posted_terms )
— the $taxonomy_name
arg isn’t referenced.
• In handle_shipping_class( $post_id, $taxonomy_name, $posted_terms )
— the $taxonomy_name
arg isn’t referenced.
• In handle_product_visibility( $post_id, $taxonomy_name, $posted_terms )
— the $taxonomy_name
arg isn’t referenced.
• Remove or rename any other unused variables reported by static analysis (or prefix them with an underscore to indicate they’re intentionally unused).
Once these are addressed, the implementation is solid and ready to merge.
🧰 Tools
🪛 PHPMD (2.15.0)
619-619: Avoid unused parameters such as '$taxonomy_name'. (Unused Code Rules)
(UnusedFormalParameter)
678-678: Avoid unused local variables such as '$result'. (Unused Code Rules)
(UnusedLocalVariable)
700-700: Avoid unused local variables such as '$saved_types'. (Unused Code Rules)
(UnusedLocalVariable)
755-755: Avoid unused parameters such as '$taxonomy_name'. (Unused Code Rules)
(UnusedFormalParameter)
🤖 Prompt for AI Agents
In includes/Traits/FieldableTrait.php between lines 557 and 774, the methods
handle_product_type, handle_shipping_class, and handle_product_visibility have
unused $taxonomy_name parameters causing static analysis warnings. Remove the
$taxonomy_name parameter from these method signatures if it is not used inside
the method. For any other unused variables in these methods, either remove them
or prefix their names with an underscore to indicate intentional non-use. This
cleanup will resolve the warnings and prepare the code for merging.
New Features
Bug Fixes
Refactor
Summary by CodeRabbit
New Features
Bug Fixes
Refactor
Style