<?php
/**
 * Name: Customize Default WordPress Emails
 * Description: Customize the content, from name, from email, and appearance of default WordPress notification emails for password resets, new user registrations, and comments
 * Category: Utilities
 * Icon: -
 * Disabled: true
 * UseCases: [
 *   {
 *     "title": "Add Useful Functionality",
 *     "description": "Extend WordPress with helpful tools that make site management easier."
 *   },
 *   {
 *     "title": "Save Time",
 *     "description": "Automate or simplify tasks that would otherwise require manual work or additional tools."
 *   }
 * ]
 * Added: 2025-08-26
 * Tags: email, mail, notifications, customize, templates
 * WhereToFind: {
 *   "location": "Email Notifications",
 *   "url": "",
 *   "instructions": "Changes apply to WordPress system emails (password reset, new user, etc.). Trigger an action to test (e.g., password reset).",
 *   "newTab": false
 * }
 */

if (!defined('ABSPATH')) {
    exit;
}

class SwitchboardCustomizeWPMails {
    private $settings;

    public function __construct() {
        $this->settings = get_option('switchboard_customize-wp-mail-defaults_settings', array());

        // Only hook if module has settings configured
        if (!empty($this->settings)) {
            $this->init_hooks();
        }
    }

    private function init_hooks() {
        // New user registration emails
        add_filter('wp_new_user_notification_email_admin', array($this, 'customize_new_user_admin_email'), 10, 3);
        add_filter('wp_new_user_notification_email', array($this, 'customize_new_user_email'), 10, 3);

        // Password reset email
        add_filter('retrieve_password_title', array($this, 'customize_password_reset_subject'), 10, 4);
        add_filter('retrieve_password_message', array($this, 'customize_password_reset_message'), 10, 4);

        // Password changed email
        add_filter('password_change_email', array($this, 'customize_password_changed_email'), 10, 3);

        // Email changed notification
        add_filter('email_change_email', array($this, 'customize_email_changed_email'), 10, 3);

        // Comment notification emails
        add_filter('comment_notification_subject', array($this, 'customize_comment_notification_subject'), 10, 2);
        add_filter('comment_notification_text', array($this, 'customize_comment_notification_text'), 10, 2);
        add_filter('comment_moderation_subject', array($this, 'customize_comment_moderation_subject'), 10, 2);
        add_filter('comment_moderation_text', array($this, 'customize_comment_moderation_text'), 10, 2);
    }

    /**
     * Replace merge tags with actual values
     */
    private function replace_merge_tags($content, $data) {
        if (empty($content)) {
            return $content;
        }

        foreach ($data as $key => $value) {
            $content = str_replace('{{' . $key . '}}', $value, $content);
        }

        return $content;
    }

    /**
     * Customize new user notification email to admin
     */
    public function customize_new_user_admin_email($wp_new_user_notification_email_admin, $user, $blogname) {
        if (!empty($this->settings['new_user_admin_subject'])) {
            $data = array(
                'username' => $user->user_login,
                'user_email' => $user->user_email,
                'site_name' => $blogname,
                'site_url' => home_url(),
                'login_url' => wp_login_url()
            );

            $wp_new_user_notification_email_admin['subject'] = $this->replace_merge_tags(
                $this->settings['new_user_admin_subject'],
                $data
            );
        }

        if (!empty($this->settings['new_user_admin_message'])) {
            $data = array(
                'username' => $user->user_login,
                'user_email' => $user->user_email,
                'site_name' => $blogname,
                'site_url' => home_url(),
                'login_url' => wp_login_url()
            );

            $wp_new_user_notification_email_admin['message'] = $this->replace_merge_tags(
                $this->settings['new_user_admin_message'],
                $data
            );
        }

        return $wp_new_user_notification_email_admin;
    }

