<?php
/**
 * Name: Custom Body Classes
 * Description: Add custom CSS classes to body tag for advanced styling control allowing you to target specific pages or conditions with CSS for design customization
 * Category: Content
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "User Role Styling",
 *     "description": "Add 'subscriber-logged-in' class to style members-only content differently than public view with CSS."
 *   },
 *   {
 *     "title": "Device Detection",
 *     "description": "Add 'mobile-device' or 'tablet-device' classes for CSS-only responsive adjustments without JavaScript."
 *   },
 *   {
 *     "title": "A/B Testing Setup",
 *     "description": "Add custom class based on user segment for styling different variations of landing pages in split tests."
 *   }
 * ]
 * Added: 2025-08-18
 * Tags: css, body-class, customization, styling, frontend
 * WhereToFind: {
 *   "location": "Configure in module settings",
 *   "url": "",
 *   "instructions": "Classes appear in frontend body tag",
 *   "newTab": false
 * }
 */

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

class SwitchboardCustomBodyClass {
    
    public function __construct() {
        add_filter('body_class', [$this, 'add_custom_classes']);
        
        // 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-body-class_settings'
            ],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
            'transients' => []
        ];
    }
    
    public function render_settings($settings = []) {
        $classes = isset($settings['classes']) ? $settings['classes'] : '';
        ?>
        <input type="hidden" name="module" value="custom-body-class">

        <div class="sb-sp-group">
            <label class="sb-sp-label" for="setting_classes"><?php esc_html_e('Custom CSS Classes', 'wpswitchboard'); ?></label>
            <input type="text" name="setting_classes" id="setting_classes" value="<?php echo esc_attr($classes); ?>">
        <p class="sb-sp-description"><?php esc_html_e('Enter comma-separated CSS class names to add to the body tag.', 'wpswitchboard'); ?><br><?php esc_html_e('Example: dark-mode, custom-layout, special-page', 'wpswitchboard'); ?></p>
        </div>
        <?php
    }
    
    public function add_custom_classes($classes) {
        $settings = get_option('switchboard_custom-body-class_settings', []);
        $custom_classes = isset($settings['classes']) ? $settings['classes'] : '';

        if (!empty($custom_classes) && is_string($custom_classes)) {
            // Support both comma and space separated classes
            // Replace commas with spaces, then split by spaces
            $custom_classes = str_replace(',', ' ', $custom_classes);
            $custom_array = preg_split('/\s+/', $custom_classes, -1, PREG_SPLIT_NO_EMPTY);

            // Trim and sanitize each class name
            $custom_array = array_filter(array_map(function($class) {
                return sanitize_html_class(trim($class));
            }, $custom_array));
            $classes = array_merge($classes, $custom_array);
        }

        return $classes;
    }
}

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