<?php
/**
 * Name: Admin Logo Customizer
 * Description: Upload custom logo for WordPress admin area to replace default WordPress branding throughout dashboard and create professional white-label client experience
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Client Handoff Branding",
 *     "description": "Replace WordPress logo with client's company logo when delivering white-label websites to make the admin feel fully custom."
 *   },
 *   {
 *     "title": "Multi-Brand Network",
 *     "description": "Different logos for each subsite in a WordPress multisite managing separate brands under one parent company."
 *   },
 *   {
 *     "title": "Agency Portfolio Sites",
 *     "description": "Display your agency's logo in client admin areas to maintain brand presence and remind clients of your ongoing support services."
 *   }
 * ]
 * WhereToFind: {
 *   "location": "Admin Bar (Top-Left Corner)",
 *   "url": "",
 *   "instructions": "Configure logo in module settings. Custom logo replaces WordPress icon in admin bar top-left corner.",
 *   "newTab": false
 * }
 * Added: 2025-07-28
 * Tags: logo, branding, admin, white-label, customization
 */

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

class SwitchboardAdminLogo {
    
    public function __construct() {
        add_action('admin_head', [$this, 'add_custom_logo']);
        
        // 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_admin-logo_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 = []) {
        $logo_url = isset($settings['logo_url']) ? $settings['logo_url'] : '';
        ?>
        <input type="hidden" name="module" value="admin-logo">
        
        <div class="sb-sp-group">
        <label class="sb-sp-label"><?php echo esc_html__('Admin Logo', 'wpswitchboard'); ?></label>
            <p class="sb-sp-description"><?php echo esc_html__('Upload a custom logo for the WordPress admin bar. The logo will replace the WordPress logo in the admin bar.', 'wpswitchboard'); ?></p>
            
        <div id="admin-logo-preview" class="sb-sp-upload-preview">
            <?php if ($logo_url): ?>
                <img src="<?php echo esc_url($logo_url); ?>" class="sb-sp-upload-image">
            <?php endif; ?>
        </div>
            
        <input type="hidden" id="setting_logo_url" name="setting_logo_url" value="<?php echo esc_attr($logo_url); ?>">
            
            <div class="sb-sp-button-group">
                <button type="button" class="button button-small button-primary upload-logo-btn" data-target="admin-logo">
                    <span class="dashicons dashicons-upload"></span> <?php echo esc_html__('Select Logo', 'wpswitchboard'); ?>
                </button>
                <button type="button" class="button button-small button-primary-outline remove-logo-btn" data-target="admin-logo" <?php echo $logo_url ? '' : 'style="display:none;"'; ?>>
                    <span class="dashicons dashicons-no"></span> <?php echo esc_html__('Remove Logo', 'wpswitchboard'); ?>
                </button>
            </div>
        </div>
        <?php
    }
    
    public function add_custom_logo() {
        $settings = get_option('switchboard_admin-logo_settings', []);
        if (!is_array($settings)) {
            $settings = [];
        }
        $logo_url = isset($settings['logo_url']) ? esc_url_raw($settings['logo_url']) : '';

        if (!empty($logo_url)) {
            echo '<style>
                #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
                    content: "" !important;
                    background-image: url(' . esc_url($logo_url) . ') !important;
                    background-size: 20px 20px !important;
                    background-repeat: no-repeat !important;
                    background-position: center !important;
                    width: 20px !important;
                    height: 20px !important;
                    display: inline-block !important;
                }
                #wpadminbar #wp-admin-bar-wp-logo > .ab-item {
                    padding: 0 7px !important;
                }
            </style>';
        }
    }
}

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