    /**
     * Customize new user notification email to user
     */
    public function customize_new_user_email($wp_new_user_notification_email, $user, $blogname) {
        if (!empty($this->settings['new_user_subject'])) {
            $data = array(
                'username' => $user->user_login,
                'site_name' => $blogname,
                'site_url' => home_url(),
                'login_url' => wp_login_url()
            );

            $wp_new_user_notification_email['subject'] = $this->replace_merge_tags(
                $this->settings['new_user_subject'],
                $data
            );
        }

        if (!empty($this->settings['new_user_message'])) {
            // Get password reset key
            $key = get_password_reset_key($user);
            $password_reset_url = network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login');

            $data = array(
                'username' => $user->user_login,
                'site_name' => $blogname,
                'site_url' => home_url(),
                'login_url' => wp_login_url(),
                'password_reset_url' => $password_reset_url
            );

            $wp_new_user_notification_email['message'] = $this->replace_merge_tags(
                $this->settings['new_user_message'],
                $data
            );
        }

        return $wp_new_user_notification_email;
    }

    /**
     * Customize password reset subject
     */
    public function customize_password_reset_subject($title, $user_login, $user_data, $key) {
        if (!empty($this->settings['password_reset_subject'])) {
            $data = array(
                'username' => $user_login,
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                'site_url' => home_url()
            );

            return $this->replace_merge_tags($this->settings['password_reset_subject'], $data);
        }

        return $title;
    }

    /**
     * Customize password reset message
     */
    public function customize_password_reset_message($message, $key, $user_login, $user_data) {
        if (!empty($this->settings['password_reset_message'])) {
            $reset_url = network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login');

            $data = array(
                'username' => $user_login,
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                'site_url' => home_url(),
                'reset_url' => $reset_url,
                'user_email' => $user_data->user_email
            );

            return $this->replace_merge_tags($this->settings['password_reset_message'], $data);
        }

        return $message;
    }

    /**
     * Customize password changed email
     */
    public function customize_password_changed_email($pass_change_email, $user, $userdata) {
        if (!empty($this->settings['password_changed_subject'])) {
            $data = array(
                'username' => $user['user_login'],
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                'site_url' => home_url()
            );

            $pass_change_email['subject'] = $this->replace_merge_tags(
                $this->settings['password_changed_subject'],
                $data
            );
        }

        if (!empty($this->settings['password_changed_message'])) {
            $data = array(
                'username' => $user['user_login'],
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                'site_url' => home_url(),
                'admin_email' => get_option('admin_email')
            );

            $pass_change_email['message'] = $this->replace_merge_tags(
                $this->settings['password_changed_message'],
                $data
            );
        }

        return $pass_change_email;
    }

    /**
     * Customize email changed notification
     */
    public function customize_email_changed_email($email_change_email, $user, $userdata) {
        if (!empty($this->settings['email_changed_subject'])) {
            $data = array(
                'username' => $user['user_login'],
                'old_email' => $userdata['user_email'],
                'new_email' => $user['user_email'],
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                'site_url' => home_url()
            );

            $email_change_email['subject'] = $this->replace_merge_tags(
                $this->settings['email_changed_subject'],
                $data
            );
        }

        if (!empty($this->settings['email_changed_message'])) {
            $data = array(
                'username' => $user['user_login'],
                'old_email' => $userdata['user_email'],
                'new_email' => $user['user_email'],
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                'site_url' => home_url(),
                'admin_email' => get_option('admin_email')
            );

            $email_change_email['message'] = $this->replace_merge_tags(
                $this->settings['email_changed_message'],
                $data
            );
        }

        return $email_change_email;
    }

    /**
     * Customize comment notification subject
     */
    public function customize_comment_notification_subject($subject, $comment_id) {
        if (!empty($this->settings['comment_notification_subject'])) {
            $comment = get_comment($comment_id);
            $post = get_post($comment->comment_post_ID);

            $data = array(
                'post_title' => $post->post_title,
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                'comment_author' => $comment->comment_author
            );

            return $this->replace_merge_tags($this->settings['comment_notification_subject'], $data);
        }

        return $subject;
    }

