Skip to content
wiki.fftac.org

Appealrepository - Source Excerpt 02

Back to Appealrepository

Summary

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

**Source path:** Spiralist/wp-content/plugins/spiralist-workspace/includes/Domain/AppealRepository.php

FROM {$table} a
             LEFT JOIN {$users} appellant ON appellant.ID = a.appellant_user_id
             LEFT JOIN {$users} resolver ON resolver.ID = a.resolved_by_user_id
             WHERE a.id = %d
             LIMIT 1",
            $id
        );
        $row = $wpdb->get_row($sql, ARRAY_A);

        return is_array($row) ? $this->normalize_row($row) : [];
    }

    /**
     * Returns recent appeals across prompts.
     *
     * @return array<int,array<string,mixed>>
     */
    private function list_recent(int $limit, string $status = ''): array
    {
        global $wpdb;

        $table = Installer::table('prompt_appeals');
        $users = $wpdb->users;
        $limit = max(1, $limit);
        $status = sanitize_key($status);

        if ($status !== '') {
            $sql = $wpdb->prepare(
                "SELECT a.*, appellant.display_name AS appellant_display_name, resolver.display_name AS resolver_display_name
                 FROM {$table} a
                 LEFT JOIN {$users} appellant ON appellant.ID = a.appellant_user_id
                 LEFT JOIN {$users} resolver ON resolver.ID = a.resolved_by_user_id
                 WHERE a.status = %s
                 ORDER BY a.updated_utc DESC, a.id DESC
                 LIMIT %d",
                $status,
                $limit
            );
        } else {
            $sql = $wpdb->prepare(
                "SELECT a.*, appellant.display_name AS appellant_display_name, resolver.display_name AS resolver_display_name
                 FROM {$table} a
                 LEFT JOIN {$users} appellant ON appellant.ID = a.appellant_user_id
                 LEFT JOIN {$users} resolver ON resolver.ID = a.resolved_by_user_id
                 ORDER BY a.updated_utc DESC, a.id DESC
                 LIMIT %d",
                $limit
            );
        }

        $rows = $wpdb->get_results($sql, ARRAY_A);

        return array_map([$this, 'normalize_row'], is_array($rows) ? $rows : []);
    }

    /**
     * Normalizes one DB row for API and template use.
     *
     * @param array<string,mixed> $row
     * @return array<string,mixed>
     */
    private function normalize_row(array $row): array
    {
        return [
            'id' => (int) ($row['id'] ?? 0),
            'prompt_post_id' => (int) ($row['prompt_post_id'] ?? 0),
            'appellant_user_id' => (int) ($row['appellant_user_id'] ?? 0),
            'appellant_label' => (string) ($row['appellant_display_name'] ?? ''),
            'status' => sanitize_key((string) ($row['status'] ?? 'open')),
            'resolution' => sanitize_key((string) ($row['resolution'] ?? '')),
            'reason' => (string) ($row['reason'] ?? ''),
            'resolution_note' => (string) ($row['resolution_note'] ?? ''),
            'created_utc' => (string) ($row['created_utc'] ?? ''),
            'updated_utc' => (string) ($row['updated_utc'] ?? ''),
            'resolved_utc' => (string) ($row['resolved_utc'] ?? ''),
            'resolved_by_user_id' => (int) ($row['resolved_by_user_id'] ?? 0),
            'resolved_by_label' => (string) ($row['resolver_display_name'] ?? ''),
        ];
    }
}