<?php
/**
 * Name: Disable Dashboard Widgets (Selective)
 * Description: Choose which default dashboard widgets to disable including At a Glance, Activity, Quick Draft, and third-party plugin widgets for cleaner focused interface
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Client-Specific Dashboard",
 *     "description": "Remove WordPress news, PHP update, and plugin notifications for non-technical clients who find them confusing or overwhelming."
 *   },
 *   {
 *     "title": "Security Compliance",
 *     "description": "Hide version information and system status widgets from lower-level users as part of security hardening measures."
 *   },
 *   {
 *     "title": "Focused Content Dashboard",
 *     "description": "Show only At a Glance and Quick Draft widgets for bloggers, hiding everything else to minimize distractions."
 *   }
 * ]
 * Added: 2025-09-08
 * Tags: dashboard, widgets, admin, clean, customize
 * WhereToFind: {
 *   "location": "Dashboard",
 *   "url": "",
 *   "instructions": "Select widgets to hide in module settings. Hidden widgets are removed from the dashboard.",
 *   "newTab": false
 * }
 */

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

class SwitchboardDisableDashboardWidgetsSelect {
    
    public function __construct() {
        add_action('wp_dashboard_setup', [$this, 'remove_widgets']);
        
        // 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_disable-dashboard-widgets-select_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 = []) {
        $selected_widgets = isset($settings['widgets']) ? explode(',', $settings['widgets']) : [];
        $selected_widgets = array_map('trim', $selected_widgets);
        
        $available_widgets = [
            'dashboard_right_now' => esc_html__('At a Glance', 'wpswitchboard'),
            'dashboard_activity' => esc_html__('Activity', 'wpswitchboard'),
            'dashboard_quick_press' => esc_html__('Quick Draft', 'wpswitchboard'),
            'dashboard_primary' => esc_html__('WordPress Events and News', 'wpswitchboard'),
            'dashboard_site_health' => esc_html__('Site Health Status', 'wpswitchboard'),
            'welcome' => esc_html__('Welcome Panel', 'wpswitchboard'),
            'dashboard_php_nag' => esc_html__('PHP Update Required', 'wpswitchboard'),
            'dashboard_browser_nag' => esc_html__('Browser Update Required', 'wpswitchboard'),
            'dashboard_recent_comments' => esc_html__('Recent Comments', 'wpswitchboard'),
            'dashboard_incoming_links' => esc_html__('Incoming Links', 'wpswitchboard'),
            'dashboard_plugins' => esc_html__('Plugins', 'wpswitchboard'),
            'dashboard_recent_drafts' => esc_html__('Recent Drafts', 'wpswitchboard'),
            'dashboard_secondary' => esc_html__('Other WordPress News', 'wpswitchboard')
        ];
        ?>
        <input type="hidden" name="module" value="disable-dashboard-widgets-select">
        <input type="hidden" name="setting_widgets" id="setting_widgets" value="<?php echo esc_attr(implode(',', $selected_widgets)); ?>">

        <?php
        // Check if the global "Clean WP Dashboard" module is active
        $active_modules = get_option('switchboard_active_modules', []);
        if (in_array('clean-dashboard', $active_modules, true)) : ?>
            <div class="sb-sp-alert sb-sp-alert-warning">
                <strong><?php esc_html_e('Conflict Detected:', 'wpswitchboard'); ?></strong>
                <?php esc_html_e('The "Clean WP Dashboard" module is currently active, which removes ALL dashboard widgets. This selective module will have no effect until you disable that module.', 'wpswitchboard'); ?>
            </div>
        <?php endif; ?>

        <div class="sb-sp-group">
            <label class="sb-sp-label"><?php esc_html_e('Select Widgets to Disable', 'wpswitchboard'); ?></label>
            <div class="switchboard-tag-selector" data-input-id="setting_widgets">
            <div class="switchboard-selected-tags" id="selected-widgets">
                <?php foreach ($selected_widgets as $widget_id): 
                    if (isset($available_widgets[$widget_id])): ?>
                        <span class="switchboard-tag" data-value="<?php echo esc_attr($widget_id); ?>">
                            <?php echo esc_html($available_widgets[$widget_id]); ?> <span class="remove-tag">×</span>
                        </span>
                <?php endif; endforeach; ?>
            </div>
            <div class="switchboard-available-tags">
                <?php foreach ($available_widgets as $widget_id => $widget_name):
                    $is_selected = in_array($widget_id, $selected_widgets) ? 'selected' : ''; ?>
                    <span class="switchboard-tag-option <?php echo esc_attr($is_selected); ?>" data-value="<?php echo esc_attr($widget_id); ?>">
                        <?php echo esc_html($widget_name); ?>
                    </span>
                <?php endforeach; ?>
            </div>
        </div>
        <p class="sb-sp-description"><?php esc_html_e('Click widgets to add/remove them from the disabled list.', 'wpswitchboard'); ?></p>
        </div>
        <?php
    }
    
    public function remove_widgets() {
        $settings = get_option('switchboard_disable-dashboard-widgets-select_settings', []);
        $widgets = isset($settings['widgets']) && is_string($settings['widgets'])
            ? array_map('trim', explode(',', $settings['widgets']))
            : [];

        foreach ($widgets as $widget) {
            if ($widget === 'welcome') {
                remove_action('welcome_panel', 'wp_welcome_panel');
            } else {
                remove_meta_box($widget, 'dashboard', 'normal');
                remove_meta_box($widget, 'dashboard', 'side');
            }
        }
    }
}

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