    /**
     * Customize comment notification text
     */
    public function customize_comment_notification_text($notify_message, $comment_id) {
        if (!empty($this->settings['comment_notification_message'])) {
            $comment = get_comment($comment_id);
            $post = get_post($comment->comment_post_ID);

            $data = array(
                'comment_author' => $comment->comment_author,
                'comment_author_email' => $comment->comment_author_email,
                'comment_author_url' => $comment->comment_author_url,
                'comment_content' => $comment->comment_content,
                'post_title' => $post->post_title,
                'post_url' => get_permalink($comment->comment_post_ID),
                'comment_url' => get_comment_link($comment),
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES)
            );

            return $this->replace_merge_tags($this->settings['comment_notification_message'], $data);
        }

        return $notify_message;
    }

    /**
     * Customize comment moderation subject
     */
    public function customize_comment_moderation_subject($subject, $comment_id) {
        if (!empty($this->settings['comment_moderation_subject'])) {
            $comment = get_comment($comment_id);
            $post = get_post($comment->comment_post_ID);

            $data = array(
                'post_title' => $post->post_title,
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                'comment_author' => $comment->comment_author
            );

            return $this->replace_merge_tags($this->settings['comment_moderation_subject'], $data);
        }

        return $subject;
    }

    /**
     * Customize comment moderation text
     */
    public function customize_comment_moderation_text($notify_message, $comment_id) {
        if (!empty($this->settings['comment_moderation_message'])) {
            $comment = get_comment($comment_id);
            $post = get_post($comment->comment_post_ID);

            $data = array(
                'comment_author' => $comment->comment_author,
                'comment_author_email' => $comment->comment_author_email,
                'comment_author_url' => $comment->comment_author_url,
                'comment_author_ip' => $comment->comment_author_IP,
                'comment_content' => $comment->comment_content,
                'post_title' => $post->post_title,
                'post_url' => get_permalink($comment->comment_post_ID),
                'approve_url' => admin_url("comment.php?action=approve&c=$comment_id"),
                'spam_url' => admin_url("comment.php?action=spam&c=$comment_id"),
                'trash_url' => admin_url("comment.php?action=trash&c=$comment_id"),
                'moderate_url' => admin_url('edit-comments.php?comment_status=moderated'),
                'site_name' => wp_specialchars_decode(get_option('blogname'), ENT_QUOTES)
            );

            return $this->replace_merge_tags($this->settings['comment_moderation_message'], $data);
        }

        return $notify_message;
    }

    /**
     * Render settings panel for Customize Default WordPress Emails
     *
     * @param array $settings Current module settings
     * @return void Outputs HTML directly
     */
    public function render_settings($settings = []) {
        // Get current settings for each email type
        $new_user_admin_subject = isset($settings['new_user_admin_subject']) ? $settings['new_user_admin_subject'] : '';
        $new_user_admin_message = isset($settings['new_user_admin_message']) ? $settings['new_user_admin_message'] : '';
        $new_user_subject = isset($settings['new_user_subject']) ? $settings['new_user_subject'] : '';
        $new_user_message = isset($settings['new_user_message']) ? $settings['new_user_message'] : '';
        $password_reset_subject = isset($settings['password_reset_subject']) ? $settings['password_reset_subject'] : '';
        $password_reset_message = isset($settings['password_reset_message']) ? $settings['password_reset_message'] : '';
        $password_changed_subject = isset($settings['password_changed_subject']) ? $settings['password_changed_subject'] : '';
        $password_changed_message = isset($settings['password_changed_message']) ? $settings['password_changed_message'] : '';
        $email_changed_subject = isset($settings['email_changed_subject']) ? $settings['email_changed_subject'] : '';
        $email_changed_message = isset($settings['email_changed_message']) ? $settings['email_changed_message'] : '';
        $comment_notification_subject = isset($settings['comment_notification_subject']) ? $settings['comment_notification_subject'] : '';
        $comment_notification_message = isset($settings['comment_notification_message']) ? $settings['comment_notification_message'] : '';
        $comment_moderation_subject = isset($settings['comment_moderation_subject']) ? $settings['comment_moderation_subject'] : '';
        $comment_moderation_message = isset($settings['comment_moderation_message']) ? $settings['comment_moderation_message'] : '';

        // Check which emails have been customized (for visual badges)
        $has_new_user_admin = !empty($new_user_admin_subject) || !empty($new_user_admin_message);
        $has_new_user = !empty($new_user_subject) || !empty($new_user_message);
        $has_password_reset = !empty($password_reset_subject) || !empty($password_reset_message);
        $has_password_changed = !empty($password_changed_subject) || !empty($password_changed_message);
        $has_email_changed = !empty($email_changed_subject) || !empty($email_changed_message);
        $has_comment_notification = !empty($comment_notification_subject) || !empty($comment_notification_message);
        $has_comment_moderation = !empty($comment_moderation_subject) || !empty($comment_moderation_message);

        // Prepare i18n strings for JavaScript
        $i18n = [
            'customizeEmailsTitle' => __('Customize Default WordPress Emails', 'wpswitchboard'),
            'customizeEmailsDesc' => __('Personalize the content of built-in WordPress notification emails. Use merge tags like {{username}}, {{site_name}}, etc.', 'wpswitchboard'),
            'newUserAdmin' => __('New User (Admin)', 'wpswitchboard'),
            'newUser' => __('New User (User)', 'wpswitchboard'),
            'passwordReset' => __('Password Reset', 'wpswitchboard'),
            'passwordChanged' => __('Password Changed', 'wpswitchboard'),
            'emailChanged' => __('Email Changed', 'wpswitchboard'),
            'commentNotification' => __('Comment Notification', 'wpswitchboard'),
            'commentModeration' => __('Comment Moderation', 'wpswitchboard'),
            'customized' => __('Customized', 'wpswitchboard')
        ];

        echo '
            <input type="hidden" name="module" value="customize-wp-mail-defaults">
            <input type="hidden" id="customize-wp-mail-i18n" value="' . esc_attr(wp_json_encode($i18n)) . '">

            <div class="sb-sp-alert-info">
                <div class="sb-sp-alert-title">' . esc_html__('Customize Default WordPress Emails', 'wpswitchboard') . '</div>
                <div class="sb-sp-text-sm">' . esc_html__('Personalize the content of built-in WordPress notification emails. Use merge tags like {{username}}, {{site_name}}, etc.', 'wpswitchboard') . '</div>
            </div>

            <div class="mail-customize-tabs">
                <button type="button" class="mail-tab-button active" data-tab="new-user-admin">
                    ' . esc_html__('New User (Admin)', 'wpswitchboard') . '
                    ' . ($has_new_user_admin ? '<span class="mail-tab-badge" title="' . esc_attr__('Customized', 'wpswitchboard') . '"></span>' : '') . '
                </button>
                <button type="button" class="mail-tab-button" data-tab="new-user">
                    ' . esc_html__('New User (User)', 'wpswitchboard') . '
                    ' . ($has_new_user ? '<span class="mail-tab-badge" title="' . esc_attr__('Customized', 'wpswitchboard') . '"></span>' : '') . '
                </button>
                <button type="button" class="mail-tab-button" data-tab="password-reset">
                    ' . esc_html__('Password Reset', 'wpswitchboard') . '
                    ' . ($has_password_reset ? '<span class="mail-tab-badge" title="' . esc_attr__('Customized', 'wpswitchboard') . '"></span>' : '') . '
                </button>
                <button type="button" class="mail-tab-button" data-tab="password-changed">
                    ' . esc_html__('Password Changed', 'wpswitchboard') . '
                    ' . ($has_password_changed ? '<span class="mail-tab-badge" title="' . esc_attr__('Customized', 'wpswitchboard') . '"></span>' : '') . '
                </button>
                <button type="button" class="mail-tab-button" data-tab="email-changed">
                    ' . esc_html__('Email Changed', 'wpswitchboard') . '
                    ' . ($has_email_changed ? '<span class="mail-tab-badge" title="' . esc_attr__('Customized', 'wpswitchboard') . '"></span>' : '') . '
                </button>
                <button type="button" class="mail-tab-button" data-tab="comment-notification">
                    ' . esc_html__('Comment Notification', 'wpswitchboard') . '
                    ' . ($has_comment_notification ? '<span class="mail-tab-badge" title="' . esc_attr__('Customized', 'wpswitchboard') . '"></span>' : '') . '
                </button>
                <button type="button" class="mail-tab-button" data-tab="comment-moderation">
                    ' . esc_html__('Comment Moderation', 'wpswitchboard') . '
                    ' . ($has_comment_moderation ? '<span class="mail-tab-badge" title="' . esc_attr__('Customized', 'wpswitchboard') . '"></span>' : '') . '
                </button>
            </div>

            <!-- New User Admin Notification Tab -->
            <div class="mail-tab-content active" data-tab-content="new-user-admin">
                <div class="info-banner">
                    Sent to site admin when a new user registers
                </div>

                <div class="merge-tags-box">
                    <h4 class="sb-sp-heading-sm">Available Merge Tags:</h4>
                    <div class="merge-tags-list">
                        <code class="merge-tag" data-tag="{{username}}">{{username}}</code>
                        <code class="merge-tag" data-tag="{{user_email}}">{{user_email}}</code>
                        <code class="merge-tag" data-tag="{{site_name}}">{{site_name}}</code>
                        <code class="merge-tag" data-tag="{{site_url}}">{{site_url}}</code>
                        <code class="merge-tag" data-tag="{{login_url}}">{{login_url}}</code>
                    </div>
                </div>

                <div class="mail-field-group">
                    <label for="new_user_admin_subject">Email Subject</label>
                    <input type="text" name="setting_new_user_admin_subject" id="new_user_admin_subject" value="' . esc_attr($new_user_admin_subject) . '" placeholder="[{{site_name}}] New User Registration" class="sb-sp-input">
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                </div>

                <div class="mail-field-group">
                    <label for="new_user_admin_message">Email Message</label>
                    <textarea name="setting_new_user_admin_message" id="new_user_admin_message" rows="8" placeholder="New user registration on {{site_name}}:

Username: {{username}}
Email: {{user_email}}" class="sb-sp-textarea">' . esc_textarea($new_user_admin_message) . '</textarea>
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                    <button type="button" class="clear-button" onclick="document.getElementById(\'new_user_admin_subject\').value=\'\'; document.getElementById(\'new_user_admin_message\').value=\'\';">Clear & Reset to Default</button>
                </div>
            </div>

            <!-- New User Notification Tab -->
            <div class="mail-tab-content" data-tab-content="new-user">
                <div class="info-banner">
                    Sent to new users when they register on your site
                </div>

                <div class="merge-tags-box">
                    <h4 class="sb-sp-heading-sm">Available Merge Tags:</h4>
                    <div class="merge-tags-list">
                        <code class="merge-tag" data-tag="{{username}}">{{username}}</code>
                        <code class="merge-tag" data-tag="{{site_name}}">{{site_name}}</code>
                        <code class="merge-tag" data-tag="{{site_url}}">{{site_url}}</code>
                        <code class="merge-tag" data-tag="{{login_url}}">{{login_url}}</code>
                        <code class="merge-tag" data-tag="{{password_reset_url}}">{{password_reset_url}}</code>
                    </div>
                </div>

                <div class="mail-field-group">
                    <label for="new_user_subject">Email Subject</label>
                    <input type="text" name="setting_new_user_subject" id="new_user_subject" value="' . esc_attr($new_user_subject) . '" placeholder="Welcome to {{site_name}}!" class="sb-sp-input">
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                </div>

                <div class="mail-field-group">
                    <label for="new_user_message">Email Message</label>
                    <textarea name="setting_new_user_message" id="new_user_message" rows="8" placeholder="Welcome to {{site_name}}!

Username: {{username}}

To set your password, visit the following link:
{{password_reset_url}}" class="sb-sp-textarea">' . esc_textarea($new_user_message) . '</textarea>
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                    <button type="button" class="clear-button" onclick="document.getElementById(\'new_user_subject\').value=\'\'; document.getElementById(\'new_user_message\').value=\'\';">Clear & Reset to Default</button>
                </div>
            </div>

            <!-- Password Reset Tab -->
            <div class="mail-tab-content" data-tab-content="password-reset">
                <div class="info-banner">
                    Sent when a user requests a password reset
                </div>

                <div class="merge-tags-box">
                    <h4 class="sb-sp-heading-sm">Available Merge Tags:</h4>
                    <div class="merge-tags-list">
                        <code class="merge-tag" data-tag="{{username}}">{{username}}</code>
                        <code class="merge-tag" data-tag="{{user_email}}">{{user_email}}</code>
                        <code class="merge-tag" data-tag="{{site_name}}">{{site_name}}</code>
                        <code class="merge-tag" data-tag="{{site_url}}">{{site_url}}</code>
                        <code class="merge-tag" data-tag="{{reset_url}}">{{reset_url}}</code>
                    </div>
                </div>

                <div class="mail-field-group">
                    <label for="password_reset_subject">Email Subject</label>
                    <input type="text" name="setting_password_reset_subject" id="password_reset_subject" value="' . esc_attr($password_reset_subject) . '" placeholder="[{{site_name}}] Password Reset Request" class="sb-sp-input">
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                </div>

                <div class="mail-field-group">
                    <label for="password_reset_message">Email Message</label>
                    <textarea name="setting_password_reset_message" id="password_reset_message" rows="8" placeholder="Someone has requested a password reset for the following account:

Username: {{username}}

To reset your password, visit:
{{reset_url}}" class="sb-sp-textarea">' . esc_textarea($password_reset_message) . '</textarea>
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                    <button type="button" class="clear-button" onclick="document.getElementById(\'password_reset_subject\').value=\'\'; document.getElementById(\'password_reset_message\').value=\'\';">Clear & Reset to Default</button>
                </div>
            </div>

            <!-- Password Changed Tab -->
            <div class="mail-tab-content" data-tab-content="password-changed">
                <div class="info-banner">
                    Sent when a user successfully changes their password
                </div>

                <div class="merge-tags-box">
                    <h4 class="sb-sp-heading-sm">Available Merge Tags:</h4>
                    <div class="merge-tags-list">
                        <code class="merge-tag" data-tag="{{username}}">{{username}}</code>
                        <code class="merge-tag" data-tag="{{site_name}}">{{site_name}}</code>
                        <code class="merge-tag" data-tag="{{site_url}}">{{site_url}}</code>
                        <code class="merge-tag" data-tag="{{admin_email}}">{{admin_email}}</code>
                    </div>
                </div>

                <div class="mail-field-group">
                    <label for="password_changed_subject">Email Subject</label>
                    <input type="text" name="setting_password_changed_subject" id="password_changed_subject" value="' . esc_attr($password_changed_subject) . '" placeholder="[{{site_name}}] Password Changed" class="sb-sp-input">
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                </div>

                <div class="mail-field-group">
                    <label for="password_changed_message">Email Message</label>
                    <textarea name="setting_password_changed_message" id="password_changed_message" rows="8" placeholder="Hi {{username}},

This notice confirms that your password was changed on {{site_name}}.

If you did not change your password, please contact {{admin_email}} immediately." class="sb-sp-textarea">' . esc_textarea($password_changed_message) . '</textarea>
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                    <button type="button" class="clear-button" onclick="document.getElementById(\'password_changed_subject\').value=\'\'; document.getElementById(\'password_changed_message\').value=\'\';">Clear & Reset to Default</button>
                </div>
            </div>

            <!-- Email Changed Tab -->
            <div class="mail-tab-content" data-tab-content="email-changed">
                <div class="info-banner">
                    Sent when a user changes their email address
                </div>

                <div class="merge-tags-box">
                    <h4 class="sb-sp-heading-sm">Available Merge Tags:</h4>
                    <div class="merge-tags-list">
                        <code class="merge-tag" data-tag="{{username}}">{{username}}</code>
                        <code class="merge-tag" data-tag="{{old_email}}">{{old_email}}</code>
                        <code class="merge-tag" data-tag="{{new_email}}">{{new_email}}</code>
                        <code class="merge-tag" data-tag="{{site_name}}">{{site_name}}</code>
                        <code class="merge-tag" data-tag="{{site_url}}">{{site_url}}</code>
                        <code class="merge-tag" data-tag="{{admin_email}}">{{admin_email}}</code>
                    </div>
                </div>

                <div class="mail-field-group">
                    <label for="email_changed_subject">Email Subject</label>
                    <input type="text" name="setting_email_changed_subject" id="email_changed_subject" value="' . esc_attr($email_changed_subject) . '" placeholder="[{{site_name}}] Email Address Changed" class="sb-sp-input">
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                </div>

                <div class="mail-field-group">
                    <label for="email_changed_message">Email Message</label>
                    <textarea name="setting_email_changed_message" id="email_changed_message" rows="8" placeholder="Hi {{username}},

This notice confirms that your email address on {{site_name}} was changed.

Old email: {{old_email}}
New email: {{new_email}}" class="sb-sp-textarea">' . esc_textarea($email_changed_message) . '</textarea>
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                    <button type="button" class="clear-button" onclick="document.getElementById(\'email_changed_subject\').value=\'\'; document.getElementById(\'email_changed_message\').value=\'\';">Clear & Reset to Default</button>
                </div>
            </div>

            <!-- Comment Notification Tab -->
            <div class="mail-tab-content" data-tab-content="comment-notification">
                <div class="info-banner">
                    Sent to post author when someone comments on their post
                </div>

                <div class="merge-tags-box">
                    <h4 class="sb-sp-heading-sm">Available Merge Tags:</h4>
                    <div class="merge-tags-list">
                        <code class="merge-tag" data-tag="{{comment_author}}">{{comment_author}}</code>
                        <code class="merge-tag" data-tag="{{comment_author_email}}">{{comment_author_email}}</code>
                        <code class="merge-tag" data-tag="{{comment_author_url}}">{{comment_author_url}}</code>
                        <code class="merge-tag" data-tag="{{comment_content}}">{{comment_content}}</code>
                        <code class="merge-tag" data-tag="{{post_title}}">{{post_title}}</code>
                        <code class="merge-tag" data-tag="{{post_url}}">{{post_url}}</code>
                        <code class="merge-tag" data-tag="{{comment_url}}">{{comment_url}}</code>
                        <code class="merge-tag" data-tag="{{site_name}}">{{site_name}}</code>
                    </div>
                </div>

                <div class="mail-field-group">
                    <label for="comment_notification_subject">Email Subject</label>
                    <input type="text" name="setting_comment_notification_subject" id="comment_notification_subject" value="' . esc_attr($comment_notification_subject) . '" placeholder="[{{site_name}}] New comment on \"{{post_title}}\"" class="sb-sp-input">
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                </div>

                <div class="mail-field-group">
                    <label for="comment_notification_message">Email Message</label>
                    <textarea name="setting_comment_notification_message" id="comment_notification_message" rows="8" placeholder="New comment on your post \"{{post_title}}\"

Author: {{comment_author}}
Comment:
{{comment_content}}

View comment: {{comment_url}}" class="sb-sp-textarea">' . esc_textarea($comment_notification_message) . '</textarea>
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                    <button type="button" class="clear-button" onclick="document.getElementById(\'comment_notification_subject\').value=\'\'; document.getElementById(\'comment_notification_message\').value=\'\';">Clear & Reset to Default</button>
                </div>
            </div>

            <!-- Comment Moderation Tab -->
            <div class="mail-tab-content" data-tab-content="comment-moderation">
                <div class="info-banner">
                    Sent to admin when a comment is held for moderation
                </div>

                <div class="merge-tags-box">
                    <h4 class="sb-sp-heading-sm">Available Merge Tags:</h4>
                    <div class="merge-tags-list">
                        <code class="merge-tag" data-tag="{{comment_author}}">{{comment_author}}</code>
                        <code class="merge-tag" data-tag="{{comment_author_email}}">{{comment_author_email}}</code>
                        <code class="merge-tag" data-tag="{{comment_author_url}}">{{comment_author_url}}</code>
                        <code class="merge-tag" data-tag="{{comment_author_ip}}">{{comment_author_ip}}</code>
                        <code class="merge-tag" data-tag="{{comment_content}}">{{comment_content}}</code>
                        <code class="merge-tag" data-tag="{{post_title}}">{{post_title}}</code>
                        <code class="merge-tag" data-tag="{{post_url}}">{{post_url}}</code>
                        <code class="merge-tag" data-tag="{{approve_url}}">{{approve_url}}</code>
                        <code class="merge-tag" data-tag="{{spam_url}}">{{spam_url}}</code>
                        <code class="merge-tag" data-tag="{{trash_url}}">{{trash_url}}</code>
                        <code class="merge-tag" data-tag="{{moderate_url}}">{{moderate_url}}</code>
                        <code class="merge-tag" data-tag="{{site_name}}">{{site_name}}</code>
                    </div>
                </div>

                <div class="mail-field-group">
                    <label for="comment_moderation_subject">Email Subject</label>
                    <input type="text" name="setting_comment_moderation_subject" id="comment_moderation_subject" value="' . esc_attr($comment_moderation_subject) . '" placeholder="[{{site_name}}] Please moderate: \"{{post_title}}\"" class="sb-sp-input">
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                </div>

                <div class="mail-field-group">
                    <label for="comment_moderation_message">Email Message</label>
                    <textarea name="setting_comment_moderation_message" id="comment_moderation_message" rows="10" placeholder="A new comment is waiting for your approval on \"{{post_title}}\"

Author: {{comment_author}} ({{comment_author_email}})
Comment:
{{comment_content}}

Approve: {{approve_url}}
Spam: {{spam_url}}
Trash: {{trash_url}}" class="sb-sp-textarea">' . esc_textarea($comment_moderation_message) . '</textarea>
                    <p class="sb-sp-description">Leave empty to use WordPress default</p>
                    <button type="button" class="clear-button" onclick="document.getElementById(\'comment_moderation_subject\').value=\'\'; document.getElementById(\'comment_moderation_message\').value=\'\';">Clear & Reset to Default</button>
                </div>
            </div>

            <script>
            jQuery(document).ready(function($) {
                // Tab switching
                $(".mail-tab-button").on("click", function() {
                    var tab = $(this).data("tab");

                    $(".mail-tab-button").removeClass("active");
                    $(this).addClass("active");

                    $(".mail-tab-content").removeClass("active");
                    $(".mail-tab-content[data-tab-content=\"" + tab + "\"]").addClass("active");
                });

                // Merge tag click to copy
                $(".merge-tag").on("click", function() {
                    var tag = $(this).data("tag");
                    var activeTab = $(".mail-tab-content.active");
                    var textarea = activeTab.find("textarea");

                    if (textarea.length) {
                        var currentVal = textarea.val();
                        var cursorPos = textarea[0].selectionStart;
                        var textBefore = currentVal.substring(0, cursorPos);
                        var textAfter = currentVal.substring(cursorPos);

                        textarea.val(textBefore + tag + textAfter);
                        textarea[0].setSelectionRange(cursorPos + tag.length, cursorPos + tag.length);
                        textarea.focus();

                        // Visual feedback
                        $(this).css("transform", "scale(0.95)");
                        setTimeout(() => {
                            $(this).css("transform", "scale(1)");
                        }, 100);
                    }
                });
            });
            </script>
        ';
    }

    /**
     * Register data locations for cleanup system
     *
     * @return array Data storage locations
     */
    public function register_data_locations() {
        return [
            'options' => [
                'switchboard_customize-wp-mail-defaults_settings',
            ],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }
}

// Initialize and register with data system
$switchboard_instance = new SwitchboardCustomizeWPMails();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
