Skip to content

Fix/variation field persistence & rich text description handling for variants#3269

Closed
devbodaghe wants to merge 10 commits intofacebook:mainfrom
devbodaghe:fix/variation-field-persistence
Closed

Fix/variation field persistence & rich text description handling for variants#3269
devbodaghe wants to merge 10 commits intofacebook:mainfrom
devbodaghe:fix/variation-field-persistence

Conversation

@devbodaghe
Copy link
Copy Markdown
Contributor

@devbodaghe devbodaghe commented May 29, 2025

Description

This PR fixes critical issues with Facebook product description handling for WooCommerce product variations, ensuring HTML content is properly preserved in rich text descriptions while maintaining plain text for regular descriptions.

Summary of Changes

🚨 Critical Fix - Preserve HTML Input in Variation Facebook Fields:

  • BREAKING BUG: Product variation Facebook description fields were stripping all HTML content on save
  • ROOT CAUSE: save_product_variation_edit_fields() in Admin.php was using sanitize_text_field() for both regular and rich text descriptions
  • SOLUTION: Implemented separate sanitization logic:
    • Regular descriptions: sanitize_text_field() (plain text only)
    • Rich text descriptions: wp_kses_post() (preserves safe HTML)
  • USER IMPACT: Users can now enter HTML content (lists, bold, links, etc.) in Facebook description fields for variations and it will be preserved after saving

Form Display Improvements:

  • Updated variation form to load and display rich text content instead of sanitized plain text
  • Updated field description to clarify HTML support: "Supports HTML formatting for rich text display"
  • Users now see their HTML content when editing variations instead of stripped text

Enhanced Rich Text Description Method:

  • Improved get_rich_text_description() method in fbproduct.php with proper fallback chain
  • Added support for variation-specific descriptions with parent inheritance
  • Added fallback to short descriptions when main description is empty
  • Implemented proper filter application for facebook_for_woocommerce_fb_rich_text_description

Updated Tests:

  • Fixed failing unit tests for rich text description functionality
  • All existing tests continue to pass (58/58 tests passing)

Root Cause

The critical issue was in the variation save method using sanitize_text_field() for both regular and rich text descriptions. This function strips ALL HTML tags, meaning users would enter formatted content with HTML (lists, bold text, links) but it would be completely removed upon saving, breaking the rich text functionality that's essential for proper Facebook product presentation.

Solution

  • Regular Description: Continue using sanitize_text_field() for plain text safety
  • Rich Text Description: Use wp_kses_post() to preserve safe HTML while sanitizing dangerous content
  • Form Display: Load rich text content instead of plain text for editing experience
  • Method Enhancement: Improved fallback logic for robust description retrieval

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have commented my code, particularly in hard-to-understand areas.
  • I have confirmed that my changes do not introduce any new PHPCS warnings or errors.
  • I have checked plugin debug logs that my changes do not introduce any new PHP warnings or FATAL errors.
  • I followed general Pull Request best practices. Meta employees to follow this wiki.
  • I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes.
  • I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality.
  • I have updated or requested update to plugin documentations (if necessary). Meta employees to follow this wiki.

Testing

  • ✅ All unit tests pass
  • ✅ Verified HTML content is preserved in rich text descriptions for variations
  • ✅ Verified regular descriptions remain plain text
  • ✅ Confirmed form properly displays rich text content after saving
  • ✅ Tested variation inheritance and fallback logic
  • ✅ Tested HTML input preservation (lists, bold, links, etc.)

Before/After

Before: User enters <p>Product with <strong>bold</strong> text and <ul><li>Feature 1</li></ul></p> → Saved as Product with bold text and Feature 1
After: User enters <p>Product with <strong>bold</strong> text and <ul><li>Feature 1</li></ul></p> → Saved as <p>Product with <strong>bold</strong> text and <ul><li>Feature 1</li></ul></p>

Changelog entry

Fixed critical issue where HTML content was being stripped from Facebook rich text descriptions for product variations, ensuring proper formatting is preserved for Facebook product listings.

@devbodaghe devbodaghe force-pushed the fix/variation-field-persistence branch from 373f4e2 to 8badfad Compare May 29, 2025 15:58
@devbodaghe devbodaghe force-pushed the fix/variation-field-persistence branch from e64d62e to 107e22a Compare May 29, 2025 16:07
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@devbodaghe has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@devbodaghe has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@devbodaghe has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@devbodaghe merged this pull request in 3c96826.

@tzahgr tzahgr mentioned this pull request Jun 4, 2025
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.

2 participants