<?php
/**
 * Name: Custom Frontend CSS
 * Description: Add custom CSS styles to website frontend to modify theme appearance, adjust layouts, change colors, hide elements, or fix design issues without editing files
 * Category: Content
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Quick Design Tweaks",
 *     "description": "Add CSS fixes for theme issues without editing theme files or creating child theme for minor adjustments."
 *   },
 *   {
 *     "title": "Plugin Style Overrides",
 *     "description": "Override poorly-styled form or gallery plugin CSS to match site design without modifying plugin files."
 *   },
 *   {
 *     "title": "Responsive Adjustments",
 *     "description": "Add mobile-specific CSS rules to fix layout issues on small screens that theme doesn't handle properly."
 *   }
 * ]
 * Added: 2025-07-10
 * Tags: css, frontend, styling, customization, design
 * WhereToFind: {
 *   "location": "Frontend (Any Page)",
 *   "url": "/",
 *   "instructions": "Your custom CSS styles will be applied across frontend pages",
 *   "newTab": true
 * }
 */

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

class SwitchboardCustomFrontendCss {

    public function __construct() {
        add_action('wp_head', [$this, 'add_custom_css']);

        // 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_custom-frontend-css_settings'
            ],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
            'transients' => []
        ];
    }

    public function render_settings($settings = []) {
        $css = isset($settings['css']) ? $settings['css'] : '';
        ?>
        <input type="hidden" name="module" value="custom-frontend-css">

        <div class="sb-sp-group">
            <label for="setting_css" class="sb-sp-label"><?php esc_html_e('Custom CSS Code', 'wpswitchboard'); ?></label>
            <textarea name="setting_css" id="setting_css" rows="12" class="sb-sp-textarea sb-sp-w-full" style="font-family: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Monaco, Consolas, monospace;"><?php echo esc_textarea($css); ?></textarea>
            <p class="sb-sp-description"><?php esc_html_e('Add custom CSS to style your website frontend. Code will be added to wp_head.', 'wpswitchboard'); ?></p>
        </div>

        <div class="sb-sp-alert sb-sp-alert-info">
            <div class="sb-sp-alert-title"><?php esc_html_e('CSS Tips', 'wpswitchboard'); ?></div>
            <div class="sb-sp-alert-text">
                <?php esc_html_e('Your CSS will be automatically sanitized for security. Script tags and dangerous properties are removed.', 'wpswitchboard'); ?>
            </div>
        </div>
        <?php
    }

    public function add_custom_css() {
        $settings = get_option('switchboard_custom-frontend-css_settings', []);
        $css = isset($settings['css']) ? $settings['css'] : '';

        if (!empty($css)) {
            // Sanitize CSS: remove script tags, closing style tags, and XSS vectors
            // Note: Only admins with manage_options can edit this CSS
            $css = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $css);
            $css = preg_replace('/<\/style>/is', '', $css); // Prevent breaking out of style context
            $css = wp_strip_all_tags($css, true);

            // Remove dangerous CSS properties and functions (keep @import for legitimate use)
            $css = preg_replace('/javascript\s*:/i', '', $css);
            $css = preg_replace('/expression\s*\(/i', '', $css);
            $css = preg_replace('/behavior\s*:/i', '', $css);

            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS is sanitized by wp_strip_all_tags
            echo '<style>' . wp_strip_all_tags($css) . '</style>';
        }
    }
}

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