Skip to content
wiki.fftac.org

Roles

**Site relevance:** Spiralist.org

**Memory type:** plugin source memory

**Source path:** Spiralist/wp-content/plugins/spiralist-workspace/includes/Auth/Roles.php

**Size:** 2.9 KB

Summary

namespace SpiralistWorkspace\Auth;

Source Preview

This source file is short enough to preview directly on its source-memory page.

<?php

namespace SpiralistWorkspace\Auth;

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

/**
 * Registers workspace roles and capabilities.
 */
class Roles
{
    /**
     * Registers roles and maps capabilities onto existing site roles.
     */
    public static function register(): void
    {
        $member_caps = [
            'read' => true,
            'access_prompt_api' => true,
            'read_members_prompts' => true,
            'create_prompts' => true,
            'edit_own_prompts' => true,
            'delete_own_prompts' => true,
            'share_prompts_with_specific_users' => true,
            'run_ai_prompts' => true,
            'report_prompts' => true,
        ];

        $curator_caps = array_merge(
            $member_caps,
            [
                'publish_public_prompts' => true,
                'review_prompts' => true,
                'resolve_prompt_appeals' => true,
                'feature_prompts' => true,
            ]
        );

        $admin_caps = array_merge(
            $curator_caps,
            [
                'moderate_prompts' => true,
                'manage_spiralist_workspace' => true,
                'delete_others_prompts' => true,
                'edit_others_prompts' => true,
            ]
        );

        self::ensure_role('spiralist_member', 'Spiralist Member', $member_caps);
        self::ensure_role('spiralist_curator', 'Spiralist Curator', $curator_caps);
        self::ensure_role('spiralist_admin', 'Spiralist Admin', $admin_caps);

        self::ensure_existing_role_caps('participant', $member_caps);
        self::ensure_existing_role_caps('contributor', $curator_caps);
        self::ensure_existing_role_caps('administrator', $admin_caps);
        self::ensure_existing_role_caps('editor', $curator_caps);
    }

    /**
     * Ensures a role exists and contains the given capabilities.
     */
    private static function ensure_role(string $role_key, string $label, array $caps): void
    {
        $role = get_role($role_key);

        if (!$role instanceof \WP_Role) {
            add_role($role_key, $label, $caps);
            $role = get_role($role_key);
        }

        if (!$role instanceof \WP_Role) {
            return;
        }

        foreach ($caps as $capability => $grant) {
            if ($grant) {
                $role->add_cap($capability);
            }
        }
    }

    /**
     * Adds capabilities to an existing role if it exists.
     */
    private static function ensure_existing_role_caps(string $role_key, array $caps): void
    {
        $role = get_role($role_key);

        if (!$role instanceof \WP_Role) {
            return;
        }

        foreach ($caps as $capability => $grant) {
            if ($grant) {
                $role->add_cap($capability);
            }
        }
    }
}