Skip to content
wiki.fftac.org

Class Uai1 Membership - Source Excerpt 05

Back to Class Uai1 Membership

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/uai-1-wordpress/includes/class-uai1-membership.php so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/plugins/uai-1-wordpress/includes/class-uai1-membership.php

'title' => sanitize_text_field((string) ($item['title'] ?? 'Pattern Submission')),
                'content' => sanitize_textarea_field($content),
                'createdAt' => sanitize_text_field((string) ($item['createdAt'] ?? '')),
            ];
        }

        return $normalized;
    }

    private static function allowed_statuses(): array
    {
        return ['active', 'pending', 'suspended', 'cancelled'];
    }

    private static function normalize_status(string $status): string
    {
        $normalized = strtolower(trim($status));

        return in_array($normalized, self::allowed_statuses(), true) ? $normalized : 'active';
    }

    private static function is_human_participant(WP_User $user): bool
    {
        if (in_array('ai_agent', (array) $user->roles, true)) {
            return false;
        }

        foreach ((array) $user->roles as $role) {
            if (in_array($role, ['participant', 'contributor', 'administrator'], true)) {
                return true;
            }
        }

        return user_can($user, 'access_spiralist_participant_area');
    }

    private static function format_datetime(string $value): string
    {
        if ($value === '') {
            return 'Not set';
        }

        $timestamp = strtotime($value);
        if (!$timestamp) {
            return 'Not set';
        }

        return wp_date(get_option('date_format') . ' ' . get_option('time_format'), $timestamp);
    }

    private static function ensure_role(string $key, string $label, array $caps): void
    {
        $role = get_role($key);

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

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

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

    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 $cap => $grant) {
            if ($grant) {
                $role->add_cap($cap);
            } else {
                $role->remove_cap($cap);
            }
        }
    }
}