fix: Add markdown parsing, variable replacement, and logo fallback

🐛 Email Rendering Issues Fixed:

1. Markdown Not Parsed
    Raw markdown showing: ## Great news...
    Created MarkdownParser.php (PHP port from TypeScript)
    Parses headings, bold, italic, lists, links
    Supports card blocks and button syntax
    Proper newline handling

2. Variables Not Replaced
    {support_email} showing literally
    Added support_email variable
    Added current_year variable
    Added estimated_delivery variable (3-5 business days)

3. Broken Logo Image
    Broken image placeholder
    Fallback to site icon if no logo set
    Fallback to text header if no icon
    Proper URL handling

4. Newline Issues
    Variables on same line
    Markdown parser handles newlines correctly
    Proper paragraph wrapping

📦 New File:
- includes/Core/Notifications/MarkdownParser.php
  - parse() - Convert markdown to HTML
  - parse_basics() - Parse standard markdown
  - nl2br_email() - Convert newlines for email

🔧 Updated Files:
- EmailRenderer.php
  - Use MarkdownParser in render_card()
  - Add support_email, current_year variables
  - Add estimated_delivery calculation
  - Logo fallback to site icon
  - Text header fallback if no logo

🎯 Result:
-  Markdown properly rendered
-  All variables replaced
-  Logo displays (or text fallback)
-  Proper line breaks
-  Professional email appearance

📝 Example:
Before: ## Great news, {customer_name}!
After: <h2>Great news, Dwindi Ramadhana!</h2>

Before: {support_email}
After: admin@example.com

Before: Broken image
After: Site icon or store name
This commit is contained in:
dwindown
2025-11-18 18:36:28 +07:00
parent af2a3d3dd5
commit c599bce71a
2 changed files with 161 additions and 2 deletions

View File

@@ -143,10 +143,15 @@ class EmailRenderer {
'store_name' => get_bloginfo('name'),
'store_url' => home_url(),
'site_title' => get_bloginfo('name'),
'support_email' => get_option('admin_email'),
'current_year' => date('Y'),
];
// Order variables
if ($data instanceof \WC_Order) {
// Calculate estimated delivery (3-5 business days from now)
$estimated_delivery = date('F j', strtotime('+3 days')) . '-' . date('j', strtotime('+5 days'));
$variables = array_merge($variables, [
'order_number' => $data->get_order_number(),
'order_id' => $data->get_id(),
@@ -160,6 +165,7 @@ class EmailRenderer {
'order_url' => $data->get_view_order_url(),
'payment_method' => $data->get_payment_method_title(),
'shipping_method' => $data->get_shipping_method(),
'estimated_delivery' => $estimated_delivery,
'customer_name' => $data->get_formatted_billing_full_name(),
'customer_first_name' => $data->get_billing_first_name(),
'customer_last_name' => $data->get_billing_last_name(),
@@ -280,6 +286,9 @@ class EmailRenderer {
$type = $attributes['type'] ?? 'default';
$bg = $attributes['bg'] ?? null;
// Parse markdown in content
$content = MarkdownParser::parse($content);
// Get email customization settings for colors
$email_settings = get_option('woonoow_email_settings', []);
$hero_gradient_start = $email_settings['hero_gradient_start'] ?? '#667eea';
@@ -397,14 +406,22 @@ class EmailRenderer {
$body_bg = '#f8f8f8';
// Email header (logo or text)
if (!empty($email_settings['logo_url'])) {
$logo_url = $email_settings['logo_url'] ?? '';
// Fallback to site icon if no logo set
if (empty($logo_url) && has_site_icon()) {
$logo_url = get_site_icon_url(200);
}
if (!empty($logo_url)) {
$header = sprintf(
'<a href="%s"><img src="%s" alt="%s" style="max-width: 200px; max-height: 60px;"></a>',
esc_url($variables['store_url']),
esc_url($email_settings['logo_url']),
esc_url($logo_url),
esc_attr($variables['store_name'])
);
} else {
// No logo, use text header
$header_text = !empty($email_settings['header_text']) ? $email_settings['header_text'] : $variables['store_name'];
$header = sprintf(
'<a href="%s" style="font-size: 24px; font-weight: 700; color: #333; text-decoration: none;">%s</a>',