1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331<?php
/**
* WHAT IS THIS FILE?
*
* The Documents screen: every PDF the plugin knows about, and what happened to it.
*
* WHY DOES IT EXIST?
*
* The Overview screen answers "how is it going?". This one answers "what happened
* to THAT file?" โ which is the question that arrives by email from someone who
* cares about one specific document.
*
* WHY NOT WP_List_Table?
*
* WordPress has a class for admin tables, and it would give us sortable columns and
* bulk actions for free. It is also officially undocumented, marked as internal, and
* changes between releases. This table is a filter, a list, and a Retry button.
* Writing those three things plainly is less code than configuring WP_List_Table to
* do them, and a reader of any experience level can follow it โ which matters more
* here than saving a few lines.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Equalify_Iris_Admin_Documents {
/** Rows per page. */
const PER_PAGE = 50;
public function render(): void {
$screen = Equalify_Iris_Admin::SLUG . '-documents';
// phpcs:disable WordPress.Security.NonceVerification -- these are read-only filters, not actions.
$status = isset( $_GET['status'] ) ? sanitize_key( wp_unslash( $_GET['status'] ) ) : '';
$site_id = isset( $_GET['site'] ) ? (int) $_GET['site'] : 0;
$search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '';
$page = isset( $_GET['paged'] ) ? max( 1, (int) $_GET['paged'] ) : 1;
// phpcs:enable
$result = Equalify_Iris_Documents::query(
array(
'status' => $status,
'site_id' => $site_id,
'search' => $search,
'per_page' => self::PER_PAGE,
'page' => $page,
)
);
echo '<div class="wrap equalify-iris">';
echo '<h1>' . esc_html__( 'Documents', 'equalify-iris' ) . '</h1>';
Equalify_Iris_Admin::tabs( $screen );
Equalify_Iris_Admin::notice();
$this->render_filters( $status, $site_id, $search );
if ( ! $result['items'] ) {
echo '<p>' . esc_html__( 'No PDFs match. If the queue is empty altogether, processing may not have run yet โ check the Overview screen.', 'equalify-iris' ) . '</p>';
echo '</div>';
return;
}
$this->render_table( $result['items'], $screen );
$this->render_pagination( $result['total'], $page, $status, $site_id, $search );
if ( Equalify_Iris_Documents::FAILED === $status ) {
echo '<h2>' . esc_html__( 'Retry everything that failed', 'equalify-iris' ) . '</h2>';
echo '<p>' . esc_html__( 'Puts every failed PDF back in the queue with a fresh set of attempts. Sensible after fixing whatever caused the failures.', 'equalify-iris' ) . '</p>';
Equalify_Iris_Admin::button( 'retry_all_failed', $screen, __( 'Retry all failed PDFs', 'equalify-iris' ), 'button' );
}
echo '</div>';
}
/**
* The filter bar.
*
* A plain GET form, so the filtered view has a URL that can be bookmarked and
* shared. "Send me a link to the failures" is a reasonable thing to ask for.
*/
private function render_filters( string $status, int $site_id, string $search ): void {
$counts = Equalify_Iris_Documents::counts_by_status();
echo '<form method="get" action="' . esc_url( network_admin_url( 'admin.php' ) ) . '" class="equalify-iris-filters">';
echo '<input type="hidden" name="page" value="' . esc_attr( Equalify_Iris_Admin::SLUG . '-documents' ) . '">';
echo '<label for="equalify-iris-status">' . esc_html__( 'Stage', 'equalify-iris' ) . '</label> ';
echo '<select name="status" id="equalify-iris-status">';
echo '<option value="">' . esc_html__( 'All stages', 'equalify-iris' ) . '</option>';
foreach ( Equalify_Iris_Documents::status_labels() as $value => $label ) {
printf(
'<option value="%s"%s>%s (%s)</option>',
esc_attr( $value ),
selected( $status, $value, false ),
esc_html( $label ),
esc_html( number_format_i18n( (int) $counts[ $value ] ) )
);
}
echo '</select> ';
echo '<label for="equalify-iris-site">' . esc_html__( 'Site', 'equalify-iris' ) . '</label> ';
echo '<select name="site" id="equalify-iris-site">';
echo '<option value="0">' . esc_html__( 'All sites', 'equalify-iris' ) . '</option>';
// Bounded at 200 sites on purpose: a select with a thousand options is not
// usable, and building it means a query per site for the name. Above that,
// filter by URL search instead.
$sites = get_sites(
array(
'number' => 200,
'orderby' => 'id',
)
);
foreach ( $sites as $site ) {
printf(
'<option value="%d"%s>%s</option>',
(int) $site->blog_id,
selected( $site_id, (int) $site->blog_id, false ),
esc_html( untrailingslashit( $site->domain . $site->path ) )
);
}
echo '</select> ';
echo '<label for="equalify-iris-search">' . esc_html__( 'File name contains', 'equalify-iris' ) . '</label> ';
echo '<input type="search" name="s" id="equalify-iris-search" value="' . esc_attr( $search ) . '"> ';
echo '<button type="submit" class="button">' . esc_html__( 'Filter', 'equalify-iris' ) . '</button>';
echo '</form>';
}
/**
* The table of documents.
*/
private function render_table( array $items, string $screen ): void {
echo '<table class="widefat striped equalify-iris-documents">';
echo '<caption class="screen-reader-text">' . esc_html__( 'PDFs found across the network', 'equalify-iris' ) . '</caption>';
echo '<thead><tr>';
echo '<th scope="col">' . esc_html__( 'PDF', 'equalify-iris' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Site', 'equalify-iris' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Pages', 'equalify-iris' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Stage', 'equalify-iris' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Appears on', 'equalify-iris' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Accessible version', 'equalify-iris' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Action', 'equalify-iris' ) . '</th>';
echo '</tr></thead><tbody>';
foreach ( $items as $document ) {
$this->render_row( $document, $screen );
}
echo '</tbody></table>';
}
/**
* One row.
*/
private function render_row( object $document, string $screen ): void {
$name = Equalify_Iris_Documents::display_name( $document );
echo '<tr>';
// The PDF itself, and any error underneath it. Putting the error here rather
// than in its own column means it gets the width it needs to be a sentence.
echo '<th scope="row">';
printf(
'<a href="%s">%s</a>',
esc_url( (string) $document->pdf_url ),
esc_html( $name )
);
if ( $document->last_error ) {
echo '<br><span class="equalify-iris-error">' . esc_html( (string) $document->last_error ) . '</span>';
}
if ( (int) $document->attempts > 0 ) {
echo '<br><span class="description">';
printf(
/* translators: %d: number of attempts. */
esc_html__( 'Attempts: %d', 'equalify-iris' ),
(int) $document->attempts
);
echo '</span>';
}
echo '</th>';
echo '<td>' . esc_html( $this->site_name( (int) $document->site_id ) ) . '</td>';
echo '<td>' . esc_html( $document->page_count ? number_format_i18n( (int) $document->page_count ) : '?' ) . '</td>';
echo '<td>' . esc_html( Equalify_Iris_Documents::status_label( (string) $document->status ) ) . '</td>';
echo '<td>' . esc_html( number_format_i18n( Equalify_Iris_Documents::sighting_count( (int) $document->id ) ) ) . '</td>';
echo '<td>';
if ( $document->doc_post_id ) {
switch_to_blog( (int) $document->site_id );
$permalink = get_permalink( (int) $document->doc_post_id );
restore_current_blog();
if ( $permalink ) {
printf(
'<a href="%s">%s</a>',
esc_url( $permalink ),
esc_html__( 'View', 'equalify-iris' )
);
} else {
echo '—';
}
} else {
echo '—';
}
echo '</td>';
echo '<td>';
// Retry is offered for anything that could plausibly succeed on another go.
// Deliberately not offered for too_long or too_big: retrying a 60-page PDF
// will fail in exactly the same way, and a button that cannot work is worse
// than no button.
$retryable = in_array(
(string) $document->status,
array(
Equalify_Iris_Documents::FAILED,
Equalify_Iris_Documents::PUBLISHED,
Equalify_Iris_Documents::RETIRED,
),
true
);
if ( $retryable ) {
Equalify_Iris_Admin::form_open( 'retry', $screen );
echo '<input type="hidden" name="document_id" value="' . esc_attr( (string) $document->id ) . '">';
printf(
'<button type="submit" class="button button-small">%s</button>',
esc_html(
Equalify_Iris_Documents::PUBLISHED === $document->status
? __( 'Convert again', 'equalify-iris' )
: __( 'Retry', 'equalify-iris' )
)
);
Equalify_Iris_Admin::form_close();
} else {
echo '—';
}
echo '</td>';
echo '</tr>';
}
/**
* A site's address, looked up once per site rather than once per row.
*/
private function site_name( int $site_id ): string {
static $names = array();
if ( isset( $names[ $site_id ] ) ) {
return $names[ $site_id ];
}
$site = get_site( $site_id );
$names[ $site_id ] = $site
? untrailingslashit( $site->domain . $site->path )
: sprintf(
/* translators: %d: a site id. */
__( 'site %d (deleted)', 'equalify-iris' ),
$site_id
);
return $names[ $site_id ];
}
/**
* Page links.
*/
private function render_pagination( int $total, int $page, string $status, int $site_id, string $search ): void {
$pages = (int) ceil( $total / self::PER_PAGE );
echo '<p class="equalify-iris-pagination">';
printf(
/* translators: 1: number shown, 2: total. */
esc_html__( 'Showing %1$s of %2$s PDFs.', 'equalify-iris' ),
esc_html( number_format_i18n( min( self::PER_PAGE, $total - ( ( $page - 1 ) * self::PER_PAGE ) ) ) ),
esc_html( number_format_i18n( $total ) )
);
if ( $pages > 1 ) {
$base = add_query_arg(
array(
'page' => Equalify_Iris_Admin::SLUG . '-documents',
'status' => $status,
'site' => $site_id ?: null,
's' => $search ?: null,
),
network_admin_url( 'admin.php' )
);
echo ' ';
echo wp_kses_post(
paginate_links(
array(
'base' => add_query_arg( 'paged', '%#%', $base ),
'format' => '',
'current' => $page,
'total' => $pages,
'prev_text' => __( '« Previous', 'equalify-iris' ),
'next_text' => __( 'Next »', 'equalify-iris' ),
)
)
);
}
echo '</p>';
}
}