• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bleedingdeacons / confur / 43103750055

11 Nov 2026 09:31PM UTC coverage: 95.889% (-0.5%) from 96.361%
43103750055

push

github

web-flow
refactor: Phase 4b — admin screens render from templates; Status page on DuplicateFinder (#90)

Each admin screen is now a controller (permission check, gather data) and a
template (markup only). Template::render() includes templates/<name>.php
with exactly the variables passed — no $this — and refuses names that
could leave templates/.

Templates: admin/status, settings, email-templates, questions, results
(the page shell; the report tables are still built in PHP), and the
dashboard widget and its update list (replacing ~35 echo lines).
Settings and Email Templates take their notice flags as variables
instead of reading $_GET in the markup; the Questions screen's notices
are data rather than echoed.

Status page
- Reads the registrations once per load (was twice) and shares them
  between the table and the duplicate warning.
- The duplicate warning uses DuplicateFinder::groups(), the same matching
  rule registration uses (it had its own copy). That fixes two bugs:
  duplicates were keyed by meeting, so two groups each registered twice
  for one meeting showed as one warning; and rows were flagged by meeting,
  so another group's single valid registration for that meeting was
  marked DUPLICATE and offered Cancel. Rows are now flagged by
  registration.
- Drops meeting-id normalisation AnswerStore already does.

Also
- The dashboard widget labels the All Committees committee from the
  question set (it said "Committee 7"), and escapes its "Last updated"
  time.
- AnswerRepository::registeredRows() builds the registered-groups rows
  from Answers already loaded.

118 of 121 new or added lines in 9 files covered. (97.52%)

2006 of 2092 relevant lines covered (95.89%)

4.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.88
/src/Admin/QuestionSetAdminPage.php
1
<?php
2

3
namespace Confur\Admin;
4

5
use Confur\Domain\QuestionSet;
6
use Confur\Storage\QuestionSetStore;
7

8
/**
9
 * Questions for Conference → Questions: which committees there are, how many
10
 * questions each has, and which one is "All Committees".
11
 *
12
 * Changing the set changes what "Complete" requires, the progress table and
13
 * the Results page. The answer pages themselves are ordinary pages built from
14
 * [answer] shortcodes, so they have to be edited to match — the screen says so.
15
 */
16
class QuestionSetAdminPage
17
{
18
    public const PAGE_SLUG = 'confur-questions';
19
    private const CAPABILITY = 'manage_options';
20
    private const ACTION = 'confur_update_questions';
21
    private const NONCE_ACTION = 'confur_questions_action';
22
    private const NONCE_FIELD = 'confur_questions_nonce';
23

24
    /** Blank rows offered for adding committees. */
25
    private const SPARE_ROWS = 2;
26

27
    public function init(): void
2 ✔
28
    {
29
        if (!is_admin()) {
2 ✔
30
            return;
1 ✔
31
        }
32

33
        add_action('admin_menu', [$this, 'addAdminMenu']);
1 ✔
34
        add_action('admin_post_' . self::ACTION, [$this, 'handleFormSubmission']);
1 ✔
35
    }
36

37
    public function addAdminMenu(): void
1 ✔
38
    {
39
        add_submenu_page(
1 ✔
40
            'confur',
1 ✔
41
            'Conference Questions',
1 ✔
42
            'Questions',
1 ✔
43
            self::CAPABILITY,
1 ✔
44
            self::PAGE_SLUG,
1 ✔
45
            [$this, 'renderAdminPage']
1 ✔
46
        );
1 ✔
47
    }
48

49
    public function handleFormSubmission(): void
2 ✔
50
    {
51
        if (!current_user_can(self::CAPABILITY)) {
2 ✔
52
            wp_die(__('You do not have sufficient permissions to access this page.'));
1 ✔
53
        }
54

55
        if (
56
            !isset($_POST[self::NONCE_FIELD]) ||
1 ✔
57
            !wp_verify_nonce($_POST[self::NONCE_FIELD], self::NONCE_ACTION)
1 ✔
58
        ) {
59
            wp_die(__('Security check failed.'));
1 ✔
60
        }
61

62
        wp_redirect($this->resolveSubmissionRedirect($_POST));
×
63
        exit;
×
64
    }
65

66
    /**
67
     * Apply the submitted form and say where to go next. Split from
68
     * handleFormSubmission() so tests can drive it without the exit.
69
     *
70
     * @param array<string, mixed> $post The submitted form.
71
     */
72
    private function resolveSubmissionRedirect(array $post): string
3 ✔
73
    {
74
        if (isset($post['reset_to_default'])) {
3 ✔
75
            return $this->pageUrl(QuestionSetStore::reset() ? ['updated' => 'reset'] : ['error' => 'save']);
1 ✔
76
        }
77

78
        try {
79
            $questions = self::parseForm($post);
2 ✔
80
        } catch (\InvalidArgumentException $e) {
1 ✔
81
            // The reason is kept server-side rather than put in the URL, so a
82
            // crafted link cannot put words in an admin notice.
83
            set_transient(self::errorTransient(), $e->getMessage(), 5 * MINUTE_IN_SECONDS);
1 ✔
84
            return $this->pageUrl(['error' => 'invalid']);
1 ✔
85
        }
86

87
        return $this->pageUrl(QuestionSetStore::save($questions) ? ['updated' => '1'] : ['error' => 'save']);
1 ✔
88
    }
89

90
    /**
91
     * Build a QuestionSet from the form's rows. A row with no committee
92
     * number or a question count of 0 is dropped, which is how a committee is
93
     * removed; the spare blank rows are how one is added.
94
     *
95
     * @param array<string, mixed> $post
96
     * @throws \InvalidArgumentException When the rows do not make a valid set.
97
     */
98
    public static function parseForm(array $post): QuestionSet
8 ✔
99
    {
100
        $committees = is_array($post['committee'] ?? null) ? $post['committee'] : [];
8 ✔
101
        $counts = is_array($post['questions'] ?? null) ? $post['questions'] : [];
8 ✔
102
        $allRow = isset($post['all_committees']) ? (string) $post['all_committees'] : '';
8 ✔
103

104
        $questionCounts = [];
8 ✔
105
        $allCommittees = null;
8 ✔
106

107
        foreach ($committees as $row => $committee) {
8 ✔
108
            $committee = trim((string) $committee);
8 ✔
109
            $count = trim((string) ($counts[$row] ?? ''));
8 ✔
110

111
            if ($committee === '' || $count === '' || $count === '0') {
8 ✔
112
                continue;
3 ✔
113
            }
114

115
            if (!ctype_digit($committee) || !ctype_digit($count)) {
7 ✔
116
                throw new \InvalidArgumentException('Committee numbers and question counts must be whole numbers.');
2 ✔
117
            }
118

119
            $number = (int) $committee;
5 ✔
120

121
            if (isset($questionCounts[$number])) {
5 ✔
122
                throw new \InvalidArgumentException("Committee $number is listed twice.");
2 ✔
123
            }
124

125
            $questionCounts[$number] = (int) $count;
5 ✔
126

127
            if ((string) $row === $allRow) {
5 ✔
128
                $allCommittees = $number;
2 ✔
129
            }
130
        }
131

132
        return new QuestionSet($questionCounts, $allCommittees);
4 ✔
133
    }
134

135
    public function renderAdminPage(): void
4 ✔
136
    {
137
        if (!current_user_can(self::CAPABILITY)) {
4 ✔
138
            wp_die(__('You do not have sufficient permissions to access this page.'));
×
139
        }
140

141
        $questions = QuestionSet::current();
4 ✔
142
        $saved = QuestionSetStore::load();
4 ✔
143
        $rows = [];
4 ✔
144

145
        foreach ($saved->committees() as $committee) {
4 ✔
146
            $count = $saved->questionCount($committee);
4 ✔
147
            $rows[] = [
4 ✔
148
                'committee' => (string) $committee,
4 ✔
149
                'questions' => (string) $count,
4 ✔
150
                'isAll' => $saved->isAllCommittees($committee),
4 ✔
151
                'fields' => self::fieldRange($committee, $count),
4 ✔
152
            ];
4 ✔
153
        }
154

155
        for ($i = 0; $i < self::SPARE_ROWS; $i++) {
4 ✔
156
            $rows[] = ['committee' => '', 'questions' => '', 'isAll' => false, 'fields' => ''];
4 ✔
157
        }
158

159
        Template::render('admin/questions', [
4 ✔
160
            'notices' => $this->notices(),
4 ✔
161
            'rows' => $rows,
4 ✔
162
            'overriddenByFilter' => !$questions->equals($saved),
4 ✔
163
            'customised' => QuestionSetStore::isCustomised(),
4 ✔
164
            'action' => self::ACTION,
4 ✔
165
            'nonceAction' => self::NONCE_ACTION,
4 ✔
166
            'nonceField' => self::NONCE_FIELD,
4 ✔
167
        ]);
4 ✔
168
    }
169

170
    /**
171
     * What to tell the administrator about the last save.
172
     *
173
     * @return list<array{type: string, message: string}>
174
     */
175
    private function notices(): array
4 ✔
176
    {
177
        $updated = isset($_GET['updated']) ? sanitize_text_field(wp_unslash($_GET['updated'])) : '';
4 ✔
178
        $error = isset($_GET['error']) ? sanitize_text_field(wp_unslash($_GET['error'])) : '';
4 ✔
179
        $notices = [];
4 ✔
180

181
        if ($updated === '1') {
4 ✔
NEW
182
            $notices[] = ['type' => 'success', 'message' => 'Questions saved.'];
×
183
        } elseif ($updated === 'reset') {
4 ✔
NEW
184
            $notices[] = ['type' => 'success', 'message' => 'Questions reset to the default set.'];
×
185
        }
186

187
        if ($error === 'invalid') {
4 ✔
188
            $reason = get_transient(self::errorTransient());
1 ✔
189
            delete_transient(self::errorTransient());
1 ✔
190
            $reason = is_string($reason) && $reason !== '' ? $reason : 'the questions are not valid.';
1 ✔
191
            $notices[] = ['type' => 'error', 'message' => 'Not saved: ' . $reason];
1 ✔
192
        } elseif ($error === 'save') {
3 ✔
NEW
193
            $notices[] = ['type' => 'error', 'message' => 'The questions could not be saved. Please try again.'];
×
194
        }
195

196
        return $notices;
4 ✔
197
    }
198

199
    /** "c1_a1 – c1_a3", or the single field name for one question. */
200
    private static function fieldRange(int $committee, int $count): string
4 ✔
201
    {
202
        $first = 'c' . $committee . '_a1';
4 ✔
203

204
        return $count <= 1 ? $first : $first . ' – c' . $committee . '_a' . $count;
4 ✔
205
    }
206

207
    private static function errorTransient(): string
1 ✔
208
    {
209
        return 'confur_questions_error_' . get_current_user_id();
1 ✔
210
    }
211

212
    /** @param array<string, string> $args */
213
    private function pageUrl(array $args): string
3 ✔
214
    {
215
        return add_query_arg(['page' => self::PAGE_SLUG] + $args, admin_url('admin.php'));
3 ✔
216
    }
217
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc