<?php
/**
 * Name: Hide Admin Notices
 * Description: Hide annoying admin notices, update nags, and plugin promotional messages to keep dashboard clean and improve focus by removing distracting banners and alerts
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Clean Client Experience",
 *     "description": "Remove plugin update nags and promotional banners that confuse clients who shouldn't manage updates."
 *   },
 *   {
 *     "title": "Distraction-Free Writing",
 *     "description": "Hide PHP warnings and minor plugin notices for content editors who just need to focus on publishing articles."
 *   },
 *   {
 *     "title": "Managed Hosting Setup",
 *     "description": "Remove update notifications when updates are handled by hosting provider, keeping admin interface clean and professional."
 *   }
 * ]
 * Added: 2025-07-02
 * Tags: admin, notices, clean, ui, dashboard
 * WhereToFind: {
 *   "location": "Throughout Admin Area",
 *   "url": "",
 *   "instructions": "Configure notice filtering in module settings. Admin notices are hidden throughout admin pages based on your preferences.",
 *   "newTab": false
 * }
 */

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

class SwitchboardHideAdminNotices {

    public function __construct() {
        add_action('admin_head', [$this, 'hide_notices'], 999);
        
        // 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_hide-admin-notices_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 = []) {
        $hide_all = isset($settings['hide_all']) ? $settings['hide_all'] : 1;
        $hide_updates = isset($settings['hide_updates']) ? $settings['hide_updates'] : 1;
        $hide_plugins = isset($settings['hide_plugins']) ? $settings['hide_plugins'] : 1;
        $hide_errors = isset($settings['hide_errors']) ? $settings['hide_errors'] : 0;
        ?>
        <input type="hidden" name="module" value="hide-admin-notices">

        <div class="sb-sp-group">
            <div class="sb-module-option-item sb-module-toggle-item">
                <div class="sb-module-option-content">
                    <div class="sb-module-toggle-header">
                        <strong class="sb-module-option-title"><?php esc_html_e('Hide all admin notices (except errors)', 'wpswitchboard'); ?></strong>
                        <label class="sb-module-toggle">
                            <input type="checkbox" name="setting_hide_all" value="1" <?php checked($hide_all, 1); ?>>
                            <span class="sb-module-toggle-slider"></span>
                        </label>
                    </div>
                    <p class="sb-module-option-text"><?php esc_html_e('Hides success, info, and warning notices from all plugins and WordPress. Error notices will still show to alert you of critical issues.', 'wpswitchboard'); ?></p>
                </div>
            </div>
            </div>

        <div class="sb-sp-group">
            <div class="sb-module-option-item sb-module-toggle-item">
                <div class="sb-module-option-content">
                    <div class="sb-module-toggle-header">
                        <strong class="sb-module-option-title"><?php esc_html_e('Hide WordPress update notices', 'wpswitchboard'); ?></strong>
                        <label class="sb-module-toggle">
                            <input type="checkbox" name="setting_hide_updates" value="1" <?php checked($hide_updates, 1); ?>>
                            <span class="sb-module-toggle-slider"></span>
                        </label>
                    </div>
                    <p class="sb-module-option-text"><?php esc_html_e('Hides "WordPress X.X.X is available! Please update now" banners. Updates will still be available in the Updates menu.', 'wpswitchboard'); ?></p>
                </div>
            </div>
            </div>

        <div class="sb-sp-group">
            <div class="sb-module-option-item sb-module-toggle-item">
                <div class="sb-module-option-content">
                    <div class="sb-module-toggle-header">
                        <strong class="sb-module-option-title"><?php esc_html_e('Hide plugin promotional notices', 'wpswitchboard'); ?></strong>
                        <label class="sb-module-toggle">
                            <input type="checkbox" name="setting_hide_plugins" value="1" <?php checked($hide_plugins, 1); ?>>
                            <span class="sb-module-toggle-slider"></span>
                        </label>
                    </div>
                    <p class="sb-module-option-text"><?php esc_html_e('Hides annoying "Upgrade to Pro", "Rate us 5 stars", newsletter signups, and other promotional banners from plugins.', 'wpswitchboard'); ?></p>
                </div>
            </div>
            </div>

        <div class="sb-sp-group">
            <div class="sb-module-option-item sb-module-toggle-item">
                <div class="sb-module-option-content">
                    <div class="sb-module-toggle-header">
                        <strong class="sb-module-option-title"><?php esc_html_e('Hide error notices too!', 'wpswitchboard'); ?></strong>
                        <label class="sb-module-toggle">
                            <input type="checkbox" name="setting_hide_errors" value="1" <?php checked($hide_errors, 1); ?>>
                            <span class="sb-module-toggle-slider"></span>
                        </label>
                    </div>
                    <p class="sb-module-option-text">
                        <strong><?php esc_html_e('Warning: Use with caution', 'wpswitchboard'); ?></strong><br>
                        <?php esc_html_e('Error notices indicate important issues like database failures, API errors, or critical plugin conflicts. Only enable this if you\'re confident your site is working properly and you monitor errors through other means.', 'wpswitchboard'); ?>
                    </p>
                </div>
            </div>
        </div>
        <?php
    }

    public function hide_notices() {
        // Get settings from JSON structure
        $settings = get_option('switchboard_hide-admin-notices_settings', []);

        $hide_all = isset($settings['hide_all']) ? (bool) $settings['hide_all'] : true;
        $hide_updates = isset($settings['hide_updates']) ? (bool) $settings['hide_updates'] : true;
        $hide_plugins = isset($settings['hide_plugins']) ? (bool) $settings['hide_plugins'] : true;
        $hide_errors = isset($settings['hide_errors']) ? (bool) $settings['hide_errors'] : false;

        ?>
        <style>
        <?php if ($hide_errors): ?>
        /* Hide ALL notices including errors */
        .notice,
        .notice-error,
        .notice-warning,
        .notice-success,
        .notice-info,
        .update-nag,
        .update-message,
        div[class*="update-nag"],
        .error,
        .updated {
            display: none !important;
        }
        <?php elseif ($hide_all): ?>
        /* Hide all notices except errors */
        .notice:not(.notice-error),
        .update-nag,
        .update-message,
        div[class*="update-nag"],
        .updated {
            display: none !important;
        }
        /* Ensure error notices are visible */
        .notice-error,
        .error {
            display: block !important;
        }
        <?php else: ?>
            <?php if ($hide_updates): ?>
            /* Hide WordPress update notices */
            .update-nag,
            .update-message,
            div[class*="update-nag"] {
                display: none !important;
            }
            <?php endif; ?>

            <?php if ($hide_plugins): ?>
            /* Hide plugin promotional notices (dismissible ones) */
            .notice.is-dismissible:not(.notice-error):not(.notice-warning) {
                display: none !important;
            }
            <?php endif; ?>
        <?php endif; ?>
        </style>
        <?php
    }
}

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