<?php
/**
 * Name: User Role Body Classes
 * Description: Add current user role as CSS class to body tag including role-administrator, role-editor, or role-guest for logged-out visitors to enable role-specific styling
 * Category: Content
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Member-Only Styling",
 *     "description": "Style pages differently for logged-in members versus guests using CSS targeting 'role-subscriber' body class."
 *   },
 *   {
 *     "title": "Admin Preview Mode",
 *     "description": "Show draft content or admin tools only to administrators using role-based CSS visibility controls."
 *   },
 *   {
 *     "title": "Tiered Membership",
 *     "description": "Apply different styling or show/hide elements based on user membership level for premium content sites."
 *   }
 * ]
 * Added: 2025-07-15
 * Tags: css, body-class, roles, styling, frontend
 * WhereToFind: {
 *   "location": "Works automatically site-wide",
 *   "url": "",
 *   "instructions": "Role classes appear in body tag automatically",
 *   "newTab": false
 * }
 */

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

class SwitchboardUserRoleBodyClass {

    public function __construct() {
        add_filter('body_class', [$this, 'add_role_classes']);
        add_filter('admin_body_class', [$this, 'add_admin_role_classes']);
    }

    /**
     * Register data locations for cleanup system
     */
    public function register_data_locations() {
        return [
            'options' => [],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }
    
    public function add_role_classes($classes) {
        if (is_user_logged_in()) {
            $user = wp_get_current_user();
            if (isset($user->roles) && is_array($user->roles)) {
                foreach ($user->roles as $role) {
                    $classes[] = 'role-' . sanitize_html_class($role);
                }
            }
        } else {
            $classes[] = 'role-guest';
        }
        return $classes;
    }
    
    public function add_admin_role_classes($classes) {
        if (is_user_logged_in()) {
            $user = wp_get_current_user();
            if (isset($user->roles) && is_array($user->roles)) {
                foreach ($user->roles as $role) {
                    $classes .= ' role-' . sanitize_html_class($role);
                }
            }
        }
        return $classes;
    }
}

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