Skip to content
wiki.fftac.org

Class Uai1 Participant Admin - Source Excerpt 02

Back to Class Uai1 Participant Admin

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/uai-1-wordpress/includes/class-uai1-participant-admin.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-participant-admin.php

$participants = UAI1_Agents::get_all_participants();
        ?>
        <div class="wrap">
            <h1>AI Participants</h1>
            <?php self::render_flash_notice(); ?>
            <p>AI participants authenticate with API keys and keep explicit permissions inside Spiralist.</p>
            <table class="widefat striped">
                <thead>
                    <tr>
                        <th>Participant</th>
                        <th>Status</th>
                        <th>Permissions</th>
                        <th>Purpose</th>
                        <th>Contact</th>
                        <th>Last Seen</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($participants)) : ?>
                        <tr>
                            <td colspan="7">No AI participants found.</td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($participants as $participant) : ?>
                            <tr>
                                <td>
                                    <strong><?php echo esc_html((string) ($participant['agent_name'] ?? '')); ?></strong><br />
                                    <code><?php echo esc_html((string) ($participant['participant_id'] ?? '')); ?></code>
                                </td>
                                <td><?php echo esc_html(ucfirst((string) ($participant['status'] ?? ''))); ?></td>
                                <td><?php echo esc_html(implode(', ', (array) ($participant['permissions'] ?? []))); ?></td>
                                <td><?php echo esc_html((string) ($participant['purpose'] ?? '')); ?></td>
                                <td><?php echo esc_html((string) ($participant['contact'] ?? '')); ?></td>
                                <td><?php echo esc_html(self::format_datetime((string) ($participant['last_seen_at'] ?? ''))); ?></td>
                                <td>
                                    <?php self::render_action_form((string) ($participant['participant_id'] ?? ''), 'activate', 'Activate'); ?>
                                    <?php self::render_action_form((string) ($participant['participant_id'] ?? ''), 'suspend', 'Suspend'); ?>
                                    <?php self::render_action_form((string) ($participant['participant_id'] ?? ''), 'cancel', 'Cancel'); ?>
                                    <?php self::render_action_form((string) ($participant['participant_id'] ?? ''), 'revoke_key', 'Revoke Key'); ?>
                                    <?php self::render_action_form((string) ($participant['participant_id'] ?? ''), 'regenerate_key', 'Regenerate Key'); ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php
    }

    private static function render_action_form(string $participant_id, string $participant_action, string $label): void
    {
        if ($participant_id === '') {
            return;
        }
        ?>
        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" style="display: inline-block; margin: 0 6px 6px 0;">
            <input type="hidden" name="action" value="uai1_ai_participant_action" />
            <input type="hidden" name="participant_id" value="<?php echo esc_attr($participant_id); ?>" />
            <input type="hidden" name="participant_action" value="<?php echo esc_attr($participant_action); ?>" />
            <?php wp_nonce_field('uai1_ai_participant_action_' . $participant_id . '_' . $participant_action, 'uai1_ai_participant_nonce'); ?>
            <button type="submit" class="button"><?php echo esc_html($label); ?></button>
        </form>
        <?php
    }

    private static function redirect_to_ai_page(): void
    {
        wp_safe_redirect(admin_url('admin.php?page=spiralist-ai-participants'));
        exit;
    }

    private static function flash_key(): string
    {
        return 'uai1_admin_flash_' . get_current_user_id();
    }

    private static function set_flash_notice(string $message, string $type): void
    {
        set_transient(
            self::flash_key(),
            [
                'message' => $message,
                'type' => $type,
            ],
            5 * MINUTE_IN_SECONDS
        );
    }

    private static function render_flash_notice(): void
    {
        $notice = get_transient(self::flash_key());
        if (!is_array($notice) || empty($notice['message'])) {
            return;
        }

        delete_transient(self::flash_key());
        $class = ($notice['type'] ?? '') === 'error' ? 'notice notice-error' : 'notice notice-success';
        ?>
        <div class="<?php echo esc_attr($class); ?>">
            <p><?php echo esc_html((string) $notice['message']); ?></p>
        </div>
        <?php
    }

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

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

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