Skip to content
wiki.fftac.org

Frontendcontroller - Source Excerpt 07

Back to Frontendcontroller

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/spiralist-workspace/includes/Web/FrontendController.php so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/plugins/spiralist-workspace/includes/Web/FrontendController.php

static function (array $prompt) use ($user_id): bool {
                    if ((int) ($prompt['owner_user_id'] ?? 0) === $user_id) {
                        return false;
                    }

                    if ((string) ($prompt['visibility'] ?? '') !== 'public') {
                        return false;
                    }

                    if ((string) ($prompt['prompt_status'] ?? '') !== 'published') {
                        return false;
                    }

                    if (!empty($prompt['soft_deleted_at'])) {
                        return false;
                    }

                    return in_array(
                        PromptMetaRegistry::normalize_moderation_status((string) ($prompt['moderation_status'] ?? 'draft')),
                        ['provisional_public', 'restricted_review', 'reverted'],
                        true
                    );
                }
            )
        );

        $counts = [
            'provisional_public' => 0,
            'restricted_review' => 0,
            'reverted' => 0,
            'stale' => 0,
            'urgent' => 0,
            'total' => count($records),
        ];

        foreach ($records as $record) {
            $status = PromptMetaRegistry::normalize_moderation_status((string) ($record['moderation_status'] ?? 'draft'));
            if (isset($counts[$status])) {
                $counts[$status]++;
            }
        }

        $items = array_map(
            fn(array $prompt): array => $this->augment_review_queue_prompt($prompt, $user_id),
            $records
        );

        foreach ($items as $item) {
            $queue_age = is_array($item['governance_summary']['queue_age'] ?? null)
                ? (array) $item['governance_summary']['queue_age']
                : [];

            if (!empty($queue_age['is_stale'])) {
                $counts['stale']++;
            }

            if (!empty($queue_age['is_urgent'])) {
                $counts['urgent']++;
            }
        }

        usort(
            $items,
            static function (array $left, array $right): int {
                $left_age = is_array($left['governance_summary']['queue_age'] ?? null)
                    ? (array) $left['governance_summary']['queue_age']
                    : [];
                $right_age = is_array($right['governance_summary']['queue_age'] ?? null)
                    ? (array) $right['governance_summary']['queue_age']
                    : [];
                $left_age_weight = !empty($left_age['is_urgent']) ? 0 : (!empty($left_age['is_stale']) ? 1 : 2);
                $right_age_weight = !empty($right_age['is_urgent']) ? 0 : (!empty($right_age['is_stale']) ? 1 : 2);
                $age_weight_compare = $left_age_weight <=> $right_age_weight;

                if ($age_weight_compare !== 0) {
                    return $age_weight_compare;
                }

                $weights = [
                    'restricted_review' => 0,
                    'reverted' => 1,
                    'provisional_public' => 2,
                ];
                $left_status = PromptMetaRegistry::normalize_moderation_status((string) ($left['moderation_status'] ?? 'draft'));
                $right_status = PromptMetaRegistry::normalize_moderation_status((string) ($right['moderation_status'] ?? 'draft'));
                $left_weight = $weights[$left_status] ?? 99;
                $right_weight = $weights[$right_status] ?? 99;
                $weight_compare = $left_weight <=> $right_weight;

                if ($weight_compare !== 0) {
                    return $weight_compare;
                }

                $age_compare = (int) ($right_age['hours_since_activity'] ?? 0) <=> (int) ($left_age['hours_since_activity'] ?? 0);
                if ($age_compare !== 0) {
                    return $age_compare;
                }

                return strcmp((string) ($right['updated_utc'] ?? ''), (string) ($left['updated_utc'] ?? ''));
            }
        );

        return [
            'items' => array_slice($items, 0, max(1, $limit)),
            'counts' => $counts,
        ];
    }

    /**
     * Returns steward-facing queue data for open prompt appeals.
     *
     * @return array{items:array<int,array<string,mixed>>,counts:array<string,int>}
     */
    private function appeal_queue_context(int $user_id, int $limit = 10): array
    {
        $empty = [
            'items' => [],
            'counts' => [
                'open' => 0,
                'restricted_review' => 0,
                'reverted' => 0,
                'total' => 0,
            ],
        ];

        if ($user_id <= 0 || !$this->permissions->can_resolve_prompt_appeals($user_id)) {
            return $empty;
        }

        $rows = $this->appeals->list_open(max(20, $limit * 4));
        $items = [];
        $counts = [
            'open' => 0,
            'restricted_review' => 0,
            'reverted' => 0,
            'total' => 0,
        ];

        foreach ($rows as $appeal) {
            $prompt = $this->prompts->get((int) ($appeal['prompt_post_id'] ?? 0), $user_id);
            if (empty($prompt) || !$this->permissions->can_view_prompt($prompt, $user_id)) {
                continue;
            }

            $status = PromptMetaRegistry::normalize_moderation_status((string) ($prompt['moderation_status'] ?? 'draft'));
            if (!in_array($status, ['restricted_review', 'reverted'], true)) {
                continue;
            }

            $counts['open']++;
            $counts['total']++;

            if (isset($counts[$status])) {
                $counts[$status]++;
            }

            $items[] = [
                'appeal' => $appeal,
                'prompt' => $this->augment_review_queue_prompt($prompt, $user_id),
            ];
        }

        usort(
            $items,
            static function (array $left, array $right): int {
                $weights = [
                    'reverted' => 0,
                    'restricted_review' => 1,
                ];
                $left_status = PromptMetaRegistry::normalize_moderation_status((string) ($left['prompt']['moderation_status'] ?? 'draft'));
                $right_status = PromptMetaRegistry::normalize_moderation_status((string) ($right['prompt']['moderation_status'] ?? 'draft'));
                $left_weight = $weights[$left_status] ?? 99;
                $right_weight = $weights[$right_status] ?? 99;
                $weight_compare = $left_weight <=> $right_weight;

                if ($weight_compare !== 0) {
                    return $weight_compare;
                }

                return strcmp(
                    (string) ($right['appeal']['updated_utc'] ?? ''),
                    (string) ($left['appeal']['updated_utc'] ?? '')
                );
            }
        );

        return [
            'items' => array_slice($items, 0, max(1, $limit)),
            'counts' => $counts,
        ];
    }

    /**
     * Tries to sync managed filesystem prompts before a public library request renders empty.
     */
    private function maybe_sync_system_prompts(): void
    {
        if (!function_exists('spiralist_workspace')) {
            return;
        }

        $plugin = spiralist_workspace();
        if (!is_object($plugin) || !method_exists($plugin, 'boot') || !method_exists($plugin, 'system_prompts')) {
            return;
        }

        $plugin->boot();
        $plugin->system_prompts()->maybe_sync();
    }

    /**
     * Returns whether a record may appear in the public prompt library.
     */
    private function is_public_library_prompt(array $prompt): bool
    {
        return (string) ($prompt['visibility'] ?? '') === 'public'
            && empty($prompt['soft_deleted_at'])
            && (string) ($prompt['prompt_status'] ?? '') === 'published'
            && PromptMetaRegistry::is_public_moderation_status((string) ($prompt['moderation_status'] ?? 'draft'));
    }

    /**
     * Adds summary and current-review state to a moderation queue prompt record.
     *
     * @param array<string,mixed> $prompt
     * @return array<string,mixed>
     */
    private function augment_review_queue_prompt(array $prompt, int $user_id): array
    {
        $prompt_id = (int) ($prompt['id'] ?? 0);
        $prompt['appeal_summary'] = $this->appeals->summary_for_prompt($prompt_id);
        $prompt['review_summary'] = $this->current_review_summary($prompt_id, $prompt['appeal_summary']);
        $prompt['governance_summary'] = PromptGovernanceSummary::build(
            $prompt,
            $prompt['review_summary'],
            $prompt['appeal_summary']
        );
        $prompt['viewer_review'] = $user_id > 0