Açıklama Yok

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

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