?? GreyFile — Mystic File Browser

Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin/Handlers/



?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin

?? Viewing: RolePermissionHandler.php

<?php

namespace KS_PAC_DCFH\Admin\Handlers;

if (!defined('ABSPATH')) {
    exit;
}

class RolePermissionHandler
{
    public function load(): void
    {
        add_filter('user_has_cap', [$this, 'maybe_add_user_has_cap'], 10, 4);
    }

    public function maybe_add_user_has_cap($allcaps, $caps, $args, $user)
    {
        $settings = get_option(KS_PAC_DCFH_SETTINGS_OPTION, []);
        $role_permissions = $settings['entries_role_permissions'] ?? [];
        // Permission groups mapped to the actual capabilities
        $permission_map = [
            'accessEntries' => array_keys($this->get_permissions()['access_entries']),
            'entryActions' => array_keys($this->get_permissions()['entry_actions']),
            'exportCSV' => array_keys($this->get_permissions()['export_csv']),
        ];
        // ---- DEFAULT: GIVE ADMIN FULL ACCESS ----
        if (in_array('administrator', $user->roles, true)) {
            foreach ($permission_map as $caps_list) {
                foreach ($caps_list as $cap) {
                    $allcaps[$cap] = true;
                }
            }

            return $allcaps;
        }
        // -----------------------------------------
        // If no permissions saved in settings or user has no roles → return
        if (empty($role_permissions) || empty($user->roles)) {
            return $allcaps;
        }
        // Apply saved role permissions
        foreach ($user->roles as $role) {
            if (!isset($role_permissions[$role])) {
                continue;
            }
            foreach ($role_permissions[$role] as $group => $enabled) {
                if (empty($enabled) || !isset($permission_map[$group])) {
                    continue;
                }
                foreach ($permission_map[$group] as $cap) {
                    $allcaps[$cap] = true;
                }
            }
        }

        return $allcaps;
    }

    private function get_permissions(): array
    {
        return [
            'access_entries' => [
                'edit_draft_pwh_dcfh' => true,
                'edit_draft_pwh_dcfhs' => true,
                'edit_others_pwh_dcfhs' => true,
                'edit_private_pwh_dcfh' => true,
                'edit_private_pwh_dcfhs' => true,
                'edit_published_pwh_dcfhs' => true,
                'edit_pwh_dcfh' => true,
                'edit_pwh_dcfhs' => true,
                'publish_pwh_dcfhs' => true,
                'read_draft_pwh_dcfhs' => true,
                'read_private_pwh_dcfhs' => true,
                'read_pwh_dcfh' => true,
                'view_forms_pwh_dcfh' => true,
            ],
            'entry_actions' => [
                'delete_others_pwh_dcfhs' => true,
                'delete_private_pwh_dcfhs' => true,
                'delete_published_pwh_dcfhs' => true,
                'delete_pwh_dcfh' => true,
                'delete_pwh_dcfhs' => true,
                'perform_entry_actions_pwh_dcfh' => true,
            ],
            'export_csv' => [
                'export_csv_pwh_dcfh' => true,
            ],
        ];
    }
}


??

??