<?php
/**
 * Name: Disable Dashicons (Frontend)
 * Description: Remove Dashicons icon font from frontend when admin bar is hidden to improve page load speed by eliminating unnecessary font file for non-logged users
 * Category: Optimization
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Improve Performance",
 *     "description": "Remove unnecessary Dashicons CSS from frontend pages to reduce page load time and improve performance."
 *   },
 *   {
 *     "title": "Reduce HTTP Requests",
 *     "description": "Eliminate unused icon font requests on frontend when the WordPress admin bar is not displayed."
 *   }
 * ]
 * Added: 2025-06-22
 * Tags: dashicons, icons, frontend, performance, optimization
 * WhereToFind: {
 *   "location": "Works on frontend automatically",
 *   "url": "",
 *   "instructions": "Dashicons CSS removed from frontend when admin bar hidden",
 *   "newTab": false
 * }
 */

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

class SwitchboardDisableDashiconsFrontend {

    public function __construct() {
        add_action('wp_enqueue_scripts', [$this, 'dequeue_dashicons'], 100);
    }

    /**
     * Render settings panel with Bricks Builder compatibility warning
     */
    public function render_settings($settings = []) {
        $enabled = isset($settings['enabled']) ? $settings['enabled'] : 0;

        echo '
        <input type="hidden" name="module" value="disable-dashicons-frontend">

        <div class="sb-sp-info-box sb-sp-info-box-tip" style="margin-bottom: 24px;">
            <h4 class="sb-sp-heading-sm">' . esc_html__('Page Builder Compatibility Warning', 'wpswitchboard') . '</h4>
            <p class="sb-sp-description" style="margin: 0;">
                <strong>' . esc_html__('Do not enable this module if you are using any frontend page builder or editor.', 'wpswitchboard') . '</strong><br>
                ' . esc_html__('Disabling Dashicons will cause media upload browsers and other UI elements in frontend page builders (Bricks, Elementor, Oxygen, etc.) to appear broken or display incorrectly. Dashicons are required for proper display of media library interfaces and admin UI components.', 'wpswitchboard') . '
            </p>
        </div>

        <div class="sb-sp-info-box sb-sp-info-box-info" style="margin-bottom: 24px;">
            <h4 class="sb-sp-heading-sm">' . esc_html__('How This Module Works', 'wpswitchboard') . '</h4>
            <p class="sb-sp-description" style="margin: 0;">
                ' . esc_html__('This module automatically removes Dashicons CSS from the frontend when the WordPress admin bar is hidden. This reduces page load time by eliminating an unnecessary ~50KB font file for non-logged-in users.', 'wpswitchboard') . '
            </p>
        </div>

        <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">' . esc_html__('Enable Dashicons Removal', 'wpswitchboard') . '</strong>
                    <label class="sb-module-toggle">
                        <input type="checkbox" name="setting_enabled" value="1" ' . checked($enabled, 1, false) . '>
                        <span class="sb-module-toggle-slider"></span>
                    </label>
                </div>
                <p class="sb-module-option-text">' . esc_html__('Check this box to confirm you want to disable Dashicons on the frontend. Only enable if you are NOT using Bricks Builder.', 'wpswitchboard') . '</p>
            </div>
        </div>
        ';
    }

    public function register_data_locations() {
        return [
            'options' => [
                'switchboard_disable-dashicons-frontend_settings'
            ],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }

    public function dequeue_dashicons() {
        // Check if module is enabled in settings
        $settings = get_option('switchboard_disable-dashicons-frontend_settings', []);
        $enabled = isset($settings['enabled']) ? $settings['enabled'] : 0;

        // Only proceed if enabled in settings
        if (!$enabled) {
            return;
        }

        // Only keep Dashicons if the admin bar is showing
        if (!is_admin_bar_showing()) {
            wp_dequeue_style('dashicons');
            wp_deregister_style('dashicons');
        }
    }
}

$switchboard_instance = new SwitchboardDisableDashiconsFrontend();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
