class-wp-privacy-requests-table.php 13 KB

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