<?php
/**
 * Name: Clean Up Admin Bar
 * Description: Remove various elements from the admin bar including WordPress logo, site name, customize menu, updates, comments, new content, and help tab for cleaner professional interface
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "White-Label Admin",
 *     "description": "Remove WordPress branding and unnecessary menu items for agencies delivering clean, professional admin interfaces to clients."
 *   },
 *   {
 *     "title": "Simplified Interface",
 *     "description": "Hide confusing elements like updates counter and help tabs from non-technical users to reduce support questions."
 *   },
 *   {
 *     "title": "Focused Workspace",
 *     "description": "Minimize distractions by removing unused menu items, keeping users focused on content creation and management."
 *   }
 * ]
 * Added: 2025-11-04
 * Tags: admin-bar, toolbar, clean, white-label, branding, professional
 * WhereToFind: {
 *   "location": "Admin Bar (Top)",
 *   "url": "",
 *   "instructions": "Configure which admin bar elements to remove in module settings. Changes are visible in the top toolbar.",
 *   "newTab": false
 * }
 */

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

class SwitchboardCleanAdminBar {

    private $settings = [];

    public function __construct() {
        // Load settings
        $this->settings = get_option('switchboard_clean-admin-bar_settings', []);

        // Apply admin bar modifications
        add_action('admin_bar_menu', [$this, 'remove_admin_bar_items'], 999);
        add_action('wp_before_admin_bar_render', [$this, 'remove_admin_bar_items_early']);

        // Remove help tab
        if ($this->is_enabled('help_tab')) {
            add_filter('contextual_help', [$this, 'remove_help_tabs'], 999, 3);
            add_action('admin_head', [$this, 'hide_help_tab']);
        }

        // Remove screen options
        if ($this->is_enabled('screen_options')) {
            add_action('admin_head', [$this, 'hide_screen_options']);
        }

        // Remove "Howdy" greeting (both admin and frontend)
        if ($this->is_enabled('howdy')) {
            add_action('admin_head', [$this, 'remove_howdy_js']);
            add_action('wp_head', [$this, 'remove_howdy_js']);
        }
    }

    /**
     * Check if a setting is enabled
     */
    private function is_enabled($key) {
        return !empty($this->settings[$key]);
    }

