<?php
/**
 * Name: Custom Admin Footer Text
 * Description: Customize footer text in WordPress admin dashboard to add company branding, support links, or custom messages visible on every admin page bottom
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Support Information Display",
 *     "description": "Replace default WordPress footer with your agency's support email, phone number, and emergency contact info for clients."
 *   },
 *   {
 *     "title": "Environment Indicator",
 *     "description": "Show 'STAGING SERVER' or 'DEVELOPMENT' in footer to prevent editors from accidentally publishing content on wrong environment."
 *   },
 *   {
 *     "title": "Version Tracking",
 *     "description": "Display custom theme version and last deployment date in footer for developer reference during troubleshooting."
 *   }
 * ]
 * WhereToFind: {
 *   "location": "Admin Footer (Bottom of Page)",
 *   "url": "",
 *   "instructions": "Configure footer text in module settings. Custom text appears at the bottom of all admin pages.",
 *   "newTab": false
 * }
 * Added: 2025-06-15
 * Tags: footer, admin, branding, customization, white-label
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardCustomAdminFooter {
    
    public function __construct() {
        add_filter('admin_footer_text', [$this, 'custom_footer_text']);
        add_filter('update_footer', [$this, 'remove_version'], 11);
        
        // Register with data registry
        $this->register_with_data_registry();
    }
    
    private function register_with_data_registry() {
        if (class_exists('Switchboard_Data_Registry')) {
            $registry = Switchboard_Data_Registry::get_instance();
            $registry->auto_register($this, __FILE__);
        }
    }
    
    public function register_data_locations() {
        return [
            'options' => [
                'switchboard_custom-admin-footer_settings'
            ],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
            'term_meta' => [],
            'transients' => []
        ];
    }
    
    /**
     * Render settings panel
     *
     * @param array $settings Current module settings
     * @return void Outputs HTML directly
     */
    public function render_settings($settings = []) {
        $footer_left = isset($settings['footer_left']) ? $settings['footer_left'] : '';
        $footer_right = isset($settings['footer_right']) ? $settings['footer_right'] : '';

        // Legacy support - migrate old 'footer_text' to 'footer_left'
        if (empty($footer_left) && isset($settings['footer_text']) && !empty($settings['footer_text'])) {
            $footer_left = $settings['footer_text'];
        }
        ?>
        <input type="hidden" name="module" value="custom-admin-footer">

        <div class="sb-sp-group">
            <label class="sb-sp-label" for="setting_footer_left"><?php echo esc_html__('Left Footer Text', 'wpswitchboard'); ?></label>
            <textarea name="setting_footer_left" id="setting_footer_left" rows="3"><?php echo esc_textarea($footer_left); ?></textarea>
            <p class="sb-sp-description"><?php echo esc_html__('Enter custom text for the left side of the admin footer. HTML and links are supported. Leave empty for default.', 'wpswitchboard'); ?></p>
        </div>

        <div class="sb-sp-group">
            <label class="sb-sp-label" for="setting_footer_right"><?php echo esc_html__('Right Footer Text', 'wpswitchboard'); ?></label>
            <textarea name="setting_footer_right" id="setting_footer_right" rows="3"><?php echo esc_textarea($footer_right); ?></textarea>
            <p class="sb-sp-description"><?php echo esc_html__('Enter custom text for the right side of the admin footer. HTML and links are supported. Leave empty to hide version info.', 'wpswitchboard'); ?></p>
        </div>

        <div class="sb-sp-alert-info">
            <p class="sb-sp-alert-text">
                <strong><?php echo esc_html__('Example with link:', 'wpswitchboard'); ?></strong><br>
                <code>&lt;a href="https://example.com" target="_blank"&gt;Visit our website&lt;/a&gt;</code>
            </p>
        </div>
        <?php
    }

    public function custom_footer_text() {
        $settings = get_option('switchboard_custom-admin-footer_settings', []);
        $custom_text = isset($settings['footer_left']) ? $settings['footer_left'] : '';

        // Legacy support - check old 'footer_text' setting
        if (empty($custom_text) && isset($settings['footer_text']) && !empty($settings['footer_text'])) {
            $custom_text = $settings['footer_text'];
        }

        if (!empty($custom_text)) {
            return wp_kses_post($custom_text);
        }

        return sprintf(
            /* translators: %s: site name with link */
            esc_html__('Thank you for using %s', 'wpswitchboard'),
            '<a href="' . esc_url(home_url()) . '">' . get_bloginfo('name') . '</a>'
        );
    }

    public function remove_version() {
        $settings = get_option('switchboard_custom-admin-footer_settings', []);
        $custom_text = isset($settings['footer_right']) ? $settings['footer_right'] : '';

        if (!empty($custom_text)) {
            return wp_kses_post($custom_text);
        }

        // Return empty string to hide version info
        return '';
    }
}

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