16
|
1 |
<?php |
|
2 |
/** |
|
3 |
* List Table API: WP_Privacy_Requests_Table class |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
* @since 4.9.6 |
|
8 |
*/ |
|
9 |
|
|
10 |
abstract class WP_Privacy_Requests_Table extends WP_List_Table { |
|
11 |
|
|
12 |
/** |
|
13 |
* Action name for the requests this table will work with. Classes |
|
14 |
* which inherit from WP_Privacy_Requests_Table should define this. |
|
15 |
* |
|
16 |
* Example: 'export_personal_data'. |
|
17 |
* |
|
18 |
* @since 4.9.6 |
|
19 |
* |
|
20 |
* @var string $request_type Name of action. |
|
21 |
*/ |
|
22 |
protected $request_type = 'INVALID'; |
|
23 |
|
|
24 |
/** |
|
25 |
* Post type to be used. |
|
26 |
* |
|
27 |
* @since 4.9.6 |
|
28 |
* |
|
29 |
* @var string $post_type The post type. |
|
30 |
*/ |
|
31 |
protected $post_type = 'INVALID'; |
|
32 |
|
|
33 |
/** |
|
34 |
* Get columns to show in the list table. |
|
35 |
* |
|
36 |
* @since 4.9.6 |
|
37 |
* |
|
38 |
* @return string[] Array of column titles keyed by their column name. |
|
39 |
*/ |
|
40 |
public function get_columns() { |
|
41 |
$columns = array( |
|
42 |
'cb' => '<input type="checkbox" />', |
|
43 |
'email' => __( 'Requester' ), |
|
44 |
'status' => __( 'Status' ), |
|
45 |
'created_timestamp' => __( 'Requested' ), |
|
46 |
'next_steps' => __( 'Next Steps' ), |
|
47 |
); |
|
48 |
return $columns; |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* Normalize the admin URL to the current page (by request_type). |
|
53 |
* |
|
54 |
* @since 5.3.0 |
|
55 |
* |
|
56 |
* @return string URL to the current admin page. |
|
57 |
*/ |
|
58 |
protected function get_admin_url() { |
|
59 |
$pagenow = str_replace( '_', '-', $this->request_type ); |
|
60 |
|
|
61 |
if ( 'remove-personal-data' === $pagenow ) { |
|
62 |
$pagenow = 'erase-personal-data'; |
|
63 |
} |
|
64 |
|
|
65 |
return admin_url( $pagenow . '.php' ); |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* Get a list of sortable columns. |
|
70 |
* |
|
71 |
* @since 4.9.6 |
|
72 |
* |
|
73 |
* @return array Default sortable columns. |
|
74 |
*/ |
|
75 |
protected function get_sortable_columns() { |
|
76 |
/* |
|
77 |
* The initial sorting is by 'Requested' (post_date) and descending. |
|
78 |
* With initial sorting, the first click on 'Requested' should be ascending. |
|
79 |
* With 'Requester' sorting active, the next click on 'Requested' should be descending. |
|
80 |
*/ |
|
81 |
$desc_first = isset( $_GET['orderby'] ); |
|
82 |
|
|
83 |
return array( |
|
84 |
'email' => 'requester', |
|
85 |
'created_timestamp' => array( 'requested', $desc_first ), |
|
86 |
); |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* Default primary column. |
|
91 |
* |
|
92 |
* @since 4.9.6 |
|
93 |
* |
|
94 |
* @return string Default primary column name. |
|
95 |
*/ |
|
96 |
protected function get_default_primary_column_name() { |
|
97 |
return 'email'; |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* Count number of requests for each status. |
|
102 |
* |
|
103 |
* @since 4.9.6 |
|
104 |
* |
|
105 |
* @return object Number of posts for each status. |
|
106 |
*/ |
|
107 |
protected function get_request_counts() { |
|
108 |
global $wpdb; |
|
109 |
|
|
110 |
$cache_key = $this->post_type . '-' . $this->request_type; |
|
111 |
$counts = wp_cache_get( $cache_key, 'counts' ); |
|
112 |
|
|
113 |
if ( false !== $counts ) { |
|
114 |
return $counts; |
|
115 |
} |
|
116 |
|
|
117 |
$query = " |
|
118 |
SELECT post_status, COUNT( * ) AS num_posts |
|
119 |
FROM {$wpdb->posts} |
|
120 |
WHERE post_type = %s |
|
121 |
AND post_name = %s |
|
122 |
GROUP BY post_status"; |
|
123 |
|
|
124 |
$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $this->post_type, $this->request_type ), ARRAY_A ); |
|
125 |
$counts = array_fill_keys( get_post_stati(), 0 ); |
|
126 |
|
|
127 |
foreach ( $results as $row ) { |
|
128 |
$counts[ $row['post_status'] ] = $row['num_posts']; |
|
129 |
} |
|
130 |
|
|
131 |
$counts = (object) $counts; |
|
132 |
wp_cache_set( $cache_key, $counts, 'counts' ); |
|
133 |
|
|
134 |
return $counts; |
|
135 |
} |
|
136 |
|
|
137 |
/** |
|
138 |
* Get an associative array ( id => link ) with the list of views available on this table. |
|
139 |
* |
|
140 |
* @since 4.9.6 |
|
141 |
* |
|
142 |
* @return string[] An array of HTML links keyed by their view. |
|
143 |
*/ |
|
144 |
protected function get_views() { |
|
145 |
$current_status = isset( $_REQUEST['filter-status'] ) ? sanitize_text_field( $_REQUEST['filter-status'] ) : ''; |
|
146 |
$statuses = _wp_privacy_statuses(); |
|
147 |
$views = array(); |
|
148 |
$counts = $this->get_request_counts(); |
|
149 |
$total_requests = absint( array_sum( (array) $counts ) ); |
|
150 |
|
|
151 |
// Normalized admin URL. |
|
152 |
$admin_url = $this->get_admin_url(); |
|
153 |
|
|
154 |
$current_link_attributes = empty( $current_status ) ? ' class="current" aria-current="page"' : ''; |
|
155 |
$status_label = sprintf( |
|
156 |
/* translators: %s: Number of requests. */ |
|
157 |
_nx( |
|
158 |
'All <span class="count">(%s)</span>', |
|
159 |
'All <span class="count">(%s)</span>', |
|
160 |
$total_requests, |
|
161 |
'requests' |
|
162 |
), |
|
163 |
number_format_i18n( $total_requests ) |
|
164 |
); |
|
165 |
|
|
166 |
$views['all'] = sprintf( |
|
167 |
'<a href="%s"%s>%s</a>', |
|
168 |
esc_url( $admin_url ), |
|
169 |
$current_link_attributes, |
|
170 |
$status_label |
|
171 |
); |
|
172 |
|
|
173 |
foreach ( $statuses as $status => $label ) { |
|
174 |
$post_status = get_post_status_object( $status ); |
|
175 |
if ( ! $post_status ) { |
|
176 |
continue; |
|
177 |
} |
|
178 |
|
|
179 |
$current_link_attributes = $status === $current_status ? ' class="current" aria-current="page"' : ''; |
|
180 |
$total_status_requests = absint( $counts->{$status} ); |
|
181 |
|
|
182 |
if ( ! $total_status_requests ) { |
|
183 |
continue; |
|
184 |
} |
|
185 |
|
|
186 |
$status_label = sprintf( |
|
187 |
translate_nooped_plural( $post_status->label_count, $total_status_requests ), |
|
188 |
number_format_i18n( $total_status_requests ) |
|
189 |
); |
|
190 |
|
|
191 |
$status_link = add_query_arg( 'filter-status', $status, $admin_url ); |
|
192 |
|
|
193 |
$views[ $status ] = sprintf( |
|
194 |
'<a href="%s"%s>%s</a>', |
|
195 |
esc_url( $status_link ), |
|
196 |
$current_link_attributes, |
|
197 |
$status_label |
|
198 |
); |
|
199 |
} |
|
200 |
|
|
201 |
return $views; |
|
202 |
} |
|
203 |
|
|
204 |
/** |
|
205 |
* Get bulk actions. |
|
206 |
* |
|
207 |
* @since 4.9.6 |
|
208 |
* |
|
209 |
* @return string[] Array of bulk action labels keyed by their action. |
|
210 |
*/ |
|
211 |
protected function get_bulk_actions() { |
|
212 |
return array( |
|
213 |
'delete' => __( 'Delete Requests' ), |
|
214 |
'resend' => __( 'Resend Confirmation Requests' ), |
|
215 |
); |
|
216 |
} |
|
217 |
|
|
218 |
/** |
|
219 |
* Process bulk actions. |
|
220 |
* |
|
221 |
* @since 4.9.6 |
|
222 |
*/ |
|
223 |
public function process_bulk_action() { |
|
224 |
$action = $this->current_action(); |
|
225 |
$request_ids = isset( $_REQUEST['request_id'] ) ? wp_parse_id_list( wp_unslash( $_REQUEST['request_id'] ) ) : array(); |
|
226 |
|
|
227 |
$count = 0; |
|
228 |
|
|
229 |
if ( $request_ids ) { |
|
230 |
check_admin_referer( 'bulk-privacy_requests' ); |
|
231 |
} |
|
232 |
|
|
233 |
switch ( $action ) { |
|
234 |
case 'delete': |
|
235 |
foreach ( $request_ids as $request_id ) { |
|
236 |
if ( wp_delete_post( $request_id, true ) ) { |
|
237 |
$count ++; |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
add_settings_error( |
|
242 |
'bulk_action', |
|
243 |
'bulk_action', |
|
244 |
/* translators: %d: Number of requests. */ |
|
245 |
sprintf( _n( 'Deleted %d request', 'Deleted %d requests', $count ), $count ), |
|
246 |
'success' |
|
247 |
); |
|
248 |
break; |
|
249 |
case 'resend': |
|
250 |
foreach ( $request_ids as $request_id ) { |
|
251 |
$resend = _wp_privacy_resend_request( $request_id ); |
|
252 |
|
|
253 |
if ( $resend && ! is_wp_error( $resend ) ) { |
|
254 |
$count++; |
|
255 |
} |
|
256 |
} |
|
257 |
|
|
258 |
add_settings_error( |
|
259 |
'bulk_action', |
|
260 |
'bulk_action', |
|
261 |
/* translators: %d: Number of requests. */ |
|
262 |
sprintf( _n( 'Re-sent %d request', 'Re-sent %d requests', $count ), $count ), |
|
263 |
'success' |
|
264 |
); |
|
265 |
break; |
|
266 |
} |
|
267 |
} |
|
268 |
|
|
269 |
/** |
|
270 |
* Prepare items to output. |
|
271 |
* |
|
272 |
* @since 4.9.6 |
|
273 |
* @since 5.1.0 Added support for column sorting. |
|
274 |
*/ |
|
275 |
public function prepare_items() { |
|
276 |
$this->items = array(); |
|
277 |
$posts_per_page = $this->get_items_per_page( $this->request_type . '_requests_per_page' ); |
|
278 |
$args = array( |
|
279 |
'post_type' => $this->post_type, |
|
280 |
'post_name__in' => array( $this->request_type ), |
|
281 |
'posts_per_page' => $posts_per_page, |
|
282 |
'offset' => isset( $_REQUEST['paged'] ) ? max( 0, absint( $_REQUEST['paged'] ) - 1 ) * $posts_per_page : 0, |
|
283 |
'post_status' => 'any', |
|
284 |
's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '', |
|
285 |
); |
|
286 |
|
|
287 |
$orderby_mapping = array( |
|
288 |
'requester' => 'post_title', |
|
289 |
'requested' => 'post_date', |
|
290 |
); |
|
291 |
|
|
292 |
if ( isset( $_REQUEST['orderby'] ) && isset( $orderby_mapping[ $_REQUEST['orderby'] ] ) ) { |
|
293 |
$args['orderby'] = $orderby_mapping[ $_REQUEST['orderby'] ]; |
|
294 |
} |
|
295 |
|
|
296 |
if ( isset( $_REQUEST['order'] ) && in_array( strtoupper( $_REQUEST['order'] ), array( 'ASC', 'DESC' ), true ) ) { |
|
297 |
$args['order'] = strtoupper( $_REQUEST['order'] ); |
|
298 |
} |
|
299 |
|
|
300 |
if ( ! empty( $_REQUEST['filter-status'] ) ) { |
|
301 |
$filter_status = isset( $_REQUEST['filter-status'] ) ? sanitize_text_field( $_REQUEST['filter-status'] ) : ''; |
|
302 |
$args['post_status'] = $filter_status; |
|
303 |
} |
|
304 |
|
|
305 |
$requests_query = new WP_Query( $args ); |
|
306 |
$requests = $requests_query->posts; |
|
307 |
|
|
308 |
foreach ( $requests as $request ) { |
|
309 |
$this->items[] = wp_get_user_request( $request->ID ); |
|
310 |
} |
|
311 |
|
|
312 |
$this->items = array_filter( $this->items ); |
|
313 |
|
|
314 |
$this->set_pagination_args( |
|
315 |
array( |
|
316 |
'total_items' => $requests_query->found_posts, |
|
317 |
'per_page' => $posts_per_page, |
|
318 |
) |
|
319 |
); |
|
320 |
} |
|
321 |
|
|
322 |
/** |
|
323 |
* Checkbox column. |
|
324 |
* |
|
325 |
* @since 4.9.6 |
|
326 |
* |
|
327 |
* @param WP_User_Request $item Item being shown. |
|
328 |
* @return string Checkbox column markup. |
|
329 |
*/ |
|
330 |
public function column_cb( $item ) { |
|
331 |
return sprintf( '<input type="checkbox" name="request_id[]" value="%1$s" /><span class="spinner"></span>', esc_attr( $item->ID ) ); |
|
332 |
} |
|
333 |
|
|
334 |
/** |
|
335 |
* Status column. |
|
336 |
* |
|
337 |
* @since 4.9.6 |
|
338 |
* |
|
339 |
* @param WP_User_Request $item Item being shown. |
|
340 |
* @return string Status column markup. |
|
341 |
*/ |
|
342 |
public function column_status( $item ) { |
|
343 |
$status = get_post_status( $item->ID ); |
|
344 |
$status_object = get_post_status_object( $status ); |
|
345 |
|
|
346 |
if ( ! $status_object || empty( $status_object->label ) ) { |
|
347 |
return '-'; |
|
348 |
} |
|
349 |
|
|
350 |
$timestamp = false; |
|
351 |
|
|
352 |
switch ( $status ) { |
|
353 |
case 'request-confirmed': |
|
354 |
$timestamp = $item->confirmed_timestamp; |
|
355 |
break; |
|
356 |
case 'request-completed': |
|
357 |
$timestamp = $item->completed_timestamp; |
|
358 |
break; |
|
359 |
} |
|
360 |
|
|
361 |
echo '<span class="status-label status-' . esc_attr( $status ) . '">'; |
|
362 |
echo esc_html( $status_object->label ); |
|
363 |
|
|
364 |
if ( $timestamp ) { |
|
365 |
echo ' (' . $this->get_timestamp_as_date( $timestamp ) . ')'; |
|
366 |
} |
|
367 |
|
|
368 |
echo '</span>'; |
|
369 |
} |
|
370 |
|
|
371 |
/** |
|
372 |
* Convert timestamp for display. |
|
373 |
* |
|
374 |
* @since 4.9.6 |
|
375 |
* |
|
376 |
* @param int $timestamp Event timestamp. |
|
377 |
* @return string Human readable date. |
|
378 |
*/ |
|
379 |
protected function get_timestamp_as_date( $timestamp ) { |
|
380 |
if ( empty( $timestamp ) ) { |
|
381 |
return ''; |
|
382 |
} |
|
383 |
|
|
384 |
$time_diff = time() - $timestamp; |
|
385 |
|
|
386 |
if ( $time_diff >= 0 && $time_diff < DAY_IN_SECONDS ) { |
|
387 |
/* translators: %s: Human-readable time difference. */ |
|
388 |
return sprintf( __( '%s ago' ), human_time_diff( $timestamp ) ); |
|
389 |
} |
|
390 |
|
|
391 |
return date_i18n( get_option( 'date_format' ), $timestamp ); |
|
392 |
} |
|
393 |
|
|
394 |
/** |
|
395 |
* Default column handler. |
|
396 |
* |
|
397 |
* @since 4.9.6 |
|
398 |
* |
|
399 |
* @param WP_User_Request $item Item being shown. |
|
400 |
* @param string $column_name Name of column being shown. |
|
401 |
* @return string Default column output. |
|
402 |
*/ |
|
403 |
public function column_default( $item, $column_name ) { |
|
404 |
$cell_value = $item->$column_name; |
|
405 |
|
|
406 |
if ( in_array( $column_name, array( 'created_timestamp' ), true ) ) { |
|
407 |
return $this->get_timestamp_as_date( $cell_value ); |
|
408 |
} |
|
409 |
|
|
410 |
return $cell_value; |
|
411 |
} |
|
412 |
|
|
413 |
/** |
|
414 |
* Actions column. Overridden by children. |
|
415 |
* |
|
416 |
* @since 4.9.6 |
|
417 |
* |
|
418 |
* @param WP_User_Request $item Item being shown. |
|
419 |
* @return string Email column markup. |
|
420 |
*/ |
|
421 |
public function column_email( $item ) { |
|
422 |
return sprintf( '<a href="%1$s">%2$s</a> %3$s', esc_url( 'mailto:' . $item->email ), $item->email, $this->row_actions( array() ) ); |
|
423 |
} |
|
424 |
|
|
425 |
/** |
|
426 |
* Next steps column. Overridden by children. |
|
427 |
* |
|
428 |
* @since 4.9.6 |
|
429 |
* |
|
430 |
* @param WP_User_Request $item Item being shown. |
|
431 |
*/ |
|
432 |
public function column_next_steps( $item ) {} |
|
433 |
|
|
434 |
/** |
|
435 |
* Generates content for a single row of the table, |
|
436 |
* |
|
437 |
* @since 4.9.6 |
|
438 |
* |
|
439 |
* @param WP_User_Request $item The current item. |
|
440 |
*/ |
|
441 |
public function single_row( $item ) { |
|
442 |
$status = $item->status; |
|
443 |
|
|
444 |
echo '<tr id="request-' . esc_attr( $item->ID ) . '" class="status-' . esc_attr( $status ) . '">'; |
|
445 |
$this->single_row_columns( $item ); |
|
446 |
echo '</tr>'; |
|
447 |
} |
|
448 |
|
|
449 |
/** |
|
450 |
* Embed scripts used to perform actions. Overridden by children. |
|
451 |
* |
|
452 |
* @since 4.9.6 |
|
453 |
*/ |
|
454 |
public function embed_scripts() {} |
|
455 |
} |