    /**
     * Register data locations for cleanup system
     *
     * @return array Data storage locations
     */
    public function register_data_locations() {
        return [
            'options' => [
                'switchboard_clean-admin-bar_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 = []) {
        $options = [
            'wp_logo' => [
                'label' => esc_html__('Remove WordPress Logo/Menu', 'wpswitchboard'),
                'description' => esc_html__('Removes the WordPress logo and its dropdown menu from the admin bar', 'wpswitchboard'),
                'where' => esc_html__('Top left corner of admin bar', 'wpswitchboard')
            ],
            'site_name' => [
                'label' => esc_html__('Remove Site Name/Home Link', 'wpswitchboard'),
                'description' => esc_html__('Removes the site name and visit site link from the admin bar', 'wpswitchboard'),
                'where' => esc_html__('Left side of admin bar, next to WordPress logo', 'wpswitchboard')
            ],
            'customize' => [
                'label' => esc_html__('Remove Customize Menu', 'wpswitchboard'),
                'description' => esc_html__('Removes the "Customize" link that opens the theme customizer', 'wpswitchboard'),
                'where' => esc_html__('Left side of admin bar (when viewing frontend)', 'wpswitchboard')
            ],
            'updates' => [
                'label' => esc_html__('Remove Updates Counter/Link', 'wpswitchboard'),
                'description' => esc_html__('Removes the updates notification counter and updates menu', 'wpswitchboard'),
                'where' => esc_html__('Top right area of admin bar (notification icon)', 'wpswitchboard')
            ],
            'comments' => [
                'label' => esc_html__('Remove Comments Counter/Link', 'wpswitchboard'),
                'description' => esc_html__('Removes the comments notification counter and comments menu', 'wpswitchboard'),
                'where' => esc_html__('Top right area of admin bar (speech bubble icon)', 'wpswitchboard')
            ],
            'new_content' => [
                'label' => esc_html__('Remove New Content Menu', 'wpswitchboard'),
                'description' => esc_html__('Removes the "+ New" menu for creating posts, pages, and media', 'wpswitchboard'),
                'where' => esc_html__('Top right area of admin bar (+ New button)', 'wpswitchboard')
            ],
            'howdy' => [
                'label' => esc_html__('Remove "Howdy" Greeting', 'wpswitchboard'),
                'description' => esc_html__('Removes the "Howdy, Username" text from the user menu', 'wpswitchboard'),
                'where' => esc_html__('Top right corner of admin bar (user menu)', 'wpswitchboard')
            ],
            'help_tab' => [
                'label' => esc_html__('Remove Help Tab', 'wpswitchboard'),
                'description' => esc_html__('Removes the help tab and drawer from admin pages', 'wpswitchboard'),
                'where' => esc_html__('Top right corner of screen (Help button)', 'wpswitchboard')
            ],
            'screen_options' => [
                'label' => esc_html__('Remove Screen Options Tab', 'wpswitchboard'),
                'description' => esc_html__('Removes the screen options tab and panel from admin pages', 'wpswitchboard'),
                'where' => esc_html__('Top right corner of screen (Screen Options button)', 'wpswitchboard')
            ]
        ];
        ?>
        <input type="hidden" name="module" value="clean-admin-bar">

        <div class="sb-sp-group">
            <div class="sb-sp-header-actions">
                <div class="sb-sp-header-content">
                    <h3 class="sb-sp-heading-sm"><?php echo esc_html__('Admin Bar Elements to Remove', 'wpswitchboard'); ?></h3>
                    <p class="sb-sp-description"><?php echo esc_html__('Toggle which elements to remove from the WordPress admin bar (toolbar at the top).', 'wpswitchboard'); ?></p>
                </div>
                <div class="sb-sp-button-group sb-sp-header-buttons">
                    <button type="button" class="button button-small button-primary switchboard-toggle-all" data-action="enable">
                        <?php echo esc_html__('Enable All', 'wpswitchboard'); ?>
                    </button>
                    <button type="button" class="button button-small button-primary-outline switchboard-toggle-all" data-action="disable">
                        <?php echo esc_html__('Disable All', 'wpswitchboard'); ?>
                    </button>
                </div>
            </div>

            <?php foreach ($options as $key => $option): ?>
                <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 echo esc_html($option['label']); ?></strong>
                            <label class="sb-module-toggle">
                                    <input type="checkbox"
                                           name="setting_<?php echo esc_attr($key); ?>"
                                           value="1"
                                           <?php checked(!empty($settings[$key]), true); ?>>
                                <span class="sb-module-toggle-slider"></span>
                                </label>
                            </div>
                        <p class="sb-module-option-text"><?php echo esc_html($option['description']); ?></p>
                        <p class="sb-module-option-location">
                                <span class="dashicons dashicons-location-alt"></span>
                                <?php echo esc_html($option['where']); ?>
                            </p>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <script>
            jQuery(document).ready(function($) {
                // Handle Enable All / Disable All buttons
                $('.switchboard-toggle-all').on('click', function(e) {
                    e.preventDefault();
                    var action = $(this).data('action');
                    var checkboxes = $('.sb-module-toggle input[type="checkbox"]');

                    if (action === 'enable') {
                        checkboxes.prop('checked', true).trigger('change');
                    } else if (action === 'disable') {
                        checkboxes.prop('checked', false).trigger('change');
                    }
                });
            });
        </script>
        <?php
    }

    /**
     * Remove admin bar items based on settings
     */
    public function remove_admin_bar_items($wp_admin_bar) {
        if (!is_object($wp_admin_bar)) {
            return;
        }

        // Remove WordPress logo
        if ($this->is_enabled('wp_logo')) {
            $wp_admin_bar->remove_node('wp-logo');
        }

        // Remove site name
        if ($this->is_enabled('site_name')) {
            $wp_admin_bar->remove_node('site-name');
            $wp_admin_bar->remove_node('view-site');
        }

        // Remove customize (appears on frontend under site-name menu)
        if ($this->is_enabled('customize')) {
            $wp_admin_bar->remove_node('customize');
            // Also remove related customizer nodes that may appear
            $wp_admin_bar->remove_node('customize-background');
            $wp_admin_bar->remove_node('customize-header');
            $wp_admin_bar->remove_node('widgets');
            $wp_admin_bar->remove_node('menus');
        }

        // Remove updates
        if ($this->is_enabled('updates')) {
            $wp_admin_bar->remove_node('updates');
        }

        // Remove comments
        if ($this->is_enabled('comments')) {
            $wp_admin_bar->remove_node('comments');
        }

        // Remove new content
        if ($this->is_enabled('new_content')) {
            $wp_admin_bar->remove_node('new-content');
        }
    }

    /**
     * Early removal of admin bar items
     */
    public function remove_admin_bar_items_early() {
        global $wp_admin_bar;

        if (is_object($wp_admin_bar)) {
            $this->remove_admin_bar_items($wp_admin_bar);
        }
    }

    /**
     * Remove "Howdy" from user menu using JavaScript
     * Note: PHP hooks don't work reliably because WordPress generates admin bar too late
     */
    public function remove_howdy_js() {
        ?>
        <script>
            // Remove "Howdy, " text from admin bar
            document.addEventListener('DOMContentLoaded', function() {
                var accountLink = document.querySelector('#wp-admin-bar-my-account > a.ab-item');
                if (accountLink) {
                    accountLink.innerHTML = accountLink.innerHTML.replace(/Howdy,\s*/g, '');
                }
            });
        </script>
        <?php
    }

    /**
     * Remove help tabs
     */
    public function remove_help_tabs($old_help, $screen_id, $screen) {
        if (is_object($screen)) {
            $screen->remove_help_tabs();
        }
        return $old_help;
    }

    /**
     * Hide help tab with CSS
     */
    public function hide_help_tab() {
        echo '<style>#contextual-help-link-wrap { display: none !important; }</style>';
    }

    /**
     * Hide screen options with CSS
     */
    public function hide_screen_options() {
        echo '<style>#screen-options-link-wrap { display: none !important; }</style>';
    }

}

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